Skip to content

Commit 8c29f4a

Browse files
committed
🐛 added multigroup check while creating resource
This change does the following: - introduces book-keeping of scaffolded resources in PROJECT file - Validates if new resource belong to existing scaffolded groups
1 parent 6fed3e9 commit 8c29f4a

File tree

4 files changed

+69
-0
lines changed

4 files changed

+69
-0
lines changed

pkg/scaffold/api.go

+24
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,10 @@ func (api *API) scaffoldV2() error {
143143
r := api.Resource
144144

145145
if api.DoResource {
146+
if err := api.validateResourceGroup(r); err != nil {
147+
return err
148+
}
149+
146150
fmt.Println(filepath.Join("api", r.Version,
147151
fmt.Sprintf("%s_types.go", strings.ToLower(r.Kind))))
148152

@@ -177,6 +181,15 @@ func (api *API) scaffoldV2() error {
177181
if err != nil {
178182
return fmt.Errorf("error updating kustomization.yaml: %v", err)
179183
}
184+
185+
// update scaffolded resource in project file
186+
api.project.Resources = append(api.project.Resources,
187+
input.Resource{Group: r.Group, Version: r.Version, Kind: r.Kind})
188+
err = saveProjectFile("PROJECT", api.project)
189+
if err != nil {
190+
fmt.Printf("error updating project file with resource information : %v \n", err)
191+
}
192+
180193
} else {
181194
// disable generation of example reconcile body if not scaffolding resource
182195
// because this could result in a fork-bomb of k8s resources where watching a
@@ -212,3 +225,14 @@ func (api *API) scaffoldV2() error {
212225

213226
return nil
214227
}
228+
229+
// Since we support single group only in v2 scaffolding, validate if resource
230+
// being created belongs to existing group.
231+
func (api *API) validateResourceGroup(resource *resourcev1.Resource) error {
232+
for _, existingGroup := range api.project.ResourceGroups() {
233+
if strings.ToLower(resource.Group) != strings.ToLower(existingGroup) {
234+
return fmt.Errorf("Group '%s' is not same as existing group '%s'. Multiple groups are not supported yet.", resource.Group, existingGroup)
235+
}
236+
}
237+
return nil
238+
}

pkg/scaffold/input/input.go

+25
Original file line numberDiff line numberDiff line change
@@ -169,4 +169,29 @@ type ProjectFile struct {
169169

170170
// Repo is the go package name of the project root
171171
Repo string `yaml:"repo,omitempty"`
172+
173+
// Resources tracks scaffolded resources in the project. This info is
174+
// tracked only in project with version 2.
175+
Resources []Resource `yaml:"resources,omitempty"`
176+
}
177+
178+
// ResourceGroups returns unique groups of scaffolded resources in the project.
179+
func (pf *ProjectFile) ResourceGroups() []string {
180+
groupSet := map[string]struct{}{}
181+
for _, r := range pf.Resources {
182+
groupSet[r.Group] = struct{}{}
183+
}
184+
185+
groups := []string{}
186+
for g, _ := range groupSet {
187+
groups = append(groups, g)
188+
}
189+
return groups
190+
}
191+
192+
// Resource contains information about scaffolded resources.
193+
type Resource struct {
194+
Group string `yaml:"group,omitempty"`
195+
Version string `yaml:"version,omitempty"`
196+
Kind string `yaml:"kind,omitempty"`
172197
}

pkg/scaffold/scaffold.go

+13
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,19 @@ func LoadProjectFile(path string) (input.ProjectFile, error) {
105105
return p, nil
106106
}
107107

108+
// saveProjectFile saves the given ProjectFile at the given path.
109+
func saveProjectFile(path string, project *input.ProjectFile) error {
110+
content, err := yaml.Marshal(project)
111+
if err != nil {
112+
return fmt.Errorf("error marshalling project info %v", err)
113+
}
114+
err = ioutil.WriteFile(path, content, os.ModePerm)
115+
if err != nil {
116+
return fmt.Errorf("failed to save project file at %s %v", path, err)
117+
}
118+
return nil
119+
}
120+
108121
// GetBoilerplate reads the boilerplate file
109122
func getBoilerplate(path string) (string, error) {
110123
b, err := ioutil.ReadFile(path) // nolint: gosec

testdata/project-v2/PROJECT

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
version: "2"
22
domain: testproject.org
33
repo: sigs.k8s.io/kubebuilder/testdata/project-v2
4+
resources:
5+
- group: crew
6+
version: v1
7+
kind: Captain
8+
- group: crew
9+
version: v1
10+
kind: FirstMate

0 commit comments

Comments
 (0)