From 1cc09b0a62d8e642560c58c73b2a06d940864dd9 Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Fri, 13 Nov 2020 23:56:34 +0100 Subject: [PATCH] Add commons library --- .github/workflows/helm-charts-test.yaml | 2 + README.md | 2 + charts/commons/.helmignore | 22 ++++++ charts/commons/Chart.yaml | 10 +++ charts/commons/README.md | 76 ++++++++++++++++++++ charts/commons/library-ci/deployment.yaml | 64 +++++++++++++++++ charts/commons/templates/_chart.tpl | 7 ++ charts/commons/templates/_labels.tpl | 15 ++++ charts/commons/templates/_name.tpl | 34 +++++++++ charts/commons/templates/_selectorLabels.tpl | 8 +++ charts/commons/values.yaml | 0 11 files changed, 240 insertions(+) create mode 100644 charts/commons/.helmignore create mode 100644 charts/commons/Chart.yaml create mode 100644 charts/commons/README.md create mode 100644 charts/commons/library-ci/deployment.yaml create mode 100644 charts/commons/templates/_chart.tpl create mode 100644 charts/commons/templates/_labels.tpl create mode 100644 charts/commons/templates/_name.tpl create mode 100644 charts/commons/templates/_selectorLabels.tpl create mode 100644 charts/commons/values.yaml diff --git a/.github/workflows/helm-charts-test.yaml b/.github/workflows/helm-charts-test.yaml index 4a4af0e..71de435 100644 --- a/.github/workflows/helm-charts-test.yaml +++ b/.github/workflows/helm-charts-test.yaml @@ -50,11 +50,13 @@ jobs: echo ::set-output name=url::$(echo "postgresql://postgres:${{ steps.postgresql_passowrd.outputs.password }}@postgresql-helm-charts-${{ github.run_number }}.default/postgres") - name: Prepare library charts run: | + cp charts/commons/library-ci/*.yaml charts/commons/templates/ cp charts/cron-jobs/library-ci/*.yaml charts/cron-jobs/templates/ cp charts/horizontal-pod-autoscalers/library-ci/*.yaml charts/horizontal-pod-autoscalers/templates/ cp charts/pi-hole-exporter/library-ci/*.yaml charts/pi-hole-exporter/templates/ sed -i '$ d' charts/commento/ci/postgresql-values.yaml echo -e " manual: \"${{ steps.postgresql.outputs.url }}\"\r\n" >> charts/commento/ci/postgresql-values.yaml + sed -i 's/library/application/g' charts/commons/Chart.yaml sed -i 's/library/application/g' charts/cron-jobs/Chart.yaml sed -i 's/library/application/g' charts/horizontal-pod-autoscalers/Chart.yaml - name: Test charts diff --git a/README.md b/README.md index 8c14567..dd04061 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,9 @@ Opinionated helm chats for my personal projects, and OSS Projects that either do ### Library +* [`commons`](https://artifacthub.io/packages/helm/wyrihaximusnet/commons) * [`cron-jobs`](https://artifacthub.io/packages/helm/wyrihaximusnet/cron-jobs) +* [`commons`](https://artifacthub.io/packages/helm/wyrihaximusnet/commons) * [`horizontal-pod-autoscalers`](https://artifacthub.io/packages/helm/wyrihaximusnet/horizontal-pod-autoscalers) ## Opinionated decisions shared by all charts diff --git a/charts/commons/.helmignore b/charts/commons/.helmignore new file mode 100644 index 0000000..50af031 --- /dev/null +++ b/charts/commons/.helmignore @@ -0,0 +1,22 @@ +# 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 +*~ +# Various IDEs +.project +.idea/ +*.tmproj +.vscode/ diff --git a/charts/commons/Chart.yaml b/charts/commons/Chart.yaml new file mode 100644 index 0000000..f5ce509 --- /dev/null +++ b/charts/commons/Chart.yaml @@ -0,0 +1,10 @@ +apiVersion: v2 +name: commons +description: commons library +home: https://github.com/wyrihaximusnet/helm-charts +icon: https://helm.wyrihaximus.net/images/charts/commons.png +type: library +version: 0.1.0 +maintainers: + - name: WyriHaximus + email: helm@wyrihaximus.net diff --git a/charts/commons/README.md b/charts/commons/README.md new file mode 100644 index 0000000..90aafc2 --- /dev/null +++ b/charts/commons/README.md @@ -0,0 +1,76 @@ +# Commons + +

+ +

+ +Opinionated helm library chart containing metadata helpers for helm application charts. + +Example deployment using helpers from this chart: +```yaml +apiVersion: apps/v1 +kind: Deployment +metadata: + name: {{ template "commons.fullname" . }} + labels: + {{- include "commons.labels" . | nindent 4 }} + app.kubernetes.io/component: controller +spec: + selector: + matchLabels: + {{- include "commons.selectorLabels" . | nindent 6 }} + replicas: 1 + template: + metadata: + annotations: + prometheus.io/scrape: "true" + prometheus.io/port: "9696" + labels: + {{- include "commons.labels" . | nindent 8 }} + spec: + containers: + - name: default-backend + image: "ghcr.io/wyrihaximusnet/default-backend:random" + imagePullPolicy: Always + ports: + - name: default-backend + containerPort: 6969 + protocol: TCP + - name: metrics + containerPort: 9696 + protocol: TCP + livenessProbe: + failureThreshold: 6 + httpGet: + path: / + port: metrics + readinessProbe: + failureThreshold: 6 + httpGet: + path: / + port: metrics + startupProbe: + httpGet: + path: / + port: metrics + failureThreshold: 90 + periodSeconds: 1 + resources: + limits: + cpu: 75m + memory: 64Mi + requests: + cpu: 75m + memory: 64Mi + affinity: + podAntiAffinity: + requiredDuringSchedulingIgnoredDuringExecution: + - labelSelector: + matchExpressions: + - key: appRevision + operator: In + values: + - {{ template "commons.nameRevision" . }} + topologyKey: "kubernetes.io/hostname" + +``` diff --git a/charts/commons/library-ci/deployment.yaml b/charts/commons/library-ci/deployment.yaml new file mode 100644 index 0000000..a27a08f --- /dev/null +++ b/charts/commons/library-ci/deployment.yaml @@ -0,0 +1,64 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: {{ template "commons.fullname" . }} + labels: + {{- include "commons.labels" . | nindent 4 }} + app.kubernetes.io/component: controller +spec: + selector: + matchLabels: + {{- include "commons.selectorLabels" . | nindent 6 }} + replicas: 1 + template: + metadata: + annotations: + prometheus.io/scrape: "true" + prometheus.io/port: "9696" + labels: + {{- include "commons.labels" . | nindent 8 }} + spec: + containers: + - name: default-backend + image: "ghcr.io/wyrihaximusnet/default-backend:random" + imagePullPolicy: Always + ports: + - name: default-backend + containerPort: 6969 + protocol: TCP + - name: metrics + containerPort: 9696 + protocol: TCP + livenessProbe: + failureThreshold: 6 + httpGet: + path: / + port: metrics + readinessProbe: + failureThreshold: 6 + httpGet: + path: / + port: metrics + startupProbe: + httpGet: + path: / + port: metrics + failureThreshold: 90 + periodSeconds: 1 + resources: + limits: + cpu: 75m + memory: 64Mi + requests: + cpu: 75m + memory: 64Mi + affinity: + podAntiAffinity: + requiredDuringSchedulingIgnoredDuringExecution: + - labelSelector: + matchExpressions: + - key: appRevision + operator: In + values: + - {{ template "commons.nameRevision" . }} + topologyKey: "kubernetes.io/hostname" diff --git a/charts/commons/templates/_chart.tpl b/charts/commons/templates/_chart.tpl new file mode 100644 index 0000000..5a8386c --- /dev/null +++ b/charts/commons/templates/_chart.tpl @@ -0,0 +1,7 @@ +{{/* vim: set filetype=mustache: */}} +{{/* +Create chart name and version as used by the chart label. +*/}} +{{- define "commons.chart" -}} +{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}} +{{- end -}} diff --git a/charts/commons/templates/_labels.tpl b/charts/commons/templates/_labels.tpl new file mode 100644 index 0000000..908dcf6 --- /dev/null +++ b/charts/commons/templates/_labels.tpl @@ -0,0 +1,15 @@ +{{/* vim: set filetype=mustache: */}} +{{/* +Expand the name of the chart. +*/}} +{{- define "commons.labels" -}} +app: {{ template "commons.name" . }} +app.kubernetes.io/instance: {{ template "commons.instance" . }} +app.kubernetes.io/name: {{ template "commons.name" . }} +app.kubernetes.io/version: {{ .Chart.Version | quote }} +appRevision: {{ template "commons.nameRevision" . }} +chart: {{ template "commons.chart" . }} +release: {{ .Release.Name }} +releaseRevision: {{ .Release.Revision | quote }} +heritage: {{ .Release.Service }} +{{- end -}} diff --git a/charts/commons/templates/_name.tpl b/charts/commons/templates/_name.tpl new file mode 100644 index 0000000..88a71e5 --- /dev/null +++ b/charts/commons/templates/_name.tpl @@ -0,0 +1,34 @@ +{{/* vim: set filetype=mustache: */}} +{{/* +Expand the name of the chart. +*/}} +{{- define "commons.name" -}} +{{- .Chart.Name | trunc 63 | trimSuffix "-" -}} +{{- end -}} + +{{/* +Append the deployed version +*/}} +{{- define "commons.instance" -}} +{{ template "commons.name" . }}-{{ .Chart.Version }} +{{- end -}} + +{{/* +Append the deployed revision +*/}} +{{- define "commons.nameRevision" -}} +{{ template "commons.name" . }}___{{ .Release.Revision }} +{{- 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 "commons.fullname" -}} +{{- if contains .Chart.Name .Release.Name -}} +{{- .Release.Name | trunc 63 | trimSuffix "-" -}} +{{- else -}} +{{- printf "%s-%s" .Release.Name .Chart.Name | trunc 63 | trimSuffix "-" -}} +{{- end -}} +{{- end -}} diff --git a/charts/commons/templates/_selectorLabels.tpl b/charts/commons/templates/_selectorLabels.tpl new file mode 100644 index 0000000..d06f7c7 --- /dev/null +++ b/charts/commons/templates/_selectorLabels.tpl @@ -0,0 +1,8 @@ +{{/* vim: set filetype=mustache: */}} +{{/* +Expand the name of the chart. +*/}} +{{- define "commons.selectorLabels" -}} +app: {{ template "commons.name" . }} +release: {{ .Release.Name }} +{{- end -}} diff --git a/charts/commons/values.yaml b/charts/commons/values.yaml new file mode 100644 index 0000000..e69de29