@@ -32,6 +32,7 @@ import (
32
32
syncagentv1alpha1 "github.com/kcp-dev/api-syncagent/sdk/apis/syncagent/v1alpha1"
33
33
"github.com/kcp-dev/api-syncagent/test/utils"
34
34
35
+ corev1 "k8s.io/api/core/v1"
35
36
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
36
37
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
37
38
"k8s.io/apimachinery/pkg/runtime"
@@ -449,3 +450,96 @@ spec:
449
450
t .Fatal ("Expected no ignored object to be found on the service cluster, but did." )
450
451
}
451
452
}
453
+
454
+ func TestSyncingOverlyLongNames (t * testing.T ) {
455
+ const (
456
+ apiExportName = "kcp.example.com"
457
+ orgWorkspace = "sync-long-names"
458
+ )
459
+
460
+ ctx := context .Background ()
461
+ ctrlruntime .SetLogger (logr .Discard ())
462
+
463
+ // setup a test environment in kcp
464
+ orgKubconfig := utils .CreateOrganization (t , ctx , orgWorkspace , apiExportName )
465
+
466
+ // start a service cluster
467
+ envtestKubeconfig , envtestClient , _ := utils .RunEnvtest (t , []string {
468
+ "test/crds/crontab.yaml" ,
469
+ })
470
+
471
+ // publish Crontabs and Backups
472
+ t .Logf ("Publishing CRDs…" )
473
+ prCrontabs := & syncagentv1alpha1.PublishedResource {
474
+ ObjectMeta : metav1.ObjectMeta {
475
+ Name : "publish-crontabs" ,
476
+ },
477
+ Spec : syncagentv1alpha1.PublishedResourceSpec {
478
+ Resource : syncagentv1alpha1.SourceResourceDescriptor {
479
+ APIGroup : "example.com" ,
480
+ Version : "v1" ,
481
+ Kind : "CronTab" ,
482
+ },
483
+ // These rules make finding the local object easier, but should not be used in production.
484
+ Naming : & syncagentv1alpha1.ResourceNaming {
485
+ Name : "$remoteName" ,
486
+ Namespace : "synced-$remoteNamespace" ,
487
+ },
488
+ },
489
+ }
490
+
491
+ if err := envtestClient .Create (ctx , prCrontabs ); err != nil {
492
+ t .Fatalf ("Failed to create PublishedResource: %v" , err )
493
+ }
494
+
495
+ // start the agent in the background to update the APIExport with the CronTabs API
496
+ utils .RunAgent (ctx , t , "bob" , orgKubconfig , envtestKubeconfig , apiExportName )
497
+
498
+ // wait until the API is available
499
+ teamCtx := kontext .WithCluster (ctx , logicalcluster .Name (fmt .Sprintf ("root:%s:team-1" , orgWorkspace )))
500
+ kcpClient := utils .GetKcpAdminClusterClient (t )
501
+ utils .WaitForBoundAPI (t , teamCtx , kcpClient , schema.GroupVersionResource {
502
+ Group : apiExportName ,
503
+ Version : "v1" ,
504
+ Resource : "crontabs" ,
505
+ })
506
+
507
+ // create a namespace and CronTab with extremely long names
508
+ namespace := & corev1.Namespace {}
509
+ namespace .Name = strings .Repeat ("yadda" , 3 ) // 250 chars in total
510
+
511
+ if err := kcpClient .Create (teamCtx , namespace ); err != nil {
512
+ t .Fatalf ("Failed to create namespace in kcp: %v" , err )
513
+ }
514
+
515
+ t .Log ("Creating CronTab in kcp…" )
516
+ ignoredCrontab := yamlToUnstructured (t , `
517
+ apiVersion: kcp.example.com/v1
518
+ kind: CronTab
519
+ metadata:
520
+ name: TBD
521
+ spec:
522
+ image: ubuntu:latest
523
+ ` )
524
+ ignoredCrontab .SetNamespace (namespace .Name )
525
+ ignoredCrontab .SetName (strings .Repeat ("yotta" , 50 ))
526
+
527
+ if err := kcpClient .Create (teamCtx , ignoredCrontab ); err != nil {
528
+ t .Fatalf ("Failed to create CronTab in kcp: %v" , err )
529
+ }
530
+
531
+ // wait for the agent to sync the object down into the service cluster
532
+
533
+ t .Logf ("Wait for CronTab to be synced…" )
534
+ copy := & unstructured.Unstructured {}
535
+ copy .SetAPIVersion ("example.com/v1" )
536
+ copy .SetKind ("CronTab" )
537
+
538
+ err := wait .PollUntilContextTimeout (ctx , 500 * time .Millisecond , 30 * time .Second , false , func (ctx context.Context ) (done bool , err error ) {
539
+ copyKey := types.NamespacedName {Namespace : "synced-default" , Name : "included" }
540
+ return envtestClient .Get (ctx , copyKey , copy ) == nil , nil
541
+ })
542
+ if err != nil {
543
+ t .Fatalf ("Failed to wait for object to be synced down: %v" , err )
544
+ }
545
+ }
0 commit comments