Skip to content

Commit

Permalink
🐛 added multigroup check while creating resource
Browse files Browse the repository at this point in the history
This change does the following:
  - introduces book-keeping of scaffolded resources in PROJECT file
  - Validates if new resource belong to existing scaffolded groups
  • Loading branch information
droot committed May 31, 2019
1 parent 6fed3e9 commit 8c29f4a
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
24 changes: 24 additions & 0 deletions pkg/scaffold/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ func (api *API) scaffoldV2() error {
r := api.Resource

if api.DoResource {
if err := api.validateResourceGroup(r); err != nil {
return err
}

fmt.Println(filepath.Join("api", r.Version,
fmt.Sprintf("%s_types.go", strings.ToLower(r.Kind))))

Expand Down Expand Up @@ -177,6 +181,15 @@ func (api *API) scaffoldV2() error {
if err != nil {
return fmt.Errorf("error updating kustomization.yaml: %v", err)
}

// update scaffolded resource in project file
api.project.Resources = append(api.project.Resources,
input.Resource{Group: r.Group, Version: r.Version, Kind: r.Kind})
err = saveProjectFile("PROJECT", api.project)
if err != nil {
fmt.Printf("error updating project file with resource information : %v \n", err)
}

} else {
// disable generation of example reconcile body if not scaffolding resource
// because this could result in a fork-bomb of k8s resources where watching a
Expand Down Expand Up @@ -212,3 +225,14 @@ func (api *API) scaffoldV2() error {

return nil
}

// Since we support single group only in v2 scaffolding, validate if resource
// being created belongs to existing group.
func (api *API) validateResourceGroup(resource *resourcev1.Resource) error {
for _, existingGroup := range api.project.ResourceGroups() {
if strings.ToLower(resource.Group) != strings.ToLower(existingGroup) {
return fmt.Errorf("Group '%s' is not same as existing group '%s'. Multiple groups are not supported yet.", resource.Group, existingGroup)
}
}
return nil
}
25 changes: 25 additions & 0 deletions pkg/scaffold/input/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,29 @@ type ProjectFile struct {

// Repo is the go package name of the project root
Repo string `yaml:"repo,omitempty"`

// Resources tracks scaffolded resources in the project. This info is
// tracked only in project with version 2.
Resources []Resource `yaml:"resources,omitempty"`
}

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

groups := []string{}
for g, _ := range groupSet {
groups = append(groups, g)
}
return groups
}

// Resource contains information about scaffolded resources.
type Resource struct {
Group string `yaml:"group,omitempty"`
Version string `yaml:"version,omitempty"`
Kind string `yaml:"kind,omitempty"`
}
13 changes: 13 additions & 0 deletions pkg/scaffold/scaffold.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,19 @@ func LoadProjectFile(path string) (input.ProjectFile, error) {
return p, nil
}

// saveProjectFile saves the given ProjectFile at the given path.
func saveProjectFile(path string, project *input.ProjectFile) error {
content, err := yaml.Marshal(project)
if err != nil {
return fmt.Errorf("error marshalling project info %v", err)
}
err = ioutil.WriteFile(path, content, os.ModePerm)
if err != nil {
return fmt.Errorf("failed to save project file at %s %v", path, err)
}
return nil
}

// GetBoilerplate reads the boilerplate file
func getBoilerplate(path string) (string, error) {
b, err := ioutil.ReadFile(path) // nolint: gosec
Expand Down
7 changes: 7 additions & 0 deletions testdata/project-v2/PROJECT
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
version: "2"
domain: testproject.org
repo: sigs.k8s.io/kubebuilder/testdata/project-v2
resources:
- group: crew
version: v1
kind: Captain
- group: crew
version: v1
kind: FirstMate

0 comments on commit 8c29f4a

Please sign in to comment.