Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

πŸ› (kustomize/v1 and kustomize/v2-alpha) : ComponentConfig scaffolds should not be done by default #2826

Merged
merged 1 commit into from
Aug 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions docs/book/src/component-config-tutorial/api-changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,68 @@ mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), options)
With that out of the way, we can get on to defining our new config!

[tutorial-source]: https://github.com/kubernetes-sigs/kubebuilder/tree/master/docs/book/src/component-config-tutorial/testdata/project

Create the file `/config/manager/controller_manager_config.yaml` with the following content:

```yaml
apiVersion: controller-runtime.sigs.k8s.io/v1alpha1
kind: ControllerManagerConfig
health:
healthProbeBindAddress: :8081
metrics:
bindAddress: 127.0.0.1:8080
webhook:
port: 9443
leaderElection:
leaderElect: true
resourceName: ecaf1259.tutorial.kubebuilder.io
# leaderElectionReleaseOnCancel defines if the leader should step down volume
# when the Manager ends. This requires the binary to immediately end when the
# Manager is stopped, otherwise, this setting is unsafe. Setting this significantly
# speeds up voluntary leader transitions as the new leader don't have to wait
# LeaseDuration time first.
# In the default scaffold provided, the program ends immediately after
# the manager stops, so would be fine to enable this option. However,
# if you are doing or is intended to do any operation such as perform cleanups
# after the manager stops then its usage might be unsafe.
# leaderElectionReleaseOnCancel: true
```

Update the file `/config/manager/kustomization.yaml` by adding at the bottom the following content:

```yaml
generatorOptions:
disableNameSuffixHash: true

configMapGenerator:
- name: manager-config
files:
- controller_manager_config.yaml
```

Update the file `default/kustomization.yaml` by adding under the patchesStrategicMerge: the following patch:

# Mount the controller config file for loading manager configurations
# through a ComponentConfig type
- manager_config_patch.yaml

Update the file `default/manager_config_patch.yaml` by adding under the spec: the following patch:

```yaml
spec:
template:
spec:
containers:
- name: manager
args:
- "--config=controller_manager_config.yaml"
volumeMounts:
- name: manager-config
mountPath: /controller_manager_config.yaml
subPath: controller_manager_config.yaml
volumes:
- name: manager-config
configMap:
name: manager-config
```

11 changes: 8 additions & 3 deletions pkg/plugins/common/kustomize/v1/scaffolds/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (s *initScaffolder) Scaffold() error {
machinery.WithConfig(s.config),
)

laxmikantbpandhare marked this conversation as resolved.
Show resolved Hide resolved
return scaffold.Execute(
templates := []machinery.Builder{
&rbac.Kustomization{},
&rbac.AuthProxyRole{},
&rbac.AuthProxyRoleBinding{},
Expand All @@ -74,11 +74,16 @@ func (s *initScaffolder) Scaffold() error {
&rbac.ServiceAccount{},
&manager.Kustomization{},
&manager.Config{Image: imageName},
&manager.ControllerManagerConfig{},
&kdefault.Kustomization{},
&kdefault.ManagerAuthProxyPatch{},
&kdefault.ManagerConfigPatch{},
&prometheus.Kustomization{},
&prometheus.Monitor{},
)
}

if s.config.IsComponentConfig() {
laxmikantbpandhare marked this conversation as resolved.
Show resolved Hide resolved
templates = append(templates, &manager.ControllerManagerConfig{})
}

return scaffold.Execute(templates...)
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,10 @@ patchesStrategicMerge:
# endpoint w/o any authn/z, please comment the following line.
- manager_auth_proxy_patch.yaml

{{ if .ComponentConfig }}
# Mount the controller config file for loading manager configurations
# through a ComponentConfig type
{{ if not .ComponentConfig }}#{{ end }}- manager_config_patch.yaml
- manager_config_patch.yaml{{ end }}

# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
# crd/kustomization.yaml
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var _ machinery.Template = &ManagerConfigPatch{}
// ManagerConfigPatch scaffolds a ManagerConfigPatch for a Resource
type ManagerConfigPatch struct {
machinery.TemplateMixin
machinery.ComponentConfigMixin
}

// SetTemplateDefaults implements input.Template
Expand All @@ -50,6 +51,7 @@ spec:
spec:
containers:
- name: manager
{{- if .ComponentConfig }}
args:
- "--config=controller_manager_config.yaml"
volumeMounts:
Expand All @@ -60,4 +62,5 @@ spec:
- name: manager-config
configMap:
name: manager-config
{{- end }}
`
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var _ machinery.Template = &Kustomization{}
// Kustomization scaffolds a file that defines the kustomization scheme for the manager folder
type Kustomization struct {
machinery.TemplateMixin
machinery.ComponentConfigMixin
}

// SetTemplateDefaults implements file.Template
Expand All @@ -45,11 +46,14 @@ func (f *Kustomization) SetTemplateDefaults() error {
const kustomizeManagerTemplate = `resources:
- manager.yaml

{{- if .ComponentConfig }}

generatorOptions:
disableNameSuffixHash: true

configMapGenerator:
- name: manager-config
files:
- controller_manager_config.yaml
{{- end }}
`
11 changes: 8 additions & 3 deletions pkg/plugins/common/kustomize/v2-alpha/scaffolds/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (s *initScaffolder) Scaffold() error {
machinery.WithConfig(s.config),
)

return scaffold.Execute(
templates := []machinery.Builder{
&rbac.Kustomization{},
&rbac.AuthProxyRole{},
&rbac.AuthProxyRoleBinding{},
Expand All @@ -74,11 +74,16 @@ func (s *initScaffolder) Scaffold() error {
&rbac.ServiceAccount{},
&manager.Kustomization{},
&manager.Config{Image: imageName},
&manager.ControllerManagerConfig{},
&kdefault.Kustomization{},
&kdefault.ManagerAuthProxyPatch{},
&kdefault.ManagerConfigPatch{},
&prometheus.Kustomization{},
&prometheus.Monitor{},
)
}

if s.config.IsComponentConfig() {
templates = append(templates, &manager.ControllerManagerConfig{})
}

return scaffold.Execute(templates...)
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,10 @@ patchesStrategicMerge:
# endpoint w/o any authn/z, please comment the following line.
- manager_auth_proxy_patch.yaml

{{ if .ComponentConfig }}
# Mount the controller config file for loading manager configurations
# through a ComponentConfig type
{{ if not .ComponentConfig }}#{{ end }}- manager_config_patch.yaml
- manager_config_patch.yaml{{ end }}

# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
# crd/kustomization.yaml
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var _ machinery.Template = &ManagerConfigPatch{}
// ManagerConfigPatch scaffolds a ManagerConfigPatch for a Resource
type ManagerConfigPatch struct {
machinery.TemplateMixin
machinery.ComponentConfigMixin
}

// SetTemplateDefaults implements input.Template
Expand All @@ -50,6 +51,7 @@ spec:
spec:
containers:
- name: manager
{{- if .ComponentConfig }}
args:
- "--config=controller_manager_config.yaml"
volumeMounts:
Expand All @@ -60,4 +62,5 @@ spec:
- name: manager-config
configMap:
name: manager-config
{{- end }}
`
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var _ machinery.Template = &Kustomization{}
// Kustomization scaffolds a file that defines the kustomization scheme for the manager folder
type Kustomization struct {
machinery.TemplateMixin
machinery.ComponentConfigMixin
}

// SetTemplateDefaults implements file.Template
Expand All @@ -45,11 +46,14 @@ func (f *Kustomization) SetTemplateDefaults() error {
const kustomizeManagerTemplate = `resources:
- manager.yaml

{{- if .ComponentConfig }}

generatorOptions:
disableNameSuffixHash: true

configMapGenerator:
- name: manager-config
files:
- controller_manager_config.yaml
{{- end }}
`
107 changes: 107 additions & 0 deletions test/e2e/v3/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,113 @@ Count int `+"`"+`json:"count,omitempty"`+"`"+`
}
}

// GenerateV3 implements a go/v3(-alpha) plugin project defined by a TestContext.
func GenerateV3ComponentConfig(kbc *utils.TestContext, crdAndWebhookVersion string) {
var err error

By("initializing a project")
err = kbc.Init(
"--plugins", "go/v3",
"--project-version", "3",
"--domain", kbc.Domain,
"--fetch-deps=false",
"--component-config=true",
laxmikantbpandhare marked this conversation as resolved.
Show resolved Hide resolved
)
ExpectWithOffset(1, err).NotTo(HaveOccurred())

By("creating API definition")
err = kbc.CreateAPI(
"--group", kbc.Group,
"--version", kbc.Version,
"--kind", kbc.Kind,
"--namespaced",
"--resource",
"--controller",
"--make=false",
"--crd-version", crdAndWebhookVersion,
)
ExpectWithOffset(1, err).NotTo(HaveOccurred())

By("implementing the API")
ExpectWithOffset(1, pluginutil.InsertCode(
filepath.Join(kbc.Dir, "api", kbc.Version, fmt.Sprintf("%s_types.go", strings.ToLower(kbc.Kind))),
fmt.Sprintf(`type %sSpec struct {
`, kbc.Kind),
` // +optional
Count int `+"`"+`json:"count,omitempty"`+"`"+`
`)).Should(Succeed())

By("scaffolding mutating and validating webhooks")
err = kbc.CreateWebhook(
"--group", kbc.Group,
"--version", kbc.Version,
"--kind", kbc.Kind,
"--defaulting",
"--programmatic-validation",
"--webhook-version", crdAndWebhookVersion,
)
ExpectWithOffset(1, err).NotTo(HaveOccurred())

By("implementing the mutating and validating webhooks")
err = pluginutil.ImplementWebhooks(filepath.Join(
kbc.Dir, "api", kbc.Version,
fmt.Sprintf("%s_webhook.go", strings.ToLower(kbc.Kind))))
ExpectWithOffset(1, err).NotTo(HaveOccurred())

By("uncomment kustomization.yaml to enable webhook and ca injection")
ExpectWithOffset(1, pluginutil.UncommentCode(
filepath.Join(kbc.Dir, "config", "default", "kustomization.yaml"),
"#- ../webhook", "#")).To(Succeed())
ExpectWithOffset(1, pluginutil.UncommentCode(
filepath.Join(kbc.Dir, "config", "default", "kustomization.yaml"),
"#- ../certmanager", "#")).To(Succeed())
ExpectWithOffset(1, pluginutil.UncommentCode(
filepath.Join(kbc.Dir, "config", "default", "kustomization.yaml"),
"#- ../prometheus", "#")).To(Succeed())
ExpectWithOffset(1, pluginutil.UncommentCode(
filepath.Join(kbc.Dir, "config", "default", "kustomization.yaml"),
"#- manager_webhook_patch.yaml", "#")).To(Succeed())
ExpectWithOffset(1, pluginutil.UncommentCode(
filepath.Join(kbc.Dir, "config", "default", "kustomization.yaml"),
"#- webhookcainjection_patch.yaml", "#")).To(Succeed())
ExpectWithOffset(1, pluginutil.UncommentCode(filepath.Join(kbc.Dir, "config", "default", "kustomization.yaml"),
`#- name: CERTIFICATE_NAMESPACE # namespace of the certificate CR
# objref:
# kind: Certificate
# group: cert-manager.io
# version: v1
# name: serving-cert # this name should match the one in certificate.yaml
# fieldref:
# fieldpath: metadata.namespace
#- name: CERTIFICATE_NAME
# objref:
# kind: Certificate
# group: cert-manager.io
# version: v1
# name: serving-cert # this name should match the one in certificate.yaml
#- name: SERVICE_NAMESPACE # namespace of the service
# objref:
# kind: Service
# version: v1
# name: webhook-service
# fieldref:
# fieldpath: metadata.namespace
#- name: SERVICE_NAME
# objref:
# kind: Service
# version: v1
# name: webhook-service`, "#")).To(Succeed())

if crdAndWebhookVersion == "v1beta1" {
_ = pluginutil.RunCmd("Update dependencies", "go", "mod", "tidy")
}

if kbc.IsRestricted {
By("uncomment kustomize files to ensure that pods are restricted")
uncommentPodStandards(kbc)
}
}

func uncommentPodStandards(kbc *utils.TestContext) {
configManager := filepath.Join(kbc.Dir, "config", "manager", "manager.yaml")

Expand Down
11 changes: 11 additions & 0 deletions test/e2e/v3/plugin_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,17 @@ var _ = Describe("kubebuilder", func() {
GenerateV3(kbc, "v1beta1")
Run(kbc)
})
It("should generate a runnable project go/v3 with v1 CRDs and Webhooks"+
"with --component-config flag enabled", func() {
// Skip if cluster version < 1.16, when v1 CRDs and webhooks did not exist.
if srvVer := kbc.K8sVersion.ServerVersion; srvVer.GetMajorInt() <= 1 && srvVer.GetMinorInt() < 16 {
Skip(fmt.Sprintf("cluster version %s does not support v1 CRDs or webhooks",
srvVer.GitVersion))
}

GenerateV3ComponentConfig(kbc, "v1")
Run(kbc)
})
})
})
})
Expand Down
Loading