Skip to content

Commit 612671c

Browse files
committed
Switch v2 scaffolding to controller-tools 0.2.0
This switches the v2 scaffolding to make use of (the upcoming) controller-tools v0.2.0, which has rewritten generators and a bundled deepcopy. It's currently using a prerelease version. controller-tools is now consumed as a binary, available via `go get` with modules turned on.
1 parent 7bf07e7 commit 612671c

20 files changed

+302
-151
lines changed

common.sh

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,6 @@ function fetch_tools {
126126
curl -sL ${kb_tools_download_url} -o "$kb_tools_archive_path"
127127
fi
128128
tar -zvxf "$kb_tools_archive_path" -C "$tmp_root/"
129-
130-
header_text "fetching controller-gen from source (till it's packaged)"
131-
GO111MODULE=on go get sigs.k8s.io/controller-tools/cmd/controller-gen@crdgeneratortmp
132129
}
133130

134131
function build_kb {

generated_golden.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ scaffold_test_project() {
5858
$kb create api --group policy --version v1beta1 --kind HealthCheckPolicy --example=false --controller=true --resource=true --namespaced=false --make=false
5959
elif [ $version == "2" ]; then
6060
export GO111MODULE=on
61-
go get sigs.k8s.io/controller-tools/cmd/controller-gen@crdgeneratortmp
6261
export PATH=$PATH:$(go env GOPATH)/bin
6362
go mod init sigs.k8s.io/kubebuilder/testdata/project_v2 # our repo autodetection will traverse up to the kb module if we don't do this
6463

pkg/scaffold/api.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,18 +148,13 @@ func (api *API) scaffoldV2() error {
148148

149149
err := (&Scaffold{}).Execute(
150150
input.Options{},
151-
&resourcev2.ResourceDoc{
152-
Input: input.Input{
153-
Path: filepath.Join("api", r.Version, "doc.go"),
154-
},
155-
Resource: r},
156-
&resourcev1.Types{
151+
&resourcev2.Types{
157152
Input: input.Input{
158153
Path: filepath.Join("api", r.Version, fmt.Sprintf("%s_types.go", strings.ToLower(r.Kind))),
159154
},
160155
Resource: r},
161156
&resourcev2.Group{Resource: r},
162-
&resourcev1.CRDSample{Resource: r},
157+
&resourcev2.CRDSample{Resource: r},
163158
&crdv2.EnableWebhookPatch{Resource: r},
164159
)
165160
if err != nil {

pkg/scaffold/project.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,12 @@ func (p *V2Project) Scaffold() error {
188188
&managerv2.Config{Image: imgName},
189189
&scaffoldv2.Main{},
190190
&scaffoldv2.GoMod{},
191-
&scaffoldv2.Doc{},
192191
&scaffoldv2.Makefile{Image: imgName},
193192
&scaffoldv2.Dockerfile{},
194193
&scaffoldv2.Kustomize{},
195194
&scaffoldv2.ManagerWebhookPatch{},
195+
&scaffoldv2.ManagerRoleBinding{},
196+
&scaffoldv2.KustomizeRBAC{},
196197
&managerv2.Kustomization{},
197198
&webhook.Kustomization{},
198199
&webhook.KustomizeConfigWebhook{},

pkg/scaffold/scaffold.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929

3030
"golang.org/x/tools/imports"
3131
yaml "gopkg.in/yaml.v2"
32-
"sigs.k8s.io/controller-tools/pkg/util"
3332
"sigs.k8s.io/kubebuilder/pkg/scaffold/input"
3433
"sigs.k8s.io/kubebuilder/pkg/scaffold/project"
3534
)

pkg/scaffold/v2/controller.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func (a *Controller) UpdateMain(path string) error {
8585

8686
reconcilerSetupCodeFragment := fmt.Sprintf(`err = (&controllers.%sReconciler{
8787
Client: mgr.GetClient(),
88-
Log: ctrl.Log.WithName("%s-controller"),
88+
Log: ctrl.Log.WithName("controllers").WithName("%s"),
8989
}).SetupWithManager(mgr)
9090
if err != nil {
9191
setupLog.Error(err, "unable to create controller", "controller", "%s")
@@ -158,9 +158,10 @@ type {{ .Resource.Kind }}Reconciler struct {
158158
159159
// +kubebuilder:rbac:groups={{.GroupDomain}},resources={{ .Plural }},verbs=get;list;watch;create;update;patch;delete
160160
// +kubebuilder:rbac:groups={{.GroupDomain}},resources={{ .Plural }}/status,verbs=get;update;patch
161+
161162
func (r *{{ .Resource.Kind }}Reconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
162163
_ = context.Background()
163-
_ = r.Log.WithValues("{{ .Resource.Kind }}", req.NamespacedName)
164+
_ = r.Log.WithValues("{{ .Resource.Kind | lower }}", req.NamespacedName)
164165
165166
// your logic here
166167

pkg/scaffold/v2/crd/enablewebhook_patch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type EnableWebhookPatch struct {
3636
// GetInput implements input.File
3737
func (p *EnableWebhookPatch) GetInput() (input.Input, error) {
3838
if p.Path == "" {
39-
p.Path = filepath.Join("config", "crds", "patches",
39+
p.Path = filepath.Join("config", "crd", "patches",
4040
fmt.Sprintf("webhook_in_%s.yaml", strings.ToLower(p.Resource.Kind)))
4141
}
4242
p.TemplateBody = enableWebhookPatchTemplate

pkg/scaffold/v2/crd/kustomization.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121
"path/filepath"
2222
"strings"
2323

24+
"github.com/markbates/inflect"
25+
2426
"sigs.k8s.io/kubebuilder/pkg/scaffold/input"
2527
"sigs.k8s.io/kubebuilder/pkg/scaffold/v1/resource"
2628
"sigs.k8s.io/kubebuilder/pkg/scaffold/v2/internal"
@@ -44,7 +46,7 @@ type Kustomization struct {
4446
// GetInput implements input.File
4547
func (c *Kustomization) GetInput() (input.Input, error) {
4648
if c.Path == "" {
47-
c.Path = filepath.Join("config", "crds", "kustomization.yaml")
49+
c.Path = filepath.Join("config", "crd", "kustomization.yaml")
4850
}
4951
c.TemplateBody = kustomizationTemplate
5052
c.Input.IfExistsAction = input.Error
@@ -53,13 +55,16 @@ func (c *Kustomization) GetInput() (input.Input, error) {
5355

5456
func (c *Kustomization) Update() error {
5557
if c.Path == "" {
56-
c.Path = filepath.Join("config", "crds", "kustomization.yaml")
58+
c.Path = filepath.Join("config", "crd", "kustomization.yaml")
5759
}
5860

59-
kustomizeResourceCodeFragment := fmt.Sprintf(`- bases/%s_%s.yaml
60-
`, c.Resource.Group, strings.ToLower(c.Resource.Kind))
61-
kustomizePatchCodeFragment := fmt.Sprintf(`#- patches/webhook_in_%s.yaml
62-
`, strings.ToLower(c.Resource.Kind))
61+
// TODO(directxman12): not technically valid if something changes from the default
62+
// (we'd need to parse the markers)
63+
rs := inflect.NewDefaultRuleset()
64+
plural := rs.Pluralize(strings.ToLower(c.Resource.Kind))
65+
66+
kustomizeResourceCodeFragment := fmt.Sprintf("- bases/%s.%s_%s.yaml\n", c.Resource.Group, c.Domain, plural)
67+
kustomizePatchCodeFragment := fmt.Sprintf("#- patches/webhook_in_%s.yaml\n", plural)
6368

6469
return internal.InsertStringsInFile(c.Path,
6570
kustomizeResourceScaffoldMarker, kustomizeResourceCodeFragment,

pkg/scaffold/v2/crd/kustomizeconfig.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ type KustomizeConfig struct {
3232
// GetInput implements input.File
3333
func (c *KustomizeConfig) GetInput() (input.Input, error) {
3434
if c.Path == "" {
35-
c.Path = filepath.Join("config", "crds", "kustomizeconfig.yaml")
35+
c.Path = filepath.Join("config", "crd", "kustomizeconfig.yaml")
3636
}
3737
c.TemplateBody = kustomizeConfigTemplate
3838
c.Input.IfExistsAction = input.Error

pkg/scaffold/v2/crd_sample.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
Copyright 2018 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 v2
18+
19+
import (
20+
"fmt"
21+
"path/filepath"
22+
"strings"
23+
24+
"sigs.k8s.io/kubebuilder/pkg/scaffold/input"
25+
"sigs.k8s.io/kubebuilder/pkg/scaffold/v1/resource"
26+
)
27+
28+
var _ input.File = &CRDSample{}
29+
30+
// CRDSample scaffolds a manifest for CRD sample.
31+
type CRDSample struct {
32+
input.Input
33+
34+
// Resource is a resource in the API group
35+
Resource *resource.Resource
36+
}
37+
38+
// GetInput implements input.File
39+
func (c *CRDSample) GetInput() (input.Input, error) {
40+
if c.Path == "" {
41+
c.Path = filepath.Join("config", "samples", fmt.Sprintf(
42+
"%s_%s_%s.yaml", c.Resource.Group, c.Resource.Version, strings.ToLower(c.Resource.Kind)))
43+
}
44+
45+
c.IfExistsAction = input.Error
46+
c.TemplateBody = crdSampleTemplate
47+
return c.Input, nil
48+
}
49+
50+
// Validate validates the values
51+
func (c *CRDSample) Validate() error {
52+
return c.Resource.Validate()
53+
}
54+
55+
var crdSampleTemplate = `apiVersion: {{ .Resource.Group }}.{{ .Domain }}/{{ .Resource.Version }}
56+
kind: {{ .Resource.Kind }}
57+
metadata:
58+
name: {{ lower .Resource.Kind }}-sample
59+
spec:
60+
# Add fields here
61+
foo: bar
62+
`

0 commit comments

Comments
 (0)