Why doesn't helm use the name defined in the deployment template?

11/20/2018

i.e. from name: {{ .Chart.Name }}-{{ .Values.module5678.name }}-pod below

# deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: {{ template "project1234.name" . }}
    chart: {{ template "project1234.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
  name: {{ template "project1234.module5678.fullname" . }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      app: {{ template "project1234.name" . }}
  template:
    metadata:
      labels:
        app: {{ template "project1234.name" . }}
    spec:
      containers:
      - image: "{{ .Values.image.name }}:{{ .Values.image.tag }}"
        name: {{ .Chart.Name }}-{{ .Values.module5678.name }}-pod
        ports:
        - containerPort: 1234
      imagePullSecrets:
      - name: {{ .Values.image.pullSecret }}

I am expecting the pod name to be:

pod/project1234-module5678-pod

Instead, the resulting Pod name is:

pod/chartname-project1234-module5678-dc7db787-skqvv

...where (in my understanding):
chartname is from: helm install --name chartname -f values.yaml .
project1234 is from:

# Chart.yaml

apiVersion: v1
appVersion: "1.0"
description: project1234 Helm chart for Kubernetes
name: project1234
version: 0.1.0

module5678 is from:

# values.yaml

rbac:
  create: true

serviceAccounts:
  module5678:
    create: true
    name:

image:
  name: <image location>
  tag: 1.5
  pullSecret: <pull secret>

gitlab:
  secretName: <secret name>
  username: foo
  password: bar

module5678:
  enabled: true
  name: module5678
  ingress:
    enabled: true
  replicaCount: 1
  resources: {}

I've tried changing name: {{ .Chart.Name }}-{{ .Values.module5678.name }}-pod into a plain string value like "podname1234" and it isn't followed. I even tried removing the name setting entirely and the resulting pod name remains the same.

-- libzz
kubernetes-helm

1 Answer

11/26/2018

Pods created from a Deployment always have a generated name based on the Deployment's name (and also the name of the intermediate ReplicaSet, if you go off and look for it). You can't override it.

Given the YAML you've shown, I'd expect that this fragment:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "project1234.module5678.fullname" . }}

expands out to a Deployment name of chartname-project1234-module5678; the remaining bits are added in by the ReplicaSet and then the Pod itself.

If you do look up the Pod and kubectl describe pod chartname-project1234-module5678-dc7db787-skqvv you will probably see that it has a single container that has the expected name project1234-module5678-pod. Pretty much the only time you need to use this is if you need to kubectl logs (or, more rarely, kubectl exec) in a multi-container pod; if you are in this case, you'll appreciate having a shorter name, and since the container names are always scoped to the specific pod in which they appear, there's nothing wrong with using a short fixed name here

spec:
  containers:
  - name: container
-- David Maze
Source: StackOverflow