Skip to content

Commit

Permalink
fix: preserve existing flags when applying metrics patch
Browse files Browse the repository at this point in the history
Ensure that enabling the manager_metrics_patch.yaml in config/default/kustomization.yaml does not overwrite existing arguments in config/manager/manager.yaml. The patch now appends the --metrics-bind-address argument without replacing other arguments.

More info: #3934
  • Loading branch information
camilamacedo86 committed May 21, 2024
1 parent 4f0e2c3 commit bd22646
Show file tree
Hide file tree
Showing 33 changed files with 95 additions and 116 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/test-sample-go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ jobs:
run: |
KUSTOMIZATION_FILE_PATH="testdata/project-v4/config/default/kustomization.yaml"
sed -i '25s/^#//' $KUSTOMIZATION_FILE_PATH
sed -i '27s/^#//' $KUSTOMIZATION_FILE_PATH
sed -i '42s/^#//' $KUSTOMIZATION_FILE_PATH
sed -i '46,142s/^#//' $KUSTOMIZATION_FILE_PATH
sed -i '39s/^#//' $KUSTOMIZATION_FILE_PATH
sed -i '44s/^#//' $KUSTOMIZATION_FILE_PATH
sed -i '48,144s/^#//' $KUSTOMIZATION_FILE_PATH
- name: Test
run: |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ patches:
# More info: https://book.kubebuilder.io/reference/metrics
# If you want to expose the metric endpoint of your controller-manager uncomment the following line.
#- path: manager_metrics_patch.yaml
# target:
# kind: Deployment

# [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
@@ -1,13 +1,4 @@
# This patch adds the args to allow exposing the metrics endpoint securely
apiVersion: apps/v1
kind: Deployment
metadata:
name: controller-manager
namespace: system
spec:
template:
spec:
containers:
- name: manager
args:
- "--metrics-bind-address=0.0.0.0:8080"
- op: replace
path: /spec/template/spec/containers/0/args/0
value: --metrics-bind-address=8080
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@ spec:
- command:
- /manager
args:
- --metrics-bind-address=0
# Add other args only bellow. Otherwise, it will make the patch
# in config/default/manager_metrics_patch.yaml stop to work
- --leader-elect
- --health-probe-bind-address=:8081
- --metrics-bind-address=0
image: controller:latest
name: manager
securityContext:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ patches:
# More info: https://book.kubebuilder.io/reference/metrics
# If you want to expose the metric endpoint of your controller-manager uncomment the following line.
#- path: manager_metrics_patch.yaml
# target:
# kind: Deployment

# [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
@@ -1,13 +1,4 @@
# This patch adds the args to allow exposing the metrics endpoint securely
apiVersion: apps/v1
kind: Deployment
metadata:
name: controller-manager
namespace: system
spec:
template:
spec:
containers:
- name: manager
args:
- "--metrics-bind-address=0.0.0.0:8080"
- op: replace
path: /spec/template/spec/containers/0/args/0
value: --metrics-bind-address=8080
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@ spec:
- command:
- /manager
args:
- --metrics-bind-address=0
# Add other args only bellow. Otherwise, it will make the patch
# in config/default/manager_metrics_patch.yaml stop to work
- --leader-elect
- --health-probe-bind-address=:8081
- --metrics-bind-address=0
image: controller:latest
name: manager
env:
Expand Down
7 changes: 5 additions & 2 deletions docs/book/src/reference/metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,12 @@ First, you will need enable the Metrics by uncommenting the following line
in the file `config/default/kustomization.yaml`, see:

```sh
# [Metrics] The following patch will enable the metrics endpoint.
# Ensure that you also protect this endpoint.
# [METRICS] The following patch will enable the metrics endpoint. Ensure that you also protect this endpoint.
# More info: https://book.kubebuilder.io/reference/metrics
# If you want to expose the metric endpoint of your controller-manager uncomment the following line.
#- path: manager_metrics_patch.yaml
# target:
# kind: Deployment
```

Note that projects are scaffolded by default passing the flag `--metrics-bind-address=0`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ patches:
# More info: https://book.kubebuilder.io/reference/metrics
# If you want to expose the metric endpoint of your controller-manager uncomment the following line.
#- path: manager_metrics_patch.yaml
# target:
# kind: Deployment
# [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 @@ -43,16 +43,7 @@ func (f *ManagerMetricsPatch) SetTemplateDefaults() error {
}

const kustomizeMetricsPatchTemplate = `# This patch adds the args to allow exposing the metrics endpoint securely
apiVersion: apps/v1
kind: Deployment
metadata:
name: controller-manager
namespace: system
spec:
template:
spec:
containers:
- name: manager
args:
- "--metrics-bind-address=0.0.0.0:8080"
- op: replace
path: /spec/template/spec/containers/0/args/0
value: --metrics-bind-address=8080
`
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,11 @@ spec:
- command:
- /manager
args:
- --metrics-bind-address=0
# Add other args only bellow. Otherwise, it will make the patch
# in config/default/manager_metrics_patch.yaml stop to work
- --leader-elect
- --health-probe-bind-address=:8081
- --metrics-bind-address=0
image: {{ .Image }}
name: manager
securityContext:
Expand Down
8 changes: 6 additions & 2 deletions test/e2e/v4/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func GenerateV4(kbc *utils.TestContext) {
"#- path: webhookcainjection_patch.yaml", "#")).To(Succeed())
ExpectWithOffset(1, pluginutil.UncommentCode(
filepath.Join(kbc.Dir, "config", "default", "kustomization.yaml"),
"#- path: manager_metrics_patch.yaml", "#")).To(Succeed())
metricsTarget, "#")).To(Succeed())

ExpectWithOffset(1, pluginutil.UncommentCode(filepath.Join(kbc.Dir, "config", "default", "kustomization.yaml"),
certManagerTarget, "#")).To(Succeed())
Expand Down Expand Up @@ -125,7 +125,7 @@ func GenerateV4WithoutWebhooks(kbc *utils.TestContext) {
"#- ../prometheus", "#")).To(Succeed())
ExpectWithOffset(1, pluginutil.UncommentCode(
filepath.Join(kbc.Dir, "config", "default", "kustomization.yaml"),
"#- path: manager_metrics_patch.yaml", "#")).To(Succeed())
metricsTarget, "#")).To(Succeed())

if kbc.IsRestricted {
By("uncomment kustomize files to ensure that pods are restricted")
Expand Down Expand Up @@ -166,6 +166,10 @@ func initingTheProject(kbc *utils.TestContext) {
ExpectWithOffset(1, err).NotTo(HaveOccurred())
}

const metricsTarget = `#- path: manager_metrics_patch.yaml
# target:
# kind: Deployment`

//nolint:lll
const certManagerTarget = `#replacements:
# - source: # Add cert-manager annotation to ValidatingWebhookConfiguration, MutatingWebhookConfiguration and CRDs
Expand Down
12 changes: 12 additions & 0 deletions test/e2e/v4/plugin_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,18 @@ func Run(kbc *utils.TestContext, hasWebhook, isToUseInstaller, hasMetrics bool)
}()
EventuallyWithOffset(1, verifyControllerUp, time.Minute, time.Second).Should(Succeed())

By("Checking if all flags are applied to the manager pod")
podOutput, err := kbc.Kubectl.Get(
true,
"pod", controllerPodName,
"-o", "jsonpath={.spec.containers[0].args}",
)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
ExpectWithOffset(1, podOutput).To(ContainSubstring("leader-elect"),
"Expected manager pod to have --leader-elect flag")
ExpectWithOffset(1, podOutput).To(ContainSubstring("health-probe-bind-address"),
"Expected manager pod to have --health-probe-bind-address flag")

By("validating the metrics endpoint")
_ = curlMetrics(kbc, hasMetrics)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ patches:
# More info: https://book.kubebuilder.io/reference/metrics
# If you want to expose the metric endpoint of your controller-manager uncomment the following line.
#- path: manager_metrics_patch.yaml
# target:
# kind: Deployment

# [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
@@ -1,13 +1,4 @@
# This patch adds the args to allow exposing the metrics endpoint securely
apiVersion: apps/v1
kind: Deployment
metadata:
name: controller-manager
namespace: system
spec:
template:
spec:
containers:
- name: manager
args:
- "--metrics-bind-address=0.0.0.0:8080"
- op: replace
path: /spec/template/spec/containers/0/args/0
value: --metrics-bind-address=8080
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@ spec:
- command:
- /manager
args:
- --metrics-bind-address=0
# Add other args only bellow. Otherwise, it will make the patch
# in config/default/manager_metrics_patch.yaml stop to work
- --leader-elect
- --health-probe-bind-address=:8081
- --metrics-bind-address=0
image: controller:latest
name: manager
securityContext:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1529,9 +1529,9 @@ spec:
spec:
containers:
- args:
- --metrics-bind-address=0
- --leader-elect
- --health-probe-bind-address=:8081
- --metrics-bind-address=0
command:
- /manager
image: controller:latest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ patches:
# More info: https://book.kubebuilder.io/reference/metrics
# If you want to expose the metric endpoint of your controller-manager uncomment the following line.
#- path: manager_metrics_patch.yaml
# target:
# kind: Deployment

# [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
@@ -1,13 +1,4 @@
# This patch adds the args to allow exposing the metrics endpoint securely
apiVersion: apps/v1
kind: Deployment
metadata:
name: controller-manager
namespace: system
spec:
template:
spec:
containers:
- name: manager
args:
- "--metrics-bind-address=0.0.0.0:8080"
- op: replace
path: /spec/template/spec/containers/0/args/0
value: --metrics-bind-address=8080
4 changes: 3 additions & 1 deletion testdata/project-v4-multigroup/config/manager/manager.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@ spec:
- command:
- /manager
args:
- --metrics-bind-address=0
# Add other args only bellow. Otherwise, it will make the patch
# in config/default/manager_metrics_patch.yaml stop to work
- --leader-elect
- --health-probe-bind-address=:8081
- --metrics-bind-address=0
image: controller:latest
name: manager
securityContext:
Expand Down
2 changes: 1 addition & 1 deletion testdata/project-v4-multigroup/dist/install.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1529,9 +1529,9 @@ spec:
spec:
containers:
- args:
- --metrics-bind-address=0
- --leader-elect
- --health-probe-bind-address=:8081
- --metrics-bind-address=0
command:
- /manager
image: controller:latest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ patches:
# More info: https://book.kubebuilder.io/reference/metrics
# If you want to expose the metric endpoint of your controller-manager uncomment the following line.
#- path: manager_metrics_patch.yaml
# target:
# kind: Deployment

# [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
@@ -1,13 +1,4 @@
# This patch adds the args to allow exposing the metrics endpoint securely
apiVersion: apps/v1
kind: Deployment
metadata:
name: controller-manager
namespace: system
spec:
template:
spec:
containers:
- name: manager
args:
- "--metrics-bind-address=0.0.0.0:8080"
- op: replace
path: /spec/template/spec/containers/0/args/0
value: --metrics-bind-address=8080
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@ spec:
- command:
- /manager
args:
- --metrics-bind-address=0
# Add other args only bellow. Otherwise, it will make the patch
# in config/default/manager_metrics_patch.yaml stop to work
- --leader-elect
- --health-probe-bind-address=:8081
- --metrics-bind-address=0
image: controller:latest
name: manager
env:
Expand Down
2 changes: 1 addition & 1 deletion testdata/project-v4-with-deploy-image/dist/install.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -605,9 +605,9 @@ spec:
spec:
containers:
- args:
- --metrics-bind-address=0
- --leader-elect
- --health-probe-bind-address=:8081
- --metrics-bind-address=0
command:
- /manager
env:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ patches:
# More info: https://book.kubebuilder.io/reference/metrics
# If you want to expose the metric endpoint of your controller-manager uncomment the following line.
#- path: manager_metrics_patch.yaml
# target:
# kind: Deployment

# [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
@@ -1,13 +1,4 @@
# This patch adds the args to allow exposing the metrics endpoint securely
apiVersion: apps/v1
kind: Deployment
metadata:
name: controller-manager
namespace: system
spec:
template:
spec:
containers:
- name: manager
args:
- "--metrics-bind-address=0.0.0.0:8080"
- op: replace
path: /spec/template/spec/containers/0/args/0
value: --metrics-bind-address=8080
4 changes: 3 additions & 1 deletion testdata/project-v4-with-grafana/config/manager/manager.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@ spec:
- command:
- /manager
args:
- --metrics-bind-address=0
# Add other args only bellow. Otherwise, it will make the patch
# in config/default/manager_metrics_patch.yaml stop to work
- --leader-elect
- --health-probe-bind-address=:8081
- --metrics-bind-address=0
image: controller:latest
name: manager
securityContext:
Expand Down
2 changes: 1 addition & 1 deletion testdata/project-v4-with-grafana/dist/install.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,9 @@ spec:
spec:
containers:
- args:
- --metrics-bind-address=0
- --leader-elect
- --health-probe-bind-address=:8081
- --metrics-bind-address=0
command:
- /manager
image: controller:latest
Expand Down
Loading

0 comments on commit bd22646

Please sign in to comment.