Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/cmd/cluster/create_subcmds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ var _ = Describe("create cluster by cluster type", func() {
const (
clusterType = "mysql"
redisCluster = "redis"
redisComponent = "redis-cluster-7"
redisComponent = "redis-cluster"
)

var (
Expand Down
59 changes: 43 additions & 16 deletions pkg/cmd/cluster/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ type OperationsOptions struct {
// Switchover options
Component string `json:"component"`
ComponentObjectName string `json:"componentObjectName"`
ShardingName string `json:"shardingName"`
Instance string `json:"instance"`
Primary string `json:"-"`
CharacterType string `json:"-"`
Expand Down Expand Up @@ -215,10 +216,20 @@ func (o *OperationsOptions) CompletePromoteOps() error {
}

if o.Component == "" {
if len(clusterObj.Spec.ComponentSpecs) > 1 {
return fmt.Errorf("there are multiple components in cluster, please use --component to specify the component for promote")
if o.ComponentObjectName == "" {
if len(clusterObj.Spec.ComponentSpecs) > 1 {
return fmt.Errorf("there are multiple components in cluster, please use --component to specify the component for promote")
}
o.Component = clusterObj.Spec.ComponentSpecs[0].Name
} else {
componentObj := &appsv1alpha1.Component{}
if err := util.GetK8SClientObject(o.Dynamic, componentObj, types.ComponentGVR(), o.Namespace, o.ComponentObjectName); err != nil {
return err
}
if componentObj.GetLabels()[constant.KBAppShardingNameLabelKey] != "" {
o.ShardingName = componentObj.GetLabels()[constant.KBAppShardingNameLabelKey]
}
}
o.Component = clusterObj.Spec.ComponentSpecs[0].Name
}
o.CompleteHaEnabled()
return o.CompleteCharacterType(clusterObj)
Expand All @@ -229,10 +240,19 @@ func (o *OperationsOptions) CompletePromoteOps() error {
func (o *OperationsOptions) CompleteCharacterType(clusterObj *appsv1alpha1.Cluster) error {
var primaryRoles []string
var componentSpec appsv1alpha1.ClusterComponentSpec
for _, compSpec := range clusterObj.Spec.ComponentSpecs {
if compSpec.Name == o.Component {
componentSpec = compSpec
break
if o.ShardingName != "" {
for _, shardingSpec := range clusterObj.Spec.ShardingSpecs {
if shardingSpec.Name == o.ShardingName {
componentSpec = shardingSpec.Template
break
}
}
} else {
for _, compSpec := range clusterObj.Spec.ComponentSpecs {
if compSpec.Name == o.Component {
componentSpec = compSpec
break
}
}
}

Expand Down Expand Up @@ -321,8 +341,12 @@ func (o *OperationsOptions) CompleteCharacterType(clusterObj *appsv1alpha1.Clust
}

func (o *OperationsOptions) CompleteHaEnabled() {
cmName := fmt.Sprintf("%s-%s-haconfig", o.Name, o.Component)

var cmName string
if o.ComponentObjectName != "" {
cmName = fmt.Sprintf("%s-haconfig", o.ComponentObjectName)
} else {
cmName = fmt.Sprintf("%s-%s-haconfig", o.Name, o.Component)
}
cm, err := o.Client.CoreV1().ConfigMaps(o.Namespace).Get(context.Background(), cmName, metav1.GetOptions{})
if err != nil {
return
Expand Down Expand Up @@ -476,9 +500,11 @@ func (o *OperationsOptions) validatePromote(cluster *appsv1alpha1.Cluster) error
podObj = &corev1.Pod{}
componentName = o.Component
)

if len(cluster.Spec.ComponentSpecs) == 0 {
return fmt.Errorf("cluster.Spec.ComponentSpecs cannot be empty")
if o.ShardingName != "" {
componentName = o.ShardingName
}
if len(cluster.Spec.ComponentSpecs) == 0 && len(cluster.Spec.ShardingSpecs) == 0 {
return fmt.Errorf("cluster.Spec.ComponentSpecs or cluster.Spec.ShardingSpecs cannot be empty")
}

getAndValidatePod := func(targetRoles ...string) error {
Expand Down Expand Up @@ -1074,10 +1100,8 @@ func NewPromoteCmd(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra
cmdutil.BehaviorOnFatal(printer.FatalWithRedColor)
cmdutil.CheckErr(o.Complete())
cmdutil.CheckErr(o.CompleteComponentsFlag())
if len(o.ComponentObjectName) == 0 {
cmdutil.CheckErr(o.CompletePromoteOps())
cmdutil.CheckErr(o.Validate())
}
cmdutil.CheckErr(o.CompletePromoteOps())
cmdutil.CheckErr(o.Validate())
if (o.LorryHAEnabled || o.CharacterType == oceanbase) && o.ExecPod != nil {
// lorryCli, err := lorryclient.NewK8sExecClientWithPod(nil, o.ExecPod)
// cmdutil.CheckErr(err)
Expand All @@ -1096,6 +1120,9 @@ func NewPromoteCmd(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra
Value: o.Instance,
},
}
if customOpr.Component == "" {
customOpr.Component = o.ShardingName
}
customOpr.CreateOptions.Options = customOpr
cmdutil.CheckErr(customOpr.Run())
} else {
Expand Down
Loading