From 16a82f36594037d3ce4a12333cffea18e9d530d3 Mon Sep 17 00:00:00 2001 From: Y-Rookie Date: Tue, 24 Oct 2023 18:13:58 +0800 Subject: [PATCH] modify buildRSMWrapper --- .../apps/transformer_component_status.go | 2 +- .../apps/transformer_component_workload.go | 4 +- pkg/controller/component/rsm_convertor.go | 39 +++++++++ .../component/synthesize_component.go | 1 + pkg/controller/factory/builder_rsm.go | 81 +++++-------------- 5 files changed, 62 insertions(+), 65 deletions(-) diff --git a/controllers/apps/transformer_component_status.go b/controllers/apps/transformer_component_status.go index d64483ad1fd..3b0ca59b7cb 100644 --- a/controllers/apps/transformer_component_status.go +++ b/controllers/apps/transformer_component_status.go @@ -117,7 +117,7 @@ func (t *ComponentStatusTransformer) Transform(ctx graph.TransformContext, dag * } // build protoRSM workload - protoRSM, err := factory.BuildRSM(clusterObj, synthesizeComp) + protoRSM, err := factory.BuildRSMWrapper(clusterObj, synthesizeComp) if err != nil { return err } diff --git a/controllers/apps/transformer_component_workload.go b/controllers/apps/transformer_component_workload.go index b10abb040ae..f71ccef68c3 100644 --- a/controllers/apps/transformer_component_workload.go +++ b/controllers/apps/transformer_component_workload.go @@ -94,8 +94,8 @@ func (t *ComponentWorkloadTransformer) Transform(ctx graph.TransformContext, dag buildPodSpecVolumeMounts(synthesizeComp) // build rsm workload - // TODO(xingran): BuildRSM relies on the deprecated fields of the component, for example component.WorkloadType, which should be removed in the future - rsm, err := factory.BuildRSM(cluster, synthesizeComp) + // TODO(xingran): BuildRSMWrapper relies on the deprecated fields of the component, for example component.WorkloadType, which should be removed in the future + rsm, err := factory.BuildRSMWrapper(cluster, synthesizeComp) if err != nil { return err } diff --git a/pkg/controller/component/rsm_convertor.go b/pkg/controller/component/rsm_convertor.go index ec0e87616c2..0f6e5df7d50 100644 --- a/pkg/controller/component/rsm_convertor.go +++ b/pkg/controller/component/rsm_convertor.go @@ -21,6 +21,8 @@ package component import ( "errors" + "github.com/apecloud/kubeblocks/pkg/constant" + corev1 "k8s.io/api/core/v1" appsv1alpha1 "github.com/apecloud/kubeblocks/apis/apps/v1alpha1" workloads "github.com/apecloud/kubeblocks/apis/workloads/v1alpha1" @@ -38,6 +40,9 @@ type rsmRolesConvertor struct{} // rsmRoleProbeConvertor is an implementation of the convertor interface, used to convert the given object into ReplicatedStateMachine.Spec.RoleProbe. type rsmRoleProbeConvertor struct{} +// rsmCredentialConvertor is an implementation of the convertor interface, used to convert the given object into ReplicatedStateMachine.Spec.Credential. +type rsmCredentialConvertor struct{} + // rsmMembershipReconfigurationConvertor is an implementation of the convertor interface, used to convert the given object into ReplicatedStateMachine.Spec.MembershipReconfiguration. type rsmMembershipReconfigurationConvertor struct{} @@ -57,6 +62,7 @@ func BuildRSMFrom(cluster *appsv1alpha1.Cluster, synthesizeComp *SynthesizedComp "alternativeservices": &rsmAlternativeServicesConvertor{}, "roles": &rsmRolesConvertor{}, "roleprobe": &rsmRoleProbeConvertor{}, + "credential": &rsmCredentialConvertor{}, "membershipreconfiguration": &rsmMembershipReconfigurationConvertor{}, "memberupdatestrategy": &rsmMemberUpdateStrategyConvertor{}, } @@ -188,6 +194,39 @@ func (c *rsmRoleProbeConvertor) convert(args ...any) (any, error) { return rsmRoleProbe, nil } +func (c *rsmCredentialConvertor) convert(args ...any) (any, error) { + cluster, _, err := parseRSMConvertorArgs(args...) + if err != nil { + return nil, err + } + + secretName := constant.GenerateDefaultConnCredential(cluster.Name) + credential := workloads.Credential{ + Username: workloads.CredentialVar{ + ValueFrom: &corev1.EnvVarSource{ + SecretKeyRef: &corev1.SecretKeySelector{ + LocalObjectReference: corev1.LocalObjectReference{ + Name: secretName, + }, + Key: constant.AccountNameForSecret, + }, + }, + }, + Password: workloads.CredentialVar{ + ValueFrom: &corev1.EnvVarSource{ + SecretKeyRef: &corev1.SecretKeySelector{ + LocalObjectReference: corev1.LocalObjectReference{ + Name: secretName, + }, + Key: constant.AccountPasswdForSecret, + }, + }, + }, + } + + return credential, nil +} + func (c *rsmMembershipReconfigurationConvertor) convert(args ...any) (any, error) { // cluster, synthesizeComp, err := parseRSMConvertorArgs(args...) return "", nil // TODO diff --git a/pkg/controller/component/synthesize_component.go b/pkg/controller/component/synthesize_component.go index 793bf1a9671..0b6a22a2884 100644 --- a/pkg/controller/component/synthesize_component.go +++ b/pkg/controller/component/synthesize_component.go @@ -268,6 +268,7 @@ func buildBackwardCompatibleFields(reqCtx intctrlutil.RequestCtx, cli roclient.R buildWorkload := func() { synthesizeComp.WorkloadType = clusterCompDef.WorkloadType synthesizeComp.CharacterType = clusterCompDef.CharacterType + synthesizeComp.ClusterCompDefName = clusterCompDef.Name synthesizeComp.HorizontalScalePolicy = clusterCompDef.HorizontalScalePolicy synthesizeComp.StatelessSpec = clusterCompDef.StatelessSpec synthesizeComp.StatefulSpec = clusterCompDef.StatefulSpec diff --git a/pkg/controller/factory/builder_rsm.go b/pkg/controller/factory/builder_rsm.go index fc9c334fae7..2311a458e65 100644 --- a/pkg/controller/factory/builder_rsm.go +++ b/pkg/controller/factory/builder_rsm.go @@ -32,8 +32,15 @@ import ( "github.com/apecloud/kubeblocks/pkg/controller/rsm" ) -func BuildRSMWrapper(cluster *appsv1alpha1.Cluster, component *component.SynthesizedComponent) (*workloads.ReplicatedStateMachine, error) { - return nil, nil +// BuildRSMWrapper builds a ReplicatedStateMachine object based on Cluster, SynthesizedComponent. +func BuildRSMWrapper(cluster *appsv1alpha1.Cluster, synthesizeComp *component.SynthesizedComponent) (*workloads.ReplicatedStateMachine, error) { + if synthesizeComp.ClusterCompDefName == "" { + // build rsm from new ClusterDefinition API, and convert componentDefinition attributes to rsm attributes. + return BuildRSMFromConvertor(cluster, synthesizeComp) + } + + // build rsm from old ClusterDefinition API + return BuildRSM(cluster, synthesizeComp) } // BuildRSMFromConvertor builds a ReplicatedStateMachine object based on the new ComponentDefinition and Component API, and does not depend on the deprecated fields in the SynthesizedComponent. @@ -69,65 +76,26 @@ func BuildRSMFromConvertor(cluster *appsv1alpha1.Cluster, synthesizeComp *compon } rsmBuilder.SetVolumeClaimTemplates(vcts...) - // TODO(xingran): call rsm convertors to convert componentDef attributes to rsm attributes. including service, credential, roles, roleProbe, membershipReconfiguration, memberUpdateStrategy, etc. - rsm, err := component.BuildRSMFrom(cluster, synthesizeComp, rsmBuilder.GetObject()) + // TODO(xingran): call convertors to convert componentDef attributes to attributes. including service, credential, roles, roleProbe, membershipReconfiguration, memberUpdateStrategy, etc. + convertedRSM, err := component.BuildRSMFrom(cluster, synthesizeComp, rsmBuilder.GetObject()) if err != nil { return nil, err } - // return rsm, nil - - service, alternativeServices := separateServices(component.Services) - addServiceCommonLabels(service, commonLabels, synthesizeComp.CompDefName) - for i := range alternativeServices { - addServiceCommonLabels(&alternativeServices[i], commonLabels, synthesizeComp.CompDefName) - } - if service != nil { - rsmBuilder.SetService(service) - } - if len(alternativeServices) == 0 { - alternativeServices = nil - } - alternativeServices = fixService(cluster.Namespace, rsmName, component, alternativeServices...) - rsmBuilder.SetAlternativeServices(alternativeServices) - - secretName := constant.GenerateDefaultConnCredential(cluster.Name) - credential := workloads.Credential{ - Username: workloads.CredentialVar{ - ValueFrom: &corev1.EnvVarSource{ - SecretKeyRef: &corev1.SecretKeySelector{ - LocalObjectReference: corev1.LocalObjectReference{ - Name: secretName, - }, - Key: constant.AccountNameForSecret, - }, - }, - }, - Password: workloads.CredentialVar{ - ValueFrom: &corev1.EnvVarSource{ - SecretKeyRef: &corev1.SecretKeySelector{ - LocalObjectReference: corev1.LocalObjectReference{ - Name: secretName, - }, - Key: constant.AccountPasswdForSecret, - }, - }, - }, - } - rsmBuilder.SetCredential(credential) - // update sts.spec.volumeClaimTemplates[].metadata.labels - if len(rsm.Spec.VolumeClaimTemplates) > 0 && len(rsm.GetLabels()) > 0 { - for index, vct := range rsm.Spec.VolumeClaimTemplates { - BuildPersistentVolumeClaimLabels(component, &vct, vct.Name) - rsm.Spec.VolumeClaimTemplates[index] = vct + // TODO(xingran): synthesizeComp.VolumeTypes has been removed, and the following code needs to be refactored. + if len(convertedRSM.Spec.VolumeClaimTemplates) > 0 && len(convertedRSM.GetLabels()) > 0 { + for index, vct := range convertedRSM.Spec.VolumeClaimTemplates { + BuildPersistentVolumeClaimLabels(synthesizeComp, &vct, vct.Name) + convertedRSM.Spec.VolumeClaimTemplates[index] = vct } } - if err := processContainersInjection(cluster, synthesizeComp, &rsm.Spec.Template.Spec); err != nil { + if err := processContainersInjection(cluster, synthesizeComp, &convertedRSM.Spec.Template.Spec); err != nil { return nil, err } - return rsm, nil + + return convertedRSM, nil } // BuildRSM builds a ReplicatedStateMachine object based on the old CLusterDefinition API, and depends on the the deprecated fields in the SynthesizedComponent. @@ -274,14 +242,3 @@ func getMonitorAnnotations(synthesizeComp *component.SynthesizedComponent) map[s } return rsm.AddAnnotationScope(rsm.HeadlessServiceScope, annotations) } - -// separateComponentDefServices separates 'services' to a main service from cd and alternative services from componentDefinition -// TODO(xingran): convert componentDef.services to rsm.Service -func separateComponentDefServices(services []corev1.Service) (*corev1.Service, []corev1.Service) { - if len(services) == 0 { - return nil, nil - } - // from component.buildComponent (which contains component.Services' building process), the first item should be the main service - // TODO(free6om): make two fields in component(i.e. Service and AlternativeServices) after RSM passes all testes. - return &services[0], services[1:] -}