@@ -571,12 +571,24 @@ func TestLazyRestMapperProvider(t *testing.T) {
571
571
c , err := client .New (restCfg , client.Options {Scheme : s })
572
572
g .Expect (err ).NotTo (gmg .HaveOccurred ())
573
573
574
- // Register a new CRD ina new group to avoid collisions when deleting versions - "taxi .inventory.example.com".
574
+ // Register a new CRD ina new group to avoid collisions when deleting versions - "taxis .inventory.example.com".
575
575
group := "inventory.example.com"
576
576
kind := "Taxi"
577
577
plural := "taxis"
578
578
crdName := plural + "." + group
579
- crd := createNewCRD (ctx , g , c , group , kind , plural )
579
+ // Create a CRD with two versions: v1alpha1 and v1 where both are served and
580
+ // v1 is the storage version so we can easily remove v1alpha1 later.
581
+ crd := newCRD (ctx , g , c , group , kind , plural )
582
+ v1alpha1 := crd .Spec .Versions [0 ]
583
+ v1alpha1 .Name = "v1alpha1"
584
+ v1alpha1 .Storage = false
585
+ v1alpha1 .Served = true
586
+ v1 := crd .Spec .Versions [0 ]
587
+ v1 .Name = "v1"
588
+ v1 .Storage = true
589
+ v1 .Served = true
590
+ crd .Spec .Versions = []apiextensionsv1.CustomResourceDefinitionVersion {v1alpha1 , v1 }
591
+ g .Expect (c .Create (ctx , crd )).To (gmg .Succeed ())
580
592
t .Cleanup (func () {
581
593
g .Expect (c .Delete (ctx , crd )).To (gmg .Succeed ())
582
594
})
@@ -599,63 +611,76 @@ func TestLazyRestMapperProvider(t *testing.T) {
599
611
// #1: GET https://host/api
600
612
// #2: GET https://host/apis
601
613
// Then, for all available versions:
602
- // #3: GET https://host/apis/inventory.example.com/v1
603
- // #4: GET https://host/apis/inventory.example.com/v2
614
+ // #3: GET https://host/apis/inventory.example.com/v1alpha1
615
+ // #4: GET https://host/apis/inventory.example.com/v1
604
616
// This should fill the cache for apiGroups and versions.
605
617
mapping , err := lazyRestMapper .RESTMapping (schema.GroupKind {Group : group , Kind : kind })
606
618
g .Expect (err ).NotTo (gmg .HaveOccurred ())
607
619
g .Expect (mapping .GroupVersionKind .Kind ).To (gmg .Equal (kind ))
608
620
g .Expect (crt .GetRequestCount ()).To (gmg .Equal (4 ))
609
621
crt .Reset () // We reset the counter to check how many additional requests are made later.
610
622
611
- // At this point v2 should be cached
612
- _ , err = lazyRestMapper .RESTMapping (schema.GroupKind {Group : group , Kind : kind }, "v2 " )
623
+ // At this point v1alpha1 should be cached
624
+ _ , err = lazyRestMapper .RESTMapping (schema.GroupKind {Group : group , Kind : kind }, "v1alpha1 " )
613
625
g .Expect (err ).NotTo (gmg .HaveOccurred ())
614
626
g .Expect (crt .GetRequestCount ()).To (gmg .Equal (0 ))
615
627
616
628
// We update the CRD to only have v1 version.
617
629
g .Expect (c .Get (ctx , types.NamespacedName {Name : crdName }, crd )).To (gmg .Succeed ())
618
- var v1 apiextensionsv1.CustomResourceDefinitionVersion
619
- for i , version := range crd .Spec .Versions {
630
+ for _ , version := range crd .Spec .Versions {
620
631
if version .Name == "v1" {
621
- crd .Spec .Versions [i ].Storage = true
622
632
v1 = version
623
- v1 . Storage = true
633
+ break
624
634
}
625
635
}
626
636
crd .Spec .Versions = []apiextensionsv1.CustomResourceDefinitionVersion {v1 }
627
637
g .Expect (c .Update (ctx , crd )).To (gmg .Succeed ())
628
638
629
- // We wait until v2 is not available anymore.
639
+ // We wait until v1alpha1 is not available anymore.
630
640
g .Eventually (func (g gmg.Gomega ) {
631
- _ , err = discClient .ServerResourcesForGroupVersion (group + "/v2 " )
632
- g .Expect (apierrors .IsNotFound (err )).To (gmg .BeTrue (), "v2 should not be available anymore" )
641
+ _ , err = discClient .ServerResourcesForGroupVersion (group + "/v1alpha1 " )
642
+ g .Expect (apierrors .IsNotFound (err )).To (gmg .BeTrue (), "v1alpha1 should not be available anymore" )
633
643
}).Should (gmg .Succeed ())
634
644
635
- // Although v2 is not available anymore, the cache is not invalidated yet so it should return a mapping.
636
- _ , err = lazyRestMapper .RESTMapping (schema.GroupKind {Group : group , Kind : kind }, "v2 " )
645
+ // Although v1alpha1 is not available anymore, the cache is not invalidated yet so it should return a mapping.
646
+ _ , err = lazyRestMapper .RESTMapping (schema.GroupKind {Group : group , Kind : kind }, "v1alpha1 " )
637
647
g .Expect (err ).NotTo (gmg .HaveOccurred ())
638
648
g .Expect (crt .GetRequestCount ()).To (gmg .Equal (0 ))
639
649
640
650
// We request Limo, which is not in the mapper because it doesn't exist.
641
651
// This will trigger a reload of the lazy mapper cache.
642
652
// Reloading the cache will read v2 again and since it's not available anymore, it should invalidate the cache.
643
- // #1: GET https://host/apis/inventory.example.com/v1
644
- // #2: GET https://host/apis/inventory.example.com/v2
653
+ // #1: GET https://host/apis/inventory.example.com/v1alpha1
654
+ // #2: GET https://host/apis/inventory.example.com/v1
645
655
_ , err = lazyRestMapper .RESTMapping (schema.GroupKind {Group : group , Kind : "Limo" })
646
656
g .Expect (err ).To (beNoMatchError ())
647
657
g .Expect (crt .GetRequestCount ()).To (gmg .Equal (2 ))
648
658
crt .Reset ()
649
659
650
- // Now we request v2 again and it should return an error since the cache was invalidated.
651
- // #1: GET https://host/apis/inventory.example.com/v2
652
- _ , err = lazyRestMapper .RESTMapping (schema.GroupKind {Group : group , Kind : kind }, "v2 " )
660
+ // Now we request v1alpha1 again and it should return an error since the cache was invalidated.
661
+ // #1: GET https://host/apis/inventory.example.com/v1alpha1
662
+ _ , err = lazyRestMapper .RESTMapping (schema.GroupKind {Group : group , Kind : kind }, "v1alpha1 " )
653
663
g .Expect (err ).To (beNoMatchError ())
654
664
g .Expect (crt .GetRequestCount ()).To (gmg .Equal (1 ))
665
+
666
+ // Verify that when requesting the mapping without a version, it doesn't error
667
+ // and it returns v1.
668
+ mapping , err = lazyRestMapper .RESTMapping (schema.GroupKind {Group : group , Kind : kind })
669
+ g .Expect (err ).NotTo (gmg .HaveOccurred ())
670
+ g .Expect (mapping .Resource .Version ).To (gmg .Equal ("v1" ))
655
671
})
656
672
}
657
673
674
+ // createNewCRD creates a new CRD with the given group, kind, and plural and returns it.
658
675
func createNewCRD (ctx context.Context , g gmg.Gomega , c client.Client , group , kind , plural string ) * apiextensionsv1.CustomResourceDefinition {
676
+ newCRD := newCRD (ctx , g , c , group , kind , plural )
677
+ g .Expect (c .Create (ctx , newCRD )).To (gmg .Succeed ())
678
+
679
+ return newCRD
680
+ }
681
+
682
+ // newCRD returns a new CRD with the given group, kind, and plural.
683
+ func newCRD (ctx context.Context , g gmg.Gomega , c client.Client , group , kind , plural string ) * apiextensionsv1.CustomResourceDefinition {
659
684
crd := & apiextensionsv1.CustomResourceDefinition {}
660
685
err := c .Get (ctx , types.NamespacedName {Name : "drivers.crew.example.com" }, crd )
661
686
g .Expect (err ).NotTo (gmg .HaveOccurred ())
@@ -671,9 +696,6 @@ func createNewCRD(ctx context.Context, g gmg.Gomega, c client.Client, group, kin
671
696
}
672
697
newCRD .ResourceVersion = ""
673
698
674
- // Create the new CRD.
675
- g .Expect (c .Create (ctx , newCRD )).To (gmg .Succeed ())
676
-
677
699
return newCRD
678
700
}
679
701
0 commit comments