Skip to content

Commit dc5ea0c

Browse files
authored
Merge pull request #1313 from Adirio/scaffold-enhancement/project-in-universe
Include PROJECT file information into model.Universe
2 parents 5aaea23 + 6534660 commit dc5ea0c

File tree

10 files changed

+356
-134
lines changed

10 files changed

+356
-134
lines changed

cmd/vendor_update.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,21 @@ kubebuilder update vendor
3939
Run: func(cmd *cobra.Command, args []string) {
4040
internal.DieIfNotConfigured()
4141

42-
err := (&scaffold.Scaffold{}).Execute(
43-
&model.Universe{},
42+
universe, err := model.NewUniverse(
43+
model.WithConfigFrom("PROJECT"),
44+
model.WithoutBoilerplate,
45+
)
46+
if err != nil {
47+
log.Fatalf("error updating vendor dependencies: %v", err)
48+
}
49+
50+
err = (&scaffold.Scaffold{}).Execute(
51+
universe,
4452
input.Options{},
4553
&project.GopkgToml{},
4654
)
4755
if err != nil {
48-
log.Fatalf("error updating vendor dependecies %v", err)
56+
log.Fatalf("error updating vendor dependencies: %v", err)
4957
}
5058
},
5159
}

cmd/webhook_v1.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,20 @@ This command is only available for v1 scaffolding project.
6666
}
6767

6868
fmt.Println("Writing scaffold for you to edit...")
69+
70+
universe, err := model.NewUniverse(
71+
model.WithConfig(projectConfig),
72+
// TODO: missing model.WithBoilerplate[From], needs boilerplate or path
73+
model.WithResource(o.res, projectConfig),
74+
)
75+
if err != nil {
76+
log.Fatalf("error scaffolding webhook: %v", err)
77+
}
78+
6979
webhookConfig := webhook.Config{Server: o.server, Type: o.webhookType, Operations: o.operations}
80+
7081
err = (&scaffold.Scaffold{}).Execute(
71-
&model.Universe{},
82+
universe,
7283
input.Options{},
7384
&manager.Webhook{},
7485
&webhook.AdmissionHandler{Resource: o.res, Config: webhookConfig},
@@ -79,7 +90,7 @@ This command is only available for v1 scaffolding project.
7990
&webhook.AddServer{Config: webhookConfig},
8091
)
8192
if err != nil {
82-
log.Fatal(err)
93+
log.Fatalf("error scaffolding webhook: %v", err)
8394
}
8495

8596
if o.doMake {

cmd/webhook_v2.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,19 +87,27 @@ func newWebhookV2Cmd() *cobra.Command {
8787
fmt.Println(`Webhook server has been set up for you.
8888
You need to implement the conversion.Hub and conversion.Convertible interfaces for your CRD types.`)
8989
}
90-
webhookScaffolder := &webhook.Webhook{
91-
Resource: o.res,
92-
Defaulting: o.defaulting,
93-
Validating: o.validation,
90+
91+
universe, err := model.NewUniverse(
92+
model.WithConfig(projectConfig),
93+
// TODO: missing model.WithBoilerplate[From], needs boilerplate or path
94+
model.WithResource(o.res, projectConfig),
95+
)
96+
if err != nil {
97+
log.Fatalf("error scaffolding webhook: %v", err)
9498
}
99+
95100
err = (&scaffold.Scaffold{}).Execute(
96-
&model.Universe{},
101+
universe,
97102
input.Options{},
98-
webhookScaffolder,
103+
&webhook.Webhook{
104+
Resource: o.res,
105+
Defaulting: o.defaulting,
106+
Validating: o.validation,
107+
},
99108
)
100109
if err != nil {
101-
fmt.Printf("error scaffolding webhook: %v", err)
102-
os.Exit(1)
110+
log.Fatalf("error scaffolding webhook: %v", err)
103111
}
104112

105113
err = (&scaffoldv2.Main{}).Update(

pkg/model/file.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
Copyright 2020 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package model
18+
19+
import (
20+
"sigs.k8s.io/kubebuilder/pkg/scaffold/input"
21+
)
22+
23+
// File describes a file that will be written
24+
type File struct {
25+
// Path is the file to write
26+
Path string `json:"path,omitempty"`
27+
28+
// Contents is the generated output
29+
Contents string `json:"contents,omitempty"`
30+
31+
// TODO: Move input.IfExistsAction into model
32+
// IfExistsAction determines what to do if the file exists
33+
IfExistsAction input.IfExistsAction `json:"ifExistsAction,omitempty"`
34+
}

pkg/model/types.go renamed to pkg/model/resource.go

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,8 @@ limitations under the License.
1616

1717
package model
1818

19-
import (
20-
"sigs.k8s.io/kubebuilder/pkg/scaffold/input"
21-
)
22-
23-
// Universe describes the entire state of file generation
24-
type Universe struct {
25-
Boilerplate string `json:"boilerplate,omitempty"`
26-
27-
Resource *Resource `json:"resource,omitempty"`
28-
29-
Files []*File `json:"files,omitempty"`
30-
31-
// Multigroup tracks if the project has more than one group
32-
MultiGroup bool `json:"multigroup,omitempty"`
33-
}
34-
3519
// Resource describes the resource currently being generated
36-
// TODO: Just use the resource type?
20+
// TODO: unify with pkg/scaffold/resource.Resource
3721
type Resource struct {
3822
// Namespaced is true if the resource is namespaced
3923
Namespaced bool `json:"namespaces,omitempty"`
@@ -59,16 +43,3 @@ type Resource struct {
5943
// GroupDomain is the Group + "." + Domain for the Resource
6044
GroupDomain string `json:"groupDomain,omitempty"`
6145
}
62-
63-
// File describes a file that will be written
64-
type File struct {
65-
// Path is the file to write
66-
Path string `json:"path,omitempty"`
67-
68-
// Contents is the generated output
69-
Contents string `json:"contents,omitempty"`
70-
71-
// TODO: Move input.IfExistsAction into model
72-
// IfExistsAction determines what to do if the file exists
73-
IfExistsAction input.IfExistsAction `json:"ifExistsAction,omitempty"`
74-
}

pkg/model/universe.go

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
Copyright 2020 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package model
18+
19+
import (
20+
"io/ioutil"
21+
"strings"
22+
23+
"github.com/gobuffalo/flect"
24+
25+
internalconfig "sigs.k8s.io/kubebuilder/internal/config"
26+
"sigs.k8s.io/kubebuilder/pkg/model/config"
27+
"sigs.k8s.io/kubebuilder/pkg/scaffold/resource"
28+
"sigs.k8s.io/kubebuilder/pkg/scaffold/util"
29+
)
30+
31+
// Universe describes the entire state of file generation
32+
type Universe struct {
33+
// Config stores the project configuration
34+
Config *config.Config `json:"config,omitempty"`
35+
36+
// Boilerplate is the copyright comment added at the top of scaffolded files
37+
Boilerplate string `json:"boilerplate,omitempty"`
38+
39+
// Resource contains the information of the API that is being scaffolded
40+
Resource *Resource `json:"resource,omitempty"`
41+
42+
// Files contains the model of the files that are being scaffolded
43+
Files []*File `json:"files,omitempty"`
44+
}
45+
46+
// NewUniverse creates a new Universe
47+
func NewUniverse(options ...UniverseOption) (*Universe, error) {
48+
universe := &Universe{}
49+
50+
// Apply options
51+
for _, option := range options {
52+
if err := option(universe); err != nil {
53+
return nil, err
54+
}
55+
}
56+
57+
return universe, nil
58+
}
59+
60+
// UniverseOption configure Universe
61+
type UniverseOption func(*Universe) error
62+
63+
// WithConfigFrom loads the project configuration from the provided path
64+
func WithConfigFrom(path string) UniverseOption {
65+
return func(universe *Universe) error {
66+
projectConfig, err := internalconfig.ReadFrom(path)
67+
if err != nil {
68+
return err
69+
}
70+
71+
universe.Config = projectConfig
72+
return nil
73+
}
74+
}
75+
76+
// WithConfig stores the already loaded project configuration
77+
func WithConfig(projectConfig *config.Config) UniverseOption {
78+
return func(universe *Universe) error {
79+
universe.Config = projectConfig
80+
return nil
81+
}
82+
}
83+
84+
// WithBoilerplateFrom loads the boilerplate from the provided path
85+
func WithBoilerplateFrom(path string) UniverseOption {
86+
return func(universe *Universe) error {
87+
boilerplate, err := ioutil.ReadFile(path)
88+
if err != nil {
89+
return err
90+
}
91+
92+
universe.Boilerplate = string(boilerplate)
93+
return nil
94+
}
95+
}
96+
97+
// WithBoilerplate stores the already loaded project configuration
98+
func WithBoilerplate(boilerplate string) UniverseOption {
99+
return func(universe *Universe) error {
100+
universe.Boilerplate = string(boilerplate)
101+
return nil
102+
}
103+
}
104+
105+
// WithoutBoilerplate is used for files that do not require a boilerplate
106+
func WithoutBoilerplate(universe *Universe) error {
107+
universe.Boilerplate = "-"
108+
return nil
109+
}
110+
111+
// WithResource stores the provided resource
112+
func WithResource(resource *resource.Resource, project *config.Config) UniverseOption {
113+
return func(universe *Universe) error {
114+
resourceModel := &Resource{
115+
Namespaced: resource.Namespaced,
116+
Group: resource.Group,
117+
Version: resource.Version,
118+
Kind: resource.Kind,
119+
Resource: resource.Resource,
120+
Plural: flect.Pluralize(strings.ToLower(resource.Kind)),
121+
}
122+
123+
resourceModel.GoPackage, resourceModel.GroupDomain = util.GetResourceInfo(
124+
resource,
125+
project.Repo,
126+
project.Domain,
127+
project.MultiGroup,
128+
)
129+
130+
universe.Resource = resourceModel
131+
return nil
132+
}
133+
}

0 commit comments

Comments
 (0)