|
| 1 | +--- |
| 2 | +apiVersion: batch/v1 |
| 3 | +kind: Job |
| 4 | +metadata: |
| 5 | + name: {{ .Release.Name }}-cleanup-acme |
| 6 | + labels: |
| 7 | + app.kubernetes.io/instance: {{ .Release.Name }} |
| 8 | + annotations: |
| 9 | + "helm.sh/hook": post-delete |
| 10 | + "helm.sh/hook-weight": "10" |
| 11 | + "helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded |
| 12 | +spec: |
| 13 | + template: |
| 14 | + metadata: |
| 15 | + labels: |
| 16 | + app.kubernetes.io/instance: {{ .Release.Name }} |
| 17 | + policy.cozystack.io/allow-to-apiserver: "true" |
| 18 | + spec: |
| 19 | + serviceAccountName: {{ .Release.Name }}-cleanup-acme |
| 20 | + restartPolicy: Never |
| 21 | + securityContext: |
| 22 | + runAsNonRoot: true |
| 23 | + runAsUser: 65534 |
| 24 | + runAsGroup: 65534 |
| 25 | + seccompProfile: |
| 26 | + type: RuntimeDefault |
| 27 | + containers: |
| 28 | + - name: cleanup |
| 29 | + image: docker.io/clastix/kubectl:v1.32 |
| 30 | + securityContext: |
| 31 | + allowPrivilegeEscalation: false |
| 32 | + readOnlyRootFilesystem: true |
| 33 | + capabilities: |
| 34 | + drop: ["ALL"] |
| 35 | + resources: |
| 36 | + requests: |
| 37 | + cpu: 10m |
| 38 | + memory: 64Mi |
| 39 | + limits: |
| 40 | + cpu: 200m |
| 41 | + memory: 128Mi |
| 42 | + env: |
| 43 | + - name: HOME |
| 44 | + value: /tmp |
| 45 | + command: |
| 46 | + - /bin/sh |
| 47 | + - -c |
| 48 | + - | |
| 49 | + echo "Cleaning up orphaned cert-manager ACME resources for {{ .Release.Name }}..." |
| 50 | + # This release's TLS host. The bucket-system chart renders its |
| 51 | + # Ingress / Certificate for "<bucketName>.<_namespace.host>", and |
| 52 | + # bucketName is set to .Release.Name in the HelmRelease. cert-manager |
| 53 | + # stamps this exact value onto its Challenge .spec.dnsName and onto |
| 54 | + # the orphaned solver Ingress .spec.rules[].host, so the host is the |
| 55 | + # one reliable per-release identifier for ACME solver objects (they |
| 56 | + # carry no app/release label of their own). |
| 57 | + HOST="{{ .Release.Name }}.{{ .Values._namespace.host }}" |
| 58 | + # Delete the per-bucket Certificate/Order/Challenge. cert-manager |
| 59 | + # then cascade-removes the cm-acme-http-solver-* solver objects it |
| 60 | + # owns. The release's TLS Certificate is named after the secret it |
| 61 | + # populates ({{ .Release.Name }}-ui-tls). |
| 62 | + kubectl delete certificate {{ .Release.Name }}-ui-tls -n {{ .Release.Namespace }} --ignore-not-found \ |
| 63 | + || echo "WARNING: cert-manager cleanup failed for {{ .Release.Name }}; orphaned ACME resources may remain" >&2 |
| 64 | + kubectl delete certificate,order,challenge -n {{ .Release.Namespace }} -l app.kubernetes.io/instance={{ .Release.Name }} --ignore-not-found \ |
| 65 | + || echo "WARNING: cert-manager cleanup failed for {{ .Release.Name }}; orphaned ACME resources may remain" >&2 |
| 66 | + # Release-scoped solver sweep. We deliberately do NOT delete |
| 67 | + # ingress/service/pod objects namespace-wide by the |
| 68 | + # acme.cert-manager.io/http01-solver=true label: that label is set |
| 69 | + # on EVERY app's solver objects in this shared tenant namespace, so |
| 70 | + # a blanket delete would tear down sibling apps' in-flight ACME |
| 71 | + # challenges. Instead we scope by HOST, which is unique to this |
| 72 | + # release, so we only ever touch our own solver objects. |
| 73 | + # |
| 74 | + # Delete Challenges for our host (cascades the solver |
| 75 | + # Ingress/Service/Pod they own). |
| 76 | + for ch in $(kubectl get challenge -n {{ .Release.Namespace }} -o jsonpath="{range .items[?(@.spec.dnsName=='$HOST')]}{.metadata.name} {end}" 2>/dev/null); do |
| 77 | + kubectl delete challenge -n {{ .Release.Namespace }} "$ch" --ignore-not-found \ |
| 78 | + || echo "WARNING: failed to delete challenge $ch for {{ .Release.Name }}; orphaned ACME resources may remain" >&2 |
| 79 | + done |
| 80 | + # Delete any orphaned solver Ingress for our host (no live owner |
| 81 | + # left to cascade it). Its solver Service/Pod are owned by a |
| 82 | + # Challenge and are cascade-deleted with it; truly owner-less |
| 83 | + # solver Service/Pod with no matching Challenge are not expected |
| 84 | + # and are not swept namespace-wide. |
| 85 | + for ing in $(kubectl get ingress -n {{ .Release.Namespace }} -l acme.cert-manager.io/http01-solver=true -o jsonpath="{range .items[?(@.spec.rules[0].host=='$HOST')]}{.metadata.name} {end}" 2>/dev/null); do |
| 86 | + kubectl delete ingress -n {{ .Release.Namespace }} "$ing" --ignore-not-found \ |
| 87 | + || echo "WARNING: failed to delete solver ingress $ing for {{ .Release.Name }}; orphaned ACME resources may remain" >&2 |
| 88 | + done |
| 89 | + echo "ACME cleanup complete." |
| 90 | + volumeMounts: |
| 91 | + - name: tmp |
| 92 | + mountPath: /tmp |
| 93 | + volumes: |
| 94 | + - name: tmp |
| 95 | + emptyDir: {} |
| 96 | +--- |
| 97 | +apiVersion: v1 |
| 98 | +kind: ServiceAccount |
| 99 | +metadata: |
| 100 | + name: {{ .Release.Name }}-cleanup-acme |
| 101 | + labels: |
| 102 | + app.kubernetes.io/instance: {{ .Release.Name }} |
| 103 | + annotations: |
| 104 | + "helm.sh/hook": post-delete |
| 105 | + "helm.sh/hook-weight": "0" |
| 106 | + "helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded |
| 107 | +--- |
| 108 | +apiVersion: rbac.authorization.k8s.io/v1 |
| 109 | +kind: Role |
| 110 | +metadata: |
| 111 | + name: {{ .Release.Name }}-cleanup-acme |
| 112 | + labels: |
| 113 | + app.kubernetes.io/instance: {{ .Release.Name }} |
| 114 | + annotations: |
| 115 | + "helm.sh/hook": post-delete |
| 116 | + "helm.sh/hook-weight": "5" |
| 117 | + "helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded |
| 118 | +rules: |
| 119 | + - apiGroups: ["cert-manager.io"] |
| 120 | + resources: ["certificates"] |
| 121 | + verbs: ["get", "list", "delete"] |
| 122 | + - apiGroups: ["acme.cert-manager.io"] |
| 123 | + resources: ["orders", "challenges"] |
| 124 | + verbs: ["get", "list", "delete"] |
| 125 | + # Needed to list+delete this release's orphaned http01 solver Ingress objects |
| 126 | + # by host. Services/Pods are intentionally omitted: the solver Service/Pod are |
| 127 | + # owned by their Challenge and cascade-delete with it, so we never delete them |
| 128 | + # directly (and thus never sweep them namespace-wide). |
| 129 | + - apiGroups: ["networking.k8s.io"] |
| 130 | + resources: ["ingresses"] |
| 131 | + verbs: ["get", "list", "delete"] |
| 132 | +--- |
| 133 | +apiVersion: rbac.authorization.k8s.io/v1 |
| 134 | +kind: RoleBinding |
| 135 | +metadata: |
| 136 | + name: {{ .Release.Name }}-cleanup-acme |
| 137 | + labels: |
| 138 | + app.kubernetes.io/instance: {{ .Release.Name }} |
| 139 | + annotations: |
| 140 | + "helm.sh/hook": post-delete |
| 141 | + "helm.sh/hook-weight": "5" |
| 142 | + "helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded |
| 143 | +roleRef: |
| 144 | + apiGroup: rbac.authorization.k8s.io |
| 145 | + kind: Role |
| 146 | + name: {{ .Release.Name }}-cleanup-acme |
| 147 | +subjects: |
| 148 | + - kind: ServiceAccount |
| 149 | + name: {{ .Release.Name }}-cleanup-acme |
0 commit comments