diff --git a/pkg/cmd/cluster/create_subcmds.go b/pkg/cmd/cluster/create_subcmds.go index dc8407ba5..c89c75cc6 100644 --- a/pkg/cmd/cluster/create_subcmds.go +++ b/pkg/cmd/cluster/create_subcmds.go @@ -133,10 +133,10 @@ func (o *CreateSubCmdsOptions) complete(cmd *cobra.Command) error { if !ok { return fmt.Errorf("cannot find spec in cluster object") } + if o.ChartInfo.ComponentDef == nil { + o.ChartInfo.ComponentDef = []string{} + } if compSpec, ok := spec["componentSpecs"].([]interface{}); ok { - if o.ChartInfo.ComponentDef == nil { - o.ChartInfo.ComponentDef = []string{} - } for i := range compSpec { comp := compSpec[i].(map[string]interface{}) if compDef, ok := comp["componentDef"]; ok { @@ -144,11 +144,21 @@ func (o *CreateSubCmdsOptions) complete(cmd *cobra.Command) error { } } } + if shardingSpec, ok := spec["shardingSpecs"].([]interface{}); ok { + for i := range shardingSpec { + shard := shardingSpec[i].(map[string]interface{}) + if compSpec, ok := shard["template"].(map[string]interface{}); ok { + if compDef, ok := compSpec["componentDef"]; ok { + o.ChartInfo.ComponentDef = append(o.ChartInfo.ComponentDef, compDef.(string)) + } + } + } + } if clusterDef, ok := spec["clusterDefinitionRef"].(string); ok { o.ChartInfo.ClusterDef = clusterDef } if o.ChartInfo.ClusterDef == "" && len(o.ChartInfo.ComponentDef) == 0 { - return fmt.Errorf("cannot find clusterDefinitionRef in cluster spec or componentDef in componentSpecs") + return fmt.Errorf("cannot find clusterDefinitionRef in cluster spec or componentDef in componentSpecs or shardingSpecs") } return nil diff --git a/pkg/cmd/cluster/create_subcmds_test.go b/pkg/cmd/cluster/create_subcmds_test.go index e84269f45..8d2c62250 100644 --- a/pkg/cmd/cluster/create_subcmds_test.go +++ b/pkg/cmd/cluster/create_subcmds_test.go @@ -47,7 +47,9 @@ import ( var _ = Describe("create cluster by cluster type", func() { const ( - clusterType = "apecloud-mysql" + clusterType = "apecloud-mysql" + redisCluster = "redis" + redisComponent = "redis-cluster-7" ) var ( @@ -126,4 +128,50 @@ var _ = Describe("create cluster by cluster type", func() { fakeDiscovery.FakedServerVersion = &version.Info{Major: "1", Minor: "27", GitVersion: "v1.27.0"} Expect(o.Run()).Should(Succeed()) }) + + It("create sharding cluster command", func() { + By("create commands") + cmds := buildCreateSubCmds(createOptions) + Expect(cmds).ShouldNot(BeNil()) + Expect(cmds[0].HasFlags()).Should(BeTrue()) + + By("create command options") + o, err := NewSubCmdsOptions(createOptions, redisCluster) + Expect(err).Should(Succeed()) + Expect(o).ShouldNot(BeNil()) + Expect(o.ChartInfo).ShouldNot(BeNil()) + + By("complete") + var shardCmd *cobra.Command + for _, c := range cmds { + if c.Name() == redisCluster { + shardCmd = c + break + } + } + + o.Format = printer.YAML + Expect(o.CreateOptions.Complete()).Should(Succeed()) + o.DryRun = "client" + o.Client = testing.FakeClientSet() + fakeDiscovery1, _ := o.Client.Discovery().(*fakediscovery.FakeDiscovery) + fakeDiscovery1.FakedServerVersion = &version.Info{Major: "1", Minor: "27", GitVersion: "v1.27.0"} + + Expect(shardCmd.Flags().Set("mode", "cluster")).Should(Succeed()) + Expect(o.complete(shardCmd)).Should(Succeed()) + Expect(o.Name).ShouldNot(BeEmpty()) + Expect(o.Values).ShouldNot(BeNil()) + Expect(o.ChartInfo.ComponentDef[0]).Should(Equal(redisComponent)) + + By("validate") + o.Dynamic = testing.FakeDynamicClient() + Expect(o.validate()).Should(Succeed()) + + By("run") + o.DryRun = "client" + o.Client = testing.FakeClientSet() + fakeDiscovery, _ := o.Client.Discovery().(*fakediscovery.FakeDiscovery) + fakeDiscovery.FakedServerVersion = &version.Info{Major: "1", Minor: "27", GitVersion: "v1.27.0"} + Expect(o.Run()).Should(Succeed()) + }) })