Skip to content

Commit

Permalink
Update according to recommendation
Browse files Browse the repository at this point in the history
  • Loading branch information
ashwani-opstree committed Jul 8, 2024
1 parent b962755 commit 5ad7399
Show file tree
Hide file tree
Showing 9 changed files with 300 additions and 109 deletions.
5 changes: 5 additions & 0 deletions charts/microservice/examples/deploy-nginx.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
deployment:
image:
name: "nginx"
tag: "latest"
pullPolicy: IfNotPresent
5 changes: 5 additions & 0 deletions charts/microservice/templates/NOTES.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
You have deployed the following release: {{ .Release.Name }}.
To get further information, you can run the commands:
$ helm status {{ .Release.Name }}
$ helm get all {{ .Release.Name }}

25 changes: 25 additions & 0 deletions charts/microservice/templates/_capabilities.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{{/* vim: set filetype=mustache: */}}

{{/*
Return the target Kubernetes version
*/}}
{{- define "microservice.capabilities.kubeVersion" -}}
{{- default (default .Capabilities.KubeVersion.Version .Values.kubeVersion) ((.Values.global).kubeVersion) -}}
{{- end -}}

{{/*
Return the appropriate apiVersion for Horizontal Pod Autoscaler.
*/}}
{{- define "microservice.capabilities.hpa.apiVersion" -}}
{{- $kubeVersion := include "microservice.capabilities.kubeVersion" .context -}}
{{- if and (not (empty $kubeVersion)) (semverCompare "<1.23-0" $kubeVersion) -}}
{{- if .beta2 -}}
{{- print "autoscaling/v2beta2" -}}
{{- else -}}
{{- print "autoscaling/v2beta1" -}}
{{- end -}}
{{- else -}}
{{- print "autoscaling/v2" -}}
{{- end -}}
{{- end -}}

54 changes: 15 additions & 39 deletions charts/microservice/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,62 +6,38 @@ It will use the release name to give the app name
*/}}

{{- define "microservice.name" -}}
{{- default .Values.global.nameOverride | trunc 60 | trimSuffix "-" }}
{{- default .Chart.Name .Values.global.nameOverride | 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 "microservice.tempname" -}}
{{- if .Values.global.fullnameOverride }}
{{- .Values.global.fullnameOverride | trunc 60 | trimSuffix "-" }}
{{- else }}
{{- $name := default .Values.global.nameOverride }}
{{- if contains $name .Release.Name }}
{{- .Release.Name | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
{{- end }}
{{- end }}
{{- end }}

{{- define "microservice.fullname" -}}
{{- printf "%s-%s" ( include "microservice.tempname" . ) "app" | trunc 63 | trimSuffix "-" }}
{{- end }}
{{- if .Values.global.fullnameOverride -}}
{{- .Values.global.fullnameOverride | trunc 63 | trimSuffix "-" -}}
{{- else -}}
{{- $name := default .Chart.Name .Values.global.nameOverride -}}
{{- if contains $name .Release.Name -}}
{{- .Release.Name | trunc 63 | trimSuffix "-" -}}
{{- else -}}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}}
{{- end -}}
{{- end -}}
{{- end -}}

{{/*
Common labels
*/}}
{{- define "microservice.labels" -}}
app: {{ include "microservice.tempname" . }}
app: {{ include "microservice.fullname" . }}
{{- end }}

{{/*
Selector labels
*/}}
{{- define "microservice.selectorLabels" -}}
app: {{ include "microservice.tempname" . }}
{{- end }}%

{{/*
service name
*/}}
{{- define "microservice.servicename" -}}
{{- printf "%s-%s" ( include "microservice.tempname" . ) "svc" | trunc 63 | trimSuffix "-" }}
{{- end }}%

{{/*
configmap name
*/}}
{{- define "microservice.configmapname" -}}
{{- printf "%s-%s" ( include "microservice.tempname" . ) "cm" | trunc 63 | trimSuffix "-" }}
{{- end }}%

{{/*
Create name of the HPA to use
*/}}
{{- define "microservice.HorizontalScalingName" -}}
{{- printf "%s-%s" ( include "microservice.tempname" . ) "hpa" | trunc 63 | trimSuffix "-" }}
{{- end }}%
app: {{ include "microservice.fullname" . }}
{{- end }}
30 changes: 30 additions & 0 deletions charts/microservice/templates/configmap.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ConfigMap mounted as volumes
{{- if .Values.deployment.volumes.configMaps }}
{{- if .Values.deployment.volumes.enabled }}
{{ $header := .Values.deployment.volumes.configFileCommonHeader | default "" }}
{{ $root := . }}
{{ range $cm := .Values.deployment.volumes.configMaps}}
---
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ include "microservice.fullname" $root }}-{{ $cm.name }}-cm
namespace: {{ .Values.global.namespace }}
data:
{{- if $cm.data }}
{{- range $filename, $content := $cm.data }}
# property-like keys; each key maps to a simple value
{{ $filename }}: |-
{{ $content | toString | indent 4}}
{{- end }}
{{- end }}
{{- if $cm.files }}
{{- range $file := $cm.files }}
{{ $file.destination }}: |
{{ $header | toString | indent 4 }}
{{ $root.Files.Get $file.source }}
{{- end}}
{{- end }}
{{- end }}
{{- end }}
{{- end }}
86 changes: 63 additions & 23 deletions charts/microservice/templates/deployment.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
{{ $root := . }}
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "microservice.fullname" . }}
name: {{ include "microservice.fullname" . }}-app
namespace: {{ .Values.global.namespace }}
{{- if .Values.annotations }}
annotations:
{{- range $key, $value := .Values.annotations }}
{{ $key }}: {{ $value }}
{{- end }}
{{- end }}
labels:
{{- include "microservice.labels" . | nindent 4 }}
spec:
Expand All @@ -16,33 +25,64 @@ spec:
spec:
containers:
- name: {{ include "microservice.fullname" . }}
image: "{{ .Values.deployment.image.repository }}:{{ .Values.deployment.image.tag }}"
imagePullPolicy: {{ .Values.deployment.image.pullPolicy | default "IfNotPresent" }}
image: "{{ .Values.deployment.image.name }}:{{ .Values.deployment.image.tag }}"
imagePullPolicy: {{ .Values.deployment.image.pullPolicy }}
ports:
- name: http
containerPort: 80
protocol: TCP
{{- range .Values.service.specs}}
- name: {{ .name }}
containerPort: {{ .targetPort | default .port}}
protocol: {{ .protocol | default "TCP" }}
{{- end }}
{{- if (merge .Values.global.environment .Values.deployment.environment) }}
env:
{{- range $name, $value := merge .Values.global.environment .Values.deployment.environment}}
- name: {{ $name | quote}}
value: {{ $value | quote }}
{{- end }}
{{- end }}
{{- if .Values.deployment.livenessProbe }}
livenessProbe:
httpGet:
path: {{ default "/health" .Values.deployment.livenessProbe.httpPath }}
port: http
initialDelaySeconds: {{ default 25 .Values.deployment.livenessProbe.initialDelaySeconds }}
periodSeconds: {{ default 10 .Values.deployment.livenessProbe.periodSeconds }}
timeoutSeconds: {{ default 5 .Values.deployment.livenessProbe.timeoutSeconds }}
successThreshold: {{ default 1 .Values.deployment.livenessProbe.successThreshold }}
failureThreshold: {{ default 5 .Values.deployment.livenessProbe.failureThreshold }}
{{- toYaml .Values.deployment.livenessProbe | nindent 12 }}
{{- end }}
{{- if .Values.deployment.readinessProbe }}
readinessProbe:
httpGet:
path: {{ default "/health" .Values.deployment.readinessProbe.httpPath }}
port: http
initialDelaySeconds: {{ default 25 .Values.deployment.readinessProbe.initialDelaySeconds }}
periodSeconds: {{ default 10 .Values.deployment.readinessProbe.periodSeconds }}
timeoutSeconds: {{ default 5 .Values.deployment.readinessProbe.timeoutSeconds }}
successThreshold: {{ default 1 .Values.deployment.readinessProbe.successThreshold }}
failureThreshold: {{ default 5 .Values.deployment.readinessProbe.failureThreshold }}
{{- toYaml .Values.deployment.readinessProbe | nindent 12 }}
{{- end }}
resources:
{{- toYaml .Values.deployment.resources | nindent 12 }}
{{- toYaml .Values.deployment.resources | nindent 12 }}
{{- if .Values.deployment.volumes.enabled }}
volumeMounts:
{{- range $conf := .Values.deployment.volumes.configMaps }}
- mountPath: {{ $conf.mountPath }}
name: {{ include "microservice.fullname" $root }}-{{ $conf.name }}-cm
{{- end }}
{{- if .Values.deployment.volumes.pvc.enabled }}
- mountPath: {{ .Values.volumes.pvc.mountPath }}
name: {{ .Values.volumes.pvc.existing_claim | default .Values.volumes.pvc.name }}-volume
{{- end }}
{{- end }}
{{- if .Values.deployment.volumes.enabled }}
volumes:
{{- range $conf := .Values.deployment.volumes.configMaps }}
- name: {{ include "microservice.fullname" $root }}-{{ $conf.name }}-cm
configMap:
name: {{ $conf.name }}
{{- end }}
{{- if .Values.deployment.volumes.pvc.enabled}}
- name: {{ .Values.deployment.volumes.pvc.existing_claim | default .Values.volumes.pvc.name }}-volume
persistentVolumeClaim:
claimName: {{ .Values.deployment.volumes.pvc.existing_claim | default .Values.volumes.pvc.name }}
{{- end}}
{{- end }}
{{- with .Values.deployment.nodeSelector }}
nodeSelector:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.deployment.affinity }}
affinity:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.deployment.tolerations }}
tolerations:
{{- toYaml . | nindent 8 }}
{{- end }}
21 changes: 11 additions & 10 deletions charts/microservice/templates/hpa.yaml
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
{{- if .Values.hpa }}
apiVersion: autoscaling/v2beta1
{{- if .Values.hpa.enabled }}
apiVersion: {{ include "microservice.capabilities.hpa.apiVersion" ( dict "context" $ ) }}
kind: HorizontalPodAutoscaler
metadata:
name: {{ include "microservice.HorizontalScalingName" . }}
name: {{ include "microservice.fullname" . }}-hpa
namespace: {{ .Values.global.namespace }}
labels:
{{- include "microservice.labels" . | nindent 4 }}
spec:
scaleTargetRef:
kind: Deployment
name: {{ include "microservice.fullname" . }}
minReplicas: {{ default 1 .Values.hpa.minReplicas }}
maxReplicas: {{ default 1 .Values.hpa.maxReplicas }}
minReplicas: {{ .Values.hpa.minReplicas }}
maxReplicas: {{ .Values.hpa.maxReplicas }}
metrics:
{{- if .Values.hpa.targetAverageCPUUtilization }}
{{- if .Values.hpa.targetCPUUtilizationPercentage }}
- type: Resource
resource:
name: cpu
targetAverageUtilization: {{ default 50 .Values.hpa.targetAverageCPUUtilization }}
targetAverageUtilization: {{ .Values.hpa.targetCPUUtilizationPercentage }}
{{- end }}
{{- if .Values.hpa.targetAverageMemoryUtilization }}
{{- if .Values.hpa.targetMemoryUtilizationPercentage }}
- type: Resource
resource:
name: memory
targetAverageUtilization: {{ default 50 .Values.hpa.targetAverageMemoryUtilization }}
targetAverageUtilization: {{ .Values.hpa.targetMemoryUtilizationPercentage }}
{{- end }}
{{- end }}
{{- end }}
31 changes: 25 additions & 6 deletions charts/microservice/templates/service.yaml
Original file line number Diff line number Diff line change
@@ -1,15 +1,34 @@
{{- $root:= . }}
---
apiVersion: v1
kind: Service
metadata:
name: {{ include "microservice.servicename" . }}
name: {{ include "microservice.fullname" . }}-svc
namespace: {{ .Values.global.namespace }}
{{- if .Values.service.annotations }}
annotations:
{{- range $key, $value := .Values.service.annotations }}
{{ $key }}: {{ $value }}
{{- end }}
{{- end }}
labels:
{{- include "microservice.labels" . | nindent 4 }}
spec:
type: {{ .Values.service.type }}
ports:
- port: {{ .Values.service.port }}
targetPort: http
protocol: TCP
name: http
selector:
{{- include "microservice.selectorLabels" . | nindent 4 }}
ports:
{{- range $spec := .Values.service.specs }}
- name: {{ $spec.name }}
port: {{ $spec.port }}
protocol: {{ $spec.protocol | default "TCP" }}
{{- if $spec.targetPort }}
targetPort: {{ $spec.targetPort }}
{{- else }}
targetPort: {{ $spec.name }}
{{- end}}
{{- if $spec.nodePort }}
nodePort: {{ $spec.nodePort }}
{{- end }}
{{- end -}}

Loading

0 comments on commit 5ad7399

Please sign in to comment.