From 15d1aac18e0c302a97943e34fa73269f9f306859 Mon Sep 17 00:00:00 2001 From: Ziang Guo Date: Tue, 15 Oct 2024 14:25:10 +0800 Subject: [PATCH] chore: enable rbac by default for elasticsearch (#460) (#461) --- .../cli/kbcli_cluster_create_elasticsearch.md | 2 +- pkg/cmd/cluster/create_subcmds.go | 2 +- pkg/cmd/cluster/create_util.go | 25 ++++++++++++++++++- pkg/cmd/cluster/create_util_test.go | 4 +-- 4 files changed, 28 insertions(+), 5 deletions(-) diff --git a/docs/user_docs/cli/kbcli_cluster_create_elasticsearch.md b/docs/user_docs/cli/kbcli_cluster_create_elasticsearch.md index d21b00bc9..aea986bac 100644 --- a/docs/user_docs/cli/kbcli_cluster_create_elasticsearch.md +++ b/docs/user_docs/cli/kbcli_cluster_create_elasticsearch.md @@ -34,7 +34,7 @@ kbcli cluster create elasticsearch NAME [flags] -o, --output format Prints the output in the specified format. Allowed values: JSON and YAML (default yaml) --pod-anti-affinity string Pod anti-affinity type, one of: (Preferred, Required) (default "Preferred") --publicly-accessible Specify whether the cluster can be accessed from the public internet. - --rbac-enabled Specify whether rbac resources will be created by client, otherwise KubeBlocks server will try to create rbac resources. + --rbac-enabled Specify whether rbac resources will be created by client, otherwise KubeBlocks server will try to create rbac resources. (default true) --replicas int The number of replicas, for single-node mode, the replicas is 1, for multi-node mode, the default replicas is 3. Value range [1, 5]. (default 1) --storage float Storage size, the unit is Gi. Value range [1, 10000]. (default 20) --tenancy string The tenancy of cluster. Legal values [SharedNode, DedicatedNode]. (default "SharedNode") diff --git a/pkg/cmd/cluster/create_subcmds.go b/pkg/cmd/cluster/create_subcmds.go index 5eed09469..1fb5c86d4 100644 --- a/pkg/cmd/cluster/create_subcmds.go +++ b/pkg/cmd/cluster/create_subcmds.go @@ -117,7 +117,7 @@ func buildCreateSubCmds(createOptions *action.CreateOptions) []*cobra.Command { // TODO: support set backup config? // add flags from chart values.schema.json - util.CheckErr(addCreateFlags(cmd, o.Factory, o.ChartInfo)) + util.CheckErr(addCreateFlags(cmd, o.Factory, o.ChartInfo, t.String())) // Schedule policy // TODO: implement them, and check whether the flag has been defined diff --git a/pkg/cmd/cluster/create_util.go b/pkg/cmd/cluster/create_util.go index 7a0c32267..e52a86a45 100644 --- a/pkg/cmd/cluster/create_util.go +++ b/pkg/cmd/cluster/create_util.go @@ -37,8 +37,16 @@ import ( "github.com/apecloud/kbcli/pkg/util/flags" ) +var ( + resetEngineFlagValues = map[string]map[string]string{ + "elasticsearch": { + "rbac-enabled": "true", + }, + } +) + // addCreateFlags adds the flags for creating a cluster, these flags are built by the cluster schema. -func addCreateFlags(cmd *cobra.Command, f cmdutil.Factory, c *cluster.ChartInfo) error { +func addCreateFlags(cmd *cobra.Command, f cmdutil.Factory, c *cluster.ChartInfo, engine string) error { if c == nil { return nil } @@ -53,6 +61,8 @@ func addCreateFlags(cmd *cobra.Command, f cmdutil.Factory, c *cluster.ChartInfo) return err } + // reset engine related flags default value, such as rbac-enabled for elasticsearch should be true by default + resetEngineDefaultFlagsValue(cmd.Flags(), engine) return nil } @@ -87,6 +97,19 @@ func getValuesFromFlags(fs *flag.FlagSet) map[string]interface{} { return values } +func resetEngineDefaultFlagsValue(fs *flag.FlagSet, engine string) { + kvs, ok := resetEngineFlagValues[engine] + if !ok { + return + } + fs.VisitAll(func(f *flag.Flag) { + if v, ok := kvs[f.Name]; ok { + f.DefValue = v + _ = f.Value.Set(v) + } + }) +} + // buildCreateSubCmdsExamples builds the creation examples for the specified ClusterType type. func buildCreateSubCmdsExamples(t cluster.ClusterType) string { exampleTpl := ` diff --git a/pkg/cmd/cluster/create_util_test.go b/pkg/cmd/cluster/create_util_test.go index c81d38e1e..236aae77e 100644 --- a/pkg/cmd/cluster/create_util_test.go +++ b/pkg/cmd/cluster/create_util_test.go @@ -52,7 +52,7 @@ var _ = Describe("cluster create util", func() { }) It("add create flags for a nil schema", func() { - Expect(addCreateFlags(cmd, tf, nil)).Should(Succeed()) + Expect(addCreateFlags(cmd, tf, nil, "")).Should(Succeed()) }) It("add create flags for a not-nil schema", func() { @@ -61,7 +61,7 @@ var _ = Describe("cluster create util", func() { Expect(err).Should(Succeed()) Expect(c.Schema).ShouldNot(BeNil()) - Expect(addCreateFlags(cmd, tf, c)).Should(Succeed()) + Expect(addCreateFlags(cmd, tf, c, "")).Should(Succeed()) Expect(cmd.Flags().Lookup("version")).ShouldNot(BeNil()) })