From 4ef33c65d88bba4f603da5d9f61f76155e7c6c7e Mon Sep 17 00:00:00 2001 From: cuisongliu Date: Mon, 14 Oct 2024 11:28:20 +0800 Subject: [PATCH] feat(main): fix helm config (#41) Signed-off-by: cuisongliu --- api/v1beta1/automq_webhook.go | 3 ++ .../automq-operator/templates/_helpers.tpl | 37 +++++++++++++++++++ .../automq-operator/templates/deployment.yaml | 7 ++-- internal/controller/automq_controller_b.go | 9 ++++- 4 files changed, 51 insertions(+), 5 deletions(-) diff --git a/api/v1beta1/automq_webhook.go b/api/v1beta1/automq_webhook.go index e0ddf9d..12eeb5a 100644 --- a/api/v1beta1/automq_webhook.go +++ b/api/v1beta1/automq_webhook.go @@ -100,6 +100,9 @@ func (r *AutoMQ) ValidateUpdate(old runtime.Object) (admission.Warnings, error) if r.Spec.ClusterID != mqOld.Spec.ClusterID { return nil, fmt.Errorf("field clusterID is immutable") } + if r.Spec.Controller.Replicas != mqOld.Spec.Controller.Replicas { + return nil, fmt.Errorf("field controller.replicas is immutable") + } if err := validate(r); err != nil { return nil, err } diff --git a/deploy/charts/automq-operator/templates/_helpers.tpl b/deploy/charts/automq-operator/templates/_helpers.tpl index be55b52..88bff29 100644 --- a/deploy/charts/automq-operator/templates/_helpers.tpl +++ b/deploy/charts/automq-operator/templates/_helpers.tpl @@ -49,3 +49,40 @@ Selector labels app.kubernetes.io/name: {{ include "automq-operator.name" . }} app.kubernetes.io/instance: {{ .Release.Name }} {{- end }} + + +{{/* +Extract the tag from the image repository string. +Assumes the tag is the part after the last colon `:`. +If no tag is present, returns an empty string. +*/}} +{{- define "automq-operator.getTag" -}} + {{- $image := .Values.image -}} + {{- $splitImage := splitList ":" $image -}} + {{- if eq (len $splitImage) 2 -}} + {{- index $splitImage 1 -}} + {{- else -}} + latest + {{- end -}} +{{- end -}} + +{{/* +Determine the image pull policy. +If the image tag is 'latest', set the pull policy to 'Always'. +Otherwise, use the default pull policy from values.yaml. +*/}} +{{- define "automq-operator.pullPolicy" -}} +{{- if eq (include "automq-operator.getTag" .) "latest" -}} +Always +{{- else -}} +IfNotPresent +{{- end -}} +{{- end -}} + +{{- define "automq-operator.revision" -}} +{{- if eq (include "automq-operator.getTag" .) "latest" -}} +{{.Release.Revision}} +{{- else -}} +{{ include "automq-operator.getTag" .}} +{{- end -}} +{{- end -}} diff --git a/deploy/charts/automq-operator/templates/deployment.yaml b/deploy/charts/automq-operator/templates/deployment.yaml index aab3ecd..459d42b 100644 --- a/deploy/charts/automq-operator/templates/deployment.yaml +++ b/deploy/charts/automq-operator/templates/deployment.yaml @@ -13,10 +13,11 @@ spec: {{- include "automq-operator.selectorLabels" . | nindent 6 }} template: metadata: - {{- with .Values.podAnnotations }} annotations: + {{- with .Values.podAnnotations }} {{- toYaml . | nindent 8 }} - {{- end }} + {{- end }} + operator-revision: {{ include "automq-operator.revision" . }} labels: {{- include "automq-operator.selectorLabels" . | nindent 8 }} spec: @@ -34,7 +35,7 @@ spec: securityContext: {{- toYaml .Values.securityContext | nindent 12 }} image: "{{ .Values.image }}" - imagePullPolicy: IfNotPresent + imagePullPolicy: {{ include "automq-operator.pullPolicy" . }} args: - "-leader-elect={{.Values.args.leader}}" - "-default-burst={{.Values.args.burst}}" diff --git a/internal/controller/automq_controller_b.go b/internal/controller/automq_controller_b.go index 0be4ef8..390b587 100644 --- a/internal/controller/automq_controller_b.go +++ b/internal/controller/automq_controller_b.go @@ -51,7 +51,7 @@ func (r *AutoMQReconciler) cleanBroker(ctx context.Context, obj *infrav1beta1.Au svcc.Name = getAutoMQName(brokerRole, &index) _ = r.Client.Delete(ctx, svcc) - deploy := &appsv1.StatefulSet{} + deploy := &appsv1.Deployment{} deploy.Namespace = obj.Namespace deploy.Name = getAutoMQName(brokerRole, &index) _ = r.Client.Delete(ctx, deploy) @@ -61,6 +61,11 @@ func (r *AutoMQReconciler) cleanBroker(ctx context.Context, obj *infrav1beta1.Au pvc.Name = getAutoMQName(brokerRole, &index) _ = r.Client.Delete(ctx, pvc) } + + bsvc := &v1.Service{} + bsvc.Namespace = obj.Namespace + bsvc.Name = getAutoMQName(brokerRole+"-bootstrap", nil) + _ = r.Client.Delete(ctx, bsvc) return nil } @@ -261,7 +266,7 @@ func (r *AutoMQReconciler) syncBrokerDeploy(ctx context.Context, obj *infrav1bet "--process.roles", "broker", "--node.id", - fmt.Sprintf("%d", index+10), + fmt.Sprintf("%d", index+obj.Spec.Controller.Replicas), "--cluster.id", obj.Spec.ClusterID, "--controller.quorum.voters",