diff --git a/cmd/helm/create_test.go b/cmd/helm/create_test.go index c9459b477..20bace864 100644 --- a/cmd/helm/create_test.go +++ b/cmd/helm/create_test.go @@ -143,7 +143,7 @@ func TestCreateStarterCmd(t *testing.T) { t.Errorf("Wrong API version: %q", c.Metadata.ApiVersion) } - expectedTemplateCount := 7 + expectedTemplateCount := 8 if l := len(c.Templates); l != expectedTemplateCount { t.Errorf("Expected %d templates, got %d", expectedTemplateCount, l) } diff --git a/pkg/chartutil/create.go b/pkg/chartutil/create.go index 0a68856e2..6828ae902 100644 --- a/pkg/chartutil/create.go +++ b/pkg/chartutil/create.go @@ -42,6 +42,8 @@ const ( DeploymentName = "deployment.yaml" // ServiceName is the name of the example service file. ServiceName = "service.yaml" + // ServiceAccountName is the name of the example serviceaccount file. + ServiceAccountName = "serviceaccount.yaml" // NotesName is the name of the example NOTES.txt file. NotesName = "NOTES.txt" // HelpersName is the name of the example helpers file. @@ -67,6 +69,13 @@ imagePullSecrets: [] nameOverride: "" fullnameOverride: "" +serviceAccount: + # Specifies whether a service account should be created + create: true + # The name of the service account to use. + # If not set and create is true, a name is generated using the fullname template + name: + service: type: ClusterIP port: 80 @@ -189,6 +198,7 @@ spec: imagePullSecrets: {{- toYaml . | nindent 8 }} {{- end }} + serviceAccountName: {{ template ".serviceAccountName" . }} containers: - name: {{ .Chart.Name }} image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}" @@ -238,6 +248,15 @@ spec: app.kubernetes.io/name: {{ include ".name" . }} app.kubernetes.io/instance: {{ .Release.Name }} ` +const defaultServiceAccount = `{{- if .Values.serviceAccount.create -}} +apiVersion: v1 +kind: ServiceAccount +metadata: + name: {{ template ".serviceAccountName" . }} + labels: +{{ include ".labels" . | indent 4 }} +{{- end -}} +` const defaultNotes = `1. Get the application URL by running these commands: {{- if .Values.ingress.enabled }} @@ -307,6 +326,17 @@ app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} {{- end }} app.kubernetes.io/managed-by: {{ .Release.Service }} {{- end -}} + +{{/* +Create the name of the service account to use +*/}} +{{- define ".serviceAccountName" -}} +{{- if .Values.serviceAccount.create -}} + {{ default (include ".fullname" .) .Values.serviceAccount.name }} +{{- else -}} + {{ default "default" .Values.serviceAccount.name }} +{{- end -}} +{{- end -}} ` const defaultTestConnection = `apiVersion: v1 @@ -425,6 +455,11 @@ func Create(chartfile *chart.Metadata, dir string) (string, error) { path: filepath.Join(cdir, TemplatesDir, ServiceName), content: Transform(defaultService, "", chartfile.Name), }, + { + // serviceaccount.yaml + path: filepath.Join(cdir, TemplatesDir, ServiceAccountName), + content: Transform(defaultServiceAccount, "", chartfile.Name), + }, { // NOTES.txt path: filepath.Join(cdir, TemplatesDir, NotesName),