Skip to content

Commit

Permalink
Merge pull request #27 from gimlet-io/schedule-support
Browse files Browse the repository at this point in the history
Using common chart
  • Loading branch information
laszlocph authored Aug 5, 2021
2 parents 8417a03 + 5910080 commit 98390f7
Show file tree
Hide file tree
Showing 39 changed files with 261 additions and 147 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
- name: Extract chart version
id: chart_version
run: |
CHART_VERSION=$(cat charts/onechart/Chart.yaml | grep version:)
CHART_VERSION=$(cat charts/onechart/Chart.yaml | grep ^version:)
CHART_VERSION=${CHART_VERSION#version: }
echo $CHART_VERSION
echo ::set-output name=chart_version::$CHART_VERSION
Expand Down
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ kubeval:
find manifests/ -name '*.yaml' | xargs kubeval --ignore-missing-schemas -v 1.18.0

test:
helm dependency update charts/onechart
helm unittest charts/onechart

helm dependency update charts/cron-job
helm unittest charts/cron-job

helm unittest charts/namespaces

package:
Expand All @@ -45,8 +49,13 @@ package:
helm repo index docs --url https://chart.onechart.dev

debug:
helm dependency update charts/onechart
helm template charts/onechart/ -f values.yaml --debug

debug-cron-job:
helm dependency update charts/cron-job
helm template charts/cron-job/ -f values-cron-job.yaml --debug

debug-ui:
#gimlet chart configure -s charts/onechart/values.schema.json -u charts/onechart/helm-ui.json onechart/onechart
/home/laszlo/projects/gimlet-cli/build/gimlet chart configure -s charts/onechart/values.schema.json -u charts/onechart/helm-ui.json onechart/onechart
23 changes: 23 additions & 0 deletions charts/common/.helmignore
Original file line number Diff line number Diff line change
@@ -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/
18 changes: 18 additions & 0 deletions charts/common/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
apiVersion: v2
name: common
description: A Helm chart for Kubernetes

# A chart can be either an 'application' or a 'library' chart.
#
# Application charts are a collection of templates that can be packaged into versioned archives
# to be deployed.
#
# Library charts provide useful utilities or functions for the chart developer. They're included as
# a dependency of application charts to inject those utilities and functions into the rendering
# pipeline. Library charts do not define any templates and therefore cannot be deployed.
type: library

# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 0.1.0
15 changes: 15 additions & 0 deletions charts/common/templates/_configmap.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{{- define "common.configmap.tpl" -}}
{{- if .Values.vars }}
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ template "common.robustName" .Release.Name }}
namespace: {{ .Release.Namespace }}
labels:
{{- include "helm-chart.labels" . | nindent 4 }}
data:
{{- range $key, $val := .Values.vars }}
{{ $key }}: {{ $val | quote }}
{{- end }}
{{- end }}
{{- end -}}
21 changes: 21 additions & 0 deletions charts/common/templates/_envFromRef.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{{- define "common.envFromRef.tpl" -}}
{{- if or (or (or (.Values.vars) (.Values.secretEnabled)) .Values.sealedSecrets) .Values.secretName }}
envFrom: &envFrom
{{- if .Values.vars }}
- configMapRef:
name: {{ template "common.robustName" .Release.Name }}
{{- end }}
{{- if .Values.secretEnabled }}
- secretRef:
name: {{ include "common.robustName" $.Release.Name }}
{{- end }}
{{- if .Values.secretName }}
- secretRef:
name: {{ .Values.secretName }}
{{- end }}
{{- if .Values.sealedSecrets }}
- secretRef:
name: {{ template "common.robustName" .Release.Name }}
{{- end }}
{{- end }}
{{- end }}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{{- define "common.fileSecret.tpl" -}}
{{- range .Values.fileSecrets }}
---
kind: Secret
Expand All @@ -11,3 +12,4 @@ data:
{{ $key }}: {{ $val | b64enc }}
{{- end }}
{{- end }}
{{- end -}}
10 changes: 10 additions & 0 deletions charts/common/templates/_helpers.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{{/* vim: set filetype=mustache: */}}

{{/*
Create robustName that can be used as Kubernetes resource name, and as subdomain as well
\w – Latin letters, digits, underscore '_' .
\W – all but \w .
*/}}
{{- define "common.robustName" -}}
{{ regexReplaceAll "[^0-9A-Za-z_\\.]+" . "-" | replace "_" "-" | lower | trunc 63 | trimSuffix "-" | trimPrefix "-" }}
{{- end }}
18 changes: 18 additions & 0 deletions charts/common/templates/_pvc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{{- define "common.pvc.tpl" -}}
{{- range .Values.volumes }}
{{- $robustName := include "common.robustName" $.Release.Name }}
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: {{ printf "%s-%s" $robustName .name }}
namespace: {{ $.Release.Namespace }}
spec:
accessModes:
- {{ .accessMode | default "ReadWriteOnce" }}
storageClassName: {{ .storageClass | default "local-path" }}
resources:
requests:
storage: {{ .size | default "1Gi" }}
{{- end }}
{{- end -}}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{{- define "common.sealedFileSecret.tpl" -}}
{{- range .Values.sealedFileSecrets }}
{{- $robustName := include "robustName" $.Release.Name }}
---
Expand All @@ -22,3 +23,4 @@ spec:
name: {{ printf "%s-%s" $.Release.Name .name }}
namespace: {{ $.Release.Namespace }}
{{- end }}
{{- end -}}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{{- if .Values.sealedSecrets }}
{{- define "common.sealedSecret.tpl" -}}
{{- if .Values.sealedSecrets }}
apiVersion: bitnami.com/v1alpha1
kind: SealedSecret
metadata:
Expand All @@ -17,4 +18,5 @@ spec:
metadata:
name: {{ template "robustName" .Release.Name }}
namespace: {{ .Release.Namespace }}
{{- end }}
{{- end }}
{{- end -}}
19 changes: 19 additions & 0 deletions charts/common/templates/_volumeMountsRef.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{{- define "common.volumeMountsRef.tpl" -}}
{{- if or (or (.Values.volumes) (.Values.sealedFileSecrets)) .Values.fileSecrets }}
volumeMounts: &volumeMounts
{{- range .Values.volumes }}
- name: {{ .name }}
mountPath: {{ .path }}
{{- end }}
{{- range .Values.sealedFileSecrets }}
- name: {{ .name }}
mountPath: {{ .path }}
readOnly: true
{{- end }}
{{- range .Values.fileSecrets }}
- name: {{ .name }}
mountPath: {{ .path }}
readOnly: true
{{- end }}
{{- end }}
{{- end }}
20 changes: 20 additions & 0 deletions charts/common/templates/_volumesRef.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{{- define "common.volumesRef.tpl" -}}
{{- if or (or (.Values.volumes) (.Values.sealedFileSecrets)) .Values.fileSecrets }}
volumes:
{{- range .Values.volumes }}
- name: {{ .name }}
persistentVolumeClaim:
claimName: {{ printf "%s-%s" $.Release.Name .name }}
{{- end }}
{{- range .Values.sealedFileSecrets }}
- name: {{ .name }}
secret:
secretName: {{ printf "%s-%s" $.Release.Name .name }}
{{- end }}
{{- range .Values.fileSecrets }}
- name: {{ .name }}
secret:
secretName: {{ printf "%s-%s" $.Release.Name .name }}
{{- end }}
{{- end }}
{{- end -}}
6 changes: 6 additions & 0 deletions charts/cron-job/Chart.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
dependencies:
- name: common
repository: file://../common
version: 0.1.0
digest: sha256:636a65e9846bdff17cc4e65b0849061f783759a37aa51fb85ff6fd8ba5e68467
generated: "2021-08-05T10:18:17.533366128+02:00"
7 changes: 6 additions & 1 deletion charts/cron-job/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ type: application
# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 0.1.2
version: 0.2.0

dependencies:
- name: common
version: 0.1.0
repository: file://../common
Binary file added charts/cron-job/charts/common-0.1.0.tgz
Binary file not shown.
14 changes: 1 addition & 13 deletions charts/cron-job/templates/configmap.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1 @@
{{- if .Values.vars }}
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}
namespace: {{ .Release.Namespace }}
labels:
{{- include "helm-chart.labels" . | nindent 4 }}
data:
{{- range $key, $val := .Values.vars }}
{{ $key }}: {{ $val | quote }}
{{- end }}
{{- end }}
{{- include "common.configmap.tpl" . -}}
32 changes: 7 additions & 25 deletions charts/cron-job/templates/cronJob.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ metadata:
{{- include "helm-chart.labels" . | nindent 4 }}
spec:
schedule: {{ .Values.schedule | quote }}
successfulJobsHistoryLimit: 1
failedJobsHistoryLimit: 1
concurrencyPolicy: Forbid
startingDeadlineSeconds: 120
{{- with .Values.constraints }}
{{- toYaml . | nindent 2 }}
{{- end }}
jobTemplate:
spec:
template:
Expand All @@ -30,29 +29,12 @@ spec:
- {{ .Values.shell }}
- -c
- {{ .Values.command | quote }}
envFrom:
{{- if .Values.vars }}
- configMapRef:
name: {{ .Release.Name }}
{{- end }}
{{- if .Values.secret.enabled }}
- secretRef:
name: {{ .Release.Name }}
{{- end }}
volumeMounts:
{{- range .Values.volumes }}
- name: {{ .name }}
mountPath: {{ .path }}
{{- end }}
{{- include "common.envFromRef.tpl" . | nindent 14 }}
{{- include "common.volumeMountsRef.tpl" . | nindent 14 }}
resources:
{{- toYaml .Values.resources | nindent 16 }}
volumes:
{{- range .Values.volumes }}
- name: {{ .name }}
persistentVolumeClaim:
claimName: {{ printf "%s-%s" $.Release.Name .name }}
{{- end }}
{{ with .Values.nodeSelector }}
{{- include "common.volumesRef.tpl" . | nindent 10 }}
{{ with .Values.nodeSelector }}
nodeSelector:
{{ toYaml . | nindent 8 }}
{{- end }}
Expand Down
1 change: 1 addition & 0 deletions charts/cron-job/templates/fileSecret.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{- include "common.fileSecret.tpl" . -}}
16 changes: 1 addition & 15 deletions charts/cron-job/templates/pvc.yaml
Original file line number Diff line number Diff line change
@@ -1,15 +1 @@
{{- range .Values.volumes }}
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: {{ printf "%s-%s" $.Release.Name .name }}
namespace: {{ $.Release.Namespace }}
spec:
accessModes:
- {{ .accessMode | default "ReadWriteOnce" }}
storageClassName: {{ .storageClass | default "local-path" }}
resources:
requests:
storage: {{ .size | default "1Gi" }}
{{- end }}
{{- include "common.pvc.tpl" . -}}
1 change: 1 addition & 0 deletions charts/cron-job/templates/sealedFileSecret.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{- include "common.sealedFileSecret.tpl" . -}}
1 change: 1 addition & 0 deletions charts/cron-job/templates/sealedSecret.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{- include "common.sealedSecret.tpl" . -}}
2 changes: 1 addition & 1 deletion charts/cron-job/tests/cronjob_configmap_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ tests:
path: spec.jobTemplate.spec.template.spec.containers[0].envFrom
content:
configMapRef:
name: RELEASE-NAME
name: release-name
- it: Should not reference configmap if vars are not set
asserts:
- isNull:
Expand Down
17 changes: 17 additions & 0 deletions charts/cron-job/tests/cronjob_costraints_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
suite: test cron job
templates:
- cronJob.yaml
tests:
- it: Should have default constraints
asserts:
- equal:
path: spec.successfulJobsHistoryLimit
value: 1
- it: Should constraints be configurable
set:
constraints:
successfulJobsHistoryLimit: 2
asserts:
- equal:
path: spec.successfulJobsHistoryLimit
value: 2
6 changes: 3 additions & 3 deletions charts/cron-job/tests/cronjob_secret_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ templates:
tests:
- it: Should reference secret if secret is enabled
set:
secret.enabled: true
secretEnabled: true
asserts:
- contains:
path: spec.jobTemplate.spec.template.spec.containers[0].envFrom
content:
secretRef:
name: RELEASE-NAME
name: release-name
- it: Should not reference secret if secret is disabled
set:
secret.enabled: false
secretEnabled: false
asserts:
- isNull:
path: spec.jobTemplate.spec.template.spec.containers[0].envFrom
6 changes: 6 additions & 0 deletions charts/cron-job/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,9 @@ affinity: {}

secret:
enabled: false

constraints:
successfulJobsHistoryLimit: 1
failedJobsHistoryLimit: 1
concurrencyPolicy: Forbid
startingDeadlineSeconds: 120
6 changes: 6 additions & 0 deletions charts/onechart/Chart.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
dependencies:
- name: common
repository: file://../common
version: 0.1.0
digest: sha256:636a65e9846bdff17cc4e65b0849061f783759a37aa51fb85ff6fd8ba5e68467
generated: "2021-08-05T10:39:10.053810673+02:00"
Loading

0 comments on commit 98390f7

Please sign in to comment.