Skip to content

Commit 367d3f6

Browse files
committed
Handle creation of duplicate API resources
1 parent ae06fc8 commit 367d3f6

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

cmd/api.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ func (o *apiOptions) bindCmdFlags(cmd *cobra.Command) {
5757
cmd.Flags().StringVar(&o.pattern, "pattern", "",
5858
"generates an API following an extension pattern (addon)")
5959
}
60+
cmd.Flags().BoolVar(&o.apiScaffolder.Force, "force", false,
61+
"attempt to create resource even if it already exists")
6062
o.apiScaffolder.Resource = resourceForFlags(cmd.Flags())
6163
}
6264

pkg/scaffold/api.go

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ type API struct {
4949

5050
// DoController indicates whether to scaffold controller files or not
5151
DoController bool
52+
53+
// Force indicates that the resource should be created even if it already exists.
54+
Force bool
5255
}
5356

5457
// Validate validates whether API scaffold has correct bits to generate
@@ -67,6 +70,10 @@ func (api *API) Validate() error {
6770
return fmt.Errorf("missing kind information for resource")
6871
}
6972

73+
if api.resourceExists() && !api.Force {
74+
return fmt.Errorf("API resource already exists")
75+
}
76+
7077
return nil
7178
}
7279

@@ -209,12 +216,14 @@ func (api *API) scaffoldV2() error {
209216
return fmt.Errorf("error updating kustomization.yaml: %v", err)
210217
}
211218

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+
}
218227
}
219228

220229
} else {
@@ -274,3 +283,17 @@ func (api *API) validateResourceGroup(resource *resourcev1.Resource) error {
274283
}
275284
return nil
276285
}
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

Comments
 (0)