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

🐛 fix: preserve existing flags when applying metrics patch #3937

Merged
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
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
3 changes: 2 additions & 1 deletion docs/book/src/cronjob-tutorial/testdata/project/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ func main() {
var probeAddr string
var secureMetrics bool
var enableHTTP2 bool
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
flag.StringVar(&metricsAddr, "metrics-bind-address", "0", "The address the metric endpoint binds to. "+
"Use the port :8080. If not set, it will be '0 in order to disable the metrics server")
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
"Enable leader election for controller manager. "+
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: add
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 @@ -63,7 +63,6 @@ spec:
args:
- --leader-elect
- --health-probe-bind-address=:8081
- --metrics-bind-address=0
image: controller:latest
name: manager
securityContext:
Expand Down
3 changes: 2 additions & 1 deletion docs/book/src/getting-started/testdata/project/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ func main() {
var probeAddr string
var secureMetrics bool
var enableHTTP2 bool
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
flag.StringVar(&metricsAddr, "metrics-bind-address", "0", "The address the metric endpoint binds to. "+
"Use the port :8080. If not set, it will be '0 in order to disable the metrics server")
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
"Enable leader election for controller manager. "+
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: add
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 @@ -63,7 +63,6 @@ spec:
args:
- --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: add
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 @@ -109,7 +109,6 @@ spec:
args:
- --leader-elect
- --health-probe-bind-address=:8081
- --metrics-bind-address=0
image: {{ .Image }}
name: manager
securityContext:
Expand Down
3 changes: 2 additions & 1 deletion pkg/plugins/golang/v4/scaffolds/internal/templates/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@ func main() {
var probeAddr string
var secureMetrics bool
var enableHTTP2 bool
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
flag.StringVar(&metricsAddr, "metrics-bind-address", "0", "The address the metric endpoint binds to. " +
"Use the port :8080. If not set, it will be '0 in order to disable the metrics server")
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
"Enable leader election for controller manager. " +
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
3 changes: 2 additions & 1 deletion testdata/project-v4-multigroup-with-deploy-image/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ func main() {
var probeAddr string
var secureMetrics bool
var enableHTTP2 bool
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
flag.StringVar(&metricsAddr, "metrics-bind-address", "0", "The address the metric endpoint binds to. "+
"Use the port :8080. If not set, it will be '0 in order to disable the metrics server")
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
"Enable leader election for controller manager. "+
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: add
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 @@ -63,7 +63,6 @@ spec:
args:
- --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 @@ -1531,7 +1531,6 @@ spec:
- args:
- --leader-elect
- --health-probe-bind-address=:8081
- --metrics-bind-address=0
command:
- /manager
image: controller:latest
Expand Down
3 changes: 2 additions & 1 deletion testdata/project-v4-multigroup/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ func main() {
var probeAddr string
var secureMetrics bool
var enableHTTP2 bool
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
flag.StringVar(&metricsAddr, "metrics-bind-address", "0", "The address the metric endpoint binds to. "+
"Use the port :8080. If not set, it will be '0 in order to disable the metrics server")
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
"Enable leader election for controller manager. "+
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: add
path: /spec/template/spec/containers/0/args/0
value: --metrics-bind-address=:8080
1 change: 0 additions & 1 deletion testdata/project-v4-multigroup/config/manager/manager.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ spec:
args:
- --leader-elect
- --health-probe-bind-address=:8081
- --metrics-bind-address=0
image: controller:latest
name: manager
securityContext:
Expand Down
1 change: 0 additions & 1 deletion testdata/project-v4-multigroup/dist/install.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1531,7 +1531,6 @@ spec:
- args:
- --leader-elect
- --health-probe-bind-address=:8081
- --metrics-bind-address=0
command:
- /manager
image: controller:latest
Expand Down
3 changes: 2 additions & 1 deletion testdata/project-v4-with-deploy-image/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ func main() {
var probeAddr string
var secureMetrics bool
var enableHTTP2 bool
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
flag.StringVar(&metricsAddr, "metrics-bind-address", "0", "The address the metric endpoint binds to. "+
"Use the port :8080. If not set, it will be '0 in order to disable the metrics server")
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
"Enable leader election for controller manager. "+
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: add
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 @@ -63,7 +63,6 @@ spec:
args:
- --leader-elect
- --health-probe-bind-address=:8081
- --metrics-bind-address=0
image: controller:latest
name: manager
env:
Expand Down
1 change: 0 additions & 1 deletion testdata/project-v4-with-deploy-image/dist/install.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,6 @@ spec:
- args:
- --leader-elect
- --health-probe-bind-address=:8081
- --metrics-bind-address=0
command:
- /manager
env:
Expand Down
3 changes: 2 additions & 1 deletion testdata/project-v4-with-grafana/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ func main() {
var probeAddr string
var secureMetrics bool
var enableHTTP2 bool
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
flag.StringVar(&metricsAddr, "metrics-bind-address", "0", "The address the metric endpoint binds to. "+
"Use the port :8080. If not set, it will be '0 in order to disable the metrics server")
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
"Enable leader election for controller manager. "+
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
Loading
Loading