diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 4fa6fb3..ec5538c 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -62,3 +62,50 @@ jobs: dockerfile: Dockerfile tags: "latest,v${{ needs.semantic-release.outputs.release-version }}" + + helm: + runs-on: ubuntu-latest + needs: [semantic-release, docker] + if: needs.semantic-release.outputs.new-release-published == 'true' + steps: + - uses: actions/checkout@ee0669bd1cc54295c223e0bb666b733df41de1c5 # v2.7.0 + - name: Download yq + run: | + wget -nv -nc -O yq https://github.com/mikefarah/yq/releases/download/v4.20.2/yq_linux_amd64 + chmod +x yq + - name: Set version + # Always use git tags as semantic release can fail due to rate limit + run: | + git fetch --prune --unshallow + echo "RELEASE_VERSION=$(git describe --abbrev=0 --tags | sed -e 's/^v//')" >> $GITHUB_ENV + - name: Update chart version + run: ./yq -i e '.version = "${{ env.RELEASE_VERSION }}"' chart/Chart.yaml + - name: Update app version + run: ./yq -i e '.appVersion = "${{ env.RELEASE_VERSION }}"' chart/Chart.yaml + - name: Update image tags + run: ./yq -i e '.image.tag = "v${{ env.RELEASE_VERSION }}"' chart/values.yaml + - name: Set up Helm + uses: azure/setup-helm@18bc76811624f360dbd7f18c2d4ecb32c7b87bab # v1.1 + with: + version: v3.8.0 + - name: Package Helm chart + run: | + helm package ./chart + - name: Clone charts repo + uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab # v3.5.2 + with: + repository: "${{ github.repository_owner }}/charts" + path: charts + token: "${{ secrets.FLANKBOT }}" + - name: Update chart repo + run: | + cd charts + cp ../batch-runner-*.tgz ./ + helm repo index --merge index.yaml . + - name: Push changes to chart repo + uses: stefanzweifel/git-auto-commit-action@3ea6ae190baf489ba007f7c92608f33ce20ef04a # v4.16.0 + with: + commit_message: "Release ${{ needs.semantic-release.outputs.release-version }} of ${{ github.repository }}" + branch: gh-pages + repository: ./charts + diff --git a/chart/Chart.yaml b/chart/Chart.yaml new file mode 100644 index 0000000..5f1ccc8 --- /dev/null +++ b/chart/Chart.yaml @@ -0,0 +1,6 @@ +apiVersion: v2 +name: batch-runner +description: A Helm chart for creating pods or jobs for every message in a queue +type: application +version: 0.1.0 +appVersion: "1.0.0" diff --git a/chart/templates/_helpers.tpl b/chart/templates/_helpers.tpl new file mode 100644 index 0000000..2204af3 --- /dev/null +++ b/chart/templates/_helpers.tpl @@ -0,0 +1,62 @@ +{{/* +Expand the name of the chart. +*/}} +{{- define "batch-runner.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 "batch-runner.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 "batch-runner.chart" -}} +{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Common labels +*/}} +{{- define "batch-runner.labels" -}} +helm.sh/chart: {{ include "batch-runner.chart" . }} +{{ include "batch-runner.selectorLabels" . }} +{{- if .Chart.AppVersion }} +app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} +{{- end }} +app.kubernetes.io/managed-by: {{ .Release.Service }} +{{- end }} + +{{/* +Selector labels +*/}} +{{- define "batch-runner.selectorLabels" -}} +app.kubernetes.io/name: {{ include "batch-runner.name" . }} +app.kubernetes.io/instance: {{ .Release.Name }} +{{- end }} + +{{/* +Create the name of the service account to use +*/}} +{{- define "batch-runner.serviceAccountName" -}} +{{- if .Values.serviceAccount.create }} +{{- default (include "batch-runner.fullname" .) .Values.serviceAccount.name }} +{{- else }} +{{- default "default" .Values.serviceAccount.name }} +{{- end }} +{{- end }} diff --git a/chart/templates/deployment.yaml b/chart/templates/deployment.yaml new file mode 100644 index 0000000..a8911b9 --- /dev/null +++ b/chart/templates/deployment.yaml @@ -0,0 +1,21 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: {{ include "batch-runner.fullname" . }} + labels: + {{- include "batch-runner.labels" . | nindent 4 }} +spec: + replicas: {{ .Values.replicaCount }} + selector: + matchLabels: + {{- include "batch-runner.selectorLabels" . | nindent 6 }} + template: + metadata: + labels: + {{- include "batch-runner.selectorLabels" . | nindent 8 }} + spec: + serviceAccountName: {{ include "batch-runner.serviceAccountName" . }} + containers: + - name: {{ .Chart.Name }} + image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}" + imagePullPolicy: {{ .Values.image.pullPolicy }} diff --git a/chart/templates/serviceaccount.yaml b/chart/templates/serviceaccount.yaml new file mode 100644 index 0000000..c48dfe5 --- /dev/null +++ b/chart/templates/serviceaccount.yaml @@ -0,0 +1,37 @@ +{{- if .Values.serviceAccount.create -}} +apiVersion: v1 +kind: ServiceAccount +metadata: + name: {{ include "batch-runner.serviceAccountName" . }} + labels: + {{- include "batch-runner.labels" . | nindent 4 }} +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: Role +metadata: + name: batch-runner-role + labels: + {{- include "batch-runner.labels" . | nindent 4 }} +rules: +- apiGroups: [""] + resources: ["pods"] + verbs: ["*"] +- apiGroups: ["batch/v1"] + resources: ["jobs"] + verbs: ["*"] +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: RoleBinding +metadata: + name: batch-runner-binding + labels: + {{- include "batch-runner.labels" . | nindent 4 }} +roleRef: + apiGroup: rbac.authorization.k8s.io + kind: Role + name: batch-runner-role +subjects: +- kind: ServiceAccount + name: {{ include "batch-runner.serviceAccountName" . }} + namespace: {{ .Release.Namespace }} +{{- end }} diff --git a/chart/values.yaml b/chart/values.yaml new file mode 100644 index 0000000..37508ad --- /dev/null +++ b/chart/values.yaml @@ -0,0 +1,9 @@ +replicaCount: 1 +image: + repository: flanksource/batch-runner + tag: latest + pullPolicy: IfNotPresent + +serviceAccount: + create: true + name: batch-runner-sa