Skip to content

Commit

Permalink
fix: support create sharding cluster (#435)
Browse files Browse the repository at this point in the history
  • Loading branch information
yipeng1030 authored Sep 2, 2024
1 parent 0657641 commit 4874b3a
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 5 deletions.
18 changes: 14 additions & 4 deletions pkg/cmd/cluster/create_subcmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,22 +133,32 @@ 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 {
o.ChartInfo.ComponentDef = append(o.ChartInfo.ComponentDef, compDef.(string))
}
}
}
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
Expand Down
50 changes: 49 additions & 1 deletion pkg/cmd/cluster/create_subcmds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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())
})
})

0 comments on commit 4874b3a

Please sign in to comment.