Skip to content

Commit a7ec419

Browse files
authored
fix: shard component support switchover customOps (#618)
1 parent 1ff66c5 commit a7ec419

2 files changed

Lines changed: 44 additions & 17 deletions

File tree

pkg/cmd/cluster/create_subcmds_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ var _ = Describe("create cluster by cluster type", func() {
5050
const (
5151
clusterType = "mysql"
5252
redisCluster = "redis"
53-
redisComponent = "redis-cluster-7"
53+
redisComponent = "redis-cluster"
5454
)
5555

5656
var (

pkg/cmd/cluster/operations.go

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ type OperationsOptions struct {
108108
// Switchover options
109109
Component string `json:"component"`
110110
ComponentObjectName string `json:"componentObjectName"`
111+
ShardingName string `json:"shardingName"`
111112
Instance string `json:"instance"`
112113
Primary string `json:"-"`
113114
CharacterType string `json:"-"`
@@ -215,10 +216,20 @@ func (o *OperationsOptions) CompletePromoteOps() error {
215216
}
216217

217218
if o.Component == "" {
218-
if len(clusterObj.Spec.ComponentSpecs) > 1 {
219-
return fmt.Errorf("there are multiple components in cluster, please use --component to specify the component for promote")
219+
if o.ComponentObjectName == "" {
220+
if len(clusterObj.Spec.ComponentSpecs) > 1 {
221+
return fmt.Errorf("there are multiple components in cluster, please use --component to specify the component for promote")
222+
}
223+
o.Component = clusterObj.Spec.ComponentSpecs[0].Name
224+
} else {
225+
componentObj := &appsv1alpha1.Component{}
226+
if err := util.GetK8SClientObject(o.Dynamic, componentObj, types.ComponentGVR(), o.Namespace, o.ComponentObjectName); err != nil {
227+
return err
228+
}
229+
if componentObj.GetLabels()[constant.KBAppShardingNameLabelKey] != "" {
230+
o.ShardingName = componentObj.GetLabels()[constant.KBAppShardingNameLabelKey]
231+
}
220232
}
221-
o.Component = clusterObj.Spec.ComponentSpecs[0].Name
222233
}
223234
o.CompleteHaEnabled()
224235
return o.CompleteCharacterType(clusterObj)
@@ -229,10 +240,19 @@ func (o *OperationsOptions) CompletePromoteOps() error {
229240
func (o *OperationsOptions) CompleteCharacterType(clusterObj *appsv1alpha1.Cluster) error {
230241
var primaryRoles []string
231242
var componentSpec appsv1alpha1.ClusterComponentSpec
232-
for _, compSpec := range clusterObj.Spec.ComponentSpecs {
233-
if compSpec.Name == o.Component {
234-
componentSpec = compSpec
235-
break
243+
if o.ShardingName != "" {
244+
for _, shardingSpec := range clusterObj.Spec.ShardingSpecs {
245+
if shardingSpec.Name == o.ShardingName {
246+
componentSpec = shardingSpec.Template
247+
break
248+
}
249+
}
250+
} else {
251+
for _, compSpec := range clusterObj.Spec.ComponentSpecs {
252+
if compSpec.Name == o.Component {
253+
componentSpec = compSpec
254+
break
255+
}
236256
}
237257
}
238258

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

323343
func (o *OperationsOptions) CompleteHaEnabled() {
324-
cmName := fmt.Sprintf("%s-%s-haconfig", o.Name, o.Component)
325-
344+
var cmName string
345+
if o.ComponentObjectName != "" {
346+
cmName = fmt.Sprintf("%s-haconfig", o.ComponentObjectName)
347+
} else {
348+
cmName = fmt.Sprintf("%s-%s-haconfig", o.Name, o.Component)
349+
}
326350
cm, err := o.Client.CoreV1().ConfigMaps(o.Namespace).Get(context.Background(), cmName, metav1.GetOptions{})
327351
if err != nil {
328352
return
@@ -476,9 +500,11 @@ func (o *OperationsOptions) validatePromote(cluster *appsv1alpha1.Cluster) error
476500
podObj = &corev1.Pod{}
477501
componentName = o.Component
478502
)
479-
480-
if len(cluster.Spec.ComponentSpecs) == 0 {
481-
return fmt.Errorf("cluster.Spec.ComponentSpecs cannot be empty")
503+
if o.ShardingName != "" {
504+
componentName = o.ShardingName
505+
}
506+
if len(cluster.Spec.ComponentSpecs) == 0 && len(cluster.Spec.ShardingSpecs) == 0 {
507+
return fmt.Errorf("cluster.Spec.ComponentSpecs or cluster.Spec.ShardingSpecs cannot be empty")
482508
}
483509

484510
getAndValidatePod := func(targetRoles ...string) error {
@@ -1074,10 +1100,8 @@ func NewPromoteCmd(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra
10741100
cmdutil.BehaviorOnFatal(printer.FatalWithRedColor)
10751101
cmdutil.CheckErr(o.Complete())
10761102
cmdutil.CheckErr(o.CompleteComponentsFlag())
1077-
if len(o.ComponentObjectName) == 0 {
1078-
cmdutil.CheckErr(o.CompletePromoteOps())
1079-
cmdutil.CheckErr(o.Validate())
1080-
}
1103+
cmdutil.CheckErr(o.CompletePromoteOps())
1104+
cmdutil.CheckErr(o.Validate())
10811105
if (o.LorryHAEnabled || o.CharacterType == oceanbase) && o.ExecPod != nil {
10821106
// lorryCli, err := lorryclient.NewK8sExecClientWithPod(nil, o.ExecPod)
10831107
// cmdutil.CheckErr(err)
@@ -1096,6 +1120,9 @@ func NewPromoteCmd(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra
10961120
Value: o.Instance,
10971121
},
10981122
}
1123+
if customOpr.Component == "" {
1124+
customOpr.Component = o.ShardingName
1125+
}
10991126
customOpr.CreateOptions.Options = customOpr
11001127
cmdutil.CheckErr(customOpr.Run())
11011128
} else {

0 commit comments

Comments
 (0)