From bc1c1ef0540eb53e9fa4c6d7f9a10ed9e4d5c901 Mon Sep 17 00:00:00 2001 From: Leo Q Date: Wed, 27 Sep 2023 17:16:58 +0800 Subject: [PATCH] Add k8s cronjob (#57) * add k8s cron chart * add more description * fix style --- charts/k8s-cron/.helmignore | 23 +++++ charts/k8s-cron/Chart.yaml | 28 ++++++ charts/k8s-cron/templates/NOTES.txt | 2 + charts/k8s-cron/templates/_helpers.tpl | 62 ++++++++++++ charts/k8s-cron/templates/configmap.yaml | 9 ++ charts/k8s-cron/templates/cronjob.yaml | 54 +++++++++++ charts/k8s-cron/templates/rbac.yaml | 57 +++++++++++ charts/k8s-cron/templates/serviceaccount.yaml | 12 +++ charts/k8s-cron/values.yaml | 94 +++++++++++++++++++ 9 files changed, 341 insertions(+) create mode 100644 charts/k8s-cron/.helmignore create mode 100644 charts/k8s-cron/Chart.yaml create mode 100644 charts/k8s-cron/templates/NOTES.txt create mode 100644 charts/k8s-cron/templates/_helpers.tpl create mode 100644 charts/k8s-cron/templates/configmap.yaml create mode 100644 charts/k8s-cron/templates/cronjob.yaml create mode 100644 charts/k8s-cron/templates/rbac.yaml create mode 100644 charts/k8s-cron/templates/serviceaccount.yaml create mode 100644 charts/k8s-cron/values.yaml diff --git a/charts/k8s-cron/.helmignore b/charts/k8s-cron/.helmignore new file mode 100644 index 0000000..0e8a0eb --- /dev/null +++ b/charts/k8s-cron/.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/charts/k8s-cron/Chart.yaml b/charts/k8s-cron/Chart.yaml new file mode 100644 index 0000000..293636a --- /dev/null +++ b/charts/k8s-cron/Chart.yaml @@ -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" diff --git a/charts/k8s-cron/templates/NOTES.txt b/charts/k8s-cron/templates/NOTES.txt new file mode 100644 index 0000000..c3b3453 --- /dev/null +++ b/charts/k8s-cron/templates/NOTES.txt @@ -0,0 +1,2 @@ +1. Get the application URL by running these commands: + diff --git a/charts/k8s-cron/templates/_helpers.tpl b/charts/k8s-cron/templates/_helpers.tpl new file mode 100644 index 0000000..9a74450 --- /dev/null +++ b/charts/k8s-cron/templates/_helpers.tpl @@ -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 }} diff --git a/charts/k8s-cron/templates/configmap.yaml b/charts/k8s-cron/templates/configmap.yaml new file mode 100644 index 0000000..e590486 --- /dev/null +++ b/charts/k8s-cron/templates/configmap.yaml @@ -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 }} diff --git a/charts/k8s-cron/templates/cronjob.yaml b/charts/k8s-cron/templates/cronjob.yaml new file mode 100644 index 0000000..44db531 --- /dev/null +++ b/charts/k8s-cron/templates/cronjob.yaml @@ -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 }} diff --git a/charts/k8s-cron/templates/rbac.yaml b/charts/k8s-cron/templates/rbac.yaml new file mode 100644 index 0000000..e256917 --- /dev/null +++ b/charts/k8s-cron/templates/rbac.yaml @@ -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 -}} \ No newline at end of file diff --git a/charts/k8s-cron/templates/serviceaccount.yaml b/charts/k8s-cron/templates/serviceaccount.yaml new file mode 100644 index 0000000..a31f4e9 --- /dev/null +++ b/charts/k8s-cron/templates/serviceaccount.yaml @@ -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 }} diff --git a/charts/k8s-cron/values.yaml b/charts/k8s-cron/values.yaml new file mode 100644 index 0000000..222617a --- /dev/null +++ b/charts/k8s-cron/values.yaml @@ -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: {}