Skip to content

Commit 16ea52d

Browse files
committed
Add automated solution that enables changing the statefulset service
1 parent 16b5bd3 commit 16ea52d

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one
3+
# or more contributor license agreements. See the NOTICE file
4+
# distributed with this work for additional information
5+
# regarding copyright ownership. The ASF licenses this file
6+
# to you under the Apache License, Version 2.0 (the
7+
# "License"); you may not use this file except in compliance
8+
# with the License. You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing,
13+
# software distributed under the License is distributed on an
14+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
# KIND, either express or implied. See the License for the
16+
# specific language governing permissions and limitations
17+
# under the License.
18+
#
19+
20+
# only when `components.broker` is true and `broker.statefulsetUpgrade.enabled` is true,
21+
# this pre-upgrade hook job will be created to clean up the old broker statefulset if the existing statefulset is created
22+
# by a chart older than 4.6.0, which has a different headless service name and will cause issue if not deleted before
23+
# the new statefulset is created.
24+
{{- if and .Values.components.broker .Values.broker.statefulsetUpgrade.enabled }}
25+
apiVersion: v1
26+
kind: ServiceAccount
27+
metadata:
28+
name: "{{ template "pulsar.fullname" . }}-{{ .Values.broker.component }}-sts-cleanup"
29+
namespace: {{ template "pulsar.namespace" . }}
30+
labels:
31+
{{- include "pulsar.standardLabels" . | nindent 4 }}
32+
component: {{ .Values.broker.component }}-sts-cleanup
33+
annotations:
34+
"helm.sh/hook": pre-upgrade
35+
"helm.sh/hook-weight": "-10"
36+
"helm.sh/hook-delete-policy": hook-succeeded
37+
---
38+
apiVersion: rbac.authorization.k8s.io/v1
39+
kind: Role
40+
metadata:
41+
name: "{{ template "pulsar.fullname" . }}-{{ .Values.broker.component }}-sts-cleanup"
42+
namespace: {{ template "pulsar.namespace" . }}
43+
labels:
44+
{{- include "pulsar.standardLabels" . | nindent 4 }}
45+
component: {{ .Values.broker.component }}-sts-cleanup
46+
annotations:
47+
"helm.sh/hook": pre-upgrade
48+
"helm.sh/hook-weight": "-10"
49+
"helm.sh/hook-delete-policy": hook-succeeded
50+
rules:
51+
- apiGroups: ["apps"]
52+
resources: ["statefulsets"]
53+
verbs: ["list", "get", "delete"]
54+
---
55+
apiVersion: rbac.authorization.k8s.io/v1
56+
kind: RoleBinding
57+
metadata:
58+
name: "{{ template "pulsar.fullname" . }}-{{ .Values.broker.component }}-sts-cleanup"
59+
namespace: {{ template "pulsar.namespace" . }}
60+
labels:
61+
{{- include "pulsar.standardLabels" . | nindent 4 }}
62+
component: {{ .Values.broker.component }}-sts-cleanup
63+
annotations:
64+
"helm.sh/hook": pre-upgrade
65+
"helm.sh/hook-weight": "-10"
66+
"helm.sh/hook-delete-policy": hook-succeeded
67+
subjects:
68+
- kind: ServiceAccount
69+
name: "{{ template "pulsar.fullname" . }}-{{ .Values.broker.component }}-sts-cleanup"
70+
roleRef:
71+
kind: Role
72+
name: "{{ template "pulsar.fullname" . }}-{{ .Values.broker.component }}-sts-cleanup"
73+
apiGroup: rbac.authorization.k8s.io
74+
---
75+
apiVersion: batch/v1
76+
kind: Job
77+
metadata:
78+
name: "{{ template "pulsar.fullname" . }}-{{ .Values.broker.component }}-sts-cleanup"
79+
namespace: {{ template "pulsar.namespace" . }}
80+
labels:
81+
{{- include "pulsar.standardLabels" . | nindent 4 }}
82+
component: {{ .Values.broker.component }}-sts-cleanup
83+
annotations:
84+
"helm.sh/hook": pre-upgrade
85+
"helm.sh/hook-weight": "0"
86+
"helm.sh/hook-delete-policy": hook-succeeded
87+
spec:
88+
backoffLimit: 1
89+
template:
90+
spec:
91+
serviceAccountName: "{{ template "pulsar.fullname" . }}-{{ .Values.broker.component }}-sts-cleanup"
92+
restartPolicy: Never
93+
containers:
94+
- name: sts-cleanup
95+
image: "{{ template "pulsar.imageFullName" (dict "image" .Values.images.kubectl "root" .) }}"
96+
command:
97+
- sh
98+
- -c
99+
- |
100+
STS="{{ template "pulsar.fullname" . }}-{{ .Values.broker.component }}"
101+
CHART_LABEL=$(kubectl get statefulset "$STS" -o jsonpath='{.metadata.labels.chart}' 2>/dev/null || true)
102+
if [ -z "$CHART_LABEL" ]; then
103+
echo "StatefulSet $STS not found or has no chart label, skipping delete"
104+
exit 0
105+
fi
106+
VERSION="${CHART_LABEL#pulsar-}"
107+
MAJOR="$(echo "$VERSION" | cut -d. -f1)"
108+
MINOR="$(echo "$VERSION" | cut -d. -f2)"
109+
if [ "$MAJOR" -lt 4 ] || { [ "$MAJOR" -eq 4 ] && [ "$MINOR" -lt 6 ]; }; then
110+
echo "Chart version $CHART_LABEL is older than pulsar-4.6.0, deleting StatefulSet $STS"
111+
kubectl delete statefulset "$STS" --cascade=orphan --ignore-not-found
112+
else
113+
echo "Chart version $CHART_LABEL is 4.6.0 or newer, skipping delete"
114+
fi
115+
{{- end }}

charts/pulsar/values.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,6 +1237,9 @@ broker:
12371237
##
12381238
headlessService:
12391239
annotations: {}
1240+
## Broker statefulset upgrade job
1241+
statefulsetUpgrade:
1242+
enabled: true
12401243
## Broker PodDisruptionBudget
12411244
## templates/broker-pdb.yaml
12421245
##

0 commit comments

Comments
 (0)