Skip to content

Commit 20828c7

Browse files
🐛 (kustomize/v1 and kustomize/v2-alpha) : ComponentConfig scaffolds should not be done by default (#2826)
ComponentConfig scaffolds should not be done by default updated testdata update v2-alpha and modified tests and added component-confit flag in it modified tests and enables componenet config flag removed unnecessary addition componenent config flag in v2 whenre this flag is not available removed for tests modified kustomization.yml file modified testdata added end to end tests with component config field marked as true line length is more than 122 characters mis spell updated doc modified docs modified docs modified default kustomization file too updated scaffold condition worked on review comments updated according to code review comments
1 parent 5c949c2 commit 20828c7

File tree

47 files changed

+413
-344
lines changed

Some content is hidden

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

47 files changed

+413
-344
lines changed

docs/book/src/component-config-tutorial/api-changes.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,68 @@ mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), options)
6767
With that out of the way, we can get on to defining our new config!
6868

6969
[tutorial-source]: https://github.com/kubernetes-sigs/kubebuilder/tree/master/docs/book/src/component-config-tutorial/testdata/project
70+
71+
Create the file `/config/manager/controller_manager_config.yaml` with the following content:
72+
73+
```yaml
74+
apiVersion: controller-runtime.sigs.k8s.io/v1alpha1
75+
kind: ControllerManagerConfig
76+
health:
77+
healthProbeBindAddress: :8081
78+
metrics:
79+
bindAddress: 127.0.0.1:8080
80+
webhook:
81+
port: 9443
82+
leaderElection:
83+
leaderElect: true
84+
resourceName: ecaf1259.tutorial.kubebuilder.io
85+
# leaderElectionReleaseOnCancel defines if the leader should step down volume
86+
# when the Manager ends. This requires the binary to immediately end when the
87+
# Manager is stopped, otherwise, this setting is unsafe. Setting this significantly
88+
# speeds up voluntary leader transitions as the new leader don't have to wait
89+
# LeaseDuration time first.
90+
# In the default scaffold provided, the program ends immediately after
91+
# the manager stops, so would be fine to enable this option. However,
92+
# if you are doing or is intended to do any operation such as perform cleanups
93+
# after the manager stops then its usage might be unsafe.
94+
# leaderElectionReleaseOnCancel: true
95+
```
96+
97+
Update the file `/config/manager/kustomization.yaml` by adding at the bottom the following content:
98+
99+
```yaml
100+
generatorOptions:
101+
disableNameSuffixHash: true
102+
103+
configMapGenerator:
104+
- name: manager-config
105+
files:
106+
- controller_manager_config.yaml
107+
```
108+
109+
Update the file `default/kustomization.yaml` by adding under the patchesStrategicMerge: the following patch:
110+
111+
# Mount the controller config file for loading manager configurations
112+
# through a ComponentConfig type
113+
- manager_config_patch.yaml
114+
115+
Update the file `default/manager_config_patch.yaml` by adding under the spec: the following patch:
116+
117+
```yaml
118+
spec:
119+
template:
120+
spec:
121+
containers:
122+
- name: manager
123+
args:
124+
- "--config=controller_manager_config.yaml"
125+
volumeMounts:
126+
- name: manager-config
127+
mountPath: /controller_manager_config.yaml
128+
subPath: controller_manager_config.yaml
129+
volumes:
130+
- name: manager-config
131+
configMap:
132+
name: manager-config
133+
```
134+

pkg/plugins/common/kustomize/v1/scaffolds/init.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func (s *initScaffolder) Scaffold() error {
6262
machinery.WithConfig(s.config),
6363
)
6464

65-
return scaffold.Execute(
65+
templates := []machinery.Builder{
6666
&rbac.Kustomization{},
6767
&rbac.AuthProxyRole{},
6868
&rbac.AuthProxyRoleBinding{},
@@ -74,11 +74,16 @@ func (s *initScaffolder) Scaffold() error {
7474
&rbac.ServiceAccount{},
7575
&manager.Kustomization{},
7676
&manager.Config{Image: imageName},
77-
&manager.ControllerManagerConfig{},
7877
&kdefault.Kustomization{},
7978
&kdefault.ManagerAuthProxyPatch{},
8079
&kdefault.ManagerConfigPatch{},
8180
&prometheus.Kustomization{},
8281
&prometheus.Monitor{},
83-
)
82+
}
83+
84+
if s.config.IsComponentConfig() {
85+
templates = append(templates, &manager.ControllerManagerConfig{})
86+
}
87+
88+
return scaffold.Execute(templates...)
8489
}

pkg/plugins/common/kustomize/v1/scaffolds/internal/templates/config/kdefault/kustomization.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,10 @@ patchesStrategicMerge:
7676
# endpoint w/o any authn/z, please comment the following line.
7777
- manager_auth_proxy_patch.yaml
7878
79+
{{ if .ComponentConfig }}
7980
# Mount the controller config file for loading manager configurations
8081
# through a ComponentConfig type
81-
{{ if not .ComponentConfig }}#{{ end }}- manager_config_patch.yaml
82+
- manager_config_patch.yaml{{ end }}
8283
8384
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
8485
# crd/kustomization.yaml

pkg/plugins/common/kustomize/v1/scaffolds/internal/templates/config/kdefault/manager_config_patch.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ var _ machinery.Template = &ManagerConfigPatch{}
2727
// ManagerConfigPatch scaffolds a ManagerConfigPatch for a Resource
2828
type ManagerConfigPatch struct {
2929
machinery.TemplateMixin
30+
machinery.ComponentConfigMixin
3031
}
3132

3233
// SetTemplateDefaults implements input.Template
@@ -50,6 +51,7 @@ spec:
5051
spec:
5152
containers:
5253
- name: manager
54+
{{- if .ComponentConfig }}
5355
args:
5456
- "--config=controller_manager_config.yaml"
5557
volumeMounts:
@@ -60,4 +62,5 @@ spec:
6062
- name: manager-config
6163
configMap:
6264
name: manager-config
65+
{{- end }}
6366
`

pkg/plugins/common/kustomize/v1/scaffolds/internal/templates/config/manager/kustomization.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ var _ machinery.Template = &Kustomization{}
2727
// Kustomization scaffolds a file that defines the kustomization scheme for the manager folder
2828
type Kustomization struct {
2929
machinery.TemplateMixin
30+
machinery.ComponentConfigMixin
3031
}
3132

3233
// SetTemplateDefaults implements file.Template
@@ -45,11 +46,14 @@ func (f *Kustomization) SetTemplateDefaults() error {
4546
const kustomizeManagerTemplate = `resources:
4647
- manager.yaml
4748
49+
{{- if .ComponentConfig }}
50+
4851
generatorOptions:
4952
disableNameSuffixHash: true
5053
5154
configMapGenerator:
5255
- name: manager-config
5356
files:
5457
- controller_manager_config.yaml
58+
{{- end }}
5559
`

pkg/plugins/common/kustomize/v2-alpha/scaffolds/init.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func (s *initScaffolder) Scaffold() error {
6262
machinery.WithConfig(s.config),
6363
)
6464

65-
return scaffold.Execute(
65+
templates := []machinery.Builder{
6666
&rbac.Kustomization{},
6767
&rbac.AuthProxyRole{},
6868
&rbac.AuthProxyRoleBinding{},
@@ -74,11 +74,16 @@ func (s *initScaffolder) Scaffold() error {
7474
&rbac.ServiceAccount{},
7575
&manager.Kustomization{},
7676
&manager.Config{Image: imageName},
77-
&manager.ControllerManagerConfig{},
7877
&kdefault.Kustomization{},
7978
&kdefault.ManagerAuthProxyPatch{},
8079
&kdefault.ManagerConfigPatch{},
8180
&prometheus.Kustomization{},
8281
&prometheus.Monitor{},
83-
)
82+
}
83+
84+
if s.config.IsComponentConfig() {
85+
templates = append(templates, &manager.ControllerManagerConfig{})
86+
}
87+
88+
return scaffold.Execute(templates...)
8489
}

pkg/plugins/common/kustomize/v2-alpha/scaffolds/internal/templates/config/kdefault/kustomization.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,10 @@ patchesStrategicMerge:
7878
# endpoint w/o any authn/z, please comment the following line.
7979
- manager_auth_proxy_patch.yaml
8080
81+
{{ if .ComponentConfig }}
8182
# Mount the controller config file for loading manager configurations
8283
# through a ComponentConfig type
83-
{{ if not .ComponentConfig }}#{{ end }}- manager_config_patch.yaml
84+
- manager_config_patch.yaml{{ end }}
8485
8586
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
8687
# crd/kustomization.yaml

pkg/plugins/common/kustomize/v2-alpha/scaffolds/internal/templates/config/kdefault/manager_config_patch.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ var _ machinery.Template = &ManagerConfigPatch{}
2727
// ManagerConfigPatch scaffolds a ManagerConfigPatch for a Resource
2828
type ManagerConfigPatch struct {
2929
machinery.TemplateMixin
30+
machinery.ComponentConfigMixin
3031
}
3132

3233
// SetTemplateDefaults implements input.Template
@@ -50,6 +51,7 @@ spec:
5051
spec:
5152
containers:
5253
- name: manager
54+
{{- if .ComponentConfig }}
5355
args:
5456
- "--config=controller_manager_config.yaml"
5557
volumeMounts:
@@ -60,4 +62,5 @@ spec:
6062
- name: manager-config
6163
configMap:
6264
name: manager-config
65+
{{- end }}
6366
`

pkg/plugins/common/kustomize/v2-alpha/scaffolds/internal/templates/config/manager/kustomization.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ var _ machinery.Template = &Kustomization{}
2727
// Kustomization scaffolds a file that defines the kustomization scheme for the manager folder
2828
type Kustomization struct {
2929
machinery.TemplateMixin
30+
machinery.ComponentConfigMixin
3031
}
3132

3233
// SetTemplateDefaults implements file.Template
@@ -45,11 +46,14 @@ func (f *Kustomization) SetTemplateDefaults() error {
4546
const kustomizeManagerTemplate = `resources:
4647
- manager.yaml
4748
49+
{{- if .ComponentConfig }}
50+
4851
generatorOptions:
4952
disableNameSuffixHash: true
5053
5154
configMapGenerator:
5255
- name: manager-config
5356
files:
5457
- controller_manager_config.yaml
58+
{{- end }}
5559
`

test/e2e/v3/generate_test.go

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,113 @@ Count int `+"`"+`json:"count,omitempty"`+"`"+`
235235
}
236236
}
237237

238+
// GenerateV3 implements a go/v3(-alpha) plugin project defined by a TestContext.
239+
func GenerateV3ComponentConfig(kbc *utils.TestContext, crdAndWebhookVersion string) {
240+
var err error
241+
242+
By("initializing a project")
243+
err = kbc.Init(
244+
"--plugins", "go/v3",
245+
"--project-version", "3",
246+
"--domain", kbc.Domain,
247+
"--fetch-deps=false",
248+
"--component-config=true",
249+
)
250+
ExpectWithOffset(1, err).NotTo(HaveOccurred())
251+
252+
By("creating API definition")
253+
err = kbc.CreateAPI(
254+
"--group", kbc.Group,
255+
"--version", kbc.Version,
256+
"--kind", kbc.Kind,
257+
"--namespaced",
258+
"--resource",
259+
"--controller",
260+
"--make=false",
261+
"--crd-version", crdAndWebhookVersion,
262+
)
263+
ExpectWithOffset(1, err).NotTo(HaveOccurred())
264+
265+
By("implementing the API")
266+
ExpectWithOffset(1, pluginutil.InsertCode(
267+
filepath.Join(kbc.Dir, "api", kbc.Version, fmt.Sprintf("%s_types.go", strings.ToLower(kbc.Kind))),
268+
fmt.Sprintf(`type %sSpec struct {
269+
`, kbc.Kind),
270+
` // +optional
271+
Count int `+"`"+`json:"count,omitempty"`+"`"+`
272+
`)).Should(Succeed())
273+
274+
By("scaffolding mutating and validating webhooks")
275+
err = kbc.CreateWebhook(
276+
"--group", kbc.Group,
277+
"--version", kbc.Version,
278+
"--kind", kbc.Kind,
279+
"--defaulting",
280+
"--programmatic-validation",
281+
"--webhook-version", crdAndWebhookVersion,
282+
)
283+
ExpectWithOffset(1, err).NotTo(HaveOccurred())
284+
285+
By("implementing the mutating and validating webhooks")
286+
err = pluginutil.ImplementWebhooks(filepath.Join(
287+
kbc.Dir, "api", kbc.Version,
288+
fmt.Sprintf("%s_webhook.go", strings.ToLower(kbc.Kind))))
289+
ExpectWithOffset(1, err).NotTo(HaveOccurred())
290+
291+
By("uncomment kustomization.yaml to enable webhook and ca injection")
292+
ExpectWithOffset(1, pluginutil.UncommentCode(
293+
filepath.Join(kbc.Dir, "config", "default", "kustomization.yaml"),
294+
"#- ../webhook", "#")).To(Succeed())
295+
ExpectWithOffset(1, pluginutil.UncommentCode(
296+
filepath.Join(kbc.Dir, "config", "default", "kustomization.yaml"),
297+
"#- ../certmanager", "#")).To(Succeed())
298+
ExpectWithOffset(1, pluginutil.UncommentCode(
299+
filepath.Join(kbc.Dir, "config", "default", "kustomization.yaml"),
300+
"#- ../prometheus", "#")).To(Succeed())
301+
ExpectWithOffset(1, pluginutil.UncommentCode(
302+
filepath.Join(kbc.Dir, "config", "default", "kustomization.yaml"),
303+
"#- manager_webhook_patch.yaml", "#")).To(Succeed())
304+
ExpectWithOffset(1, pluginutil.UncommentCode(
305+
filepath.Join(kbc.Dir, "config", "default", "kustomization.yaml"),
306+
"#- webhookcainjection_patch.yaml", "#")).To(Succeed())
307+
ExpectWithOffset(1, pluginutil.UncommentCode(filepath.Join(kbc.Dir, "config", "default", "kustomization.yaml"),
308+
`#- name: CERTIFICATE_NAMESPACE # namespace of the certificate CR
309+
# objref:
310+
# kind: Certificate
311+
# group: cert-manager.io
312+
# version: v1
313+
# name: serving-cert # this name should match the one in certificate.yaml
314+
# fieldref:
315+
# fieldpath: metadata.namespace
316+
#- name: CERTIFICATE_NAME
317+
# objref:
318+
# kind: Certificate
319+
# group: cert-manager.io
320+
# version: v1
321+
# name: serving-cert # this name should match the one in certificate.yaml
322+
#- name: SERVICE_NAMESPACE # namespace of the service
323+
# objref:
324+
# kind: Service
325+
# version: v1
326+
# name: webhook-service
327+
# fieldref:
328+
# fieldpath: metadata.namespace
329+
#- name: SERVICE_NAME
330+
# objref:
331+
# kind: Service
332+
# version: v1
333+
# name: webhook-service`, "#")).To(Succeed())
334+
335+
if crdAndWebhookVersion == "v1beta1" {
336+
_ = pluginutil.RunCmd("Update dependencies", "go", "mod", "tidy")
337+
}
338+
339+
if kbc.IsRestricted {
340+
By("uncomment kustomize files to ensure that pods are restricted")
341+
uncommentPodStandards(kbc)
342+
}
343+
}
344+
238345
func uncommentPodStandards(kbc *utils.TestContext) {
239346
configManager := filepath.Join(kbc.Dir, "config", "manager", "manager.yaml")
240347

0 commit comments

Comments
 (0)