From e78c8c4dcec53a2a8bb8dadc604c3df7f26e9145 Mon Sep 17 00:00:00 2001 From: Igor Kliushnikov Date: Wed, 23 Sep 2020 16:39:07 +0000 Subject: [PATCH 1/2] [metricbeat] support deployment/daemonset specific metrics --- metricbeat/README.md | 4 +- metricbeat/templates/daemonset.yaml | 12 ++++ metricbeat/templates/deployment.yaml | 15 +++++ metricbeat/tests/metricbeat_test.py | 87 ++++++++++++++++++++++++++-- metricbeat/values.yaml | 8 ++- 5 files changed, 117 insertions(+), 9 deletions(-) diff --git a/metricbeat/README.md b/metricbeat/README.md index 904e624be..88e28e195 100644 --- a/metricbeat/README.md +++ b/metricbeat/README.md @@ -90,6 +90,7 @@ as a reference. They are also used in the automated testing of this chart. |--------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------| | `clusterRoleRules` | Configurable [cluster role rules][] that Metricbeat uses to access Kubernetes resources | see [values.yaml][] | | `daemonset.annotations` | Configurable [annotations][] for Metricbeat daemonset | `{}` | +| `daemonset.labels` | Configurable [labels][] applied to all Metricbeat DaemonSet pods | `{}` | | `daemonset.affinity` | Configurable [affinity][] for Metricbeat daemonset | `{}` | | `daemonset.enabled` | If true, enable daemonset | `true` | | `daemonset.envFrom` | Templatable string of `envFrom` to be passed to the [environment from variables][] which will be appended to Metricbeat container for DaemonSet | `[]` | @@ -104,6 +105,7 @@ as a reference. They are also used in the automated testing of this chart. | `daemonset.securityContext` | Configurable [securityContext][] for Metricbeat DaemonSet pod execution environment | see [values.yaml][] | | `daemonset.tolerations` | Configurable [tolerations][] for Metricbeat DaemonSet | `[]` | | `deployment.annotations` | Configurable [annotations][] for Metricbeat Deployment | `{}` | +| `deployment.labels` | Configurable [labels][] applied to all Metricbeat Deployment pods | `{}` | | `deployment.affinity` | Configurable [affinity][] for Metricbeat Deployment | `{}` | | `deployment.enabled` | If true, enable deployment | `true` | | `deployment.envFrom` | Templatable string of `envFrom` to be passed to the [environment from variables][] which will be appended to Metricbeat container for Deployment | `[]` | @@ -126,7 +128,6 @@ as a reference. They are also used in the automated testing of this chart. | `image` | The Metricbeat Docker image | `docker.elastic.co/beats/metricbeat` | | `kube_state_metrics.enabled` | Install [kube-state-metrics](https://github.com/helm/charts/tree/master/stable/kube-state-metrics) as a dependency | `true` | | `kube_state_metrics.host` | Define kube-state-metrics endpoint for an existing deployment. Works only if `kube_state_metrics.enabled: false` | `""` | -| `labels` | Configurable [labels][] applied to all Metricbeat pods | `{}` | | `livenessProbe` | Parameters to pass to liveness [probe][] checks for values such as timeouts and thresholds | see [values.yaml][] | | `managedServiceAccount` | Whether the `serviceAccount` should be managed by this helm chart. Set this to `false` in order to manage your own service account and related roles | `true` | | `nameOverride` | Overrides the chart name for resources. If not set the name will default to `.Chart.Name` | `""` | @@ -155,6 +156,7 @@ as a reference. They are also used in the automated testing of this chart. | `resources` | Allows you to set the [resources][] for both Metricbeat DaemonSet and Deployment | `{}` | | `secretMounts` | Allows you easily mount a secret as a file inside DaemonSet and Deployment Useful for mounting certificates and other secrets | `[]` | | `tolerations` | Configurable [tolerations][] for both Metricbeat DaemonSet and Deployment | `[]` | +| `labels` | Configurable [labels][] applied to all Metricbeat pods | `[]` | ## FAQ diff --git a/metricbeat/templates/daemonset.yaml b/metricbeat/templates/daemonset.yaml index 14a6ef6d6..4779e756c 100644 --- a/metricbeat/templates/daemonset.yaml +++ b/metricbeat/templates/daemonset.yaml @@ -9,9 +9,15 @@ metadata: chart: "{{ .Chart.Name }}-{{ .Chart.Version }}" heritage: {{ .Release.Service | quote }} release: {{ .Release.Name | quote }} + {{- if .Values.daemonset.labels }} + {{- range $key, $value := .Values.daemonset.labels }} + {{ $key }}: {{ $value | quote }} + {{- end }} + {{- else }} {{- range $key, $value := .Values.labels }} {{ $key }}: {{ $value | quote }} {{- end }} + {{- end }} {{- if .Values.daemonset.annotations}} annotations: {{- range $key, $value := .Values.daemonset.annotations }} @@ -41,9 +47,15 @@ spec: chart: "{{ .Chart.Name }}-{{ .Chart.Version }}" heritage: {{ .Release.Service | quote }} release: {{ .Release.Name | quote }} + {{- if .Values.daemonset.labels }} + {{- range $key, $value := .Values.daemonset.labels }} + {{ $key }}: {{ $value | quote }} + {{- end }} + {{- else }} {{- range $key, $value := .Values.labels }} {{ $key }}: {{ $value | quote }} {{- end }} + {{- end }} spec: affinity: {{ toYaml ( .Values.affinity | default .Values.daemonset.affinity ) | nindent 8 }} nodeSelector: {{ toYaml ( .Values.nodeSelector | default .Values.daemonset.nodeSelector ) | nindent 8 }} diff --git a/metricbeat/templates/deployment.yaml b/metricbeat/templates/deployment.yaml index 6e9c28128..e9d682b2e 100644 --- a/metricbeat/templates/deployment.yaml +++ b/metricbeat/templates/deployment.yaml @@ -10,6 +10,15 @@ metadata: chart: '{{ .Chart.Name }}-{{ .Chart.Version }}' heritage: '{{ .Release.Service }}' release: '{{ .Release.Name }}' + {{- if .Values.deployment.labels }} + {{- range $key, $value := .Values.deployment.labels }} + {{ $key }}: {{ $value | quote }} + {{- end }} + {{- else }} + {{- range $key, $value := .Values.labels }} + {{ $key }}: {{ $value | quote }} + {{- end }} + {{- end }} {{- if .Values.deployment.annotations}} annotations: {{- range $key, $value := .Values.deployment.annotations }} @@ -38,9 +47,15 @@ spec: chart: '{{ .Chart.Name }}-{{ .Chart.Version }}' heritage: '{{ .Release.Service }}' release: '{{ .Release.Name }}' + {{- if .Values.deployment.labels }} + {{- range $key, $value := .Values.deployment.labels }} + {{ $key }}: {{ $value | quote }} + {{- end }} + {{- else }} {{- range $key, $value := .Values.labels }} {{ $key }}: {{ $value | quote }} {{- end }} + {{- end }} spec: affinity: {{ toYaml .Values.deployment.affinity | nindent 8 }} nodeSelector: {{ toYaml .Values.deployment.nodeSelector | nindent 8 }} diff --git a/metricbeat/tests/metricbeat_test.py b/metricbeat/tests/metricbeat_test.py index fcc476c4c..d24ebfdd5 100644 --- a/metricbeat/tests/metricbeat_test.py +++ b/metricbeat/tests/metricbeat_test.py @@ -968,24 +968,101 @@ def test_cluster_role_rules(): assert rules["resources"][0] == "something" -def test_adding_pod_labels(): +def test_adding_legacy_labels(): config = """ labels: - app.kubernetes.io/name: metricbeat + app-test: metricbeat """ r = helm_template(config) + assert r["daemonset"][name]["metadata"]["labels"]["app-test"] == "metricbeat" assert ( - r["daemonset"][name]["metadata"]["labels"]["app.kubernetes.io/name"] + r["deployment"][name + "-metrics"]["metadata"]["labels"]["app-test"] == "metricbeat" ) assert ( - r["daemonset"][name]["spec"]["template"]["metadata"]["labels"][ - "app.kubernetes.io/name" + r["daemonset"][name]["spec"]["template"]["metadata"]["labels"]["app-test"] + == "metricbeat" + ) + assert ( + r["deployment"][name + "-metrics"]["spec"]["template"]["metadata"]["labels"][ + "app-test" + ] + == "metricbeat" + ) + + +def test_adding_daemonset_labels(): + config = """ +daemonset: + labels: + app-test: metricbeat +""" + r = helm_template(config) + assert r["daemonset"][name]["metadata"]["labels"]["app-test"] == "metricbeat" + assert ( + r["daemonset"][name]["spec"]["template"]["metadata"]["labels"]["app-test"] + == "metricbeat" + ) + + +def test_adding_daemonset_labels_surpasses_root_labels(): + config = """ +labels: + app-test: root-metricbeat +daemonset: + labels: + app-test: daemonset-metricbeat +""" + r = helm_template(config) + assert ( + r["daemonset"][name]["metadata"]["labels"]["app-test"] == "daemonset-metricbeat" + ) + assert ( + r["daemonset"][name]["spec"]["template"]["metadata"]["labels"]["app-test"] + == "daemonset-metricbeat" + ) + + +def test_adding_deployment_labels(): + config = """ +deployment: + labels: + app-test: metricbeat +""" + r = helm_template(config) + assert ( + r["deployment"][name + "-metrics"]["metadata"]["labels"]["app-test"] + == "metricbeat" + ) + assert ( + r["deployment"][name + "-metrics"]["spec"]["template"]["metadata"]["labels"][ + "app-test" ] == "metricbeat" ) +def test_adding_deployment_labels_surpasses_root_labels(): + config = """ +labels: + app-test: root-metricbeat +deployment: + labels: + app-test: deployment-metricbeat +""" + r = helm_template(config) + assert ( + r["deployment"][name + "-metrics"]["metadata"]["labels"]["app-test"] + == "deployment-metricbeat" + ) + assert ( + r["deployment"][name + "-metrics"]["spec"]["template"]["metadata"]["labels"][ + "app-test" + ] + == "deployment-metricbeat" + ) + + def test_adding_serviceaccount_annotations(): config = """ serviceAccountAnnotations: diff --git a/metricbeat/values.yaml b/metricbeat/values.yaml index f53d542a6..06993abc0 100755 --- a/metricbeat/values.yaml +++ b/metricbeat/values.yaml @@ -3,6 +3,8 @@ daemonset: # Annotations to apply to the daemonset annotations: {} + # additionals labels + labels: {} affinity: {} # Include the daemonset enabled: true @@ -97,6 +99,8 @@ daemonset: deployment: # Annotations to apply to the deployment annotations: {} + # additionals labels + labels: {} affinity: {} # Include the deployment enabled: true @@ -197,9 +201,6 @@ readinessProbe: periodSeconds: 10 timeoutSeconds: 5 -# additionals labels -labels: {} - # Whether this chart should self-manage its service account, role, and associated role binding. managedServiceAccount: true @@ -285,3 +286,4 @@ podSecurityContext: {} resources: {} secretMounts: [] tolerations: [] +labels: {} From 10045611c377556542cc80bfd5833ad2882273ff Mon Sep 17 00:00:00 2001 From: Igor Kliushnikov Date: Fri, 2 Oct 2020 14:27:12 +0200 Subject: [PATCH 2/2] chore: rename test_adding_legacy_labels to test_adding_deprecated_labels --- metricbeat/tests/metricbeat_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metricbeat/tests/metricbeat_test.py b/metricbeat/tests/metricbeat_test.py index d24ebfdd5..79adb379b 100644 --- a/metricbeat/tests/metricbeat_test.py +++ b/metricbeat/tests/metricbeat_test.py @@ -968,7 +968,7 @@ def test_cluster_role_rules(): assert rules["resources"][0] == "something" -def test_adding_legacy_labels(): +def test_adding_deprecated_labels(): config = """ labels: app-test: metricbeat