Skip to content

Commit 8549466

Browse files
authored
Merge pull request #1396 from Adirio/scaffold-enhancement/resource-exists
Delegate config checks to the config model
2 parents feecfc6 + b2a1f45 commit 8549466

File tree

2 files changed

+26
-56
lines changed

2 files changed

+26
-56
lines changed

cmd/api.go

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -163,41 +163,15 @@ func (o *apiOptions) validate(c *config.Config) error {
163163

164164
// In case we want to scaffold a resource API we need to do some checks
165165
if o.doResource {
166-
// Skip the following check for v1 as resources aren't tracked
167-
if !c.IsV1() {
168-
// Check that resource doesn't exist or flag force was set
169-
if !o.force {
170-
resourceExists := false
171-
for _, r := range c.Resources {
172-
if r.Group == o.resource.Group &&
173-
r.Version == o.resource.Version &&
174-
r.Kind == o.resource.Kind {
175-
resourceExists = true
176-
break
177-
}
178-
}
179-
if resourceExists {
180-
return errors.New("API resource already exists")
181-
}
182-
}
166+
// Check that resource doesn't exist or flag force was set
167+
if !o.force && c.HasResource(o.resource.GVK()) {
168+
return errors.New("API resource already exists")
183169
}
184170

185-
// The following check is v2 specific as multi-group isn't enabled by default
186-
if c.IsV2() {
187-
// Check the group is the same for single-group projects
188-
if !c.MultiGroup {
189-
validGroup := true
190-
for _, existingGroup := range c.ResourceGroups() {
191-
if !strings.EqualFold(o.resource.Group, existingGroup) {
192-
validGroup = false
193-
break
194-
}
195-
}
196-
if !validGroup {
197-
return fmt.Errorf("multiple groups are not allowed by default, to enable multi-group visit %s",
198-
"kubebuilder.io/migration/multi-group.html")
199-
}
200-
}
171+
// Check that the provided group can be added to the project
172+
if c.IsV2() && !c.MultiGroup && len(c.Resources) != 0 && !c.HasGroup(o.resource.Group) {
173+
return fmt.Errorf("multiple groups are not allowed by default, to enable multi-group visit %s",
174+
"kubebuilder.io/migration/multi-group.html")
201175
}
202176
}
203177

pkg/model/config/config.go

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ limitations under the License.
1616

1717
package config
1818

19+
import (
20+
"strings"
21+
)
22+
1923
// Scaffolding versions
2024
const (
2125
Version1 = "1"
@@ -51,29 +55,8 @@ func (config Config) IsV2() bool {
5155
return config.Version == Version2
5256
}
5357

54-
// ResourceGroups returns unique groups of scaffolded resources in the project
55-
func (config Config) ResourceGroups() []string {
56-
groupSet := map[string]struct{}{}
57-
for _, r := range config.Resources {
58-
groupSet[r.Group] = struct{}{}
59-
}
60-
61-
groups := make([]string, 0, len(groupSet))
62-
for g := range groupSet {
63-
groups = append(groups, g)
64-
}
65-
66-
return groups
67-
}
68-
6958
// HasResource returns true if API resource is already tracked
70-
// NOTE: this works only for v2, since in v1 resources are not tracked
7159
func (config Config) HasResource(target GVK) bool {
72-
// Short-circuit v1
73-
if config.Version == Version1 {
74-
return false
75-
}
76-
7760
// Return true if the target resource is found in the tracked resources
7861
for _, r := range config.Resources {
7962
if r.isEqualTo(target) {
@@ -87,10 +70,10 @@ func (config Config) HasResource(target GVK) bool {
8770

8871
// AddResource appends the provided resource to the tracked ones
8972
// It returns if the configuration was modified
90-
// NOTE: this works only for v2, since in v1 resources are not tracked
73+
// NOTE: in v1 resources are not tracked, so we return false
9174
func (config *Config) AddResource(gvk GVK) bool {
9275
// Short-circuit v1
93-
if config.Version == Version1 {
76+
if config.IsV1() {
9477
return false
9578
}
9679

@@ -104,6 +87,19 @@ func (config *Config) AddResource(gvk GVK) bool {
10487
return true
10588
}
10689

90+
// HasGroup returns true if group is already tracked
91+
func (config Config) HasGroup(group string) bool {
92+
// Return true if the target group is found in the tracked resources
93+
for _, r := range config.Resources {
94+
if strings.EqualFold(group, r.Group) {
95+
return true
96+
}
97+
}
98+
99+
// Return false otherwise
100+
return false
101+
}
102+
107103
// GVK contains information about scaffolded resources
108104
type GVK struct {
109105
Group string `json:"group,omitempty"`

0 commit comments

Comments
 (0)