项目作者: hahow

项目描述 :
A Helm helper chart for building Helm charts
高级语言: Smarty
项目地址: git://github.com/hahow/common-chart.git
创建时间: 2020-04-15T06:45:54Z
项目社区:https://github.com/hahow/common-chart

开源协议:

下载


Common: The Helm Helper Chart

This chart was originally forked from incubator/common, which is designed to make it easier for you to build and maintain Helm charts.

It provides utilities that reflect best practices of Kubernetes chart development, making it faster for you to write charts.

Contents

Getting Started

Adding Repository

The following command allows you to download and install all the charts from our repository:

  1. $ helm repo add hahow https://hahow-helm-charts.storage.googleapis.com/

Adding Dependency

To use the library chart, common should be listed in dependencies field in your Chart.yaml:

  1. dependencies:
  2. - name: common
  3. version: 0.4.1
  4. repository: https://hahow-helm-charts.storage.googleapis.com/

Once you have defined dependencies, you should run the following command to download this chart into your charts/ directory:

  1. $ helm dep build

Using Starter

The best way to get started is to use the create script to generate a new chart.

You can fetch that script, and then execute it locally:

  1. $ curl -fsSL -o create.sh https://raw.githubusercontent.com/hahow/common-chart/master/create.sh
  2. $ chmod 700 create.sh
  3. $ ./create.sh mychart

or simply

  1. $ curl https://raw.githubusercontent.com/hahow/common-chart/master/create.sh | bash -s -- mychart

Now, there is a chart in ./mychart. You can edit it and create your own templates.

Resource Kinds

Kubernetes defines a variety of resource kinds, from Secret to StatefulSet. We define some of the most common kinds in a way that lets you easily work with them.

The resource kind templates are designed to make it much faster for you to define basic versions of these resources. They allow you to extend and modify just what you need, without having to copy around lots of boilerplate.

To make use of these templates you must define a template that will extend the base template (though it can be empty). The name of this template is then passed to the base template, for example:

  1. {{- include "common.service" (list . .Values.service "mychart.service") }}
  2. {{- define "mychart.service" -}}
  3. ## Define overrides for your Service resource here, e.g.
  4. # metadata:
  5. # labels:
  6. # custom: label
  7. # spec:
  8. # ports:
  9. # - port: 8080
  10. # targetPort: http
  11. # protocol: TCP
  12. # name: http
  13. {{- end }}

Note that the common.service template defines three parameters:

  • The root context (usually .)
  • A dictionary of values which are used in the template
  • A optional template name containing the service definition overrides

A limitation of the Go template library is that a template can only take a single argument. The list function is used to workaround this by constructing a list or array of arguments that is passed to the template.

The common.service template is responsible for rendering the templates with the root context and merging any overrides. As you can see, this makes it very easy to create a basic Service resource without having to copy around the standard metadata and labels.

Each implemented base resource is described in greater detail below.

common.configMap

The common.configMap template accepts a list of two values:

  • $top, the top context
  • [optional] the template name of the overrides

It creates an empty ConfigMap resource that you can override with your configuration.

Example use:

  1. {{- include "common.configMap" (list . "mychart.configMap") }}
  2. {{- define "mychart.configMap" -}}
  3. data:
  4. zeus: cat
  5. athena: cat
  6. julius: cat
  7. one: |-
  8. {{ .Files.Get "file1.txt" }}
  9. {{- end }}

Output:

  1. apiVersion: v1
  2. data:
  3. athena: cat
  4. julius: cat
  5. one: This is a file.
  6. zeus: cat
  7. kind: ConfigMap
  8. metadata:
  9. labels:
  10. app.kubernetes.io/instance: release-name
  11. app.kubernetes.io/managed-by: Helm
  12. app.kubernetes.io/name: mychart
  13. app.kubernetes.io/version: 1.16.0
  14. helm.sh/chart: mychart-0.1.0
  15. name: release-name-mychart

common.cronJob

The common.cronJob template accepts a list of five values:

  • $top, the top context
  • $cronJob, a dictionary of values used in the cronjob template
  • $pod, a dictionary of values used in the pod template
  • $serviceAccount, a dictionary of values used in the service account template
  • [optional] the template name of the overrides

It defines a basic CronJob with the following defaults:

  • Labels of JobTemplate are defined with common.selectorLabels as this is also used as the selector.
  • Restart policy of pod is set to OnFailure

In addition, it uses the following configuration from the $cronJob:

Value Description
$cronJob.schedule Schedule for the cronjob
$cronJob.concurrencyPolicy [optional] `Allow\ Forbid\ Replace` concurrent jobs
$cronJob.failedJobsHistoryLimit [optional] Specify the number of failed jobs to keep
$cronJob.successfulJobsHistoryLimit [optional] Specify the number of completed jobs to keep
$cronJob.suspend [optional] Specify cronjob is suspend, default false
$cronJob.activeDeadlineSeconds [optional] Specify cronjob activeDeadlineSeconds

Underneath the hood, it invokes common.pod.template template with $pod to populate the PodTemplate.

Example use:

  1. {
  2. {
  3. - include "common.cronJob" (list . .Values.cronJob .Values .Values.serviceAccount),
  4. },
  5. }
  6. ## The following is the same as above:
  7. # {{- include "common.cronJob" (list . .Values.cronJob .Values .Values.serviceAccount "mychart.cronJob") }}
  8. # {{- define "mychart.cronJob" -}}
  9. # {{- end }}

common.deployment

The common.deployment template accepts a list of five values:

  • $top, the top context
  • $deployment, a dictionary of values used in the deployment template
  • $autoscaling, a dictionary of values used in the hpa template
  • $serviceAccount, a dictionary of values used in the service account template
  • [optional] the template name of the overrides

It defines a basic Deployment with the following settings:

Value Description
$deployment.replicaCount Number of replica. If autoscaling enabled, this field will be ignored
$deployment.imagePullSecrets [optional] Name of Secret resource containing private registry credentials
$deployment.podSecurityContext [optional] Security options for pod
$deployment.nodeSelector [optional] Node labels for pod assignment
$deployment.affinity [optional] Expressions for affinity
$deployment.tolerations [optional] Toleration labels for pod assignment
$autoscaling.enabled [optional] Set this to true to enable autoscaling

Underneath the hood, it invokes common.pod.template template with $deployment to populate the PodTemplate.

Example use:

  1. {
  2. {
  3. - include "common.deployment" (list . .Values .Values.autoscaling .Values.serviceAccount),
  4. },
  5. }
  6. ## The following is the same as above:
  7. # {{- include "common.deployment" (list . .Values .Values.autoscaling .Values.serviceAccount "mychart.deployment") }}
  8. # {{- define "mychart.deployment" -}}
  9. # {{- end }}

common.hpa

The common.hpa template accepts a list of three values:

  • $top, the top context
  • $autoscaling, a dictionary of values used in the hpa template
  • [optional] the template name of the overrides

It creates a basic HorizontalPodAutoscaler resource with the following defaults:

An example values file that can be used to configure the HorizontalPodAutoscaler resource is:

  1. autoscaling:
  2. enabled: true
  3. minReplicas: 3
  4. maxReplicas: 5
  5. cpuUtilizationPercentage: 50
  6. memoryUtilizationPercentage: 90

Example use:

  1. { { - include "common.hpa" (list . .Values.autoscaling) } }
  2. ## The following is the same as above:
  3. # {{- include "common.hpa" (list . .Values.autoscaling "mychart.hpa") }}
  4. # {{- define "mychart.hpa" -}}
  5. # {{- end }}

Output:

  1. apiVersion: autoscaling/v2
  2. kind: HorizontalPodAutoscaler
  3. metadata:
  4. labels:
  5. app.kubernetes.io/instance: release-name
  6. app.kubernetes.io/managed-by: Helm
  7. app.kubernetes.io/name: mychart
  8. app.kubernetes.io/version: 1.16.0
  9. helm.sh/chart: mychart-0.1.0
  10. name: release-name-mychart
  11. spec:
  12. maxReplicas: 5
  13. metrics:
  14. - resource:
  15. name: cpu
  16. target:
  17. averageUtilization: 50
  18. type: Utilization
  19. type: Resource
  20. - resource:
  21. name: memory
  22. target:
  23. averageUtilization: 90
  24. type: Utilization
  25. type: Resource
  26. minReplicas: 3
  27. scaleTargetRef:
  28. apiVersion: apps/v1
  29. kind: Deployment
  30. name: release-name-mychart

common.ingress

The common.ingress template accepts a list of four values:

  • $top, the top context
  • $ingress, a dictionary of values used in the ingress template
  • $service, a dictionary of values used in the service template
  • [optional] the template name of the overrides

It is designed to give you a well-defined Ingress resource, that can be configured using $ingress. An example values file that can be used to configure the Ingress resource is:

  1. ingress:
  2. enabled: true
  3. annotations:
  4. kubernetes.io/ingress.class: nginx
  5. kubernetes.io/tls-acme: "true"
  6. hosts:
  7. - host: chart-example.local
  8. paths:
  9. - path: /path/to/somewhere
  10. pathType: ImplementationSpecific
  11. tls:
  12. - secretName: chart-example-tls
  13. hosts:
  14. - chart-example.local
  15. service:
  16. type: ClusterIP
  17. port: 80

Example use:

  1. { { - include "common.ingress" (list . .Values.ingress .Values.service) } }
  2. ## The following is the same as above:
  3. # {{- include "common.ingress" (list . .Values.ingress .Values.service "mychart.ingress") }}
  4. # {{- define "mychart.ingress" -}}
  5. # {{- end }}

Output:

  1. apiVersion: networking.k8s.io/v1beta1
  2. kind: Ingress
  3. metadata:
  4. annotations:
  5. kubernetes.io/ingress.class: nginx
  6. kubernetes.io/tls-acme: "true"
  7. labels:
  8. app.kubernetes.io/instance: release-name
  9. app.kubernetes.io/managed-by: Helm
  10. app.kubernetes.io/name: mychart
  11. app.kubernetes.io/version: 1.16.0
  12. helm.sh/chart: mychart-0.1.0
  13. name: release-name-mychart
  14. spec:
  15. rules:
  16. - host: "chart-example.local"
  17. http:
  18. paths:
  19. - backend:
  20. service:
  21. name: release-name-mychart
  22. port:
  23. number: 80
  24. path: /path/to/somewhere
  25. pathType: ImplementationSpecific
  26. tls:
  27. - hosts:
  28. - "chart-example.local"
  29. secretName: chart-example-tls

common.pdb

The common.pdb template accepts a list of five values:

  • $top, the top context
  • $pdb, a dictionary of values used in the hpa template
  • $deployment, a dictionary of values used in the deployment template
  • $autoscaling, a dictionary of values used in the hpa template
  • [optional] the template name of the overrides

It creates a basic PodDisruptionBudget resource with the following defaults:

An example values file that can be used to configure the PodDisruptionBudget resource is:

  1. podDisruptionBudget:
  2. ## You can specify only one of maxUnavailable and minAvailable in a single PodDisruptionBudget
  3. minAvailable: 2
  4. # maxUnavailable: 1

Example use:

  1. {
  2. {
  3. - include "common.pdb" (list . .Values.podDisruptionBudget .Values .Values.autoscaling),
  4. },
  5. }
  6. ## The following is the same as above:
  7. # {{- include "common.pdb" (list . .Values.podDisruptionBudget .Values .Values.autoscaling "mychart.pdb") }}
  8. # {{- define "mychart.pdb" -}}
  9. # {{- end }}

Output:

  1. apiVersion: policy/v1
  2. kind: PodDisruptionBudget
  3. metadata:
  4. labels:
  5. app.kubernetes.io/instance: release-name
  6. app.kubernetes.io/managed-by: Helm
  7. app.kubernetes.io/name: mychart
  8. app.kubernetes.io/version: 1.16.0
  9. helm.sh/chart: mychart-0.1.0
  10. name: release-name-mychart
  11. spec:
  12. minAvailable: 2
  13. selector:
  14. matchLabels:
  15. app.kubernetes.io/instance: release-name
  16. app.kubernetes.io/name: mychart

common.secret

The common.secret template accepts a list of two values:

  • $top, the top context
  • [optional] the template name of the overrides

It creates an empty Secret resource that you can override with your secrets.

Example use:

  1. {{- include "common.secret" (list . "mychart.secret") }}
  2. {{- define "mychart.secret" -}}
  3. data:
  4. zeus: {{ print "cat" | b64enc }}
  5. athena: {{ print "cat" | b64enc }}
  6. julius: {{ print "cat" | b64enc }}
  7. one: |-
  8. {{ .Files.Get "file1.txt" | b64enc }}
  9. {{- end }}

Output:

  1. apiVersion: v1
  2. data:
  3. athena: Y2F0
  4. julius: Y2F0
  5. one: VGhpcyBpcyBhIGZpbGUuCg==
  6. zeus: Y2F0
  7. kind: Secret
  8. metadata:
  9. labels:
  10. app.kubernetes.io/instance: release-name
  11. app.kubernetes.io/managed-by: Helm
  12. app.kubernetes.io/name: mychart
  13. app.kubernetes.io/version: 1.16.0
  14. helm.sh/chart: mychart-0.1.0
  15. name: release-name-mychart
  16. type: Opaque

common.service

The common.service template accepts a list of three values:

  • $top, the top context
  • $service, a dictionary of values used in the service template
  • [optional] the template name of the overrides

It creates a basic Service resource with the following defaults:

  • Service type (ClusterIP, NodePort, LoadBalancer) made configurable by $service.type
  • Named port http configured on port $service.port
  • Selector set with common.selectorLabels to match the default used in the Deployment resource

Example template:

  1. {{- include "common.service" (list . .Values.service "mychart.mail.service") }}
  2. {{- define "mychart.mail.service" -}}
  3. {{- $top := first . }}
  4. metadata:
  5. name: {{ include "common.fullname" $top }}-mail # overrides the default name to add a suffix
  6. labels: # appended to the labels section
  7. protocol: mail
  8. spec:
  9. ports: # composes the `ports` section of the service definition.
  10. - name: smtp
  11. port: 25
  12. targetPort: 25
  13. - name: imaps
  14. port: 993
  15. targetPort: 993
  16. selector: # this is appended to the default selector
  17. protocol: mail
  18. {{- end }}
  19. ---
  20. {{ include "common.service" (list . .Values.service "mychart.web.service") }}
  21. {{- define "mychart.web.service" -}}
  22. {{- $top := first . }}
  23. metadata:
  24. name: {{ include "common.fullname" $top }}-www # overrides the default name to add a suffix
  25. labels: # appended to the labels section
  26. protocol: www
  27. spec:
  28. ports: # composes the `ports` section of the service definition.
  29. - name: www
  30. port: 80
  31. targetPort: 8080
  32. {{- end }}

The above template defines two services: a web service and a mail service.

The most important part of a service definition is the ports object, which defines the ports that this service will listen on. Most of the time, selector is computed for you. But you can replace it or add to it.

The output of the example above is:

  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4. labels:
  5. app.kubernetes.io/instance: release-name
  6. app.kubernetes.io/managed-by: Helm
  7. app.kubernetes.io/name: mychart
  8. app.kubernetes.io/version: 1.16.0
  9. helm.sh/chart: mychart-0.1.0
  10. protocol: www
  11. name: release-name-mychart-www
  12. spec:
  13. ports:
  14. - name: www
  15. port: 80
  16. targetPort: 8080
  17. selector:
  18. app.kubernetes.io/instance: release-name
  19. app.kubernetes.io/name: mychart
  20. type: ClusterIP
  21. ---
  22. apiVersion: v1
  23. kind: Service
  24. metadata:
  25. labels:
  26. app.kubernetes.io/instance: release-name
  27. app.kubernetes.io/managed-by: Helm
  28. app.kubernetes.io/name: mychart
  29. app.kubernetes.io/version: 1.16.0
  30. helm.sh/chart: mychart-0.1.0
  31. protocol: mail
  32. name: release-name-mychart-mail
  33. spec:
  34. ports:
  35. - name: smtp
  36. port: 25
  37. targetPort: 25
  38. - name: imaps
  39. port: 993
  40. targetPort: 993
  41. selector:
  42. app.kubernetes.io/instance: release-name
  43. app.kubernetes.io/name: mychart
  44. protocol: mail
  45. type: ClusterIP

common.serviceAccount

The common.serviceAccount template accepts a list of three values:

  • $top, the top context
  • $serviceAccount, a dictionary of values used in the service account template
  • [optional] the template name of the overrides

It creates a basic ServiceAccount resource with the following defaults:

An example values file that can be used to configure the ServiceAccount resource is:

  1. serviceAccount:
  2. create: true
  3. annotations: {}
  4. name:

Example use:

  1. { { - include "common.serviceAccount" (list . .Values.serviceAccount) } }
  2. ## The following is the same as above:
  3. # {{- include "common.serviceAccount" (list . .Values.serviceAccount "mychart.serviceAccount") }}
  4. # {{- define "mychart.serviceAccount" -}}
  5. # {{- end }}

Output:

  1. apiVersion: v1
  2. kind: ServiceAccount
  3. metadata:
  4. labels:
  5. app.kubernetes.io/instance: release-name
  6. app.kubernetes.io/managed-by: Helm
  7. app.kubernetes.io/name: mychart
  8. app.kubernetes.io/version: 1.16.0
  9. helm.sh/chart: mychart-0.1.0
  10. name: release-name-mychart

common.serviceMonitor

The common.serviceMonitor template accepts a list of three values:

  • $top, the top context
  • $serviceMonitor, a dictionary of values used in the service account template
  • [optional] the template name of the overrides

It creates a basic ServiceMonitor resource with the following defaults:

  • Namespace selector is set to the release namespace
  • Selector is set with common.selectorLabels to match the default used in the Service resource

An example values file that can be used to configure the ServiceMonitor resource is:

  1. serviceMonitor:
  2. enabled: true
  3. namespace: monitoring
  4. port: 80
  5. path: /path/to/metrics
  6. interval: 30s
  7. scrapeTimeout: 30s
  8. basicAuth:
  9. enabled: true
  10. username: administrator
  11. password: password

Example use:

  1. { { - include "common.serviceMonitor" (list . .Values.serviceMonitor) } }
  2. ## The following is the same as above:
  3. # {{- include "common.serviceMonitor" (list . .Values.serviceMonitor "mychart.serviceMonitor") }}
  4. # {{- define "mychart.serviceMonitor" -}}
  5. # {{- end }}

Output:

  1. apiVersion: monitoring.coreos.com/v1
  2. kind: ServiceMonitor
  3. metadata:
  4. labels:
  5. app.kubernetes.io/instance: release-name
  6. app.kubernetes.io/managed-by: Helm
  7. app.kubernetes.io/name: mychart
  8. app.kubernetes.io/version: 1.16.0
  9. helm.sh/chart: mychart-0.1.0
  10. name: release-name-mychart
  11. namespace: monitoring
  12. spec:
  13. endpoints:
  14. - basicAuth:
  15. password:
  16. key: password
  17. name: release-name-mychart
  18. username:
  19. key: username
  20. name: release-name-mychart
  21. interval: 30s
  22. path: /path/to/metrics
  23. port: 80
  24. scrapeTimeout: 30s
  25. namespaceSelector:
  26. matchNames:
  27. - default
  28. selector:
  29. matchLabels:
  30. app.kubernetes.io/instance: release-name
  31. app.kubernetes.io/name: mychart

common.serviceMonitor.secret

The common.serviceMonitor.secret template accepts a list of three values:

  • $top, the top context
  • $serviceMonitor, a dictionary of values used in the service account template
  • [optional] the template name of the overrides

It creates a Secret resource contains the BasicAuth information for the ServiceMonitor.

An example values.yaml for your ServiceMonitor could look like:

  1. serviceMonitor:
  2. basicAuth:
  3. enabled: true
  4. username: administrator
  5. password: password

Example use:

  1. { { - include "common.serviceMonitor.secret" (list . .Values.serviceMonitor) } }
  2. ## The following is the same as above:
  3. # {{- include "common.serviceMonitor.secret" (list . .Values.serviceMonitor "mychart.serviceMonitor.secret") }}
  4. # {{- define "mychart.serviceMonitor.secret" -}}
  5. # {{- end }}

Output:

  1. apiVersion: v1
  2. data:
  3. password: cGFzc3dvcmQ=
  4. username: YWRtaW5pc3RyYXRvcg==
  5. kind: Secret
  6. metadata:
  7. labels:
  8. app.kubernetes.io/instance: release-name
  9. app.kubernetes.io/managed-by: Helm
  10. app.kubernetes.io/name: mychart
  11. app.kubernetes.io/version: 1.16.0
  12. helm.sh/chart: mychart-0.1.0
  13. name: release-name-mychart
  14. namespace: monitoring
  15. type: Opaque

Partial Objects

When writing Kubernetes resources, you may find the following helpers useful to construct parts of the spec.

common.chart

The common.chart helper prints the chart name and version, escaped to be legal in a Kubernetes label field.

Example template:

  1. helm.sh/chart: { { include "common.chart" . } }

For the chart foo with version 1.2.3-beta.55+1234, this will render:

  1. helm.sh/chart: foo-1.2.3-beta.55_1234

(Note that + is an illegal character in label values)

common.container

The common.container template accepts a list of three values:

  • $top, the top context
  • $container, a dictionary of values used in the container template
  • [optional] the template name of the overrides

It creates a basic Container spec to be used within a Deployment or CronJob. It holds the following defaults:

  • The name is set to the chart name
  • Uses $container.image to describe the image to run, with the following spec:
    1. image:
    2. repository: nginx
    3. tag: stable
    4. pullPolicy: IfNotPresent
  • Lays out the security options using $container.securityContext
  • Lays out the compute resources using $container.resources

Example use:

  1. {{- include "common.deployment" (list . .Values .Values.autoscaling "mychart.deployment") }}
  2. {{- define "mychart.deployment" -}}
  3. ## Define overrides for your Deployment resource here, e.g.
  4. {{- $top := first . }}
  5. {{- $deployment := index . 1 }}
  6. spec:
  7. template:
  8. spec:
  9. containers:
  10. - {{- include "common.container" (list $top $deployment "mychart.deployment.container") | nindent 8 }}
  11. {{- end }}
  12. {{- define "mychart.deployment.container" -}}
  13. ## Define overrides for your Container here, e.g.
  14. ports:
  15. - name: http
  16. containerPort: 80
  17. protocol: TCP
  18. livenessProbe:
  19. httpGet:
  20. path: /
  21. port: http
  22. readinessProbe:
  23. httpGet:
  24. path: /
  25. port: http
  26. {{- end }}

The above example creates a Deployment resource which makes use of the common.container template to populate the PodSpec‘s container list. The usage of this template is similar to the other resources, you must define and reference a template that contains overrides for the container object.

The most important part of a container definition is the image you want to run. As mentioned above, this is derived from $container.image by default. It is a best practice to define the image, tag and pull policy in your charts’ values as this makes it easy for an operator to change the image registry, or use a specific tag or version. Another example of configuration that should be exposed to chart operators is the container’s required compute resources, as this is also very specific to an operators environment. An example values.yaml for your chart could look like:

  1. replicaCount: 1
  2. image:
  3. repository: nginx
  4. tag: stable
  5. pullPolicy: IfNotPresent
  6. securityContext:
  7. capabilities:
  8. drop:
  9. - ALL
  10. readOnlyRootFilesystem: true
  11. runAsNonRoot: true
  12. runAsUser: 1000
  13. resources:
  14. limits:
  15. cpu: 100m
  16. memory: 128Mi
  17. requests:
  18. cpu: 100m
  19. memory: 128Mi

The output of running the above values through the earlier template is:

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. labels:
  5. app.kubernetes.io/instance: release-name
  6. app.kubernetes.io/managed-by: Helm
  7. app.kubernetes.io/name: mychart
  8. app.kubernetes.io/version: 1.16.0
  9. helm.sh/chart: mychart-0.1.0
  10. name: release-name-mychart
  11. spec:
  12. replicas: 1
  13. selector:
  14. matchLabels:
  15. app.kubernetes.io/instance: release-name
  16. app.kubernetes.io/name: mychart
  17. template:
  18. metadata:
  19. labels:
  20. app.kubernetes.io/instance: release-name
  21. app.kubernetes.io/name: mychart
  22. spec:
  23. containers:
  24. - image: nginx:stable
  25. imagePullPolicy: IfNotPresent
  26. livenessProbe:
  27. httpGet:
  28. path: /
  29. port: http
  30. name: mychart
  31. ports:
  32. - containerPort: 80
  33. name: http
  34. protocol: TCP
  35. readinessProbe:
  36. httpGet:
  37. path: /
  38. port: http
  39. resources:
  40. limits:
  41. cpu: 100m
  42. memory: 128Mi
  43. requests:
  44. cpu: 100m
  45. memory: 128Mi
  46. securityContext:
  47. capabilities:
  48. drop:
  49. - ALL
  50. readOnlyRootFilesystem: true
  51. runAsNonRoot: true
  52. runAsUser: 1000
  53. serviceAccountName: release-name-mychart

common.fullname

The common.fullname template generates a name suitable for the name: field in Kubernetes metadata. It is used like this:

  1. name: { { include "common.fullname" . } }

This prints the value of {{ .Release.Name }}-{{ .Chart.Name }} by default, but can be overridden with .Values. fullnameOverride:

  1. fullnameOverride: some-name

Example output:

  1. ---
  2. # with the values above
  3. name: some-name
  4. ---
  5. # the default, for release "release-name" and chart "mychart"
  6. name: release-name-mychart

Output of this function is truncated at 63 characters, which is the maximum length of name.

common.labels

common.selectorLabels prints the standard set of labels.

Example usage:

  1. {{ include "common.labels" . }}

Example output:

  1. app.kubernetes.io/instance: release-name
  2. app.kubernetes.io/managed-by: Helm
  3. app.kubernetes.io/name: mychart
  4. app.kubernetes.io/version: 1.16.0
  5. helm.sh/chart: mychart-0.1.0

common.metadata

The common.metadata helper generates value for the metadata: section of a Kubernetes resource.

This takes a list of two values:

  • $top, the top context
  • [optional] the template name of the overrides

It generates standard labels and a name field.

Example template:

  1. metadata: { { - include "common.metadata" (list .) | nindent 2 } }
  2. ## The following is the same as above:
  3. # metadata:
  4. # {{- include "common.metadata" (list . "mychart.metadata") | nindent 2 }}
  5. # {{- define "mychart.metadata" -}}
  6. # {{- end }}

Example output:

  1. metadata:
  2. labels:
  3. app.kubernetes.io/instance: release-name
  4. app.kubernetes.io/managed-by: Helm
  5. app.kubernetes.io/name: mychart
  6. app.kubernetes.io/version: 1.16.0
  7. helm.sh/chart: mychart-0.1.0
  8. name: release-name-mychart

Most of the common templates that define a resource type (e.g. common.configMap or common.cronJob) use this to generate the metadata, which means they inherit the same labels and name fields.

common.name

The common.name template generates a name suitable for the app.kubernetes.io/name label. It is used like this:

  1. app.kubernetes.io/name: { { include "common.name" . } }

This prints the value of {{ .Chart.Name }} by default, but can be overridden with .Values.nameOverride:

  1. nameOverride: some-name

Example output:

  1. ---
  2. # with the values above
  3. app.kubernetes.io/name: some-name
  4. ---
  5. # the default, for chart "mychart"
  6. app.kubernetes.io/name: mychart

Output of this function is truncated at 63 characters, which is the maximum length of name.

common.pod.template

The common.pod.template template accepts a list of four values:

  • $top, the top context
  • $pod, a dictionary of values used in the container template
  • $serviceAccount, a dictionary of values used in the service account template
  • [optional] the template name of the overrides

It creates a basic PodTemplate spec to be used within a Deployment or CronJob. It holds the following defaults:

It also uses the following configuration from the $pod:

Value Description
$pod.imagePullSecrets Names of secrets containing private registry credentials
$pod.podAnnotations Pod annotations
$pod.podSecurityContext Security options
$pod.nodeSelector Node labels for pod assignment
$pod.affinity Expressions for affinity
$pod.tolerations Toleration labels for pod assignment
$pod.podLabels Pod extra labels
$pod.priorityClassName Pod priorityClassName

Underneath the hood, it invokes common.container template with $pod to populate the PodSpec‘s container list.

common.selectorLabels

common.selectorLabels prints the standard set of selector labels.

Example usage:

  1. {{ include "common.selectorLabels" . }}

Example output:

  1. app.kubernetes.io/instance: release-name
  2. app.kubernetes.io/name: mychart

common.serviceAccountName

The common.serviceAccountName template accepts a list of two values:

  • $top, the top context
  • $serviceAccount, a dictionary of values used in the service account template

It generates a name suitable for the serviceAccountName field of a Pod resource.

Example usage:

  1. serviceAccountName: {{ include "common.serviceAccountName" . .Values.serviceAccount }}

The following values can influence the output:

  1. serviceAccount:
  2. create: true
  3. # The name of the service account to use.
  4. # If not set and create is true, a name is generated using the fullname template
  5. name: some-name

Example output:

  1. ---
  2. # with the values above
  3. serviceAccountName: some-name
  4. ---
  5. # if serviceAccount.name is not set, the value will be the same as "common.fullname"
  6. serviceAccountName: release-name-mychart
  7. ---
  8. # if serviceAccount.create is false, the value will be "default"
  9. serviceAccountName: default