From 649a95cb04d0922d0d91cbed05e333ccfa443f12 Mon Sep 17 00:00:00 2001 From: SrishT Date: Thu, 4 Feb 2021 23:13:54 +0530 Subject: [PATCH 1/2] Issue 300: Zookeeper Operator pre-delete hook Signed-off-by: SrishT --- .../templates/pre-delete-hooks.yaml | 110 ++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 charts/zookeeper-operator/templates/pre-delete-hooks.yaml diff --git a/charts/zookeeper-operator/templates/pre-delete-hooks.yaml b/charts/zookeeper-operator/templates/pre-delete-hooks.yaml new file mode 100644 index 000000000..da411f856 --- /dev/null +++ b/charts/zookeeper-operator/templates/pre-delete-hooks.yaml @@ -0,0 +1,110 @@ +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 From 2a72101f2c2ec35de1be311bc6daf765a69b22f9 Mon Sep 17 00:00:00 2001 From: SrishT Date: Mon, 8 Feb 2021 07:54:31 +0530 Subject: [PATCH 2/2] Issue 300: Adding a flag to make the hook optional Signed-off-by: SrishT --- charts/zookeeper-operator/templates/pre-delete-hooks.yaml | 2 ++ charts/zookeeper-operator/values.yaml | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/charts/zookeeper-operator/templates/pre-delete-hooks.yaml b/charts/zookeeper-operator/templates/pre-delete-hooks.yaml index da411f856..c36aa6723 100644 --- a/charts/zookeeper-operator/templates/pre-delete-hooks.yaml +++ b/charts/zookeeper-operator/templates/pre-delete-hooks.yaml @@ -1,3 +1,4 @@ +{{- if .Values.hooks.delete }} kind: ClusterRole apiVersion: rbac.authorization.k8s.io/v1beta1 metadata: @@ -108,3 +109,4 @@ spec: configMap: name: {{ template "zookeeper-operator.fullname" . }}-pre-delete defaultMode: 0555 +{{- end }} diff --git a/charts/zookeeper-operator/values.yaml b/charts/zookeeper-operator/values.yaml index a2c16b832..7b8f40913 100644 --- a/charts/zookeeper-operator/values.yaml +++ b/charts/zookeeper-operator/values.yaml @@ -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