Skip to content

Commit

Permalink
Add k8s cronjob (#57)
Browse files Browse the repository at this point in the history
* add k8s cron chart

* add more description

* fix style
  • Loading branch information
LeoQuote authored Sep 27, 2023
1 parent bb6397a commit bc1c1ef
Show file tree
Hide file tree
Showing 9 changed files with 341 additions and 0 deletions.
23 changes: 23 additions & 0 deletions charts/k8s-cron/.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/
28 changes: 28 additions & 0 deletions charts/k8s-cron/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
apiVersion: v2
name: k8s-cron
description: A Helm chart to install cronjob on k8s cluster
maintainers:
- name: douban

home: https://github.com/alpine-docker/k8s

# 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: 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.0

# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
# follow Semantic Versioning. They should reflect the version the application is using.
# It is recommended to use it with quotes.
appVersion: "1.16.0"
2 changes: 2 additions & 0 deletions charts/k8s-cron/templates/NOTES.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1. Get the application URL by running these commands:

62 changes: 62 additions & 0 deletions charts/k8s-cron/templates/_helpers.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{{/*
Expand the name of the chart.
*/}}
{{- define "k8s-cron.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 "k8s-cron.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 "k8s-cron.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Common labels
*/}}
{{- define "k8s-cron.labels" -}}
helm.sh/chart: {{ include "k8s-cron.chart" . }}
{{ include "k8s-cron.selectorLabels" . }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}

{{/*
Selector labels
*/}}
{{- define "k8s-cron.selectorLabels" -}}
app.kubernetes.io/name: {{ include "k8s-cron.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}

{{/*
Create the name of the service account to use
*/}}
{{- define "k8s-cron.serviceAccountName" -}}
{{- if .Values.serviceAccount.create }}
{{- default (include "k8s-cron.fullname" .) .Values.serviceAccount.name }}
{{- else }}
{{- default "default" .Values.serviceAccount.name }}
{{- end }}
{{- end }}
9 changes: 9 additions & 0 deletions charts/k8s-cron/templates/configmap.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
kind: ConfigMap
apiVersion: v1
metadata:
name: {{ include "k8s-cron.fullname" . }}
labels:
{{- include "k8s-cron.labels" . | nindent 4 }}
data:
run.sh: |
{{ .Values.script | indent 4 }}
54 changes: 54 additions & 0 deletions charts/k8s-cron/templates/cronjob.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
kind: CronJob
apiVersion: batch/v1
metadata:
name: {{ include "k8s-cron.fullname" . }}
{{- if .Values.annotations }}
annotations:
{{ toYaml .Values.annotations | indent 4}}
{{- end }}
labels:
{{- include "k8s-cron.labels" . | nindent 4 }}
spec:
schedule: {{ .Values.schedule | quote }}
jobTemplate:
spec:
template:
spec:
{{- with .Values.imagePullSecrets }}
imagePullSecrets:
{{- toYaml . | nindent 8 }}
{{- end }}
serviceAccountName: {{ include "k8s-cron.serviceAccountName" . }}
securityContext:
{{- toYaml .Values.podSecurityContext | nindent 12 }}
restartPolicy: {{ .Values.restartPolicy }}
containers:
- name: {{ .Chart.Name }}
securityContext:
{{- toYaml .Values.securityContext | nindent 16 }}
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
command: ["/bin/bash"]
args:
- "/scripts/run.sh"
volumeMounts:
- name: scripts
mountPath: /scripts
resources:
{{- toYaml .Values.resources | nindent 16 }}
volumes:
- name: scripts
configMap:
name: {{ include "k8s-cron.fullname" . }}
{{- with .Values.nodeSelector }}
nodeSelector:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.affinity }}
affinity:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.tolerations }}
tolerations:
{{- toYaml . | nindent 8 }}
{{- end }}
57 changes: 57 additions & 0 deletions charts/k8s-cron/templates/rbac.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{{- if .Values.role.create -}}
---
# role for namespace level
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: {{ include "k8s-cron.fullname" . }}
labels:
{{- include "k8s-cron.labels" . | nindent 4 }}
rules:
{{- toYaml .Values.role.roles | nindent 2 }}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: {{ include "k8s-cron.fullname" . }}
labels:
{{- include "k8s-cron.labels" . | nindent 4 }}
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: {{ include "k8s-cron.fullname" . }}
subjects:
- kind: ServiceAccount
name: {{ include "k8s-cron.serviceAccountName" . }}
namespace: {{ .Release.Namespace }}
{{- end -}}

{{- if .Values.clusterRole.create -}}
---
# clusterrole
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: {{ include "k8s-cron.fullname" . }}
labels:
{{- include "k8s-cron.labels" . | nindent 4 }}
rules:
{{- toYaml .Values.clusterRole.roles | nindent 2 }}
---
# cluterrolebinding
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: {{ include "k8s-cron.fullname" . }}
labels:
{{- include "k8s-cron.labels" . | nindent 4 }}
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: {{ include "k8s-cron.fullname" . }}
subjects:
- kind: ServiceAccount
name: {{ include "k8s-cron.serviceAccountName" . }}
namespace: {{ .Release.Namespace }}

{{- end -}}
12 changes: 12 additions & 0 deletions charts/k8s-cron/templates/serviceaccount.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{{- if .Values.serviceAccount.create -}}
apiVersion: v1
kind: ServiceAccount
metadata:
name: {{ include "k8s-cron.serviceAccountName" . }}
labels:
{{- include "k8s-cron.labels" . | nindent 4 }}
{{- with .Values.serviceAccount.annotations }}
annotations:
{{- toYaml . | nindent 4 }}
{{- end }}
{{- end }}
94 changes: 94 additions & 0 deletions charts/k8s-cron/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# Default values for k8s-cron.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.

image:
repository: alpine/k8s
pullPolicy: IfNotPresent
# Overrides the image tag whose default is the chart appVersion.
tag: "1.28.2"

imagePullSecrets: []
nameOverride: ""
fullnameOverride: ""

schedule: "*/5 * * * *"
restartPolicy: Never
script: |
#!/bin/bash# Loop through all Namespaces
for namespace in $(kubectl get namespace -o=json | jq -r '.items[] .metadata.name') ; do
echo "Processing namespace ${namespace}"
# Get the list of Pods, then select the items that have
# been evicted, sort by the startTime (ascending), then
# select all but the most recent 3. Then pass just the
# names of those Pods to kubectl to be deleted
kubectl -n ${namespace} get pod -o=json | jq '[.items[] | select(.status.reason=="Evicted")] | sort_by(.status.startTime) | .[0:-3]' | jq -r '.[] .metadata.name' | xargs --no-run-if-empty kubectl -n ${namespace} delete pod
done
serviceAccount:
# Specifies whether a service account should be created
create: 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: ""

role:
# Specifies whether a ClusterRole should be created
create: false
# Annotations to add to the ClusterRole
annotations: {}
# The name of the ClusterRole to use.
# If not set and create is true, a name is generated using the fullname template
roles:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "list", "delete"]

clusterRole:
# Specifies whether a ClusterRoleBinding should be created
create: true
# Annotations to add to the ClusterRoleBinding
annotations: {}
# The name of the ClusterRoleBinding to use.
# If not set and create is true, a name is generated using the fullname template
roles:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "list", "delete"]
- apiGroups: [""]
resources: ["namespaces"]
verbs: ["get", "list"]

podAnnotations: {}

podSecurityContext: {}
# fsGroup: 2000

securityContext: {}
# capabilities:
# drop:
# - ALL
# readOnlyRootFilesystem: true
# runAsNonRoot: true
# runAsUser: 1000

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

nodeSelector: {}

tolerations: []

affinity: {}

0 comments on commit bc1c1ef

Please sign in to comment.