@@ -49,13 +49,34 @@ func RegisterCRDs(client crd_cs.Interface, crds []*CustomResourceDefinition) err
4949 context .TODO (),
5050 client ,
5151 crd .V1 .Name ,
52- func (in * crdv1.CustomResourceDefinition ) * crdv1.CustomResourceDefinition {
53- in .Labels = meta_util .OverwriteKeys (in .Labels , crd .V1 .Labels )
54- in .Annotations = meta_util .OverwriteKeys (in .Annotations , crd .V1 .Annotations )
52+ transform (crd ),
53+ metav1.UpdateOptions {},
54+ )
55+ if err != nil && ! kerr .IsAlreadyExists (err ) {
56+ return err
57+ }
58+ }
59+ return WaitForCRDReady (client , crds )
60+ }
61+
62+ func UpdateCRDs (client crd_cs.Interface , crds []* CustomResourceDefinition ) error {
63+ for _ , crd := range crds {
64+ // Use crd v1 for k8s >= 1.16, if available
65+ // ref: https://github.com/kubernetes/kubernetes/issues/91395
66+ if crd .V1 == nil {
67+ gvr := schema.GroupVersionResource {
68+ Group : crd .V1beta1 .Spec .Group ,
69+ Version : crd .V1beta1 .Spec .Versions [0 ].Name ,
70+ Resource : crd .V1beta1 .Spec .Names .Plural ,
71+ }
72+ return fmt .Errorf ("missing V1 definition for %s" , gvr )
73+ }
5574
56- in .Spec = crd .V1 .Spec
57- return in
58- },
75+ _ , _ , err := v1 .UpdateCustomResourceDefinitionIfPresent (
76+ context .TODO (),
77+ client ,
78+ crd .V1 .Name ,
79+ transform (crd ),
5980 metav1.UpdateOptions {},
6081 )
6182 if err != nil && ! kerr .IsAlreadyExists (err ) {
@@ -65,6 +86,21 @@ func RegisterCRDs(client crd_cs.Interface, crds []*CustomResourceDefinition) err
6586 return WaitForCRDReady (client , crds )
6687}
6788
89+ func transform (crd * CustomResourceDefinition ) func (in * crdv1.CustomResourceDefinition ) * crdv1.CustomResourceDefinition {
90+ return func (in * crdv1.CustomResourceDefinition ) * crdv1.CustomResourceDefinition {
91+ in .Labels = meta_util .OverwriteKeys (in .Labels , crd .V1 .Labels )
92+ in .Annotations = meta_util .OverwriteKeys (in .Annotations , crd .V1 .Annotations )
93+
94+ conversion := in .Spec .Conversion
95+ in .Spec = crd .V1 .Spec
96+ // preserve conversion
97+ if in .Spec .Conversion == nil && conversion != nil {
98+ in .Spec .Conversion = conversion
99+ }
100+ return in
101+ }
102+ }
103+
68104func WaitForCRDReady (client crd_cs.Interface , crds []* CustomResourceDefinition ) error {
69105 err := wait .PollUntilContextTimeout (context .Background (), 3 * time .Second , 5 * time .Minute , false , func (ctx context.Context ) (bool , error ) {
70106 for _ , crd := range crds {
0 commit comments