Skip to content

Commit 16d5cef

Browse files
authored
Merge pull request #3937 from camilamacedo86/fix-patch-metrics
🐛 fix: preserve existing flags when applying metrics patch
2 parents 1798f4a + d05aae5 commit 16d5cef

File tree

41 files changed

+82
-124
lines changed

Some content is hidden

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

41 files changed

+82
-124
lines changed

.github/workflows/test-sample-go.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ jobs:
2424
run: |
2525
KUSTOMIZATION_FILE_PATH="testdata/project-v4/config/default/kustomization.yaml"
2626
sed -i '25s/^#//' $KUSTOMIZATION_FILE_PATH
27-
sed -i '27s/^#//' $KUSTOMIZATION_FILE_PATH
28-
sed -i '42s/^#//' $KUSTOMIZATION_FILE_PATH
29-
sed -i '46,142s/^#//' $KUSTOMIZATION_FILE_PATH
27+
sed -i '39s/^#//' $KUSTOMIZATION_FILE_PATH
28+
sed -i '44s/^#//' $KUSTOMIZATION_FILE_PATH
29+
sed -i '48,144s/^#//' $KUSTOMIZATION_FILE_PATH
3030
3131
- name: Test
3232
run: |

docs/book/src/cronjob-tutorial/testdata/project/cmd/main.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ func main() {
7676
var probeAddr string
7777
var secureMetrics bool
7878
var enableHTTP2 bool
79-
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
79+
flag.StringVar(&metricsAddr, "metrics-bind-address", "0", "The address the metric endpoint binds to. "+
80+
"Use the port :8080. If not set, it will be '0 in order to disable the metrics server")
8081
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
8182
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
8283
"Enable leader election for controller manager. "+

docs/book/src/cronjob-tutorial/testdata/project/config/default/kustomization.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ patches:
3131
# More info: https://book.kubebuilder.io/reference/metrics
3232
# If you want to expose the metric endpoint of your controller-manager uncomment the following line.
3333
#- path: manager_metrics_patch.yaml
34+
# target:
35+
# kind: Deployment
3436

3537
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
3638
# crd/kustomization.yaml
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,4 @@
11
# This patch adds the args to allow exposing the metrics endpoint securely
2-
apiVersion: apps/v1
3-
kind: Deployment
4-
metadata:
5-
name: controller-manager
6-
namespace: system
7-
spec:
8-
template:
9-
spec:
10-
containers:
11-
- name: manager
12-
args:
13-
- "--metrics-bind-address=0.0.0.0:8080"
2+
- op: add
3+
path: /spec/template/spec/containers/0/args/0
4+
value: --metrics-bind-address=:8080

docs/book/src/cronjob-tutorial/testdata/project/config/manager/manager.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ spec:
6363
args:
6464
- --leader-elect
6565
- --health-probe-bind-address=:8081
66-
- --metrics-bind-address=0
6766
image: controller:latest
6867
name: manager
6968
securityContext:

docs/book/src/getting-started/testdata/project/cmd/main.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ func main() {
5757
var probeAddr string
5858
var secureMetrics bool
5959
var enableHTTP2 bool
60-
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
60+
flag.StringVar(&metricsAddr, "metrics-bind-address", "0", "The address the metric endpoint binds to. "+
61+
"Use the port :8080. If not set, it will be '0 in order to disable the metrics server")
6162
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
6263
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
6364
"Enable leader election for controller manager. "+

docs/book/src/getting-started/testdata/project/config/default/kustomization.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ patches:
3131
# More info: https://book.kubebuilder.io/reference/metrics
3232
# If you want to expose the metric endpoint of your controller-manager uncomment the following line.
3333
#- path: manager_metrics_patch.yaml
34+
# target:
35+
# kind: Deployment
3436

3537
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
3638
# crd/kustomization.yaml
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,4 @@
11
# This patch adds the args to allow exposing the metrics endpoint securely
2-
apiVersion: apps/v1
3-
kind: Deployment
4-
metadata:
5-
name: controller-manager
6-
namespace: system
7-
spec:
8-
template:
9-
spec:
10-
containers:
11-
- name: manager
12-
args:
13-
- "--metrics-bind-address=0.0.0.0:8080"
2+
- op: add
3+
path: /spec/template/spec/containers/0/args/0
4+
value: --metrics-bind-address=:8080

docs/book/src/getting-started/testdata/project/config/manager/manager.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ spec:
6363
args:
6464
- --leader-elect
6565
- --health-probe-bind-address=:8081
66-
- --metrics-bind-address=0
6766
image: controller:latest
6867
name: manager
6968
env:

docs/book/src/reference/metrics.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,12 @@ First, you will need enable the Metrics by uncommenting the following line
4646
in the file `config/default/kustomization.yaml`, see:
4747

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

5457
Note that projects are scaffolded by default passing the flag `--metrics-bind-address=0`

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

+2
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ patches:
7676
# More info: https://book.kubebuilder.io/reference/metrics
7777
# If you want to expose the metric endpoint of your controller-manager uncomment the following line.
7878
#- path: manager_metrics_patch.yaml
79+
# target:
80+
# kind: Deployment
7981
8082
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
8183
# crd/kustomization.yaml

pkg/plugins/common/kustomize/v2/scaffolds/internal/templates/config/kdefault/enable_matrics_patch.go pkg/plugins/common/kustomize/v2/scaffolds/internal/templates/config/kdefault/manager_metrics_patch.go

+3-12
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,7 @@ func (f *ManagerMetricsPatch) SetTemplateDefaults() error {
4343
}
4444

4545
const kustomizeMetricsPatchTemplate = `# This patch adds the args to allow exposing the metrics endpoint securely
46-
apiVersion: apps/v1
47-
kind: Deployment
48-
metadata:
49-
name: controller-manager
50-
namespace: system
51-
spec:
52-
template:
53-
spec:
54-
containers:
55-
- name: manager
56-
args:
57-
- "--metrics-bind-address=0.0.0.0:8080"
46+
- op: add
47+
path: /spec/template/spec/containers/0/args/0
48+
value: --metrics-bind-address=:8080
5849
`

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

-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ spec:
109109
args:
110110
- --leader-elect
111111
- --health-probe-bind-address=:8081
112-
- --metrics-bind-address=0
113112
image: {{ .Image }}
114113
name: manager
115114
securityContext:

pkg/plugins/golang/v4/scaffolds/internal/templates/main.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,8 @@ func main() {
222222
var probeAddr string
223223
var secureMetrics bool
224224
var enableHTTP2 bool
225-
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
225+
flag.StringVar(&metricsAddr, "metrics-bind-address", "0", "The address the metric endpoint binds to. " +
226+
"Use the port :8080. If not set, it will be '0 in order to disable the metrics server")
226227
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
227228
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
228229
"Enable leader election for controller manager. " +

test/e2e/v4/generate_test.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func GenerateV4(kbc *utils.TestContext) {
6565
"#- path: webhookcainjection_patch.yaml", "#")).To(Succeed())
6666
ExpectWithOffset(1, pluginutil.UncommentCode(
6767
filepath.Join(kbc.Dir, "config", "default", "kustomization.yaml"),
68-
"#- path: manager_metrics_patch.yaml", "#")).To(Succeed())
68+
metricsTarget, "#")).To(Succeed())
6969

7070
ExpectWithOffset(1, pluginutil.UncommentCode(filepath.Join(kbc.Dir, "config", "default", "kustomization.yaml"),
7171
certManagerTarget, "#")).To(Succeed())
@@ -125,7 +125,7 @@ func GenerateV4WithoutWebhooks(kbc *utils.TestContext) {
125125
"#- ../prometheus", "#")).To(Succeed())
126126
ExpectWithOffset(1, pluginutil.UncommentCode(
127127
filepath.Join(kbc.Dir, "config", "default", "kustomization.yaml"),
128-
"#- path: manager_metrics_patch.yaml", "#")).To(Succeed())
128+
metricsTarget, "#")).To(Succeed())
129129

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

169+
const metricsTarget = `#- path: manager_metrics_patch.yaml
170+
# target:
171+
# kind: Deployment`
172+
169173
//nolint:lll
170174
const certManagerTarget = `#replacements:
171175
# - source: # Add cert-manager annotation to ValidatingWebhookConfiguration, MutatingWebhookConfiguration and CRDs

test/e2e/v4/plugin_cluster_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,18 @@ func Run(kbc *utils.TestContext, hasWebhook, isToUseInstaller, hasMetrics bool)
163163
}()
164164
EventuallyWithOffset(1, verifyControllerUp, time.Minute, time.Second).Should(Succeed())
165165

166+
By("Checking if all flags are applied to the manager pod")
167+
podOutput, err := kbc.Kubectl.Get(
168+
true,
169+
"pod", controllerPodName,
170+
"-o", "jsonpath={.spec.containers[0].args}",
171+
)
172+
ExpectWithOffset(1, err).NotTo(HaveOccurred())
173+
ExpectWithOffset(1, podOutput).To(ContainSubstring("leader-elect"),
174+
"Expected manager pod to have --leader-elect flag")
175+
ExpectWithOffset(1, podOutput).To(ContainSubstring("health-probe-bind-address"),
176+
"Expected manager pod to have --health-probe-bind-address flag")
177+
166178
By("validating the metrics endpoint")
167179
_ = curlMetrics(kbc, hasMetrics)
168180

testdata/project-v4-multigroup-with-deploy-image/cmd/main.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ func main() {
8282
var probeAddr string
8383
var secureMetrics bool
8484
var enableHTTP2 bool
85-
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
85+
flag.StringVar(&metricsAddr, "metrics-bind-address", "0", "The address the metric endpoint binds to. "+
86+
"Use the port :8080. If not set, it will be '0 in order to disable the metrics server")
8687
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
8788
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
8889
"Enable leader election for controller manager. "+

testdata/project-v4-multigroup-with-deploy-image/config/default/kustomization.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ patches:
3131
# More info: https://book.kubebuilder.io/reference/metrics
3232
# If you want to expose the metric endpoint of your controller-manager uncomment the following line.
3333
#- path: manager_metrics_patch.yaml
34+
# target:
35+
# kind: Deployment
3436

3537
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
3638
# crd/kustomization.yaml
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,4 @@
11
# This patch adds the args to allow exposing the metrics endpoint securely
2-
apiVersion: apps/v1
3-
kind: Deployment
4-
metadata:
5-
name: controller-manager
6-
namespace: system
7-
spec:
8-
template:
9-
spec:
10-
containers:
11-
- name: manager
12-
args:
13-
- "--metrics-bind-address=0.0.0.0:8080"
2+
- op: add
3+
path: /spec/template/spec/containers/0/args/0
4+
value: --metrics-bind-address=:8080

testdata/project-v4-multigroup-with-deploy-image/config/manager/manager.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ spec:
6363
args:
6464
- --leader-elect
6565
- --health-probe-bind-address=:8081
66-
- --metrics-bind-address=0
6766
image: controller:latest
6867
name: manager
6968
securityContext:

testdata/project-v4-multigroup-with-deploy-image/dist/install.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -1531,7 +1531,6 @@ spec:
15311531
- args:
15321532
- --leader-elect
15331533
- --health-probe-bind-address=:8081
1534-
- --metrics-bind-address=0
15351534
command:
15361535
- /manager
15371536
image: controller:latest

testdata/project-v4-multigroup/cmd/main.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ func main() {
8282
var probeAddr string
8383
var secureMetrics bool
8484
var enableHTTP2 bool
85-
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
85+
flag.StringVar(&metricsAddr, "metrics-bind-address", "0", "The address the metric endpoint binds to. "+
86+
"Use the port :8080. If not set, it will be '0 in order to disable the metrics server")
8687
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
8788
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
8889
"Enable leader election for controller manager. "+

testdata/project-v4-multigroup/config/default/kustomization.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ patches:
3131
# More info: https://book.kubebuilder.io/reference/metrics
3232
# If you want to expose the metric endpoint of your controller-manager uncomment the following line.
3333
#- path: manager_metrics_patch.yaml
34+
# target:
35+
# kind: Deployment
3436

3537
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
3638
# crd/kustomization.yaml
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,4 @@
11
# This patch adds the args to allow exposing the metrics endpoint securely
2-
apiVersion: apps/v1
3-
kind: Deployment
4-
metadata:
5-
name: controller-manager
6-
namespace: system
7-
spec:
8-
template:
9-
spec:
10-
containers:
11-
- name: manager
12-
args:
13-
- "--metrics-bind-address=0.0.0.0:8080"
2+
- op: add
3+
path: /spec/template/spec/containers/0/args/0
4+
value: --metrics-bind-address=:8080

testdata/project-v4-multigroup/config/manager/manager.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ spec:
6363
args:
6464
- --leader-elect
6565
- --health-probe-bind-address=:8081
66-
- --metrics-bind-address=0
6766
image: controller:latest
6867
name: manager
6968
securityContext:

testdata/project-v4-multigroup/dist/install.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -1531,7 +1531,6 @@ spec:
15311531
- args:
15321532
- --leader-elect
15331533
- --health-probe-bind-address=:8081
1534-
- --metrics-bind-address=0
15351534
command:
15361535
- /manager
15371536
image: controller:latest

testdata/project-v4-with-deploy-image/cmd/main.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ func main() {
5757
var probeAddr string
5858
var secureMetrics bool
5959
var enableHTTP2 bool
60-
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
60+
flag.StringVar(&metricsAddr, "metrics-bind-address", "0", "The address the metric endpoint binds to. "+
61+
"Use the port :8080. If not set, it will be '0 in order to disable the metrics server")
6162
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
6263
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
6364
"Enable leader election for controller manager. "+

testdata/project-v4-with-deploy-image/config/default/kustomization.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ patches:
3131
# More info: https://book.kubebuilder.io/reference/metrics
3232
# If you want to expose the metric endpoint of your controller-manager uncomment the following line.
3333
#- path: manager_metrics_patch.yaml
34+
# target:
35+
# kind: Deployment
3436

3537
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
3638
# crd/kustomization.yaml
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,4 @@
11
# This patch adds the args to allow exposing the metrics endpoint securely
2-
apiVersion: apps/v1
3-
kind: Deployment
4-
metadata:
5-
name: controller-manager
6-
namespace: system
7-
spec:
8-
template:
9-
spec:
10-
containers:
11-
- name: manager
12-
args:
13-
- "--metrics-bind-address=0.0.0.0:8080"
2+
- op: add
3+
path: /spec/template/spec/containers/0/args/0
4+
value: --metrics-bind-address=:8080

testdata/project-v4-with-deploy-image/config/manager/manager.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ spec:
6363
args:
6464
- --leader-elect
6565
- --health-probe-bind-address=:8081
66-
- --metrics-bind-address=0
6766
image: controller:latest
6867
name: manager
6968
env:

testdata/project-v4-with-deploy-image/dist/install.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,6 @@ spec:
607607
- args:
608608
- --leader-elect
609609
- --health-probe-bind-address=:8081
610-
- --metrics-bind-address=0
611610
command:
612611
- /manager
613612
env:

testdata/project-v4-with-grafana/cmd/main.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ func main() {
5353
var probeAddr string
5454
var secureMetrics bool
5555
var enableHTTP2 bool
56-
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
56+
flag.StringVar(&metricsAddr, "metrics-bind-address", "0", "The address the metric endpoint binds to. "+
57+
"Use the port :8080. If not set, it will be '0 in order to disable the metrics server")
5758
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
5859
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
5960
"Enable leader election for controller manager. "+

testdata/project-v4-with-grafana/config/default/kustomization.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ patches:
3131
# More info: https://book.kubebuilder.io/reference/metrics
3232
# If you want to expose the metric endpoint of your controller-manager uncomment the following line.
3333
#- path: manager_metrics_patch.yaml
34+
# target:
35+
# kind: Deployment
3436

3537
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
3638
# crd/kustomization.yaml

0 commit comments

Comments
 (0)