From 5ad7399ce20fe1395f812ce2ee2a8b6c54076929 Mon Sep 17 00:00:00 2001 From: Ashwani Singh Date: Mon, 8 Jul 2024 15:32:32 +0530 Subject: [PATCH] Update according to recommendation --- .../microservice/examples/deploy-nginx.yaml | 5 + charts/microservice/templates/NOTES.txt | 5 + .../microservice/templates/_capabilities.tpl | 25 +++ charts/microservice/templates/_helpers.tpl | 54 ++----- charts/microservice/templates/configmap.yaml | 30 ++++ charts/microservice/templates/deployment.yaml | 86 +++++++--- charts/microservice/templates/hpa.yaml | 21 +-- charts/microservice/templates/service.yaml | 31 +++- charts/microservice/values.yaml | 152 ++++++++++++++---- 9 files changed, 300 insertions(+), 109 deletions(-) create mode 100644 charts/microservice/examples/deploy-nginx.yaml create mode 100644 charts/microservice/templates/NOTES.txt create mode 100644 charts/microservice/templates/_capabilities.tpl create mode 100644 charts/microservice/templates/configmap.yaml diff --git a/charts/microservice/examples/deploy-nginx.yaml b/charts/microservice/examples/deploy-nginx.yaml new file mode 100644 index 00000000..03e4b923 --- /dev/null +++ b/charts/microservice/examples/deploy-nginx.yaml @@ -0,0 +1,5 @@ +deployment: + image: + name: "nginx" + tag: "latest" + pullPolicy: IfNotPresent \ No newline at end of file diff --git a/charts/microservice/templates/NOTES.txt b/charts/microservice/templates/NOTES.txt new file mode 100644 index 00000000..c12295b8 --- /dev/null +++ b/charts/microservice/templates/NOTES.txt @@ -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 }} + diff --git a/charts/microservice/templates/_capabilities.tpl b/charts/microservice/templates/_capabilities.tpl new file mode 100644 index 00000000..5c7814bd --- /dev/null +++ b/charts/microservice/templates/_capabilities.tpl @@ -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 -}} + diff --git a/charts/microservice/templates/_helpers.tpl b/charts/microservice/templates/_helpers.tpl index 7f8af161..c6684172 100644 --- a/charts/microservice/templates/_helpers.tpl +++ b/charts/microservice/templates/_helpers.tpl @@ -6,7 +6,7 @@ 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 }} {{/* @@ -14,54 +14,30 @@ 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 }} diff --git a/charts/microservice/templates/configmap.yaml b/charts/microservice/templates/configmap.yaml new file mode 100644 index 00000000..ce28fa0f --- /dev/null +++ b/charts/microservice/templates/configmap.yaml @@ -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 }} \ No newline at end of file diff --git a/charts/microservice/templates/deployment.yaml b/charts/microservice/templates/deployment.yaml index 4d7651f6..adbdfb10 100644 --- a/charts/microservice/templates/deployment.yaml +++ b/charts/microservice/templates/deployment.yaml @@ -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: @@ -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 }} \ No newline at end of file + {{- 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 }} \ No newline at end of file diff --git a/charts/microservice/templates/hpa.yaml b/charts/microservice/templates/hpa.yaml index 8b021f8c..b2a4e86d 100644 --- a/charts/microservice/templates/hpa.yaml +++ b/charts/microservice/templates/hpa.yaml @@ -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 }} \ No newline at end of file +{{- end }} diff --git a/charts/microservice/templates/service.yaml b/charts/microservice/templates/service.yaml index 7bec37d2..8d052686 100644 --- a/charts/microservice/templates/service.yaml +++ b/charts/microservice/templates/service.yaml @@ -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 -}} + diff --git a/charts/microservice/values.yaml b/charts/microservice/values.yaml index e9deaa77..fd10418f 100644 --- a/charts/microservice/values.yaml +++ b/charts/microservice/values.yaml @@ -1,42 +1,132 @@ # -- global variables global: + namespace: "" replicaCount: 1 - fullnameOverride: "nginx" - nameOverride: "" + nameOverride: "ashwani" + fullnameOverride: "ashwani" + environment: {} + # list of key: value + # GLOBAL1: value + +## @param kubeVersion Override Kubernetes version +## +kubeVersion: "" # -- Object that configures Deployment instance deployment: # -- Override default container image format image: - # -- Image repository - repository: "nginx" - # -- Image tag - tag: "latest" - # -- Variable used to control when the image should be fetched - pullPolicy: always - # -- Definition of the liveness probe - livenessProbe: - httpPath: '/' - # -- Definition of the readiness probe - readinessProbe: - httpPath: '/' - # -- Configure container's resource request and limits - resources: - # -- Resources that the container is guaranteed to get - requests: - # -- Memory that the container is guaranteed to get - memory: - # -- CPU that the container is guaranteed to get - cpu: - # -- Defines a hard ceiling on resource use - limits: - # -- Defines a hard ceiling on how much Memory time that the container can use - memory: - # -- Defines a hard ceiling on how much CPU time that the container can use - cpu: - -hpa: {} + name: "" + tag: "" + pullPolicy: IfNotPresent + + # Annotation for the Deployment + annotations: {} + + livenessProbe: {} + # livenessProbe: + # httpGet: + # path: "/" + # port: http + # initialDelaySeconds: 25 + # periodSeconds: 10 + # timeoutSeconds: 5 + # successThreshold: 1 + # failureThreshold: 5 + + readinessProbe: {} + # readinessProbe: + # httpGet: + # path: "/" + # port: http + # initialDelaySeconds: 25 + # periodSeconds: 10 + # timeoutSeconds: 5 + # successThreshold: 1 + # failureThreshold: 5 + + environment: + ashwani: singh + + # environment: {} + # VAR1: value1 + + resources: {} + + # resources: + # requests: + # memory: 100m + # cpu: 100m + # limits: + # memory: 100m + # cpu: 100m + + # Additional volumes on the output Deployment definition. + volumes: + enabled: true + pvc: + enabled: false + existing_claim: + name: pvc + mountPath: /pv + size: 1G + class: + accessModes: + - ReadWriteOnce + + # configFileCommonHeader: | + # line1 + # line2 + + configMaps: + - name: test + mountPath: /test + data: + test.conf: | + hello + # hello2 + + # - name: test-from-file + # mountPath: /test2 + # files: + # - source: config.conf + # destination: application.conf + # - name: test-mixed + # mountPath: /test3 + # data: + # test2.conf: | + # another hello + # files: + # - source: config.conf + # destination: application2.conf + + # - name: foo + # secret: + # secretName: mysecret + # optional: false + + # Additional volumeMounts on the output Deployment definition. + volumeMounts: [] + # - name: foo + # mountPath: "/etc/foo" + # readOnly: true + + nodeSelector: {} + + tolerations: [] + + affinity: {} +hpa: + enabled: true + minReplicas: 1 + maxReplicas: 1 + targetCPUUtilizationPercentage: 80 + targetMemoryUtilizationPercentage: 80 + service: type: ClusterIP - port: 80 \ No newline at end of file + annotations: {} + specs: + - port: 80 + name: http