Skip to content
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
10 changes: 10 additions & 0 deletions charts/common/templates/_serviceaccount.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{{- define "common.serviceaccount.tpl" -}}
{{- if .Values.serviceaccount }}
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: {{ .Values.serviceAccount }}
namespace: {{ .Release.Namespace }}
{{- end }}
Comment on lines +2 to +9

Choose a reason for hiding this comment

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

high

This implementation has a critical bug and could be made more flexible and robust.

  1. Bug: There is a case mismatch between .Values.serviceaccount in the if condition and .Values.serviceAccount used for the name. This will prevent the template from working as intended.
  2. Inflexible Design: Using a single value to both enable creation and provide the name is not ideal. For example, if a user sets serviceAccount: true, the resource name would be invalid. A structured object is the standard practice in Helm charts.

I recommend refactoring this to use a structured value. This also makes it easy to add labels and use a default name for the service account, improving consistency with other resources. This would require adding the following to onechart/values.yaml:

serviceAccount:
  create: false
  # name: my-service-account

Note: The suggestion below uses onechart.fullname and onechart.labels. Since this template is in a library chart, hardcoding the parent chart name (onechart) is not ideal for reusability. A more advanced approach would be needed for a truly generic template, but this is a good improvement for the current use case.

{{- if .Values.serviceAccount.create -}}
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: {{ .Values.serviceAccount.name | default (include "onechart.fullname" .) }}
  namespace: {{ .Release.Namespace }}
  labels:
    {{- include "onechart.labels" . | nindent 4 }}
{{- end }}

{{- end -}}
Binary file modified charts/onechart/charts/common-0.7.0.tgz
Binary file not shown.
1 change: 1 addition & 0 deletions charts/onechart/templates/serviceaccount.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{- include "common.serviceaccount.tpl" . -}}
2 changes: 2 additions & 0 deletions charts/onechart/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ image:
# vars:
# MY_VAR: "value"

serviceAccount: nginx

replicas: 1

nameOverride: ""
Expand Down