From e5f223b4465f4c7ececa900fbc5f187d4d6e07ad Mon Sep 17 00:00:00 2001 From: cesium147 <3448068+cesium147@users.noreply.github.com> Date: Mon, 23 Jan 2023 16:33:57 +0000 Subject: [PATCH] add labels templating --- charts/log-router/templates/_helpers.tpl | 27 +++++++++++++++++++ charts/log-router/templates/clusterrole.yaml | 5 +--- charts/log-router/templates/daemonset.yaml | 11 +++----- charts/log-router/templates/rolebinding.yaml | 5 +--- charts/log-router/templates/sa.yaml | 7 ++--- charts/log-router/templates/secret.yaml | 5 +--- charts/log-router/templates/service.yaml | 18 ++++--------- .../log-router/templates/servicemonitor.yaml | 20 +++++--------- charts/log-router/values.yaml | 2 +- 9 files changed, 47 insertions(+), 53 deletions(-) diff --git a/charts/log-router/templates/_helpers.tpl b/charts/log-router/templates/_helpers.tpl index 02aec4f1..e417179d 100755 --- a/charts/log-router/templates/_helpers.tpl +++ b/charts/log-router/templates/_helpers.tpl @@ -19,6 +19,33 @@ We truncate at 63 chars because some Kubernetes name fields are limited to this {{- end -}} {{- end -}} +{{/* +Create chart name and version as used by the chart label. +*/}} +{{- define "fluentd-router.chart" -}} +{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Common labels +*/}} +{{- define "fluentd-router.labels" -}} +helm.sh/chart: {{ include "fluentd-router.chart" . }} +{{ include "fluentd-router.selectorLabels" . }} +{{- if .Chart.AppVersion }} +app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} +{{- end }} +app.kubernetes.io/managed-by: {{ .Release.Service }} +{{- end }} + +{{/* +Selector labels +*/}} +{{- define "fluentd-router.selectorLabels" -}} +app.kubernetes.io/name: {{ include "fluentd-router.name" . }} +app.kubernetes.io/instance: {{ .Release.Name }} +{{- end }} + {{/* Set apiVersion based on .Capabilities.APIVersions */}} diff --git a/charts/log-router/templates/clusterrole.yaml b/charts/log-router/templates/clusterrole.yaml index da69bdf9..41d201ec 100755 --- a/charts/log-router/templates/clusterrole.yaml +++ b/charts/log-router/templates/clusterrole.yaml @@ -8,10 +8,7 @@ apiVersion: {{ template "rbacAPIVersion" . }} kind: ClusterRole metadata: labels: - app: {{ template "fluentd-router.name" . }} - chart: {{ .Chart.Name }}-{{ .Chart.Version }} - heritage: {{ .Release.Service }} - release: {{ .Release.Name }} + {{- include "fluentd-router.labels" . | nindent 4 }} {{- if .Values.extraLabels }} {{ toYaml .Values.extraLabels | indent 4 }} {{- end }} diff --git a/charts/log-router/templates/daemonset.yaml b/charts/log-router/templates/daemonset.yaml index 2c796e07..88a78090 100755 --- a/charts/log-router/templates/daemonset.yaml +++ b/charts/log-router/templates/daemonset.yaml @@ -8,20 +8,15 @@ kind: DaemonSet metadata: name: {{ template "fluentd-router.fullname" . }} labels: - app: {{ template "fluentd-router.name" . }} - chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }} - release: {{ .Release.Name }} - heritage: {{ .Release.Service }} + {{- include "fluentd-router.labels" . | nindent 4 }} spec: selector: matchLabels: - app: {{ template "fluentd-router.name" . }} - release: {{ .Release.Name }} + {{- include "fluentd-router.selectorLabels" . | nindent 6 }} template: metadata: labels: - app: {{ template "fluentd-router.name" . }} - release: {{ .Release.Name }} + {{- include "fluentd-router.selectorLabels" . | nindent 8 }} annotations: checksum/fluentd-extraenv: {{ toYaml .Values.fluentd.extraEnv | sha256sum }} checksum/reloader-extraenv: {{ toYaml .Values.reloader.extraEnv | sha256sum }} diff --git a/charts/log-router/templates/rolebinding.yaml b/charts/log-router/templates/rolebinding.yaml index cccfb08c..dca97bdd 100755 --- a/charts/log-router/templates/rolebinding.yaml +++ b/charts/log-router/templates/rolebinding.yaml @@ -8,10 +8,7 @@ apiVersion: {{ template "rbacAPIVersion" . }} kind: ClusterRoleBinding metadata: labels: - app: {{ template "fluentd-router.name" . }} - chart: {{ .Chart.Name }}-{{ .Chart.Version }} - heritage: {{ .Release.Service }} - release: {{ .Release.Name }} + {{- include "fluentd-router.labels" . | nindent 4 }} {{- if .Values.extraLabels }} {{ toYaml .Values.extraLabels | indent 4 }} {{- end }} diff --git a/charts/log-router/templates/sa.yaml b/charts/log-router/templates/sa.yaml index 5bdfe4c7..ed79af99 100755 --- a/charts/log-router/templates/sa.yaml +++ b/charts/log-router/templates/sa.yaml @@ -7,10 +7,7 @@ apiVersion: v1 kind: ServiceAccount metadata: labels: - app: {{ template "fluentd-router.name" . }} - chart: {{ .Chart.Name }}-{{ .Chart.Version }} - heritage: {{ .Release.Service }} - release: {{ .Release.Name }} + {{- include "fluentd-router.labels" . | nindent 4 }} name: {{ template "fluentd-router.fullname" . }} {{- if .Values.rbac.serviceAccount.annotations }} annotations: {{ toYaml .Values.rbac.serviceAccount.annotations | nindent 4 }} @@ -19,4 +16,4 @@ metadata: imagePullSecrets: - name: {{ .Values.imagePullSecret}} {{end}} -{{- end }} \ No newline at end of file +{{- end }} diff --git a/charts/log-router/templates/secret.yaml b/charts/log-router/templates/secret.yaml index 11958611..501a4542 100755 --- a/charts/log-router/templates/secret.yaml +++ b/charts/log-router/templates/secret.yaml @@ -8,10 +8,7 @@ kind: Secret metadata: name: {{ template "fluentd-router.fullname" . }} labels: - app: {{ template "fluentd-router.name" . }} - chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }} - release: {{ .Release.Name }} - heritage: {{ .Release.Service }} + {{- include "fluentd-router.labels" . | nindent 4 }} annotations: checksum/fluentd-extraenv: {{ toYaml .Values.fluentd.extraEnv | sha256sum }} checksum/reloader-extraenv: {{ toYaml .Values.reloader.extraEnv | sha256sum }} diff --git a/charts/log-router/templates/service.yaml b/charts/log-router/templates/service.yaml index a8989778..27d712ca 100755 --- a/charts/log-router/templates/service.yaml +++ b/charts/log-router/templates/service.yaml @@ -7,10 +7,7 @@ apiVersion: v1 kind: Service metadata: labels: - app: {{ template "fluentd-router.name" . }} - chart: {{ .Chart.Name }}-{{ .Chart.Version }} - heritage: {{ .Release.Service }} - release: {{ .Release.Name }} + {{- include "fluentd-router.labels" . | nindent 4 }} metrics: fluentd annotations: prometheus.io/scrape: "true" @@ -18,8 +15,7 @@ metadata: name: {{ template "fluentd-router.fullname" . }} spec: selector: - app: {{ template "fluentd-router.name" . }} - release: {{ .Release.Name }} + {{- include "fluentd-router.selectorLabels" . | nindent 4 }} ports: - port: 24231 name: prometheus @@ -31,10 +27,7 @@ apiVersion: v1 kind: Service metadata: labels: - app: {{ template "fluentd-router.name" . }} - chart: {{ .Chart.Name }}-{{ .Chart.Version }} - heritage: {{ .Release.Service }} - release: {{ .Release.Name }} + {{- include "fluentd-router.labels" . | nindent 4 }} metrics: reloader annotations: prometheus.io/scrape: "true" @@ -42,10 +35,9 @@ metadata: name: {{ template "fluentd-router.fullname" . }}-reloader spec: selector: - app: {{ template "fluentd-router.name" . }} - release: {{ .Release.Name }} + {{- include "fluentd-router.selectorLabels" . | nindent 4 }} ports: - port: {{ .Values.metricsPort }} name: metrics targetPort: metrics -{{- end }} \ No newline at end of file +{{- end }} diff --git a/charts/log-router/templates/servicemonitor.yaml b/charts/log-router/templates/servicemonitor.yaml index f0c28550..7384eb71 100755 --- a/charts/log-router/templates/servicemonitor.yaml +++ b/charts/log-router/templates/servicemonitor.yaml @@ -4,16 +4,12 @@ kind: ServiceMonitor metadata: name: {{ template "fluentd-router.fullname" . }} labels: - app: {{ template "fluentd-router.name" . }} - chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }} - release: {{ .Release.Name }} - heritage: {{ .Release.Service }} + {{- include "fluentd-router.labels" . | nindent 4 }} spec: - jobLabel: app + jobLabel: {{ .Values.serviceMonitor.jobLabel | quote }} selector: matchLabels: - app: {{ template "fluentd-router.name" . }} - release: {{ .Release.Name }} + {{- include "fluentd-router.selectorLabels" . | nindent 6 }} metrics: fluentd endpoints: - port: prometheus @@ -28,16 +24,12 @@ kind: ServiceMonitor metadata: name: {{ template "fluentd-router.fullname" . }}-reloader labels: - app: {{ template "fluentd-router.name" . }} - chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }} - release: {{ .Release.Name }} - heritage: {{ .Release.Service }} + {{- include "fluentd-router.labels" . | nindent 4 }} spec: - jobLabel: app + jobLabel: {{ .Values.serviceMonitor.jobLabel | quote }} selector: matchLabels: - app: {{ template "fluentd-router.name" . }} - release: {{ .Release.Name }} + {{- include "fluentd-router.selectorLabels" . | nindent 6 }} metrics: reloader endpoints: - port: metrics diff --git a/charts/log-router/values.yaml b/charts/log-router/values.yaml index 30efa47b..257e87c9 100755 --- a/charts/log-router/values.yaml +++ b/charts/log-router/values.yaml @@ -120,7 +120,7 @@ serviceMonitor: enabled: false # Job label for scrape target - jobLabel: app + jobLabel: app.kubernetes.io/name # Scrape interval. If not set, the Prometheus default scrape interval is used. interval: ""