apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: zpm-cleaner
  namespace: iris
spec:
  schedule: "0 0 * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: zpm-cleaner
            image: curlimages/curl:7.70.0
            command:
            - /bin/sh
            - -c
            - |
              curl -XPATCH --cacert /var/run/secrets/kubernetes.io/serviceaccount/ca.crt               \
                -H "Authorization: Bearer $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)"  \
                -H "Content-Type: application/strategic-merge-patch+json"                              \
                --data '{"spec":{"replicas":0}}'                                                       \
                https://kubernetes.default.svc/apis/apps/v1/namespaces/iris/statefulsets/zpm-registry
        
              sleep 10
        
              curl -XDELETE --cacert /var/run/secrets/kubernetes.io/serviceaccount/ca.crt                                       \
                -H "Authorization: Bearer $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)"                           \
                -H "Content-Type: application/json"                                                                             \
                https://kubernetes.default.svc/api/v1/namespaces/iris/persistentvolumeclaims/zpm-registry-volume-zpm-registry-0
        
              sleep 10
        
              curl -XPATCH --cacert /var/run/secrets/kubernetes.io/serviceaccount/ca.crt               \
                -H "Authorization: Bearer $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)"  \
                -H "Content-Type: application/strategic-merge-patch+json"                              \
                --data '{"spec":{"replicas":1}}'                                                       \
                https://kubernetes.default.svc/apis/apps/v1/namespaces/iris/statefulsets/zpm-registry
        
          serviceAccountName: zpm-cleaner
          restartPolicy: OnFailure
