Skip to content

Commit

Permalink
Merge pull request #1215 from kyrylogy/f-helm-cmsmon
Browse files Browse the repository at this point in the history
Create a helm chart for cmsmonitoring spark running cronjobs
  • Loading branch information
mrceyhun authored Oct 20, 2022
2 parents e3938d8 + 3794405 commit 93527f4
Show file tree
Hide file tree
Showing 9 changed files with 396 additions and 0 deletions.
23 changes: 23 additions & 0 deletions helm/cmsmon-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/
24 changes: 24 additions & 0 deletions helm/cmsmon-cron/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
apiVersion: v2
name: cmsmon-cron
description: A Helm chart for k8s CMSSpark cronjobs

# 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"
25 changes: 25 additions & 0 deletions helm/cmsmon-cron/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
This Helm chart provides all-in-one template for CMSSpark cronjobs.

### Notes
Go template language provides a range block, which is actively used in this template. It allows `values.yaml` to have a dictionary containing multiple cronjob configurations. <br> Provided key naming has to be followed, e.g. "0", "1", "2" ... These keys are used as indexes to increment NodePorts of each CronJob service.

Each CronJob runs a bash script specified in *command* field. Arguments have to be provided as a YAML multiline string. Both fields are then concatenated with common cronjob arguments in _helpers.tpl and included as a complete argument for the `/bin/bash -c` in CronJob.

Depending on the `test.enabled` parameter value, `cronjob.yaml` or `test-job.yaml` will be deployed. **Output directories for testing have to be provided individually.** **Some cronjobs don't support --test flag.**

Some CronJobs may need EOS access. This can be specified with `eosEnabled: true` individually in the same field as `name` and `schedule`.


### Testing
If `test.enabled` parameter is set to true, `--test` flag will be added as an argument for each CronJob. Moreover, each CronJob will be deployed as a Job and executed right after deployment without waiting for the schedule time to be reached.

Following command can be used to deploy in test mode using `--set`

```
helm install cmsmon-cron cmsmon-cron/ --set test.enabled=true --debug
```

### TODO
- [ ] Migrate Google's [spark-on-k8s-operator](https://github.com/GoogleCloudPlatform/spark-on-k8s-operator)
- [ ] Convert to Ingress
- [ ] Utilize Chart in FluxCD pipeline
17 changes: 17 additions & 0 deletions helm/cmsmon-cron/templates/NOTES.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Display running pods:
kubectl get pods -n cron-hdfs

Show the logs:
kubectl logs <pod_name> -n cron-hdfs

Show command and arguments:
kubectl describe pod <pod_name> -n cron-hdfs

Connect to the container:
kubectl exec --stdin --tty <pod_name> -n cron-hdfs -- /bin/bash

List the charts:
helm list

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

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

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

{{/*
Create the name of the service account to use
*/}}
{{- define "cmsmon-cron.serviceAccountName" -}}
{{- if .Values.serviceAccount.create }}
{{- default (include "cmsmon-cron.fullname" .) .Values.serviceAccount.name }}
{{- else }}
{{- default "default" .Values.serviceAccount.name }}
{{- end }}
{{- end }}

{{/*
Compile CronJob run command with all parameters
*/}}
{{- define "cmsmon-cron.run" -}}
{{ $.cron.command }} {{ $.cron.args }} --p1=${{ $.cron.name | upper | replace "-" "_" }}_SERVICE_PORT_PORT_1 --p2=${{ $.cron.name | upper | replace "-" "_" }}_SERVICE_PORT_PORT_2 --host=$MY_NODE_NAME --wdir=$WDIR {{ if and (eq $.Values.test.enabled true) (eq $.cron.testFlagExists true) }}--test{{ end }} 2>&1
{{- end }}
69 changes: 69 additions & 0 deletions helm/cmsmon-cron/templates/cronjob.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{{- if eq false .Values.test.enabled -}}
{{- range $i, $cron := .Values.cronjob.crons }}
---
apiVersion: batch/v1
kind: CronJob
metadata:
name: {{ $cron.name }}
namespace: cron-hdfs
spec:
schedule: {{ $cron.schedule | quote }}
concurrencyPolicy: Forbid
failedJobsHistoryLimit: 1
jobTemplate:
spec:
backoffLimit: 0
template:
metadata:
labels:
app: {{ $cron.name }}
spec:
restartPolicy: Never
hostname: {{ $cron.name }}
containers:
- name: {{ $cron.name }}
image: {{ $.Values.cronjob.repository }}
imagePullPolicy: {{ $.Values.image.pullPolicy }}
command: ["/bin/bash", "-c"]
args:
- export >/etc/environment;
source /etc/environment;{{ include "cmsmon-cron.run" (dict "cron" $cron "Values" $.Values) | trim | nindent 18 }}
env:
- name: MY_NODE_NAME
valueFrom:
fieldRef:
fieldPath: spec.nodeName
ports:
- containerPort: {{ add 32501 (mul 2 $i)}} # spark.driver.port
name: port-1
- containerPort: {{ add 32502 (mul 2 $i)}} # spark.driver.blockManager.port
name: port-2
resources:
limits:
cpu: 2000m
memory: 6Gi
requests:
cpu: 500m
memory: 750Mi
stdin: true
tty: true
volumeMounts:
- name: {{ $cron.name }}-secrets
mountPath: /etc/secrets
readOnly: true
{{- with $cron.eosEnabled}}
- name: eos # EOS access
mountPath: /eos
mountPropagation: HostToContainer
{{- end}}
volumes:
- name: {{ $cron.name }}-secrets
secret:
secretName: {{ $cron.name }}-secrets
{{- with $cron.eosEnabled}}
- name: eos
hostPath:
path: /var/eos
{{- end}}
{{- end}}
{{- end}}
23 changes: 23 additions & 0 deletions helm/cmsmon-cron/templates/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{{- range $i, $cron := .Values.cronjob.crons }}
---
apiVersion: v1
kind: Service
metadata:
name: {{ $cron.name }}
namespace: cron-hdfs
spec:
selector:
app: {{ $cron.name }}
type: NodePort
ports:
- name: port-1
nodePort: {{ add 32501 (mul 2 $i)}}
port: {{ add 32501 (mul 2 $i)}}
protocol: TCP
targetPort: {{ add 32501 (mul 2 $i)}}
- name: port-2
nodePort: {{ add 32502 (mul 2 $i)}}
port: {{ add 32502 (mul 2 $i)}}
protocol: TCP
targetPort: {{ add 32502 (mul 2 $i)}}
{{- end}}
66 changes: 66 additions & 0 deletions helm/cmsmon-cron/templates/test-job.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{{- if eq true .Values.test.enabled -}}
{{- range $i, $cron := .Values.cronjob.crons }}
---
apiVersion: batch/v1
kind: Job
metadata:
name: {{ $cron.name }}
namespace: cron-hdfs
labels:
app: {{ $cron.name }}
spec:
backoffLimit: 0
template:
metadata:
labels:
app: {{ $cron.name }}
spec:
restartPolicy: Never
hostname: {{ $cron.name }}
containers:
- name: {{ $cron.name }}
image: {{ $.Values.cronjob.repository }}
imagePullPolicy: {{ $.Values.image.pullPolicy }}
command: ["/bin/bash", "-c"]
args:
- export >/etc/environment;
source /etc/environment;{{ include "cmsmon-cron.run" (dict "cron" $cron "Values" $.Values) | trim | nindent 18 }}
env:
- name: MY_NODE_NAME
valueFrom:
fieldRef:
fieldPath: spec.nodeName
ports:
- containerPort: {{ add 32501 (mul 2 $i)}} # spark.driver.port
name: port-1
- containerPort: {{ add 32502 (mul 2 $i)}} # spark.driver.blockManager.port
name: port-2
resources:
limits:
cpu: 2000m
memory: 6Gi
requests:
cpu: 500m
memory: 750Mi
stdin: true
tty: true
volumeMounts:
- name: {{ $cron.name }}-secrets
mountPath: /etc/secrets
readOnly: true
{{- with $cron.eosEnabled}}
- name: eos # EOS access
mountPath: /eos
mountPropagation: HostToContainer
{{- end}}
volumes:
- name: {{ $cron.name }}-secrets
secret:
secretName: {{ $cron.name }}-secrets
{{- with $cron.eosEnabled}}
- name: eos
hostPath:
path: /var/eos
{{- end}}
{{- end}}
{{- end}}
Loading

0 comments on commit 93527f4

Please sign in to comment.