From 4ebf9ca607da06a9dc6ecfebe91c169d3edb89dc Mon Sep 17 00:00:00 2001 From: wangyelei Date: Tue, 23 Apr 2024 09:34:10 +0800 Subject: [PATCH 1/5] chore: support sharding opsRequest --- .../template/cluster_operations_template.cue | 88 +++++++++++++++++-- pkg/cmd/cluster/operations.go | 38 ++++---- 2 files changed, 101 insertions(+), 25 deletions(-) diff --git a/pkg/action/template/cluster_operations_template.cue b/pkg/action/template/cluster_operations_template.cue index 453f51785..e3e7d3137 100644 --- a/pkg/action/template/cluster_operations_template.cue +++ b/pkg/action/template/cluster_operations_template.cue @@ -27,6 +27,7 @@ options: { component: string instance: string componentNames: [...string] + shardingNames: [...string] rebuildInstanceFrom: [ ...{ componentName: string @@ -110,18 +111,32 @@ content: { name: vctName storage: options.storage }] - }] + }, + for _, sName in options.shardingNames { + shardingName: sName + volumeClaimTemplates: [ for _, vctName in options.vctNames { + name: vctName + storage: options.storage + }] + }] } if options.type == "HorizontalScaling" { horizontalScaling: [ for _, cName in options.componentNames { componentName: cName replicas: options.replicas - }] + }, + for _, sName in options.shardingNames { + shardingName: sName + replicas: options.replicas + }] } if options.type == "Restart" { restart: [ for _, cName in options.componentNames { componentName: cName - }] + }, + for _, sName in options.shardingNames { + shardingName: sName + }] } if options.type == "VerticalScaling" { verticalScaling: [ for _, cName in options.componentNames { @@ -142,7 +157,26 @@ content: { cpu: options.cpu } } - }] + }, + for _, sName in options.shardingNames { + shardingName: sName + requests: { + if options.memory != "" { + memory: options.memory + } + if options.cpu != "" { + cpu: options.cpu + } + } + limits: { + if options.memory != "" { + memory: options.memory + } + if options.cpu != "" { + cpu: options.cpu + } + } + }] } if options.type == "Reconfiguring" { if len(options.componentNames) == 1 { @@ -189,7 +223,28 @@ content: { } }] }] - }] + }, + for _, sName in options.shardingNames { + shardName: sName + configurations: [ { + name: options.cfgTemplateName + if options.forceRestart { + policy: "simple" + } + keys: [{ + key: options.cfgFile + if options.fileContent != "" { + fileContent: options.fileContent + } + if options.hasPatch { + parameters: [ for k, v in options.keyValues { + key: k + value: v + }] + } + }] + }] + }] } } if options.type == "Expose" { @@ -209,7 +264,24 @@ content: { } }] - }] + }, + for _, sName in options.shardingNames { + shardName: sName + if options.exposeEnabled == "true" { + switch: "Enable" + } + if options.exposeEnabled == "false" { + switch: "Disable" + } + services: [ for _, svc in options.services { + name: svc.name + serviceType: svc.serviceType + if len(svc.annotations) > 0 { + annotations: svc.annotations + } + + }] + }] } if options.type == "Switchover" { switchover: [{ @@ -233,9 +305,9 @@ content: { if options.type == "Custom" { customSpec: { opsDefinitionRef: options.opsDefinitionName - components: [ + items: [ { - name: options.component + componentName: options.component parameters: options.params } ] diff --git a/pkg/cmd/cluster/operations.go b/pkg/cmd/cluster/operations.go index 366a82ef4..c58ec545b 100755 --- a/pkg/cmd/cluster/operations.go +++ b/pkg/cmd/cluster/operations.go @@ -59,11 +59,12 @@ import ( const oceanbase = "oceanbase" type OperationsOptions struct { - action.CreateOptions `json:"-"` - HasComponentNamesFlag bool `json:"-"` + action.CreateOptions `json:"-"` + HasCompNamesOrShardingNamesFlag bool `json:"-"` // AutoApprove when set true, skip the double check. AutoApprove bool `json:"-"` ComponentNames []string `json:"componentNames,omitempty"` + ShardingNames []string `json:"shardingNames,omitempty"` OpsRequestName string `json:"opsRequestName"` TTLSecondsAfterSucceed int `json:"ttlSecondsAfterSucceed"` Force bool `json:"force"` @@ -117,7 +118,7 @@ type OperationsOptions struct { } func newBaseOperationsOptions(f cmdutil.Factory, streams genericiooptions.IOStreams, - opsType appsv1alpha1.OpsType, hasComponentNamesFlag bool) *OperationsOptions { + opsType appsv1alpha1.OpsType, hasCompNamesOrShardingNamesFlag bool) *OperationsOptions { customOutPut := func(opt *action.CreateOptions) { output := fmt.Sprintf("OpsRequest %s created successfully, you can view the progress:", opt.Name) printer.PrintLine(output) @@ -127,11 +128,11 @@ func newBaseOperationsOptions(f cmdutil.Factory, streams genericiooptions.IOStre o := &OperationsOptions{ // nil cannot be set to a map struct in CueLang, so init the map of KeyValues. - KeyValues: map[string]*string{}, - HasPatch: true, - OpsType: opsType, - HasComponentNamesFlag: hasComponentNamesFlag, - AutoApprove: false, + KeyValues: map[string]*string{}, + HasPatch: true, + OpsType: opsType, + HasCompNamesOrShardingNamesFlag: hasCompNamesOrShardingNamesFlag, + AutoApprove: false, CreateOptions: action.CreateOptions{ Factory: f, IOStreams: streams, @@ -155,8 +156,9 @@ func (o *OperationsOptions) addCommonFlags(cmd *cobra.Command, f cmdutil.Factory cmd.Flags().IntVar(&o.TTLSecondsAfterSucceed, "ttlSecondsAfterSucceed", 0, "Time to live after the OpsRequest succeed") cmd.Flags().StringVar(&o.DryRun, "dry-run", "none", `Must be "client", or "server". If with client strategy, only print the object that would be sent, and no data is actually sent. If with server strategy, submit the server-side request, but no data is persistent.`) cmd.Flags().Lookup("dry-run").NoOptDefVal = "unchanged" - if o.HasComponentNamesFlag { + if o.HasCompNamesOrShardingNamesFlag { flags.AddComponentsFlag(f, cmd, &o.ComponentNames, "Component names to this operations") + cmd.Flags().StringSliceVar(&o.ShardingNames, "shardings", nil, "sharding names to this operations") } } @@ -166,7 +168,7 @@ func (o *OperationsOptions) CompleteRestartOps() error { if o.Name == "" { return makeMissingClusterNameErr() } - if len(o.ComponentNames) != 0 { + if len(o.ComponentNames) != 0 || len(o.ShardingNames) != 0 { return nil } clusterObj, err := cluster.GetClusterByName(o.Dynamic, o.Name, o.Namespace) @@ -178,6 +180,9 @@ func (o *OperationsOptions) CompleteRestartOps() error { for i := range componentSpecs { o.ComponentNames[i] = componentSpecs[i].Name } + for i := range clusterObj.Spec.ShardingSpecs { + o.ShardingNames = append(o.ShardingNames, clusterObj.Spec.ShardingSpecs[i].Name) + } return nil } @@ -422,8 +427,8 @@ func (o *OperationsOptions) Validate() error { } // common validate for componentOps - if o.HasComponentNamesFlag && len(o.ComponentNames) == 0 { - return fmt.Errorf(`missing components, please specify the "--components" flag for multi-components cluster`) + if o.HasCompNamesOrShardingNamesFlag && len(o.ComponentNames) == 0 && len(o.ShardingNames) == 0 { + return fmt.Errorf(`missing components or shardings, please specify the "--components" or "--shardings" flag for the cluster`) } switch o.OpsType { @@ -696,7 +701,6 @@ func NewUpgradeCmd(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra o.addCommonFlags(cmd, f) cmd.Flags().StringVar(&o.ClusterVersionRef, "cluster-version", "", "Reference cluster version (required)") cmd.Flags().BoolVar(&o.AutoApprove, "auto-approve", false, "Skip interactive approval before upgrading the cluster") - _ = cmd.MarkFlagRequired("cluster-version") return cmd } @@ -726,7 +730,7 @@ func NewVerticalScalingCmd(f cmdutil.Factory, streams genericiooptions.IOStreams cmd.Flags().StringVar(&o.CPU, "cpu", "", "Request and limit size of component cpu") cmd.Flags().StringVar(&o.Memory, "memory", "", "Request and limit size of component memory") cmd.Flags().BoolVar(&o.AutoApprove, "auto-approve", false, "Skip interactive approval before vertically scaling the cluster") - _ = cmd.MarkFlagRequired("components") + cmd.MarkFlagsOneRequired("components", "shardings") return cmd } @@ -754,10 +758,10 @@ func NewHorizontalScalingCmd(f cmdutil.Factory, streams genericiooptions.IOStrea } o.addCommonFlags(cmd, f) - cmd.Flags().IntVar(&o.Replicas, "replicas", o.Replicas, "Replicas with the specified components") + cmd.Flags().IntVar(&o.Replicas, "replicas", 0, "Replicas with the specified components") cmd.Flags().BoolVar(&o.AutoApprove, "auto-approve", false, "Skip interactive approval before horizontally scaling the cluster") _ = cmd.MarkFlagRequired("replicas") - _ = cmd.MarkFlagRequired("components") + cmd.MarkFlagsOneRequired("components", "shardings") return cmd } @@ -789,7 +793,7 @@ func NewVolumeExpansionCmd(f cmdutil.Factory, streams genericiooptions.IOStreams cmd.Flags().BoolVar(&o.AutoApprove, "auto-approve", false, "Skip interactive approval before expanding the cluster volume") _ = cmd.MarkFlagRequired("volume-claim-templates") _ = cmd.MarkFlagRequired("storage") - _ = cmd.MarkFlagRequired("components") + cmd.MarkFlagsOneRequired("components", "shardings") return cmd } From 31ab9f5565e59fe1745da5f85e15d42db26e4bbd Mon Sep 17 00:00:00 2001 From: wangyelei Date: Tue, 23 Apr 2024 01:37:15 +0000 Subject: [PATCH 2/5] chore: auto update cli doc changes --- docs/user_docs/cli/kbcli_cluster_configure.md | 1 + docs/user_docs/cli/kbcli_cluster_edit-config.md | 1 + docs/user_docs/cli/kbcli_cluster_expose.md | 1 + docs/user_docs/cli/kbcli_cluster_hscale.md | 1 + docs/user_docs/cli/kbcli_cluster_restart.md | 1 + docs/user_docs/cli/kbcli_cluster_volume-expand.md | 1 + docs/user_docs/cli/kbcli_cluster_vscale.md | 1 + 7 files changed, 7 insertions(+) diff --git a/docs/user_docs/cli/kbcli_cluster_configure.md b/docs/user_docs/cli/kbcli_cluster_configure.md index 1e3489d36..edf95bd20 100644 --- a/docs/user_docs/cli/kbcli_cluster_configure.md +++ b/docs/user_docs/cli/kbcli_cluster_configure.md @@ -35,6 +35,7 @@ kbcli cluster configure NAME --set key=value[,key=value] [--components=component -o, --output format Prints the output in the specified format. Allowed values: JSON and YAML (default yaml) --replace Boolean flag to enable replacing config file. Default with false. --set strings Specify parameters list to be updated. For more details, refer to 'kbcli cluster describe-config'. + --shardings strings sharding names to this operations --ttlSecondsAfterSucceed int Time to live after the OpsRequest succeed ``` diff --git a/docs/user_docs/cli/kbcli_cluster_edit-config.md b/docs/user_docs/cli/kbcli_cluster_edit-config.md index 3bcea146f..32a7323fb 100644 --- a/docs/user_docs/cli/kbcli_cluster_edit-config.md +++ b/docs/user_docs/cli/kbcli_cluster_edit-config.md @@ -31,6 +31,7 @@ kbcli cluster edit-config NAME [--component=component-name] [--config-spec=confi -o, --output format Prints the output in the specified format. Allowed values: JSON and YAML (default yaml) --replace Boolean flag to enable replacing config file. Default with false. --set strings Specify parameters list to be updated. For more details, refer to 'kbcli cluster describe-config'. + --shardings strings sharding names to this operations --ttlSecondsAfterSucceed int Time to live after the OpsRequest succeed ``` diff --git a/docs/user_docs/cli/kbcli_cluster_expose.md b/docs/user_docs/cli/kbcli_cluster_expose.md index 2ec6d1065..a18574d5c 100644 --- a/docs/user_docs/cli/kbcli_cluster_expose.md +++ b/docs/user_docs/cli/kbcli_cluster_expose.md @@ -32,6 +32,7 @@ kbcli cluster expose NAME --enable=[true|false] --type=[vpc|internet] [flags] -h, --help help for expose --name string OpsRequest name. if not specified, it will be randomly generated -o, --output format Prints the output in the specified format. Allowed values: JSON and YAML (default yaml) + --shardings strings sharding names to this operations --ttlSecondsAfterSucceed int Time to live after the OpsRequest succeed --type string Expose type, currently supported types are 'vpc', 'internet' ``` diff --git a/docs/user_docs/cli/kbcli_cluster_hscale.md b/docs/user_docs/cli/kbcli_cluster_hscale.md index ee8f1c75c..ec2d09391 100644 --- a/docs/user_docs/cli/kbcli_cluster_hscale.md +++ b/docs/user_docs/cli/kbcli_cluster_hscale.md @@ -26,6 +26,7 @@ kbcli cluster hscale NAME [flags] --name string OpsRequest name. if not specified, it will be randomly generated -o, --output format Prints the output in the specified format. Allowed values: JSON and YAML (default yaml) --replicas int Replicas with the specified components + --shardings strings sharding names to this operations --ttlSecondsAfterSucceed int Time to live after the OpsRequest succeed ``` diff --git a/docs/user_docs/cli/kbcli_cluster_restart.md b/docs/user_docs/cli/kbcli_cluster_restart.md index 4f5f9af25..d26cf3361 100644 --- a/docs/user_docs/cli/kbcli_cluster_restart.md +++ b/docs/user_docs/cli/kbcli_cluster_restart.md @@ -28,6 +28,7 @@ kbcli cluster restart NAME [flags] -h, --help help for restart --name string OpsRequest name. if not specified, it will be randomly generated -o, --output format Prints the output in the specified format. Allowed values: JSON and YAML (default yaml) + --shardings strings sharding names to this operations --ttlSecondsAfterSucceed int Time to live after the OpsRequest succeed ``` diff --git a/docs/user_docs/cli/kbcli_cluster_volume-expand.md b/docs/user_docs/cli/kbcli_cluster_volume-expand.md index 42fed0c33..4bffb80ad 100644 --- a/docs/user_docs/cli/kbcli_cluster_volume-expand.md +++ b/docs/user_docs/cli/kbcli_cluster_volume-expand.md @@ -25,6 +25,7 @@ kbcli cluster volume-expand NAME [flags] -h, --help help for volume-expand --name string OpsRequest name. if not specified, it will be randomly generated -o, --output format Prints the output in the specified format. Allowed values: JSON and YAML (default yaml) + --shardings strings sharding names to this operations --storage string Volume storage size (required) --ttlSecondsAfterSucceed int Time to live after the OpsRequest succeed -t, --volume-claim-templates strings VolumeClaimTemplate names in components (required) diff --git a/docs/user_docs/cli/kbcli_cluster_vscale.md b/docs/user_docs/cli/kbcli_cluster_vscale.md index c254d9af7..0ba702175 100644 --- a/docs/user_docs/cli/kbcli_cluster_vscale.md +++ b/docs/user_docs/cli/kbcli_cluster_vscale.md @@ -27,6 +27,7 @@ kbcli cluster vscale NAME [flags] --memory string Request and limit size of component memory --name string OpsRequest name. if not specified, it will be randomly generated -o, --output format Prints the output in the specified format. Allowed values: JSON and YAML (default yaml) + --shardings strings sharding names to this operations --ttlSecondsAfterSucceed int Time to live after the OpsRequest succeed ``` From a85627c2f66dc350af18a5474c0853a723c7a1f3 Mon Sep 17 00:00:00 2001 From: wangyelei Date: Tue, 23 Apr 2024 10:03:47 +0800 Subject: [PATCH 3/5] chore: fix make test bug --- pkg/cmd/cluster/operations_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/cmd/cluster/operations_test.go b/pkg/cmd/cluster/operations_test.go index 39d9065ac..47b0b1da7 100644 --- a/pkg/cmd/cluster/operations_test.go +++ b/pkg/cmd/cluster/operations_test.go @@ -185,7 +185,7 @@ var _ = Describe("operations", func() { } o := initCommonOperationOps(appsv1alpha1.VolumeExpansionType, clusterName, true, persistentVolumeClaim) By("validate volumeExpansion when components is null") - Expect(o.Validate()).To(MatchError(`missing components, please specify the "--components" flag for multi-components cluster`)) + Expect(o.Validate()).To(MatchError(`missing components or shardings, please specify the "--components" or "--shardings" flag for multi-components cluster`)) By("validate volumeExpansion when vct-names is null") o.ComponentNames = []string{compName} From 114c649d3c5f4231fc36fb2b205b879b7ee6a366 Mon Sep 17 00:00:00 2001 From: wangyelei Date: Tue, 23 Apr 2024 10:04:46 +0800 Subject: [PATCH 4/5] chore: fix make test bug --- go.sum | 2 -- 1 file changed, 2 deletions(-) diff --git a/go.sum b/go.sum index 01f174dad..76c110fc0 100644 --- a/go.sum +++ b/go.sum @@ -272,8 +272,6 @@ github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be/go.mod h1:ySMOLuW github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230305170008-8188dc5388df h1:7RFfzj4SSt6nnvCPbCqijJi1nWCd+TqAT3bYCStRC18= github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230305170008-8188dc5388df/go.mod h1:pSwJ0fSY5KhvocuWSx4fz3BA8OrA1bQn+K1Eli3BRwM= -github.com/apecloud/kubebench v0.0.0-20240313105909-ba8654f654fc h1:BkFyEBEmDMdDhkWNAs9yg6zAj7xZjWON2u8b265T2a8= -github.com/apecloud/kubebench v0.0.0-20240313105909-ba8654f654fc/go.mod h1:5IZiDkFdgiZRGLsL+FOlvPsiF9LbyU55DVj4/5vT7+4= github.com/apecloud/kubebench v0.0.0-20240327101848-6a031d3f4ebe h1:OFkCAToRcNt0VqgCndw0mw1Ke3P2XgRJLrXsNOyc3sQ= github.com/apecloud/kubebench v0.0.0-20240327101848-6a031d3f4ebe/go.mod h1:5IZiDkFdgiZRGLsL+FOlvPsiF9LbyU55DVj4/5vT7+4= github.com/apecloud/kubeblocks v0.9.0-beta.1 h1:spCqfGH53vXVQhrvemZeSGJpx+D60TJ36PGVJvCXENQ= From 7e2e902bede9ffbaf36a36bf238c091ffbe4071c Mon Sep 17 00:00:00 2001 From: wangyelei Date: Tue, 23 Apr 2024 10:15:15 +0800 Subject: [PATCH 5/5] fix make test bug --- pkg/cmd/cluster/operations_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/cmd/cluster/operations_test.go b/pkg/cmd/cluster/operations_test.go index 47b0b1da7..f780a3bb4 100644 --- a/pkg/cmd/cluster/operations_test.go +++ b/pkg/cmd/cluster/operations_test.go @@ -185,7 +185,7 @@ var _ = Describe("operations", func() { } o := initCommonOperationOps(appsv1alpha1.VolumeExpansionType, clusterName, true, persistentVolumeClaim) By("validate volumeExpansion when components is null") - Expect(o.Validate()).To(MatchError(`missing components or shardings, please specify the "--components" or "--shardings" flag for multi-components cluster`)) + Expect(o.Validate()).To(MatchError(`missing components or shardings, please specify the "--components" or "--shardings" flag for the cluster`)) By("validate volumeExpansion when vct-names is null") o.ComponentNames = []string{compName}