@@ -49,13 +49,34 @@ func RegisterCRDs(client crd_cs.Interface, crds []*CustomResourceDefinition) err
49
49
context .TODO (),
50
50
client ,
51
51
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
+ }
55
74
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 ),
59
80
metav1.UpdateOptions {},
60
81
)
61
82
if err != nil && ! kerr .IsAlreadyExists (err ) {
@@ -65,6 +86,21 @@ func RegisterCRDs(client crd_cs.Interface, crds []*CustomResourceDefinition) err
65
86
return WaitForCRDReady (client , crds )
66
87
}
67
88
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
+
68
104
func WaitForCRDReady (client crd_cs.Interface , crds []* CustomResourceDefinition ) error {
69
105
err := wait .PollUntilContextTimeout (context .Background (), 3 * time .Second , 5 * time .Minute , false , func (ctx context.Context ) (bool , error ) {
70
106
for _ , crd := range crds {
0 commit comments