Skip to content

Commit a3e6e25

Browse files
author
Mengqi Yu
committed
✨ use new scaffolding and more kustomize features
1 parent 9672e57 commit a3e6e25

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1388
-122
lines changed

generated_golden.sh

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ scaffold_test_project() {
5151
$kb alpha webhook --group core --version v1 --kind Namespace --type=mutating --operations=update --make=false
5252
$kb create api --group policy --version v1beta1 --kind HealthCheckPolicy --example=false --controller=true --resource=true --namespaced=false --make=false
5353
elif [ $version == "2" ]; then
54+
$kb create api --group crew --version v1 --kind Captain --controller=true --resource=true --make=false
5455
$kb create api --group crew --version v1 --kind FirstMate --controller=true --resource=true --make=false
5556
$kb alpha webhook --group crew --version v1 --kind FirstMate --type=mutating --operations=create,update --make=false
5657
$kb alpha webhook --group crew --version v1 --kind FirstMate --type=mutating --operations=delete --make=false

pkg/scaffold/api.go

+17
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"sigs.k8s.io/kubebuilder/pkg/scaffold/v1/controller"
2727
resourcev1 "sigs.k8s.io/kubebuilder/pkg/scaffold/v1/resource"
2828
resourcev2 "sigs.k8s.io/kubebuilder/pkg/scaffold/v2"
29+
crdv2 "sigs.k8s.io/kubebuilder/pkg/scaffold/v2/crd"
2930
)
3031

3132
// API contains configuration for generating scaffolding for Go type
@@ -159,10 +160,26 @@ func (api *API) scaffoldV2() error {
159160
Resource: r},
160161
&resourcev2.Group{Resource: r},
161162
&resourcev1.CRDSample{Resource: r},
163+
&crdv2.EnableWebhookPatch{Resource: r},
162164
)
163165
if err != nil {
164166
return fmt.Errorf("error scaffolding APIs: %v", err)
165167
}
168+
169+
crdKustomization := &crdv2.Kustomization{Resource: r}
170+
err = (&Scaffold{}).Execute(
171+
input.Options{},
172+
crdKustomization,
173+
&crdv2.KustomizeConfig{},
174+
)
175+
if err != nil && !isAlreadyExistsError(err) {
176+
return fmt.Errorf("error scaffolding kustomization: %v", err)
177+
}
178+
179+
err = crdKustomization.Update()
180+
if err != nil {
181+
return fmt.Errorf("error updating kustomization.yaml: %v", err)
182+
}
166183
} else {
167184
// disable generation of example reconcile body if not scaffolding resource
168185
// because this could result in a fork-bomb of k8s resources where watching a

pkg/scaffold/project.go

+16-6
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ import (
2424
"sigs.k8s.io/kubebuilder/pkg/scaffold/v1/manager"
2525

2626
scaffoldv2 "sigs.k8s.io/kubebuilder/pkg/scaffold/v2"
27+
"sigs.k8s.io/kubebuilder/pkg/scaffold/v2/certmanager"
28+
managerv2 "sigs.k8s.io/kubebuilder/pkg/scaffold/v2/manager"
29+
"sigs.k8s.io/kubebuilder/pkg/scaffold/v2/webhook"
2730
)
2831

2932
// Project contains configuration for generating project scaffolding.
@@ -60,17 +63,11 @@ func (p *Project) Scaffold() error {
6063
return err
6164
}
6265

63-
// default controller manager image name
64-
imgName := "controller:latest"
65-
6666
s = &Scaffold{}
6767
err = s.Execute(
6868
input.Options{ProjectPath: projectInput.Path, BoilerplatePath: bpInput.Path},
69-
&manager.Config{Image: imgName},
7069
&project.GitIgnore{},
71-
&project.Kustomize{},
7270
&project.KustomizeRBAC{},
73-
&project.KustomizeManager{},
7471
&project.KustomizeImagePatch{},
7572
&project.KustomizePrometheusMetricsPatch{},
7673
&project.KustomizeAuthProxyPatch{},
@@ -105,9 +102,12 @@ func (p *Project) scaffoldV1() error {
105102
imgName := "controller:latest"
106103
return (&Scaffold{}).Execute(
107104
input.Options{ProjectPath: p.Info.Path, BoilerplatePath: p.Boilerplate.Path},
105+
&manager.Config{Image: imgName},
108106
&project.Makefile{Image: imgName},
109107
&project.GopkgToml{},
110108
&manager.Dockerfile{},
109+
&project.Kustomize{},
110+
&project.KustomizeManager{},
111111
&manager.APIs{},
112112
&manager.Controller{},
113113
&manager.Webhook{},
@@ -120,10 +120,20 @@ func (p *Project) scaffoldV2() error {
120120
imgName := "controller:latest"
121121
return (&Scaffold{}).Execute(
122122
input.Options{ProjectPath: p.Info.Path, BoilerplatePath: p.Boilerplate.Path},
123+
&managerv2.Config{Image: imgName},
123124
&scaffoldv2.Main{},
124125
&scaffoldv2.GopkgToml{},
125126
&scaffoldv2.Doc{},
126127
&scaffoldv2.Makefile{Image: imgName},
127128
&scaffoldv2.Dockerfile{},
129+
&scaffoldv2.Kustomize{},
130+
&scaffoldv2.ManagerWebhookPatch{},
131+
&managerv2.Kustomization{},
132+
&webhook.Kustomization{},
133+
&webhook.KustomizeConfigWebhook{},
134+
&webhook.InjectCAPatch{},
135+
&certmanager.CertManager{},
136+
&certmanager.Kustomization{},
137+
&certmanager.KustomizeConfig{},
128138
)
129139
}

pkg/scaffold/scaffold.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,19 @@ func (s *Scaffold) Execute(options input.Options, files ...input.File) error {
154154
return nil
155155
}
156156

157+
type errorAlreadyExists struct {
158+
path string
159+
}
160+
161+
func (e *errorAlreadyExists) Error() string {
162+
return fmt.Sprintf("%s already exists", e.path)
163+
}
164+
165+
func isAlreadyExistsError(e error) bool {
166+
_, ok := e.(*errorAlreadyExists)
167+
return ok
168+
}
169+
157170
// doFile scaffolds a single file
158171
func (s *Scaffold) doFile(e input.File) error {
159172
// Set common fields
@@ -175,7 +188,7 @@ func (s *Scaffold) doFile(e input.File) error {
175188
case input.Skip:
176189
return nil
177190
case input.Error:
178-
return fmt.Errorf("%s already exists", i.Path)
191+
return &errorAlreadyExists{path: i.Path}
179192
}
180193
}
181194

pkg/scaffold/v1/resource/crd.go

-82
This file was deleted.

pkg/scaffold/v1/resource/crd_sample.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
"sigs.k8s.io/kubebuilder/pkg/scaffold/input"
2525
)
2626

27-
var _ input.File = &CRD{}
27+
var _ input.File = &CRDSample{}
2828

2929
// CRDSample scaffolds a manifest for CRD sample.
3030
type CRDSample struct {
+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
Copyright 2019 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 certmanager
18+
19+
import (
20+
"path/filepath"
21+
22+
"sigs.k8s.io/kubebuilder/pkg/scaffold/input"
23+
)
24+
25+
// CertManager scaffolds an issuer CR and a certificate CR
26+
type CertManager struct {
27+
input.Input
28+
}
29+
30+
// GetInput implements input.File
31+
func (p *CertManager) GetInput() (input.Input, error) {
32+
if p.Path == "" {
33+
p.Path = filepath.Join("config", "certmanager", "certificate.yaml")
34+
}
35+
p.TemplateBody = certManagerTemplate
36+
return p.Input, nil
37+
}
38+
39+
var certManagerTemplate = `# The following manifests contain a self-signed issuer CR and a certificate CR.
40+
# More document can be found at https://docs.cert-manager.io
41+
apiVersion: certmanager.k8s.io/v1alpha1
42+
kind: Issuer
43+
metadata:
44+
name: selfsigned-issuer
45+
namespace: system
46+
spec:
47+
selfSigned: {}
48+
---
49+
apiVersion: certmanager.k8s.io/v1alpha1
50+
kind: Certificate
51+
metadata:
52+
name: serving-cert # this name should match the one appeared in kustomizeconfig.yaml
53+
namespace: system
54+
spec:
55+
# $(SERVICENAME) and $(NAMESPACE) will be substituted by kustomize
56+
commonName: $(SERVICENAME).$(NAMESPACE).svc
57+
dnsNames:
58+
- $(SERVICENAME).$(NAMESPACE).svc.cluster.local
59+
issuerRef:
60+
kind: Issuer
61+
name: selfsigned-issuer
62+
secretName: webhook-server-cert # this secret will not be prefixed, since it's not managed by kustomize
63+
`
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
Copyright 2019 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 certmanager
18+
19+
import (
20+
"path/filepath"
21+
22+
"sigs.k8s.io/kubebuilder/pkg/scaffold/input"
23+
)
24+
25+
// Kustomization scaffolds the kustomizaiton in the certmanager folder
26+
type Kustomization struct {
27+
input.Input
28+
}
29+
30+
// GetInput implements input.File
31+
func (p *Kustomization) GetInput() (input.Input, error) {
32+
if p.Path == "" {
33+
p.Path = filepath.Join("config", "certmanager", "kustomization.yaml")
34+
}
35+
p.TemplateBody = kustomizationTemplate
36+
return p.Input, nil
37+
}
38+
39+
var kustomizationTemplate = `resources:
40+
- certificate.yaml
41+
42+
vars:
43+
- name: CERTIFICATENAME
44+
objref:
45+
kind: Certificate
46+
group: certmanager.k8s.io
47+
version: v1alpha1
48+
name: serving-cert # this name should match the one in certificate.yaml
49+
50+
configurations:
51+
- kustomizeconfig.yaml
52+
`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
Copyright 2019 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 certmanager
18+
19+
import (
20+
"path/filepath"
21+
22+
"sigs.k8s.io/kubebuilder/pkg/scaffold/input"
23+
)
24+
25+
// KustomizeConfig scaffolds the kustomizeconfig in the certmanager folder
26+
type KustomizeConfig struct {
27+
input.Input
28+
}
29+
30+
// GetInput implements input.File
31+
func (p *KustomizeConfig) GetInput() (input.Input, error) {
32+
if p.Path == "" {
33+
p.Path = filepath.Join("config", "certmanager", "kustomizeconfig.yaml")
34+
}
35+
p.TemplateBody = kustomizeConfigTemplate
36+
return p.Input, nil
37+
}
38+
39+
var kustomizeConfigTemplate = `# This configuration is for teaching kustomize how to update name ref and var substitution
40+
nameReference:
41+
- kind: Issuer
42+
group: certmanager.k8s.io
43+
fieldSpecs:
44+
- kind: Certificate
45+
group: certmanager.k8s.io
46+
path: spec/issuerRef/name
47+
48+
varReference:
49+
- kind: Certificate
50+
group: certmanager.k8s.io
51+
path: spec/commonName
52+
- kind: Certificate
53+
group: certmanager.k8s.io
54+
path: spec/dnsNames
55+
`

0 commit comments

Comments
 (0)