Skip to content

Commit

Permalink
modify buildRSMWrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
Y-Rookie committed Oct 24, 2023
1 parent 3754112 commit 16a82f3
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 65 deletions.
2 changes: 1 addition & 1 deletion controllers/apps/transformer_component_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions controllers/apps/transformer_component_workload.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
39 changes: 39 additions & 0 deletions pkg/controller/component/rsm_convertor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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{}

Expand All @@ -57,6 +62,7 @@ func BuildRSMFrom(cluster *appsv1alpha1.Cluster, synthesizeComp *SynthesizedComp
"alternativeservices": &rsmAlternativeServicesConvertor{},
"roles": &rsmRolesConvertor{},
"roleprobe": &rsmRoleProbeConvertor{},
"credential": &rsmCredentialConvertor{},
"membershipreconfiguration": &rsmMembershipReconfigurationConvertor{},
"memberupdatestrategy": &rsmMemberUpdateStrategyConvertor{},
}
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions pkg/controller/component/synthesize_component.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
81 changes: 19 additions & 62 deletions pkg/controller/factory/builder_rsm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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:]
}

0 comments on commit 16a82f3

Please sign in to comment.