From 6d3b19e1f393bebfd155b28138fc50f29ddc34cd Mon Sep 17 00:00:00 2001 From: Peter Bueschel Date: Thu, 25 Apr 2024 17:55:49 +0200 Subject: [PATCH] feat: add [recommended labels](https://kubernetes.io/docs/concepts/overview/working-with-objects/common-labels/) to the Helm Chart --- .../k8s-aws-operator/templates/_helpers.tpl | 43 +++++++++++++++++++ .../templates/deployment.yaml | 4 +- charts/k8s-aws-operator/templates/rbac.yaml | 20 ++++----- .../k8s-aws-operator/templates/service.yaml | 6 +-- .../templates/servicemonitor.yaml | 6 +-- 5 files changed, 62 insertions(+), 17 deletions(-) create mode 100644 charts/k8s-aws-operator/templates/_helpers.tpl diff --git a/charts/k8s-aws-operator/templates/_helpers.tpl b/charts/k8s-aws-operator/templates/_helpers.tpl new file mode 100644 index 0000000..423a929 --- /dev/null +++ b/charts/k8s-aws-operator/templates/_helpers.tpl @@ -0,0 +1,43 @@ +{{/* +Expand the name of the chart. +*/}} +{{- define "k8s-aws-operator.name" -}} +{{- default .Chart.Name .Values.name | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Create a default fully qualified app name. +We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec). +If release name contains chart name it will be used as a full name. +*/}} +{{- define "k8s-aws-operator.fullname" -}} +{{- if .Values.fullnameOverride }} +{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }} +{{- else }} +{{- $name := default .Chart.Name .Values.name }} +{{- if contains $name .Release.Name }} +{{- .Release.Name | trunc 63 | trimSuffix "-" }} +{{- else }} +{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }} +{{- end }} +{{- end }} +{{- end }} + +{{/* +Create chart name and version as used by the chart label. +*/}} +{{- define "k8s-aws-operator.chart" -}} +{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Common labels +*/}} +{{- define "k8s-aws-operator.labels" -}} +helm.sh/chart: {{ include "k8s-aws-operator.chart" . }} +{{- if .Chart.AppVersion }} +app.kubernetes.io/version: {{ default .Chart.AppVersion .Values.image.tag | quote }} +{{- end }} +app.kubernetes.io/managed-by: {{ .Release.Service }} +app.kubernetes.io/instance: {{ .Release.Name }} +{{- end }} diff --git a/charts/k8s-aws-operator/templates/deployment.yaml b/charts/k8s-aws-operator/templates/deployment.yaml index 1fa32d2..8325cf7 100644 --- a/charts/k8s-aws-operator/templates/deployment.yaml +++ b/charts/k8s-aws-operator/templates/deployment.yaml @@ -1,9 +1,10 @@ apiVersion: apps/v1 kind: Deployment metadata: - name: {{ .Chart.Name }} + name: {{ include "k8s-aws-operator.fullname" . }} labels: app.kubernetes.io/name: {{ .Chart.Name }} + {{- include "k8s-aws-operator.labels" . | nindent 4 }} spec: replicas: {{ .Values.replicas }} selector: @@ -13,6 +14,7 @@ spec: metadata: labels: app.kubernetes.io/name: {{ .Chart.Name }} + {{- include "k8s-aws-operator.labels" . | nindent 8 }} spec: serviceAccountName: {{ .Chart.Name }} {{- with .Values.nodeSelector }} diff --git a/charts/k8s-aws-operator/templates/rbac.yaml b/charts/k8s-aws-operator/templates/rbac.yaml index e3c9230..2fe2f36 100644 --- a/charts/k8s-aws-operator/templates/rbac.yaml +++ b/charts/k8s-aws-operator/templates/rbac.yaml @@ -1,9 +1,9 @@ apiVersion: v1 kind: ServiceAccount metadata: - name: {{ .Chart.Name }} + name: {{ include "k8s-aws-operator.fullname" . }} labels: - app.kubernetes.io/name: {{ .Chart.Name }} + {{- include "k8s-aws-operator.labels" . | nindent 4 }} {{- with .Values.serviceAccount.annotations }} annotations: {{ . | toYaml | nindent 4 }} @@ -13,9 +13,9 @@ metadata: apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole metadata: - name: {{ .Chart.Name }} + name: {{ include "k8s-aws-operator.fullname" . }} labels: - app.kubernetes.io/name: {{ .Chart.Name }} + {{- include "k8s-aws-operator.labels" . | nindent 4 }} rules: - apiGroups: [""] resources: ["pods"] @@ -27,9 +27,9 @@ rules: apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRoleBinding metadata: - name: {{ .Chart.Name }} + name: {{ include "k8s-aws-operator.fullname" . }} labels: - app.kubernetes.io/name: {{ .Chart.Name }} + {{- include "k8s-aws-operator.labels" . | nindent 4 }} roleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole @@ -44,9 +44,9 @@ subjects: apiVersion: rbac.authorization.k8s.io/v1 kind: Role metadata: - name: {{ .Chart.Name }} + name: {{ include "k8s-aws-operator.fullname" . }} labels: - app.kubernetes.io/name: {{ .Chart.Name }} + {{- include "k8s-aws-operator.labels" . | nindent 4 }} rules: - apiGroups: ["coordination.k8s.io"] resources: ["leases"] @@ -62,9 +62,9 @@ rules: apiVersion: rbac.authorization.k8s.io/v1 kind: RoleBinding metadata: - name: {{ .Chart.Name }} + name: {{ include "k8s-aws-operator.fullname" . }} labels: - app.kubernetes.io/name: {{ .Chart.Name }} + {{- include "k8s-aws-operator.labels" . | nindent 4 }} roleRef: apiGroup: rbac.authorization.k8s.io kind: Role diff --git a/charts/k8s-aws-operator/templates/service.yaml b/charts/k8s-aws-operator/templates/service.yaml index 21ec2a1..5c29639 100644 --- a/charts/k8s-aws-operator/templates/service.yaml +++ b/charts/k8s-aws-operator/templates/service.yaml @@ -2,9 +2,9 @@ apiVersion: v1 kind: Service metadata: - name: {{ .Chart.Name }}-metrics + name: {{ include "k8s-aws-operator.fullname" . }} labels: - app.kubernetes.io/name: {{ .Chart.Name }} + {{- include "k8s-aws-operator.labels" . | nindent 4 }} spec: {{- with .Values.metrics.service.clusterIP }} clusterIP: {{ . | quote }} @@ -15,5 +15,5 @@ spec: protocol: TCP targetPort: 8080 selector: - app.kubernetes.io/name: {{ .Chart.Name }} + {{- include "k8s-aws-operator.selectorLabels" . | nindent 4 }} {{- end }} diff --git a/charts/k8s-aws-operator/templates/servicemonitor.yaml b/charts/k8s-aws-operator/templates/servicemonitor.yaml index 3b3b9f2..9a5a206 100644 --- a/charts/k8s-aws-operator/templates/servicemonitor.yaml +++ b/charts/k8s-aws-operator/templates/servicemonitor.yaml @@ -2,13 +2,13 @@ apiVersion: monitoring.coreos.com/v1 kind: ServiceMonitor metadata: - name: {{ .Chart.Name }} + name: {{ include "k8s-aws-operator.fullname" . }} labels: - app.kubernetes.io/name: {{ .Chart.Name }} + {{- include "k8s-aws-operator.labels" . | nindent 4 }} spec: selector: matchLabels: - app.kubernetes.io/name: {{ .Chart.Name }} + {{- include "k8s-aws-operator.selectorLabels" . | nindent 6 }} endpoints: {{- with .Values.metrics.serviceMonitor.endpoints }} {{ . | toYaml | nindent 2 }}