Commit f47e1619 authored by Reinhard Nägele's avatar Reinhard Nägele

Update initial chart experience

* Add template for chart name
* Use named ports and simplify service configuration
* Add affinity
* Add tolerations
* Use 'with' statements where possible
* Enhance ingress
* Add fullnameOverride to fullname template
parent e9203b82
...@@ -52,29 +52,31 @@ const ( ...@@ -52,29 +52,31 @@ const (
const defaultValues = `# Default values for %s. const defaultValues = `# Default values for %s.
# This is a YAML-formatted file. # This is a YAML-formatted file.
# Declare variables to be passed into your templates. # Declare variables to be passed into your templates.
replicaCount: 1 replicaCount: 1
image: image:
repository: nginx repository: nginx
tag: stable tag: stable
pullPolicy: IfNotPresent pullPolicy: IfNotPresent
service: service:
name: nginx
type: ClusterIP type: ClusterIP
externalPort: 80 port: 80
internalPort: 80
ingress: ingress:
enabled: false enabled: false
# Used to create an Ingress record. annotations: {}
hosts:
- chart-example.local
annotations:
# kubernetes.io/ingress.class: nginx # kubernetes.io/ingress.class: nginx
# kubernetes.io/tls-acme: "true" # kubernetes.io/tls-acme: "true"
tls: path: /
# Secrets must be manually created in the namespace. hosts:
# - secretName: chart-example-tls - chart-example.local
# hosts: tls: []
# - chart-example.local # - secretName: chart-example-tls
# hosts:
# - chart-example.local
resources: {} resources: {}
# We usually recommend not to specify default resources and to leave this as a conscious # We usually recommend not to specify default resources and to leave this as a conscious
# choice for the user. This also increases chances charts run on environments with little # choice for the user. This also increases chances charts run on environments with little
...@@ -86,6 +88,12 @@ resources: {} ...@@ -86,6 +88,12 @@ resources: {}
# requests: # requests:
# cpu: 100m # cpu: 100m
# memory: 128Mi # memory: 128Mi
nodeSelector: {}
tolerations: []
affinity: {}
` `
const defaultIgnore = `# Patterns to ignore when building packages. const defaultIgnore = `# Patterns to ignore when building packages.
...@@ -112,50 +120,61 @@ const defaultIgnore = `# Patterns to ignore when building packages. ...@@ -112,50 +120,61 @@ const defaultIgnore = `# Patterns to ignore when building packages.
` `
const defaultIngress = `{{- if .Values.ingress.enabled -}} const defaultIngress = `{{- if .Values.ingress.enabled -}}
{{- $serviceName := include "<CHARTNAME>.fullname" . -}} {{- $fullName := include "<CHARTNAME>.fullname" . -}}
{{- $servicePort := .Values.service.externalPort -}} {{- $servicePort := .Values.service.port -}}
{{- $ingressPath := .Values.ingress.path -}}
apiVersion: extensions/v1beta1 apiVersion: extensions/v1beta1
kind: Ingress kind: Ingress
metadata: metadata:
name: {{ template "<CHARTNAME>.fullname" . }} name: {{ $fullName }}
labels: labels:
app: {{ template "<CHARTNAME>.name" . }} app: {{ template "<CHARTNAME>.name" . }}
chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }} chart: {{ template "<CHARTNAME>.chart" . }}
release: {{ .Release.Name }} release: {{ .Release.Name }}
heritage: {{ .Release.Service }} heritage: {{ .Release.Service }}
{{- with .Values.ingress.annotations }}
annotations: annotations:
{{- range $key, $value := .Values.ingress.annotations }} {{ toYaml . | indent 4 }}
{{ $key }}: {{ $value | quote }} {{- end }}
{{- end }}
spec: spec:
{{- if .Values.ingress.tls }}
tls:
{{- range .Values.ingress.tls }}
- hosts:
{{- range .hosts }}
- {{ . }}
{{- end }}
secretName: {{ .secretName }}
{{- end }}
{{- end }}
rules: rules:
{{- range $host := .Values.ingress.hosts }} {{- range .Values.ingress.hosts }}
- host: {{ $host }} - host: {{ . }}
http: http:
paths: paths:
- path: / - path: {{ $ingressPath }}
backend: backend:
serviceName: {{ $serviceName }} serviceName: {{ $fullName }}
servicePort: {{ $servicePort }} servicePort: http
{{- end -}} {{- end }}
{{- if .Values.ingress.tls }} {{- end }}
tls:
{{ toYaml .Values.ingress.tls | indent 4 }}
{{- end -}}
{{- end -}}
` `
const defaultDeployment = `apiVersion: extensions/v1beta1 const defaultDeployment = `apiVersion: apps/v1beta2
kind: Deployment kind: Deployment
metadata: metadata:
name: {{ template "<CHARTNAME>.fullname" . }} name: {{ template "<CHARTNAME>.fullname" . }}
labels: labels:
app: {{ template "<CHARTNAME>.name" . }} app: {{ template "<CHARTNAME>.name" . }}
chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }} chart: {{ template "<CHARTNAME>.chart" . }}
release: {{ .Release.Name }} release: {{ .Release.Name }}
heritage: {{ .Release.Service }} heritage: {{ .Release.Service }}
spec: spec:
replicas: {{ .Values.replicaCount }} replicas: {{ .Values.replicaCount }}
selector:
matchLabels:
app: {{ template "<CHARTNAME>.name" . }}
release: {{ .Release.Name }}
template: template:
metadata: metadata:
labels: labels:
...@@ -167,20 +186,30 @@ spec: ...@@ -167,20 +186,30 @@ spec:
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}" image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
imagePullPolicy: {{ .Values.image.pullPolicy }} imagePullPolicy: {{ .Values.image.pullPolicy }}
ports: ports:
- containerPort: {{ .Values.service.internalPort }} - name: http
containerPort: 80
protocol: TCP
livenessProbe: livenessProbe:
httpGet: httpGet:
path: / path: /
port: {{ .Values.service.internalPort }} port: http
readinessProbe: readinessProbe:
httpGet: httpGet:
path: / path: /
port: {{ .Values.service.internalPort }} port: http
resources: resources:
{{ toYaml .Values.resources | indent 12 }} {{ toYaml .Values.resources | indent 12 }}
{{- if .Values.nodeSelector }} {{- with .Values.nodeSelector }}
nodeSelector: nodeSelector:
{{ toYaml .Values.nodeSelector | indent 8 }} {{ toYaml . | indent 8 }}
{{- end }}
{{- with .Values.affinity }}
affinity:
{{ toYaml . | indent 8 }}
{{- end }}
{{- with .Values.tolerations }}
tolerations:
{{ toYaml . | indent 8 }}
{{- end }} {{- end }}
` `
...@@ -190,16 +219,16 @@ metadata: ...@@ -190,16 +219,16 @@ metadata:
name: {{ template "<CHARTNAME>.fullname" . }} name: {{ template "<CHARTNAME>.fullname" . }}
labels: labels:
app: {{ template "<CHARTNAME>.name" . }} app: {{ template "<CHARTNAME>.name" . }}
chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }} chart: {{ template "<CHARTNAME>.chart" . }}
release: {{ .Release.Name }} release: {{ .Release.Name }}
heritage: {{ .Release.Service }} heritage: {{ .Release.Service }}
spec: spec:
type: {{ .Values.service.type }} type: {{ .Values.service.type }}
ports: ports:
- port: {{ .Values.service.externalPort }} - port: {{ .Values.service.port }}
targetPort: {{ .Values.service.internalPort }} targetPort: http
protocol: TCP protocol: TCP
name: {{ .Values.service.name }} name: http
selector: selector:
app: {{ template "<CHARTNAME>.name" . }} app: {{ template "<CHARTNAME>.name" . }}
release: {{ .Release.Name }} release: {{ .Release.Name }}
...@@ -208,7 +237,7 @@ spec: ...@@ -208,7 +237,7 @@ spec:
const defaultNotes = `1. Get the application URL by running these commands: const defaultNotes = `1. Get the application URL by running these commands:
{{- if .Values.ingress.enabled }} {{- if .Values.ingress.enabled }}
{{- range .Values.ingress.hosts }} {{- range .Values.ingress.hosts }}
http://{{ . }} http{{ if $.Values.ingress.tls }}s{{ end }}://{{ . }}{{ $.Values.ingress.path }}
{{- end }} {{- end }}
{{- else if contains "NodePort" .Values.service.type }} {{- else if contains "NodePort" .Values.service.type }}
export NODE_PORT=$(kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {{ template "<CHARTNAME>.fullname" . }}) export NODE_PORT=$(kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {{ template "<CHARTNAME>.fullname" . }})
...@@ -218,11 +247,11 @@ const defaultNotes = `1. Get the application URL by running these commands: ...@@ -218,11 +247,11 @@ const defaultNotes = `1. Get the application URL by running these commands:
NOTE: It may take a few minutes for the LoadBalancer IP to be available. NOTE: It may take a few minutes for the LoadBalancer IP to be available.
You can watch the status of by running 'kubectl get svc -w {{ template "<CHARTNAME>.fullname" . }}' You can watch the status of by running 'kubectl get svc -w {{ template "<CHARTNAME>.fullname" . }}'
export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ template "<CHARTNAME>.fullname" . }} -o jsonpath='{.status.loadBalancer.ingress[0].ip}') export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ template "<CHARTNAME>.fullname" . }} -o jsonpath='{.status.loadBalancer.ingress[0].ip}')
echo http://$SERVICE_IP:{{ .Values.service.externalPort }} echo http://$SERVICE_IP:{{ .Values.service.port }}
{{- else if contains "ClusterIP" .Values.service.type }} {{- else if contains "ClusterIP" .Values.service.type }}
export POD_NAME=$(kubectl get pods --namespace {{ .Release.Namespace }} -l "app={{ template "<CHARTNAME>.name" . }},release={{ .Release.Name }}" -o jsonpath="{.items[0].metadata.name}") export POD_NAME=$(kubectl get pods --namespace {{ .Release.Namespace }} -l "app={{ template "<CHARTNAME>.name" . }},release={{ .Release.Name }}" -o jsonpath="{.items[0].metadata.name}")
echo "Visit http://127.0.0.1:8080 to use your application" echo "Visit http://127.0.0.1:8080 to use your application"
kubectl port-forward $POD_NAME 8080:{{ .Values.service.internalPort }} kubectl port-forward $POD_NAME 8080:80
{{- end }} {{- end }}
` `
...@@ -240,6 +269,9 @@ We truncate at 63 chars because some Kubernetes name fields are limited to this ...@@ -240,6 +269,9 @@ We truncate at 63 chars because some Kubernetes name fields are limited to this
If release name contains chart name it will be used as a full name. If release name contains chart name it will be used as a full name.
*/}} */}}
{{- define "<CHARTNAME>.fullname" -}} {{- define "<CHARTNAME>.fullname" -}}
{{- if .Values.fullnameOverride -}}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" -}}
{{- else -}}
{{- $name := default .Chart.Name .Values.nameOverride -}} {{- $name := default .Chart.Name .Values.nameOverride -}}
{{- if contains $name .Release.Name -}} {{- if contains $name .Release.Name -}}
{{- .Release.Name | trunc 63 | trimSuffix "-" -}} {{- .Release.Name | trunc 63 | trimSuffix "-" -}}
...@@ -247,6 +279,14 @@ If release name contains chart name it will be used as a full name. ...@@ -247,6 +279,14 @@ If release name contains chart name it will be used as a full name.
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}} {{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}}
{{- end -}} {{- end -}}
{{- end -}} {{- end -}}
{{- end -}}
{{/*
Create chart name and version as used by the chart label.
*/}}
{{- define "<CHARTNAME>.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}}
{{- end -}}
` `
// CreateFrom creates a new chart, but scaffolds it from the src chart. // CreateFrom creates a new chart, but scaffolds it from the src chart.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment