Skip to content

Commit

Permalink
feat: Update labels and names to app
Browse files Browse the repository at this point in the history
Updates for better named helm charts:
* Use new app.version and app.name labels to identify resource names.
* Include release namespace.
* Remove chart name from main resource names.
  • Loading branch information
colinjfw committed Sep 7, 2019
1 parent 46c0ae1 commit 9387304
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 56 deletions.
2 changes: 1 addition & 1 deletion charts/app/templates/NOTES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ include "app.fullname" . }} -o jsonpath='{.status.loadBalancer.ingress[0].ip}')
echo http://$SERVICE_IP:{{ .Values.service.port }}
{{- else if contains "ClusterIP" .Values.service.type }}
export POD_NAME=$(kubectl get pods --namespace {{ .Release.Namespace }} -l "app.kubernetes.io/name={{ include "app.name" . }},app.kubernetes.io/instance={{ .Release.Name }}" -o jsonpath="{.items[0].metadata.name}")
export POD_NAME=$(kubectl get pods --namespace {{ .Release.Namespace }} -l "app.kubernetes.io/name={{ .Values.app.name }},app.kubernetes.io/instance={{ .Release.Name }}" -o jsonpath="{.items[0].metadata.name}")
echo "Visit http://127.0.0.1:8080 to use your application"
kubectl port-forward $POD_NAME 8080:80
{{- end }}
53 changes: 19 additions & 34 deletions charts/app/templates/_helpers.tpl
Original file line number Diff line number Diff line change
@@ -1,43 +1,28 @@
{{/* vim: set filetype=mustache: */}}
{{/*
Expand the name of the chart.
*/}}
{{- define "app.name" -}}
{{- default .Chart.Name .Values.appName | trunc 63 | trimSuffix "-" -}}
{{/* Create chart name and version as used by the chart label. */}}
{{- define "app.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
If release name contains chart name it will be used as a full name.
*/}}
{{- define "app.fullname" -}}
{{- if .Values.fullnameOverride -}}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" -}}
{{- else -}}
{{- $name := default .Chart.Name .Values.appName -}}
{{- if contains $name .Release.Name -}}
{{- .Release.Name | trunc 63 | trimSuffix "-" -}}
{{- else -}}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}}
{{- end -}}
{{- end -}}
{{- end -}}

{{/*
Create chart name and version as used by the chart label.
*/}}
{{- define "app.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}}
{{ .Release.Name }}
{{- end -}}

{{/*
Common labels
*/}}
{{/* Common labels */}}
{{- define "app.labels" -}}
app.kubernetes.io/name: {{ include "app.name" . }}
helm.sh/chart: {{ include "app.chart" . }}
app.kubernetes.io/name: {{ required "app.name variable is required" .Values.app.name }}
app.kubernetes.io/instance: {{ .Release.Name }}
app.kubernetes.io/version: {{ .Values.version | quote }}
app.kubernetes.io/version: {{ required "app.version variable is required" .Values.app.version | quote }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
helm.sh/chart: {{ include "app.chart" . }}
{{- end -}}

{{/* Match labels to find this instance */}}
{{- define "app.matchLabels" -}}
app.kubernetes.io/name: {{ .Values.app.name }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end -}}

{{/* Selector labels for load balancing */}}
{{- define "app.selector" -}}
app.kubernetes.io/name: {{ .Values.app.name }}
{{- end -}}
9 changes: 5 additions & 4 deletions charts/app/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@ apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "app.fullname" . }}
namespace: {{ .Release.Namespace }}
annotations:
"deliverybot.io/secret-checksum": {{ toJson .Values.secrets | sha256sum }}
labels:
{{ include "app.labels" . | indent 4 }}
spec:
replicas: {{ .Values.replicaCount }}
selector:
matchLabels:
app.kubernetes.io/name: {{ include "app.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{ include "app.matchLabels" . | indent 6 }}
template:
metadata:
labels:
app.kubernetes.io/name: {{ include "app.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{ include "app.matchLabels" . | indent 8 }}
spec:
{{- with .Values.imagePullSecrets }}
imagePullSecrets:
Expand Down
1 change: 1 addition & 0 deletions charts/app/templates/ingress.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: {{ $fullName }}
namespace: {{ .Release.Namespace }}
labels:
{{ include "app.labels" . | indent 4 }}
{{- with .Values.ingress.annotations }}
Expand Down
1 change: 1 addition & 0 deletions charts/app/templates/secret.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ apiVersion: v1
kind: Secret
metadata:
name: {{ include "app.fullname" . }}
namespace: {{ .Release.Namespace }}
labels:
{{ include "app.labels" . | indent 4 }}
type: Opaque
Expand Down
8 changes: 2 additions & 6 deletions charts/app/templates/service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ apiVersion: v1
kind: Service
metadata:
name: {{ include "app.fullname" . }}
namespace: {{ .Release.Namespace }}
labels:
{{ include "app.labels" . | indent 4 }}
spec:
Expand All @@ -13,10 +14,5 @@ spec:
protocol: TCP
name: http
selector:
{{- if .Values.appName }}
app.kubernetes.io/name: {{ .Values.appName }}
{{- else }}
app.kubernetes.io/name: {{ include "app.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}
{{ include "app.selector" . | indent 4 }}
{{- end }}
32 changes: 23 additions & 9 deletions charts/app/values.yaml
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
# replicaCount -- Replica count for deployments.
replicaCount: 1

# appname -- Application name. Set this value to use canary deploys.
# Keep this value consistent between canary deploys, if the stable release name
# is prod-myapp-stable and the canary release name is prod-myapp-canary, then
# the appName should be prod-myapp. The service will use this as the selector.
appName: ""
app:
# app.name -- Application name: Can be consistent between tracks. (required)
name: null
# app.version -- Application version: Unique tag for this release. (required)
version: null

image:
# image.repository -- Docker image repository.
repository: nginx
# image.tag -- Docker image tag.
tag: latest
pullPolicy: IfNotPresent

# imagePullSecrets -- Defines secrets to use for pulling docker images.
imagePullSecrets: []
fullnameOverride: ""

service:
# service.enabled -- Enable service resource.
Expand Down Expand Up @@ -67,9 +69,9 @@ tolerations: []
affinity: {}

# env -- Environment variables for the application.
env: # []
# - name: Test
# value: "foobar"
env: []
# - name: ENVIRONMENT
# value: production

# secrets -- Creates a secret for the application. All variables passed in env.
# Uses envFrom in the deployment specification to pass these all through.
Expand All @@ -88,3 +90,15 @@ readinessProbe:
httpGet:
path: /
port: http

migrate:
# migrate.enabled -- Run a pod before the application is released to migrate.
enabled: false
# migrate.command -- Command to run on the migrate pod.
command: "rails db:migrate"

# workers -- Deploy background jobs with the same config as the main app.
workers: []
# - name: delayed-job
# command: ["rails", "jobs:work"]
# enableProbes: true
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,10 @@ async function run() {
"--atomic",
`--namespace=${namespace}`,
"--values=./values.yml",
`--set=appName=${appName}`,
];
if (dryRun) args.push("--dry-run");
if (version) args.push(`--set=version=${version}`);
if (appName) args.push(`--set=app.name=${appName}`);
if (version) args.push(`--set=app.version=${version}`);
valueFiles.forEach(f => args.push(`--values=${f}`));

// Stable track only deploys service and ingress resources. Any other track
Expand Down

0 comments on commit 9387304

Please sign in to comment.