diff --git a/apis/apps/v1alpha1/cluster_conversion.go b/apis/apps/v1alpha1/cluster_conversion.go index 44b5e75d0b3..648263cb20f 100644 --- a/apis/apps/v1alpha1/cluster_conversion.go +++ b/apis/apps/v1alpha1/cluster_conversion.go @@ -154,6 +154,7 @@ type clusterConverter struct { } type clusterSpecConverter struct { + ClusterDefRef string `json:"clusterDefRef,omitempty"` ClusterVersionRef string `json:"clusterVersionRef,omitempty"` TerminationPolicy TerminationPolicyType `json:"terminationPolicy"` Affinity *Affinity `json:"affinity,omitempty"` @@ -193,6 +194,7 @@ type clusterCompStatusConverter struct { func (c *clusterConverter) fromCluster(cluster *Cluster) { c.Spec.ClusterVersionRef = cluster.Spec.ClusterVersionRef + c.Spec.ClusterDefRef = cluster.Spec.ClusterDefRef c.Spec.TerminationPolicy = cluster.Spec.TerminationPolicy c.Spec.Affinity = cluster.Spec.Affinity c.Spec.Tolerations = cluster.Spec.Tolerations @@ -244,6 +246,7 @@ func (c *clusterConverter) fromCluster(cluster *Cluster) { func (c *clusterConverter) toCluster(cluster *Cluster) { cluster.Spec.ClusterVersionRef = c.Spec.ClusterVersionRef + cluster.Spec.ClusterDefRef = c.Spec.ClusterDefRef cluster.Spec.TerminationPolicy = c.Spec.TerminationPolicy cluster.Spec.Affinity = c.Spec.Affinity cluster.Spec.Tolerations = c.Spec.Tolerations diff --git a/apis/apps/v1alpha1/conversion.go b/apis/apps/v1alpha1/conversion.go index 19a2fd8aea3..f6541cb2af0 100644 --- a/apis/apps/v1alpha1/conversion.go +++ b/apis/apps/v1alpha1/conversion.go @@ -23,6 +23,7 @@ import ( "encoding/json" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "sigs.k8s.io/controller-runtime/pkg/client" ) const ( @@ -77,13 +78,26 @@ func incrementConvertFrom(converter incrementConverter, source metav1.Object, ic if annotations != nil { data, ok := annotations[kbIncrementConverterAK] if ok { + // Convert from the incremental converter only if the annotation exists. if err := json.Unmarshal([]byte(data), ic); err != nil { return err } delete(annotations, kbIncrementConverterAK) source.SetAnnotations(annotations) + return converter.incrementConvertFrom(source, ic) } } + return nil +} - return converter.incrementConvertFrom(source, ic) +func GetClusterDefFromIncrementConverter(obj client.Object) (string, error) { + incrementConverterStr := obj.GetAnnotations()[kbIncrementConverterAK] + if len(incrementConverterStr) == 0 { + return "", nil + } + var alpha1Cluster Cluster + if err := json.Unmarshal([]byte(incrementConverterStr), &alpha1Cluster); err != nil { + return "", err + } + return alpha1Cluster.Spec.ClusterDefRef, nil } diff --git a/controllers/apps/transformer_cluster_normalization.go b/controllers/apps/transformer_cluster_normalization.go index aaaa93b0f6f..34c18a64edb 100644 --- a/controllers/apps/transformer_cluster_normalization.go +++ b/controllers/apps/transformer_cluster_normalization.go @@ -29,6 +29,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" appsv1 "github.com/apecloud/kubeblocks/apis/apps/v1" + appsv1alpha1 "github.com/apecloud/kubeblocks/apis/apps/v1alpha1" "github.com/apecloud/kubeblocks/pkg/constant" "github.com/apecloud/kubeblocks/pkg/controller/component" "github.com/apecloud/kubeblocks/pkg/controller/graph" @@ -416,37 +417,55 @@ func (t *clusterNormalizationTransformer) writeBackCompNShardingSpecs(transCtx * } func (t *clusterNormalizationTransformer) checkNPatchCRDAPIVersionKey(transCtx *clusterTransformContext) error { - apiVersions := map[string][]string{} + 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 { + return appsv1alpha1.GroupVersion.String(), nil + } - from := func(name string, annotations map[string]string) { - key := annotations[constant.CRDAPIVersionAnnotationKey] - apiVersions[key] = append(apiVersions[key], name) - } + // get the CRD API version from the annotations of the clusterDef or componentDefs + apiVersions := map[string][]string{} + from := func(name string, annotations map[string]string) { + key := annotations[constant.CRDAPIVersionAnnotationKey] + apiVersions[key] = append(apiVersions[key], name) + } - if transCtx.clusterDef != nil { - from(transCtx.clusterDef.Name, transCtx.clusterDef.Annotations) - } else { - for _, compDef := range transCtx.componentDefs { - from(compDef.Name, compDef.Annotations) + if transCtx.clusterDef != nil { + from(transCtx.clusterDef.Name, transCtx.clusterDef.Annotations) + } else { + for _, compDef := range transCtx.componentDefs { + from(compDef.Name, compDef.Annotations) + } + for _, shardingDef := range transCtx.shardingDefs { + from(shardingDef.Name, shardingDef.Annotations) + } } - for _, shardingDef := range transCtx.shardingDefs { - from(shardingDef.Name, shardingDef.Annotations) + switch { + case len(apiVersions) > 1: + return "", fmt.Errorf("multiple CRD API versions found: %v", apiVersions) + case len(apiVersions) == 1: + return maps.Keys(apiVersions)[0], nil + default: + return "", nil } } - if len(apiVersions) > 1 { - return fmt.Errorf("multiple CRD API versions found: %v", apiVersions) + apiVersion, err := getCRDAPIVersion() + if err != nil { + return err } - - apiVersion := "" - if len(apiVersions) == 1 { - apiVersion = maps.Keys(apiVersions)[0] - if transCtx.Cluster.Annotations == nil { - transCtx.Cluster.Annotations = make(map[string]string) - } - transCtx.Cluster.Annotations[constant.CRDAPIVersionAnnotationKey] = apiVersion + if transCtx.Cluster.Annotations == nil { + transCtx.Cluster.Annotations = make(map[string]string) } - + transCtx.Cluster.Annotations[constant.CRDAPIVersionAnnotationKey] = apiVersion if controllerutil.IsAPIVersionSupported(apiVersion) { return nil } diff --git a/controllers/apps/transformer_component_init.go b/controllers/apps/transformer_component_init.go index 5ce068cd1e3..0564c45b9b1 100644 --- a/controllers/apps/transformer_component_init.go +++ b/controllers/apps/transformer_component_init.go @@ -32,10 +32,6 @@ var _ graph.Transformer = &componentInitTransformer{} func (t *componentInitTransformer) Transform(ctx graph.TransformContext, dag *graph.DAG) error { transCtx, _ := ctx.(*componentTransformContext) - if !intctrlutil.ObjectAPIVersionSupported(transCtx.Component) { - return graph.ErrPrematureStop - } - // init dag rootVertex := &model.ObjectVertex{Obj: transCtx.Component, OriObj: transCtx.ComponentOrig, Action: model.ActionStatusPtr()} dag.AddVertex(rootVertex) @@ -43,5 +39,8 @@ func (t *componentInitTransformer) Transform(ctx graph.TransformContext, dag *gr // init placement transCtx.Context = intoContext(transCtx.Context, placement(transCtx.Component)) + if !intctrlutil.ObjectAPIVersionSupported(transCtx.Component) { + return graph.ErrPrematureStop + } return nil } diff --git a/deploy/helm/templates/rbac/apps_sidecardefinition_editor_role.yaml b/deploy/helm/templates/rbac/apps_sidecardefinition_editor_role.yaml new file mode 100644 index 00000000000..7018820c026 --- /dev/null +++ b/deploy/helm/templates/rbac/apps_sidecardefinition_editor_role.yaml @@ -0,0 +1,26 @@ +# permissions for end users to edit sidecardefinitions. +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + name: {{ include "kubeblocks.fullname" . }}-sidecardefinition-editor-role + labels: + {{- include "kubeblocks.labels" . | nindent 4 }} +rules: +- apiGroups: + - apps.kubeblocks.io + resources: + - sidecardefinitions + verbs: + - create + - delete + - get + - list + - patch + - update + - watch +- apiGroups: + - apps.kubeblocks.io + resources: + - sidecardefinitions/status + verbs: + - get