@@ -49,6 +49,9 @@ type API struct {
49
49
50
50
// DoController indicates whether to scaffold controller files or not
51
51
DoController bool
52
+
53
+ // Force indicates that the resource should be created even if it already exists.
54
+ Force bool
52
55
}
53
56
54
57
// Validate validates whether API scaffold has correct bits to generate
@@ -67,6 +70,10 @@ func (api *API) Validate() error {
67
70
return fmt .Errorf ("missing kind information for resource" )
68
71
}
69
72
73
+ if api .resourceExists () && ! api .Force {
74
+ return fmt .Errorf ("API resource already exists" )
75
+ }
76
+
70
77
return nil
71
78
}
72
79
@@ -209,12 +216,14 @@ func (api *API) scaffoldV2() error {
209
216
return fmt .Errorf ("error updating kustomization.yaml: %v" , err )
210
217
}
211
218
212
- // update scaffolded resource in project file
213
- api .project .Resources = append (api .project .Resources ,
214
- input.Resource {Group : r .Group , Version : r .Version , Kind : r .Kind })
215
- err = saveProjectFile ("PROJECT" , api .project )
216
- if err != nil {
217
- fmt .Printf ("error updating project file with resource information : %v \n " , err )
219
+ if ! api .resourceExists () {
220
+ // update scaffolded resource in project file
221
+ api .project .Resources = append (api .project .Resources ,
222
+ input.Resource {Group : r .Group , Version : r .Version , Kind : r .Kind })
223
+ err = saveProjectFile ("PROJECT" , api .project )
224
+ if err != nil {
225
+ fmt .Printf ("error updating project file with resource information : %v \n " , err )
226
+ }
218
227
}
219
228
220
229
} else {
@@ -274,3 +283,17 @@ func (api *API) validateResourceGroup(resource *resourcev1.Resource) error {
274
283
}
275
284
return nil
276
285
}
286
+
287
+ // resourceExists returns true if API resource is already tracked by the PROJECT file.
288
+ // Note that this works only for v2, since in v1 resources are not tracked by the PROJECT file.
289
+ func (api * API ) resourceExists () bool {
290
+ for _ , resource := range api .project .Resources {
291
+ if resource .Group == api .Resource .Group &&
292
+ resource .Version == api .Resource .Version &&
293
+ resource .Kind == api .Resource .Kind {
294
+ return true
295
+ }
296
+ }
297
+
298
+ return false
299
+ }
0 commit comments