Skip to content

Commit 49c143d

Browse files
authored
Merge pull request #1164 from hypnoglow/api-duplicates
Handle creation of duplicate API resources
2 parents 72a0ef1 + 367d3f6 commit 49c143d

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
@@ -51,6 +51,9 @@ type API struct {
5151

5252
// DoController indicates whether to scaffold controller files or not
5353
DoController bool
54+
55+
// Force indicates that the resource should be created even if it already exists.
56+
Force bool
5457
}
5558

5659
// Validate validates whether API scaffold has correct bits to generate
@@ -63,6 +66,10 @@ func (api *API) Validate() error {
6366
return err
6467
}
6568

69+
if api.resourceExists() && !api.Force {
70+
return fmt.Errorf("API resource already exists")
71+
}
72+
6673
return nil
6774
}
6875

@@ -205,12 +212,14 @@ func (api *API) scaffoldV2() error {
205212
return fmt.Errorf("error updating kustomization.yaml: %v", err)
206213
}
207214

208-
// update scaffolded resource in project file
209-
api.project.Resources = append(api.project.Resources,
210-
input.Resource{Group: r.Group, Version: r.Version, Kind: r.Kind})
211-
err = saveProjectFile("PROJECT", api.project)
212-
if err != nil {
213-
fmt.Printf("error updating project file with resource information : %v \n", err)
215+
if !api.resourceExists() {
216+
// update scaffolded resource in project file
217+
api.project.Resources = append(api.project.Resources,
218+
input.Resource{Group: r.Group, Version: r.Version, Kind: r.Kind})
219+
err = saveProjectFile("PROJECT", api.project)
220+
if err != nil {
221+
fmt.Printf("error updating project file with resource information : %v \n", err)
222+
}
214223
}
215224

216225
} else {
@@ -270,3 +279,17 @@ func (api *API) validateResourceGroup(r *resource.Resource) error {
270279
}
271280
return nil
272281
}
282+
283+
// resourceExists returns true if API resource is already tracked by the PROJECT file.
284+
// Note that this works only for v2, since in v1 resources are not tracked by the PROJECT file.
285+
func (api *API) resourceExists() bool {
286+
for _, resource := range api.project.Resources {
287+
if resource.Group == api.Resource.Group &&
288+
resource.Version == api.Resource.Version &&
289+
resource.Kind == api.Resource.Kind {
290+
return true
291+
}
292+
}
293+
294+
return false
295+
}

0 commit comments

Comments
 (0)