From c8bdf25b1249346e3184f38c08f3be8f13816f1d Mon Sep 17 00:00:00 2001 From: mengyipeng <413183498@qq.com> Date: Thu, 29 Aug 2024 16:34:43 +0800 Subject: [PATCH 1/3] support create sharding cluster --- pkg/cmd/cluster/create_subcmds.go | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) 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 From d4e2c558efbcbc57b874855deb8c97d26059e4c8 Mon Sep 17 00:00:00 2001 From: mengyipeng <413183498@qq.com> Date: Mon, 2 Sep 2024 14:15:31 +0800 Subject: [PATCH 2/3] add ut --- pkg/cmd/cluster/create_subcmds_test.go | 50 +++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/pkg/cmd/cluster/create_subcmds_test.go b/pkg/cmd/cluster/create_subcmds_test.go index e84269f45..4262b8c16 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"} + + shardCmd.Flags().Set("mode", "cluster") + 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()) + }) }) From 7a668e9d1515436bf0bfc4b43c8151953da275d8 Mon Sep 17 00:00:00 2001 From: mengyipeng <413183498@qq.com> Date: Mon, 2 Sep 2024 14:23:29 +0800 Subject: [PATCH 3/3] fix lint --- pkg/cmd/cluster/create_subcmds_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/cmd/cluster/create_subcmds_test.go b/pkg/cmd/cluster/create_subcmds_test.go index 4262b8c16..8d2c62250 100644 --- a/pkg/cmd/cluster/create_subcmds_test.go +++ b/pkg/cmd/cluster/create_subcmds_test.go @@ -157,7 +157,7 @@ var _ = Describe("create cluster by cluster type", func() { fakeDiscovery1, _ := o.Client.Discovery().(*fakediscovery.FakeDiscovery) fakeDiscovery1.FakedServerVersion = &version.Info{Major: "1", Minor: "27", GitVersion: "v1.27.0"} - shardCmd.Flags().Set("mode", "cluster") + Expect(shardCmd.Flags().Set("mode", "cluster")).Should(Succeed()) Expect(o.complete(shardCmd)).Should(Succeed()) Expect(o.Name).ShouldNot(BeEmpty()) Expect(o.Values).ShouldNot(BeNil())