From 2352ff12eab49c21bb725a3c291aebab07968c92 Mon Sep 17 00:00:00 2001 From: wangyelei Date: Tue, 7 Jan 2025 10:29:25 +0800 Subject: [PATCH] chore: fix create cluster with cd/cv failed when existing 0.9 kb --- apis/apps/v1alpha1/conversion.go | 8 ++++---- .../apps/transformer_cluster_normalization.go | 16 ++++++++++------ deploy/helm/templates/conversion-patch-hook.yaml | 2 ++ 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/apis/apps/v1alpha1/conversion.go b/apis/apps/v1alpha1/conversion.go index f6541cb2af0..7a2d9fda862 100644 --- a/apis/apps/v1alpha1/conversion.go +++ b/apis/apps/v1alpha1/conversion.go @@ -90,14 +90,14 @@ func incrementConvertFrom(converter incrementConverter, source metav1.Object, ic return nil } -func GetClusterDefFromIncrementConverter(obj client.Object) (string, error) { +func GetV1Alpha1ClusterFromIncrementConverter(obj client.Object) (*Cluster, error) { incrementConverterStr := obj.GetAnnotations()[kbIncrementConverterAK] if len(incrementConverterStr) == 0 { - return "", nil + return nil, nil } var alpha1Cluster Cluster if err := json.Unmarshal([]byte(incrementConverterStr), &alpha1Cluster); err != nil { - return "", err + return &alpha1Cluster, err } - return alpha1Cluster.Spec.ClusterDefRef, nil + return &alpha1Cluster, nil } diff --git a/controllers/apps/transformer_cluster_normalization.go b/controllers/apps/transformer_cluster_normalization.go index 34c18a64edb..570e7908d75 100644 --- a/controllers/apps/transformer_cluster_normalization.go +++ b/controllers/apps/transformer_cluster_normalization.go @@ -417,17 +417,17 @@ func (t *clusterNormalizationTransformer) writeBackCompNShardingSpecs(transCtx * } func (t *clusterNormalizationTransformer) checkNPatchCRDAPIVersionKey(transCtx *clusterTransformContext) error { + // get the v1Alpha1Cluster from the annotations + v1Alpha1Cluster, err := appsv1alpha1.GetV1Alpha1ClusterFromIncrementConverter(transCtx.Cluster) + if err != nil { + return err + } getCRDAPIVersion := func() (string, error) { apiVersion := transCtx.Cluster.Annotations[constant.CRDAPIVersionAnnotationKey] if len(apiVersion) > 0 { return apiVersion, nil } - // check if the cluster is the alpha1 version - clusterDefRef, err := appsv1alpha1.GetClusterDefFromIncrementConverter(transCtx.Cluster) - if err != nil { - return "", err - } - if len(clusterDefRef) > 0 { + if v1Alpha1Cluster != nil && len(v1Alpha1Cluster.Spec.ClusterDefRef) > 0 { return appsv1alpha1.GroupVersion.String(), nil } @@ -469,5 +469,9 @@ func (t *clusterNormalizationTransformer) checkNPatchCRDAPIVersionKey(transCtx * if controllerutil.IsAPIVersionSupported(apiVersion) { return nil } + if v1Alpha1Cluster != nil && len(v1Alpha1Cluster.Spec.ClusterVersionRef) > 0 { + // revert the topology to empty + transCtx.Cluster.Spec.Topology = "" + } return graph.ErrPrematureStop // un-supported CRD API version, stop the transformation } diff --git a/deploy/helm/templates/conversion-patch-hook.yaml b/deploy/helm/templates/conversion-patch-hook.yaml index e25833b6e15..8062479a9e1 100644 --- a/deploy/helm/templates/conversion-patch-hook.yaml +++ b/deploy/helm/templates/conversion-patch-hook.yaml @@ -82,7 +82,9 @@ spec: # 3. start KubeBlocks kubectl patch deployment {{ include "kubeblocks.fullname" . }} -n {{ .Release.Namespace }} -p '{"spec":{"replicas":{{ .Values.replicaCount }}}}' + {{- if .Values.dataProtection.enabled }} kubectl patch deployment {{ include "kubeblocks.fullname" . }}-dataprotection -n {{ .Release.Namespace }} -p '{"spec":{"replicas":{{ .Values.replicaCount }}}}' + {{- end }} volumeMounts: - mountPath: /opt name: crd-multi-version-conversion