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

Issue 300: Zookeeper Operator pre-delete hook #301

Merged
merged 2 commits into from
Feb 9, 2021
Merged
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
112 changes: 112 additions & 0 deletions charts/zookeeper-operator/templates/pre-delete-hooks.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
{{- if .Values.hooks.delete }}
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
name: {{ template "zookeeper-operator.fullname" . }}-pre-delete
annotations:
"helm.sh/hook": pre-delete
"helm.sh/hook-weight": "1"
"helm.sh/hook-delete-policy": hook-succeeded, before-hook-creation, hook-failed
rules:
- apiGroups:
- zookeeper.pravega.io
resources:
- "*"
verbs:
- get
- list
---

kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
name: {{ template "zookeeper-operator.fullname" . }}-pre-delete
annotations:
"helm.sh/hook": pre-delete
"helm.sh/hook-weight": "1"
"helm.sh/hook-delete-policy": hook-succeeded, before-hook-creation, hook-failed
subjects:
- kind: ServiceAccount
name: {{ template "zookeeper-operator.fullname" . }}-pre-delete
namespace: {{ .Release.Namespace }}
roleRef:
kind: ClusterRole
name: {{ template "zookeeper-operator.fullname" . }}-pre-delete
apiGroup: rbac.authorization.k8s.io

---

apiVersion: v1
kind: ServiceAccount
metadata:
name: {{ template "zookeeper-operator.fullname" . }}-pre-delete
annotations:
"helm.sh/hook": pre-delete
"helm.sh/hook-weight": "1"
"helm.sh/hook-delete-policy": hook-succeeded, before-hook-creation, hook-failed

---

apiVersion: v1
kind: ConfigMap
metadata:
name: {{ template "zookeeper-operator.fullname" . }}-pre-delete
annotations:
"helm.sh/hook": pre-delete
"helm.sh/hook-weight": "1"
"helm.sh/hook-delete-policy": hook-succeeded, before-hook-creation, hook-failed
data:
pre-delete.sh: |
#!/bin/sh
exit_code=0
echo "Checking for ZookeeperCluster Resource"

ret=$(kubectl get zookeepercluster --all-namespaces --no-headers 2>&1)
if (echo $ret | grep -e "No resources found" -e "the server doesn't have a resource type \"zookeepercluster\"" > /dev/null);
then
echo "None"
else
echo "$ret"
exit_code=1
fi

if [ $exit_code -ne 0 ];
then
echo "Pre-delete Check Failed"
exit $exit_code
fi
echo "Pre-delete Check OK"
---

apiVersion: batch/v1
kind: Job
metadata:
name: {{ template "zookeeper-operator.fullname" . }}-pre-delete
annotations:
"helm.sh/hook": pre-delete
"helm.sh/hook-weight": "2"
"helm.sh/hook-delete-policy": hook-succeeded, before-hook-creation, hook-failed
spec:
backoffLimit: 1
activeDeadlineSeconds: 20
template:
metadata:
name: {{ template "zookeeper-operator.fullname" . }}-pre-delete
spec:
serviceAccountName: {{ template "zookeeper-operator.fullname" . }}-pre-delete
restartPolicy: Never
containers:
- name: pre-delete-job
image: "{{ .Values.hooks.image.repository }}:{{ .Values.hooks.image.tag }}"
command:
- /scripts/pre-delete.sh
volumeMounts:
- name: sh
mountPath: /scripts
readOnly: true
volumes:
- name: sh
configMap:
name: {{ template "zookeeper-operator.fullname" . }}-pre-delete
defaultMode: 0555
{{- end }}
4 changes: 4 additions & 0 deletions charts/zookeeper-operator/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,7 @@ hooks:
image:
repository: lachlanevenson/k8s-kubectl
tag: v1.16.10
## Whether to create pre-delete hook which ensures that
## the operator cannot be deleted till the zookeeper cluster
## custom resources have been cleaned up
delete: true