Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding cleanup job #13

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions hokstack/files/jobs/cleanup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/bin/bash

function create_kubeconfig() {
# export KUBECONFIG=./hok
API_SERVER="https://${KUBERNETES_PORT_443_TCP_ADDR}:443"
TOKEN=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token)
CA_FILE="/var/run/secrets/kubernetes.io/serviceaccount/ca.crt"
kubectl config set-cluster hok --server=$API_SERVER --certificate-authority=$CA_FILE --embed-certs=true
kubectl config set-credentials hok-admin --token=$TOKEN
kubectl config set-context hok --cluster hok --user hok-admin
kubectl config use-context hok
echo "[$(date '+%Y-%m-%d %H:%M:%S')] [INFO] kubeconfig generation complete."
}

function remove_statefulsets() {
for i in $(kubectl get sts -n $NAMESPACE |awk '{print $1}'|grep -iv name); do
kubectl patch sts $i -p '{"metadata":{"finalizers":null}}' -n $NAMESPACE;
# kubectl delete sts $i --grace-period=0 --force -n $NAMESPACE;
echo "[$(date '+%Y-%m-%d %H:%M:%S')] [INFO] Patching statefulset $i"
done;
}

function remove_pvc() {
# PODCOUNT=$(kubectl get pods -o json |jq .items[].metadata.name -r |wc -l)
for i in $(kubectl get pvc -n $NAMESPACE |awk '{print $1}'|grep -iv name); do
kubectl delete pvc $i -n $NAMESPACE;
echo "[$(date '+%Y-%m-%d %H:%M:%S')] [INFO] Removing PVC $i"
done;
}

function remove_residual() {
sleep 30
kubectl delete cm cleanup-sh -n $NAMESPACE
echo "[$(date '+%Y-%m-%d %H:%M:%S')] [INFO] Removing cleanup-sh configMap"
kubectl delete job cleanup-job -n $NAMESPACE
echo "[$(date '+%Y-%m-%d %H:%M:%S')] [INFO] Removing cleanup-job"
}

if [ "{{.Values.cleanup.enabled}}" == "true" ]; then
create_kubeconfig
fi

if [ "{{.Values.cleanup.removestatefulest.enabled}}" == "true" ]; then
remove_statefulsets
fi

if [ "{{.Values.cleanup.enabled}}" == "true" ]; then
remove_residual
fi
3 changes: 3 additions & 0 deletions hokstack/templates/datanode/datanode.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ spec:
postStart:
exec:
command: {{.Values.datanode.lifecycle.postStart.exec.command}}
# preStop:
# exec:
# command: ["/bin/sh", "-c", "PID=$(pidof java) && kill $PID && while ps -p $PID > /dev/null; do sleep 1; done"]
ports:
- containerPort: 2181
- containerPort: 50070
Expand Down
10 changes: 10 additions & 0 deletions hokstack/templates/jobs/cleanup-cm.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{{- if .Values.cleanup.enabled }}
apiVersion: v1
kind: ConfigMap
metadata:
name: cleanup-sh
annotations:
"helm.sh/hook": "pre-install"
data:
{{ (tpl (.Files.Glob "files/jobs/cleanup.sh").AsConfig . ) | indent 4 }}
{{- end }}
48 changes: 48 additions & 0 deletions hokstack/templates/jobs/cleanup-job.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{{- if .Values.cleanup.enabled }}
apiVersion: batch/v1
kind: Job
metadata:
name: cleanup-job
annotations:
"helm.sh/hook": "pre-delete"
spec:
template:
spec:
serviceAccountName: {{ .Values.teamname }}-hok-admin
containers:
- name: cleanup
image: {{ .Values.ambariserver.image.repository }}
imagePullPolicy: {{ .Values.imagePullPolicy }}
command: ["bash","-c", "/scripts/cleanup.sh"]
env:
- name: AMBARISERVER
value: "ambariserver-0"
- name: DATANODE
value: "datanode"
- name: PODIP
valueFrom:
fieldRef:
fieldPath: status.podIP
- name: PODNAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
volumeMounts:
- name: cleanup-sh
mountPath: /scripts/cleanup.sh
subPath: cleanup.sh
restartPolicy: OnFailure
terminationGracePeriodSeconds: 0
volumes:
- name: cleanup-sh
configMap:
name: cleanup-sh
defaultMode: 0777
backoffLimit: 5
completions: 1
parallelism: 1
{{- end }}
2 changes: 1 addition & 1 deletion hokstack/templates/jobs/cluster-watch-cm.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{- if .Values.ambariserver.enabled }}
{{- if .Values.clusterwatch.enabled }}
apiVersion: v1
kind: ConfigMap
metadata:
Expand Down
4 changes: 2 additions & 2 deletions hokstack/templates/jobs/cluster-watch-job.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{- if .Values.ambariserver.enabled }}
{{- if .Values.clusterwatch.enabled }}
apiVersion: batch/v1
kind: Job
metadata:
Expand Down Expand Up @@ -44,4 +44,4 @@ spec:
backoffLimit: 5
completions: 1
parallelism: 1
{{- end }}
{{- end }}
45 changes: 45 additions & 0 deletions hokstack/templates/rbac/hok-admin.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{{- if .Values.cleanup.enabled }}
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: {{ .Values.teamname }}-hok-admin
annotations:
# "helm.sh/hook": "pre-delete"
# "helm.sh/hook-delete-policy": "hook-succeeded"
rules:
- apiGroups:
- '*'
resources:
- '*'
verbs:
- '*'
- nonResourceURLs:
- '*'
verbs:
- '*'
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: {{ .Values.teamname }}-hok-admin
annotations:
# "helm.sh/hook": "pre-delete"
# "helm.sh/hook-delete-policy": "hook-succeeded"
subjects:
- kind: ServiceAccount
name: {{ .Values.teamname }}-hok-admin
namespace: {{ .Values.teamname }}
roleRef:
kind: ClusterRole
name: {{ .Values.teamname }}-hok-admin
apiGroup: rbac.authorization.k8s.io
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: {{ .Values.teamname }}-hok-admin
namespace: {{ .Values.teamname }}
annotations:
# "helm.sh/hook": "pre-delete"
# "helm.sh/hook-delete-policy": "hook-succeeded"
{{- end }}
11 changes: 10 additions & 1 deletion hokstack/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.

teamname: shubh-demo1
teamname: team1
imagePullPolicy: Always

postgres:
Expand Down Expand Up @@ -205,3 +205,12 @@ kdc:
storageClassName: gp2
accessModes: ReadWriteOnce
storage: 10Gi

clusterwatch:
enabled: true

cleanup:
enabled: true

removestatefulest:
enabled: true