From 869a756e4b17f5e7a24dff410179ecd1d1a9d9c3 Mon Sep 17 00:00:00 2001 From: Jonas Forsberg Date: Tue, 15 Oct 2024 16:19:57 +0200 Subject: [PATCH] first commit --- .helmignore | 23 ++++++ Chart.yaml | 11 +++ Makefile | 6 ++ templates/_all.tpl | 15 ++++ templates/_helpers.tpl | 72 +++++++++++++++++++ templates/_ingress.tpl | 62 ++++++++++++++++ templates/_notes.tpl | 8 +++ templates/_service.tpl | 29 ++++++++ templates/_serviceaccount.tpl | 15 ++++ templates/_statefulset.tpl | 79 +++++++++++++++++++++ values.yaml | 129 ++++++++++++++++++++++++++++++++++ 11 files changed, 449 insertions(+) create mode 100644 .helmignore create mode 100644 Chart.yaml create mode 100644 Makefile create mode 100644 templates/_all.tpl create mode 100644 templates/_helpers.tpl create mode 100644 templates/_ingress.tpl create mode 100644 templates/_notes.tpl create mode 100644 templates/_service.tpl create mode 100644 templates/_serviceaccount.tpl create mode 100644 templates/_statefulset.tpl create mode 100644 values.yaml diff --git a/.helmignore b/.helmignore new file mode 100644 index 0000000..0e8a0eb --- /dev/null +++ b/.helmignore @@ -0,0 +1,23 @@ +# Patterns to ignore when building packages. +# This supports shell glob matching, relative path matching, and +# negation (prefixed with !). Only one pattern per line. +.DS_Store +# Common VCS dirs +.git/ +.gitignore +.bzr/ +.bzrignore +.hg/ +.hgignore +.svn/ +# Common backup files +*.swp +*.bak +*.tmp +*.orig +*~ +# Various IDEs +.project +.idea/ +*.tmproj +.vscode/ diff --git a/Chart.yaml b/Chart.yaml new file mode 100644 index 0000000..12ec667 --- /dev/null +++ b/Chart.yaml @@ -0,0 +1,11 @@ +--- +apiVersion: v2 +name: lib +description: Common functions for my helm charts at home +type: library +version: 0.0.0 +kubeVersion: ">=1.27.0" +home: git.rre.nu +maintainers: + - name: Jonas Forsberg + email: barregargamel@gmail.com diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..0ff974f --- /dev/null +++ b/Makefile @@ -0,0 +1,6 @@ +NEW_VERSION:=$(shell yq '.version |= (split(".") | .[-1] |= ((. tag = "!!int") + 1) | join("."))' Chart.yaml | yq '.version') + +branch: + @echo "New chart version is: $(NEW_VERSION)" + git checkout -b lib_$(NEW_VERSION) + yq '.version="$(NEW_VERSION)"' Chart.yaml diff --git a/templates/_all.tpl b/templates/_all.tpl new file mode 100644 index 0000000..258fb93 --- /dev/null +++ b/templates/_all.tpl @@ -0,0 +1,15 @@ +{{/* +Main entrypoint for the common library chart. It will render all underlying templates based on the provided values. +*/}} +{{- define "lib.all" -}} + {{- include "lib.values.setup" . }} + {{- if .Values.serviceAccount.create -}} + {{- include "lib.serviceaccount" . | nindent 0 }} + {{- end -}} + {{ include "lib.service" . | nindent 0 }} + {{ if eq .Values.controller.type "statefulset" }} + {{- include "lib.statefulset" . | nindent 0}} + {{- end -}} +{{- end -}} + + diff --git a/templates/_helpers.tpl b/templates/_helpers.tpl new file mode 100644 index 0000000..55b9640 --- /dev/null +++ b/templates/_helpers.tpl @@ -0,0 +1,72 @@ +{{/* +Expand the name of the chart. +*/}} +{{- define "lib.name" -}} +{{- default .Chart.Name .Values.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 "lib.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 "lib.chart" -}} +{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Common labels +*/}} +{{- define "lib.labels" -}} +helm.sh/chart: {{ include "lib.chart" . }} +{{ include "lib.selectorLabels" . }} +{{- if .Chart.AppVersion }} +app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} +{{- end }} +app.kubernetes.io/managed-by: {{ .Release.Service }} +{{- end }} + +{{/* +Selector labels +*/}} +{{- define "lib.selectorLabels" -}} +app.kubernetes.io/name: {{ include "lib.name" . }} +app.kubernetes.io/instance: {{ .Release.Name }} +{{- end }} + +{{/* +Create the name of the service account to use +*/}} +{{- define "lib.serviceAccountName" -}} +{{- if .Values.serviceAccount.create }} +{{- default (include "lib.fullname" .) .Values.serviceAccount.name }} +{{- else }} +{{- default "default" .Values.serviceAccount.name }} +{{- end }} +{{- end }} + +{{/* Merge the local chart values and the library chart defaults */}} +{{- define "lib.values.setup" -}} + {{- if .Values.lib -}} + {{- $defaultValues := deepCopy .Values.lib -}} + {{- $userValues := deepCopy (omit .Values "lib") -}} + {{- $mergedValues := mustMergeOverwrite $defaultValues $userValues -}} + {{- $_ := set . "Values" (deepCopy $mergedValues) -}} + {{- end -}} +{{- end -}} diff --git a/templates/_ingress.tpl b/templates/_ingress.tpl new file mode 100644 index 0000000..f2ea24e --- /dev/null +++ b/templates/_ingress.tpl @@ -0,0 +1,62 @@ +{{- if .Values.ingress.enabled -}} +{{- $fullName := include "lib.fullname" . -}} +{{- $svcName := .Values.ingress.service -}} +{{- $svcPort := .Values.ingress.servicePort -}} +{{- if and .Values.ingress.className (not (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion)) }} + {{- if not (hasKey .Values.ingress.annotations "kubernetes.io/ingress.class") }} + {{- $_ := set .Values.ingress.annotations "kubernetes.io/ingress.class" .Values.ingress.className}} + {{- end }} +{{- end }} +{{- if semverCompare ">=1.19-0" .Capabilities.KubeVersion.GitVersion -}} +apiVersion: networking.k8s.io/v1 +{{- else if semverCompare ">=1.14-0" .Capabilities.KubeVersion.GitVersion -}} +apiVersion: networking.k8s.io/v1beta1 +{{- else -}} +apiVersion: extensions/v1beta1 +{{- end }} +kind: Ingress +metadata: + name: {{ $fullName }} + labels: + {{- include "lib.labels" . | nindent 4 }} + {{- with .Values.ingress.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +spec: + {{- if and .Values.ingress.className (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion) }} + ingressClassName: {{ .Values.ingress.className }} + {{- end }} + {{- if .Values.ingress.tls }} + tls: + {{- range .Values.ingress.tls }} + - hosts: + {{- range .hosts }} + - {{ . | quote }} + {{- end }} + secretName: {{ .secretName }} + {{- end }} + {{- end }} + rules: + {{- range .Values.ingress.hosts }} + - host: {{ .host | quote }} + http: + paths: + {{- range .paths }} + - path: {{ .path }} + {{- if and .pathType (semverCompare ">=1.18-0" $.Capabilities.KubeVersion.GitVersion) }} + pathType: {{ .pathType }} + {{- end }} + backend: + {{- if semverCompare ">=1.19-0" $.Capabilities.KubeVersion.GitVersion }} + service: + name: {{ $fullName }} + port: + number: {{ $svcPort }} + {{- else }} + serviceName: {{ $fullName }} + servicePort: {{ $svcPort }} + {{- end }} + {{- end }} + {{- end }} +{{- end }} diff --git a/templates/_notes.tpl b/templates/_notes.tpl new file mode 100644 index 0000000..a290afe --- /dev/null +++ b/templates/_notes.tpl @@ -0,0 +1,8 @@ +{{/* +Default NOTES.txt content. +*/}} +{{- define "lib.defaultNotes" -}} + + + +{{- end -}} diff --git a/templates/_service.tpl b/templates/_service.tpl new file mode 100644 index 0000000..5f924df --- /dev/null +++ b/templates/_service.tpl @@ -0,0 +1,29 @@ +{{- define "lib.service" -}} +{{- $servicename := include "lib.fullname" . }} +{{- $labels := include "lib.labels" . }} +{{- $selectorlabels := include "lib.selectorLabels" . }} +{{- $ns := .Values.namespace }} +{{- range $name, $values := .Values.service }} +{{- if $values.enabled }} +--- +apiVersion: v1 +kind: Service +metadata: + name: {{ $servicename }}-{{ $name }} + namespace: {{ $ns }} + labels: + {{- $labels | nindent 4 }} +spec: + type: {{ $values.type }} + ports: + {{- range $k, $v := $values.ports }} + - port: {{ $v.port }} + targetPort: {{ default $v.targetPort $v.port }} + protocol: {{ $v.protocol }} + name: {{ $k }} + {{- end }} + selector: + {{- $selectorlabels | nindent 4 }} +{{- end -}} +{{- end -}} +{{- end -}} \ No newline at end of file diff --git a/templates/_serviceaccount.tpl b/templates/_serviceaccount.tpl new file mode 100644 index 0000000..e0f37d2 --- /dev/null +++ b/templates/_serviceaccount.tpl @@ -0,0 +1,15 @@ + +{{- define "lib.serviceaccount" -}} +apiVersion: v1 +kind: ServiceAccount +metadata: + name: {{ include "lib.serviceAccountName" . }} + namespace: {{ .Values.namespace }} + labels: + {{- include "lib.labels" . | nindent 4 }} + {{- with .Values.serviceAccount.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +automountServiceAccountToken: {{ .Values.serviceAccount.automount }} +{{- end }} \ No newline at end of file diff --git a/templates/_statefulset.tpl b/templates/_statefulset.tpl new file mode 100644 index 0000000..01ab70a --- /dev/null +++ b/templates/_statefulset.tpl @@ -0,0 +1,79 @@ +{{- define "lib.statefulset" }} +--- +apiVersion: apps/v1 +kind: StatefulSet +metadata: + name: {{ include "lib.fullname" . }} + namespace: {{ .Values.namespace }} + labels: + {{- include "lib.labels" . | nindent 4 }} +spec: + persistentVolumeClaimRetentionPolicy: + whenDeleted: Retain + whenScaled: Retain + replicas: {{ .Values.controller.replicas }} + selector: + matchLabels: + {{- include "lib.selectorLabels" . | nindent 6 }} + template: + metadata: + {{- with .Values.controller.annotations }} + annotations: + {{- toYaml . | nindent 8 }} + {{- end }} + labels: + {{- include "lib.labels" . | nindent 8 }} + {{- with .Values.controller.labels }} + {{- toYaml . | nindent 8 }} + {{- end }} + spec: + {{- with .Values.imagePullSecrets }} + imagePullSecrets: + {{- toYaml . | nindent 8 }} + {{- end }} + serviceAccountName: {{ include "lib.serviceAccountName" . }} + securityContext: + {{- toYaml .Values.controller.podSecurityContext | nindent 8 }} + containers: + - name: {{ .Chart.Name }} + securityContext: + {{- toYaml .Values.controller.securityContext | nindent 12 }} + image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}" + imagePullPolicy: {{ .Values.image.pullPolicy }} + ports: + {{- range $name, $values := .Values.service -}} + {{- if $values.enabled -}} + {{- range $k, $v := $values.ports }} + - name: {{ $k }} + containerPort: {{ $v.targetPort | default $v.port }} + protocol: {{ $v.protocol }} + {{- end -}} + {{- end -}} + {{- end }} + livenessProbe: + {{- toYaml .Values.livenessProbe | nindent 12 }} + readinessProbe: + {{- toYaml .Values.readinessProbe | nindent 12 }} + resources: + {{- toYaml .Values.resources | nindent 12 }} + {{- with .Values.volumeMounts }} + volumeMounts: + {{- toYaml . | nindent 12 }} + {{- end }} + {{- with .Values.volumes }} + volumes: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.nodeSelector }} + nodeSelector: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.affinity }} + affinity: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.tolerations }} + tolerations: + {{- toYaml . | nindent 8 }} + {{- end }} +{{- end -}} \ No newline at end of file diff --git a/values.yaml b/values.yaml new file mode 100644 index 0000000..bdc3f7d --- /dev/null +++ b/values.yaml @@ -0,0 +1,129 @@ +--- +# Namespace to deploy to, if not set it targets default namespace +namespace: default + +controller: + # -- enable the controller + enabled: true + # type of controller + # valid options are deployment, statefulset, job + type: deployment + annotations: {} + labels: {} + replicas: 1 + podSecurityContext: {} + securityContext: {} + # capabilities: + # drop: + # - ALL + # readOnlyRootFilesystem: true + # runAsNonRoot: true + # runAsUser: 1000 + +image: + repository: + pullPolicy: + # Overrides the image tag whose default is the chart appVersion. + tag: "" + +imagePullSecrets: [] +nameOverride: "" +fullnameOverride: "" + +serviceAccount: + # Specifies whether a service account should be created + create: true + # Automatically mount a ServiceAccount's API credentials? + automount: true + # Annotations to add to the service account + annotations: {} + # The name of the service account to use. + # If not set and create is true, a name is generated using the fullname template + name: "" + + +service: + main: + enabled: false + type: ClusterIP + annotations: {} + labels: {} + ports: {} +# http: +# # -- Enables or disables the port +# enabled: false# +# +# # -- The port number +# port: +# +# # -- Port protocol. +# # Support values are `HTTP`, `HTTPS`, `TCP` and `UDP`. +# # HTTPS and HTTPS spawn a TCP service and get used for internal URL and name generation +# protocol: HTTP +# +# # -- Specify a service targetPort if you wish to differ the service port from the application port. +# # If `targetPort` is specified, this port number is used in the container definition instead of +# # the `port` value. Therefore named ports are not supported for this field. +# targetPort: +# +# # -- Specify the nodePort value for the LoadBalancer and NodePort service types. +# # [[ref]](https://kubernetes.io/docs/concepts/services-networking/service/#type-nodeport) +# nodePort: + + +ingress: + main: + enabled: false + className: "" + annotations: {} + # kubernetes.io/ingress.class: nginx + # kubernetes.io/tls-acme: "true" + service: main + servicePort: 80 + hosts: + - host: chart-example.local + paths: + - path: / + pathType: ImplementationSpecific + tls: [] + # - secretName: chart-example-tls + # hosts: + # - chart-example.local + +resources: {} + # 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 + # resources, such as Minikube. If you do want to specify resources, uncomment the following + # lines, adjust them as necessary, and remove the curly braces after 'resources:'. + # limits: + # cpu: 100m + # memory: 128Mi + # requests: + # cpu: 100m + # memory: 128Mi + +autoscaling: + enabled: false + minReplicas: 1 + maxReplicas: 5 + targetCPUUtilizationPercentage: 80 + # targetMemoryUtilizationPercentage: 80 + +# Additional volumes on the output Deployment definition. +volumes: [] +# - 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: {}