Skip to content

Commit

Permalink
Delegate config checks to the config model
Browse files Browse the repository at this point in the history
Signed-off-by: Adrian Orive <[email protected]>
  • Loading branch information
Adirio committed Feb 29, 2020
1 parent feecfc6 commit b2a1f45
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 56 deletions.
40 changes: 7 additions & 33 deletions cmd/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,41 +163,15 @@ func (o *apiOptions) validate(c *config.Config) error {

// In case we want to scaffold a resource API we need to do some checks
if o.doResource {
// Skip the following check for v1 as resources aren't tracked
if !c.IsV1() {
// Check that resource doesn't exist or flag force was set
if !o.force {
resourceExists := false
for _, r := range c.Resources {
if r.Group == o.resource.Group &&
r.Version == o.resource.Version &&
r.Kind == o.resource.Kind {
resourceExists = true
break
}
}
if resourceExists {
return errors.New("API resource already exists")
}
}
// Check that resource doesn't exist or flag force was set
if !o.force && c.HasResource(o.resource.GVK()) {
return errors.New("API resource already exists")
}

// The following check is v2 specific as multi-group isn't enabled by default
if c.IsV2() {
// Check the group is the same for single-group projects
if !c.MultiGroup {
validGroup := true
for _, existingGroup := range c.ResourceGroups() {
if !strings.EqualFold(o.resource.Group, existingGroup) {
validGroup = false
break
}
}
if !validGroup {
return fmt.Errorf("multiple groups are not allowed by default, to enable multi-group visit %s",
"kubebuilder.io/migration/multi-group.html")
}
}
// Check that the provided group can be added to the project
if c.IsV2() && !c.MultiGroup && len(c.Resources) != 0 && !c.HasGroup(o.resource.Group) {
return fmt.Errorf("multiple groups are not allowed by default, to enable multi-group visit %s",
"kubebuilder.io/migration/multi-group.html")
}
}

Expand Down
42 changes: 19 additions & 23 deletions pkg/model/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ limitations under the License.

package config

import (
"strings"
)

// Scaffolding versions
const (
Version1 = "1"
Expand Down Expand Up @@ -51,29 +55,8 @@ func (config Config) IsV2() bool {
return config.Version == Version2
}

// ResourceGroups returns unique groups of scaffolded resources in the project
func (config Config) ResourceGroups() []string {
groupSet := map[string]struct{}{}
for _, r := range config.Resources {
groupSet[r.Group] = struct{}{}
}

groups := make([]string, 0, len(groupSet))
for g := range groupSet {
groups = append(groups, g)
}

return groups
}

// HasResource returns true if API resource is already tracked
// NOTE: this works only for v2, since in v1 resources are not tracked
func (config Config) HasResource(target GVK) bool {
// Short-circuit v1
if config.Version == Version1 {
return false
}

// Return true if the target resource is found in the tracked resources
for _, r := range config.Resources {
if r.isEqualTo(target) {
Expand All @@ -87,10 +70,10 @@ func (config Config) HasResource(target GVK) bool {

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

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

// HasGroup returns true if group is already tracked
func (config Config) HasGroup(group string) bool {
// Return true if the target group is found in the tracked resources
for _, r := range config.Resources {
if strings.EqualFold(group, r.Group) {
return true
}
}

// Return false otherwise
return false
}

// GVK contains information about scaffolded resources
type GVK struct {
Group string `json:"group,omitempty"`
Expand Down

0 comments on commit b2a1f45

Please sign in to comment.