Skip to content

Commit

Permalink
setup templates for proper name prefixing
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellh committed Aug 18, 2018
1 parent 323feba commit 60e57f7
Show file tree
Hide file tree
Showing 14 changed files with 129 additions and 42 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.terraform/
terraform.tfstate*
terraform.tfvars
values.dev.yaml
2 changes: 2 additions & 0 deletions .helmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.git/
.terraform/
20 changes: 19 additions & 1 deletion templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,32 @@ 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 "consul.namePrefix" -}}
{{- define "consul.fullname" -}}
{{- if .Values.fullnameOverride -}}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" -}}
{{- else -}}
{{- $name := default .Chart.Name .Values.nameOverride -}}
{{- 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 "consul.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{/*
Expand the name of the chart.
*/}}
{{- define "consul.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{/*
Compute the maximum number of unavailable replicas for the PodDisruptionBudget.
Expand Down
21 changes: 17 additions & 4 deletions templates/client-daemonset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,26 @@
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: consul
name: {{ template "consul.fullname" . }}
labels:
app: {{ template "consul.name" . }}
chart: {{ template "consul.chart" . }}
heritage: {{ .Release.Service }}
release: {{ .Release.Name }}
spec:
selector:
matchLabels:
app: consul
app: {{ template "consul.name" . }}
chart: {{ template "consul.chart" . }}
release: {{ .Release.Name }}
component: client
template:
metadata:
labels:
app: consul
app: {{ template "consul.name" . }}
chart: {{ template "consul.chart" . }}
release: {{ .Release.Name }}
component: client
annotations:
"consul.hashicorp.com/connect-inject": "false"
spec:
Expand Down Expand Up @@ -40,6 +51,8 @@ spec:
- "/bin/sh"
- "-ec"
- |
CONSUL_FULLNAME="{{template "consul.fullname" . }}"
exec /bin/consul agent \
-advertise="${POD_IP}" \
-bind=0.0.0.0 \
Expand All @@ -53,7 +66,7 @@ spec:
{{- else }}
{{- if .Values.server.enabled }}
{{- range $index := until (.Values.server.replicas | int) }}
-retry-join=consul-server-{{ $index }}.consul-server.${NAMESPACE}.svc \
-retry-join=${CONSUL_FULLNAME}-server-{{ $index }}.${CONSUL_FULLNAME}-server.${NAMESPACE}.svc \
{{- end }}
{{- end }}
{{- end }}
Expand Down
21 changes: 16 additions & 5 deletions templates/connect-inject-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,27 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: consul-connect-injector-webhook-deployment
name: {{ template "consul.fullname" }}-connect-injector-webhook-deployment
labels:
app: consul-connect-injector
app: {{ template "consul.name" . }}
chart: {{ template "consul.chart" . }}
heritage: {{ .Release.Service }}
release: {{ .Release.Name }}
spec:
replicas: 1
selector:
matchLabels:
app: consul-connect-injector
app: {{ template "consul.name" . }}
chart: {{ template "consul.chart" . }}
release: {{ .Release.Name }}
component: connect-injector
template:
metadata:
labels:
app: consul-connect-injector
app: {{ template "consul.name" . }}
chart: {{ template "consul.chart" . }}
release: {{ .Release.Name }}
component: connect-injector
spec:
containers:
- name: sidecar-injector
Expand All @@ -28,6 +37,8 @@ spec:
- "/bin/sh"
- "-ec"
- |
CONSUL_FULLNAME="{{template "consul.fullname" . }}"
consul-k8s inject \
-default-inject={{ .Values.connectInject.default }} \
-listen=:8080 \
Expand All @@ -36,7 +47,7 @@ spec:
-tls-key-file=/etc/connect-injector/certs/{{ .Values.connectInject.certs.keyName }}
{{- else }}
-tls-auto=consul-connect-injector-cfg \
-tls-auto-hosts=consul-connect-injector-svc,consul-connect-injector-svc.${NAMESPACE},consul-connect-injector-svc.${NAMESPACE}.svc
-tls-auto-hosts=${CONSUL_FULLNAME}-connect-injector-svc,${CONSUL_FULLNAME}-connect-injector-svc.${NAMESPACE},${CONSUL_FULLNAME}-connect-injector-svc.${NAMESPACE}.svc
{{- end }}
livenessProbe:
tcpSocket:
Expand Down
11 changes: 7 additions & 4 deletions templates/connect-inject-mutatingwebhook.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
apiVersion: admissionregistration.k8s.io/v1beta1
kind: MutatingWebhookConfiguration
metadata:
name: consul-connect-injector-cfg
name: {{ template "consul.fullname" . }}-connect-injector-cfg
labels:
app: consul-connect-injector
app: {{ template "consul.name" . }}
chart: {{ template "consul.chart" . }}
heritage: {{ .Release.Service }}
release: {{ .Release.Name }}
webhooks:
- name: consul-connect-injector.consul.hashicorp.com
- name: {{ template "consul.fullname" . }}-connect-injector.consul.hashicorp.com
clientConfig:
service:
name: consul-connect-injector-svc
name: {{ template "consul.fullname" . }}-connect-injector-svc
namespace: default
path: "/mutate"
caBundle: {{ .Values.connectInject.caBundle }}
Expand Down
11 changes: 8 additions & 3 deletions templates/connect-inject-service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@
apiVersion: v1
kind: Service
metadata:
name: consul-connect-injector-svc
name: {{ template "consul.fullname" . }}-connect-injector-svc
labels:
app: consul-connect-injector
app: {{ template "consul.name" . }}
chart: {{ template "consul.chart" . }}
heritage: {{ .Release.Service }}
release: {{ .Release.Name }}
spec:
ports:
- port: 443
targetPort: 8080
selector:
app: consul-connect-injector
app: {{ template "consul.name" . }}
release: "{{ .Release.Name }}"
component: connect-injector
{{- end }}

7 changes: 6 additions & 1 deletion templates/server-config-configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: consul-server-config
name: {{ template "consul.fullname" . }}-server-config
labels:
app: {{ template "consul.name" . }}
chart: {{ template "consul.chart" . }}
heritage: {{ .Release.Service }}
release: {{ .Release.Name }}
data:
extra-from-values.json: |-
{{ tpl .Values.server.extraConfig . | indent 4 }}
Expand Down
11 changes: 9 additions & 2 deletions templates/server-disruptionbudget.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@
apiVersion: policy/v1beta1
kind: PodDisruptionBudget
metadata:
name: consul-pdb
name: {{ template "consul.fullname" . }}-server
labels:
app: {{ template "consul.name" . }}
chart: {{ template "consul.chart" . }}
heritage: {{ .Release.Service }}
release: {{ .Release.Name }}
spec:
maxUnavailable: {{ template "consul.pdb.maxUnavailable" . }}
selector:
matchLabels:
app: consul-server
app: {{ template "consul.name" . }}
release: "{{ .Release.Name }}"
component: server
{{- end }}
11 changes: 8 additions & 3 deletions templates/server-service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
apiVersion: v1
kind: Service
metadata:
name: consul-server
name: {{ template "consul.fullname" . }}-server
labels:
name: consul-server
app: {{ template "consul.name" . }}
chart: {{ template "consul.chart" . }}
heritage: {{ .Release.Service }}
release: {{ .Release.Name }}
annotations:
# This must be set in addition to publishNotReadyAddresses due
# to an open issue where it may not work:
Expand Down Expand Up @@ -47,5 +50,7 @@ spec:
port: 8600
targetPort: 8600
selector:
app: consul-server
app: {{ template "consul.name" . }}
release: "{{ .Release.Name }}"
component: server
{{- end }}
38 changes: 25 additions & 13 deletions templates/server-statefulset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: consul-server
name: {{ template "consul.fullname" . }}-server
labels:
app: {{ template "consul.name" . }}
chart: {{ template "consul.chart" . }}
heritage: {{ .Release.Service }}
release: {{ .Release.Name }}
spec:
selector:
matchLabels:
app: consul-server
serviceName: consul-server
serviceName: {{ template "consul.fullname" . }}-server
podManagementPolicy: Parallel
replicas: {{ .Values.server.replicas }}
{{- if (gt (int .Values.server.updatePartition) 0) }}
Expand All @@ -17,30 +19,38 @@ spec:
rollingUpdate:
partition: {{ .Values.server.updatePartition }}
{{- end }}
selector:
matchLabels:
app: {{ template "consul.name" . }}
chart: {{ template "consul.chart" . }}
release: {{ .Release.Name }}
component: server
template:
metadata:
labels:
app: consul-server
app: {{ template "consul.name" . }}
chart: {{ template "consul.chart" . }}
release: {{ .Release.Name }}
component: server
annotations:
"consul.hashicorp.com/connect-inject": "false"
spec:
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values:
- consul-server
matchLabels:
app: {{ template "consul.name" . }}
release: "{{ .Release.Name }}"
component: server
topologyKey: kubernetes.io/hostname
terminationGracePeriodSeconds: 10
securityContext:
fsGroup: 1000
volumes:
- name: config
configMap:
name: consul-server-config
name: {{ template "consul.fullname" . }}-server-config
containers:
- name: consul
image: "{{ .Values.server.image }}"
Expand All @@ -57,6 +67,8 @@ spec:
- "/bin/sh"
- "-ec"
- |
CONSUL_FULLNAME="{{template "consul.fullname" . }}"
exec /bin/consul agent \
-advertise="${POD_IP}" \
-bind=0.0.0.0 \
Expand All @@ -73,7 +85,7 @@ spec:
-ui \
{{- end }}
{{- range $index := until (.Values.server.replicas | int) }}
-retry-join=consul-server-{{ $index }}.consul-server.${NAMESPACE}.svc \
-retry-join=${CONSUL_FULLNAME}-server-{{ $index }}.${CONSUL_FULLNAME}-server.${NAMESPACE}.svc \
{{- end }}
-server
volumeMounts:
Expand Down
4 changes: 2 additions & 2 deletions templates/tests/test-config.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ template "consul.namePrefix" . }}-tests
name: {{ template "consul.fullname" . }}-tests
data:
run.sh: |-
@test "Testing Consul cluster has quorum" {
[ `kubectl exec {{ template "consul.namePrefix" . }}-server-0 consul members --namespace={{ .Release.Namespace }} | grep server | wc -l` -ge "3" ]
[ `kubectl exec {{ template "consul.fullname" . }}-server-0 consul members --namespace={{ .Release.Namespace }} | grep server | wc -l` -ge "3" ]
}
2 changes: 1 addition & 1 deletion templates/tests/test-runner.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ spec:
volumes:
- name: tests
configMap:
name: {{ template "consul.namePrefix" . }}-tests
name: {{ template "consul.fullname" . }}-tests
- name: tools
emptyDir: {}
restartPolicy: Never
11 changes: 8 additions & 3 deletions templates/ui-service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@
apiVersion: v1
kind: Service
metadata:
name: consul-ui
name: {{ template "consul.fullname" . }}-ui
labels:
name: consul-ui
app: {{ template "consul.name" . }}
chart: {{ template "consul.chart" . }}
heritage: {{ .Release.Service }}
release: {{ .Release.Name }}
spec:
selector:
app: consul-server
app: {{ template "consul.name" . }}
release: "{{ .Release.Name }}"
component: server
ports:
- name: http
port: 80
Expand Down

0 comments on commit 60e57f7

Please sign in to comment.