@@ -37,81 +37,89 @@ import (
3737
3838var logger = logging .Global ().RegisterAndGetLogger ("crd" , logging .Info )
3939
40- func EnsureCRD (ctx context.Context , client kclient.Client ) {
40+ func EnsureCRD (ctx context.Context , client kclient.Client , ignoreErrors bool ) error {
4141 crdsLock .Lock ()
4242 defer crdsLock .Unlock ()
4343
4444 for crd , spec := range crds {
4545 getAccess := verifyCRDAccess (ctx , client , crd , "get" )
46-
4746 if ! getAccess .Allowed {
4847 logger .Str ("crd" , crd ).Info ("Get Operations is not allowed. Continue" )
4948 continue
5049 }
5150
52- c , err := client .KubernetesExtensions ().ApiextensionsV1 ().CustomResourceDefinitions ().Get (ctx , crd , meta.GetOptions {})
53- if err != nil {
54- if ! errors .IsNotFound (err ) {
55- logger .Err (err ).Str ("crd" , crd ).Warn ("Get Operations is not allowed due to error. Continue" )
56- continue
57- }
58-
59- createAccess := verifyCRDAccess (ctx , client , crd , "create" )
60-
61- if ! createAccess .Allowed {
62- logger .Str ("crd" , crd ).Info ("Create Operations is not allowed but CRD is missing. Continue" )
63- continue
64- }
65-
66- c = & apiextensions.CustomResourceDefinition {
67- ObjectMeta : meta.ObjectMeta {
68- Name : crd ,
69- Labels : map [string ]string {
70- Version : string (spec .version ),
71- },
72- },
73- Spec : spec .spec ,
74- }
51+ err := tryApplyCRD (ctx , client , crd , spec )
52+ if ! ignoreErrors && err != nil {
53+ return err
54+ }
55+ }
56+ return nil
57+ }
7558
76- if _ , err := client .KubernetesExtensions ().ApiextensionsV1 ().CustomResourceDefinitions ().Create (ctx , c , meta.CreateOptions {}); err != nil {
77- logger .Err (err ).Str ("crd" , crd ).Warn ("Create Operations is not allowed due to error. Continue" )
78- continue
79- }
59+ func tryApplyCRD (ctx context.Context , client kclient.Client , crd string , spec crd ) error {
60+ crdDefinitions := client .KubernetesExtensions ().ApiextensionsV1 ().CustomResourceDefinitions ()
8061
81- logger .Str ("crd" , crd ).Info ("CRD Created" )
82- continue
62+ c , err := crdDefinitions .Get (ctx , crd , meta.GetOptions {})
63+ if err != nil {
64+ if ! errors .IsNotFound (err ) {
65+ logger .Err (err ).Str ("crd" , crd ).Warn ("Get Operations is not allowed due to error" )
66+ return err
8367 }
8468
85- updateAccess := verifyCRDAccess (ctx , client , crd , "update " )
69+ createAccess := verifyCRDAccess (ctx , client , crd , "create " )
8670
87- if ! updateAccess .Allowed {
88- logger .Str ("crd" , crd ).Info ("Update Operations is not allowed. Continue" )
89- continue
71+ if ! createAccess .Allowed {
72+ logger .Str ("crd" , crd ).Info ("Create Operations is not allowed but CRD is missing . Continue" )
73+ return nil
9074 }
9175
92- if c .ObjectMeta .Labels == nil {
93- c .ObjectMeta .Labels = map [string ]string {}
76+ c = & apiextensions.CustomResourceDefinition {
77+ ObjectMeta : meta.ObjectMeta {
78+ Name : crd ,
79+ Labels : map [string ]string {
80+ Version : string (spec .version ),
81+ },
82+ },
83+ Spec : spec .spec ,
9484 }
9585
96- if v , ok := c .ObjectMeta .Labels [Version ]; ok {
97- if v != "" {
98- if ! isUpdateRequired (spec .version , driver .Version (v )) {
99- logger .Str ("crd" , crd ).Info ("CRD Update not required" )
100- continue
101- }
102- }
86+ if _ , err := crdDefinitions .Create (ctx , c , meta.CreateOptions {}); err != nil {
87+ logger .Err (err ).Str ("crd" , crd ).Warn ("Create Operations is not allowed due to error" )
88+ return err
10389 }
10490
105- c .ObjectMeta .Labels [Version ] = string (spec .version )
91+ logger .Str ("crd" , crd ).Info ("CRD Created" )
92+ return nil
93+ }
10694
107- c .Spec = spec .spec
95+ updateAccess := verifyCRDAccess (ctx , client , crd , "update" )
96+ if ! updateAccess .Allowed {
97+ logger .Str ("crd" , crd ).Info ("Update Operations is not allowed. Continue" )
98+ return nil
99+ }
108100
109- if _ , err := client .KubernetesExtensions ().ApiextensionsV1 ().CustomResourceDefinitions ().Update (ctx , c , meta.UpdateOptions {}); err != nil {
110- logger .Err (err ).Str ("crd" , crd ).Warn ("Create Operations is not allowed due to error. Continue" )
111- continue
101+ if c .ObjectMeta .Labels == nil {
102+ c .ObjectMeta .Labels = map [string ]string {}
103+ }
104+
105+ if v , ok := c .ObjectMeta .Labels [Version ]; ok {
106+ if v != "" {
107+ if ! isUpdateRequired (spec .version , driver .Version (v )) {
108+ logger .Str ("crd" , crd ).Info ("CRD Update not required" )
109+ return nil
110+ }
112111 }
113- logger .Str ("crd" , crd ).Info ("CRD Updated" )
114112 }
113+
114+ c .ObjectMeta .Labels [Version ] = string (spec .version )
115+ c .Spec = spec .spec
116+
117+ if _ , err := crdDefinitions .Update (ctx , c , meta.UpdateOptions {}); err != nil {
118+ logger .Err (err ).Str ("crd" , crd ).Warn ("Create Operations is not allowed due to error" )
119+ return err
120+ }
121+ logger .Str ("crd" , crd ).Info ("CRD Updated" )
122+ return nil
115123}
116124
117125func verifyCRDAccess (ctx context.Context , client kclient.Client , crd string , verb string ) authorization.SubjectAccessReviewStatus {
0 commit comments