Skip to content

Enhance Helm Chart with Consistent Naming and Labeling Conventions #47

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

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
1 change: 0 additions & 1 deletion chart/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ Create chart name and version as used by the chart label.
Common labels
*/}}
{{- define "k8s-event-logger.labels" -}}
app.kubernetes.io/name: {{ include "k8s-event-logger.name" . }}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why isn't app.kubernetes.io/name just part of this definition? Is there anywhere it's used separately?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it very well could be - seems most charts I've been working with leave the name out of the template for "reasons" unknown to me, for this purpose I originally had it as part of the label helper

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a default from helm (helm create ) does this:

Expand the name of the chart.
*/}}
{{- define "k8s-event-logger.name" -}}
{{- default .Chart.Name .Values.nameOverride | 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-event-logger.fullname" -}}
{{- if .Values.fullnameOverride }}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- $name := default .Chart.Name .Values.nameOverride }}
{{- 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-event-logger.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Common labels
*/}}
{{- define "k8s-event-logger.labels" -}}
helm.sh/chart: {{ include "k8s-event-logger.chart" . }}
{{ include "k8s-event-logger.selectorLabels" . }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}

{{/*
Selector labels
*/}}
{{- define "k8s-event-logger.selectorLabels" -}}
app.kubernetes.io/name: {{ include "k8s-event-logger.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}

{{/*
Create the name of the service account to use
*/}}
{{- define "k8s-event-logger.serviceAccountName" -}}
{{- if .Values.serviceAccount.create }}
{{- default (include "k8s-event-logger.fullname" .) .Values.serviceAccount.name }}
{{- else }}
{{- default "default" .Values.serviceAccount.name }}
{{- end }}
{{- end }}```

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So a difference of .labels and .selectorLabels, it seems, but the .lables includes all the selectorLabels and then just adds the chart metadata.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And then the deployment and or services use the selectorLables as part of their "selector" key in the spec, but the "labels" are used int he "metadata" section.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anywho.... I have no strong opinions, it just caught my eye.

Copy link
Contributor Author

@webdz9r webdz9r Sep 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it - we use the chart at work, in our case we need to modify the label pre-deployment so our overlay chart we put our own help for app.labels but we're not able to do that with this chart so here we are. I've forked it accordingly and made the change, thought I'd share - cheers

This should produce the same output as the current method. Only diff is the version: label

diff original.yaml new.yaml

22a23
>     app.kubernetes.io/version: "2.2"
37a39
>     app.kubernetes.io/version: "2.2"

helm.sh/chart: {{ include "k8s-event-logger.chart" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- if .Chart.AppVersion }}
Expand Down
4 changes: 1 addition & 3 deletions chart/templates/clusterrole.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ metadata:
name: {{ include "k8s-event-logger.fullname" . }}
labels:
app.kubernetes.io/name: {{ include "k8s-event-logger.name" . }}
helm.sh/chart: {{ include "k8s-event-logger.chart" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- include "k8s-event-logger.labels" . | nindent 4 }}
rules:
- apiGroups: [""]
resources: ["events"]
Expand Down
4 changes: 1 addition & 3 deletions chart/templates/clusterrolebinding.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ metadata:
name: {{ include "k8s-event-logger.fullname" . }}
labels:
app.kubernetes.io/name: {{ include "k8s-event-logger.name" . }}
helm.sh/chart: {{ include "k8s-event-logger.chart" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- include "k8s-event-logger.labels" . | nindent 4 }}
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
Expand Down
3 changes: 2 additions & 1 deletion chart/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ metadata:
name: {{ include "k8s-event-logger.fullname" . }}
namespace: {{ .Release.Namespace }}
labels:
{{ include "k8s-event-logger.labels" . | indent 4 }}
app.kubernetes.io/name: {{ include "k8s-event-logger.name" . }}
{{- include "k8s-event-logger.labels" . | nindent 4 }}
spec:
replicas: 1
selector:
Expand Down