Skip to content

Commit

Permalink
chore: enable rbac by default for elasticsearch (#460) (#461)
Browse files Browse the repository at this point in the history
  • Loading branch information
iziang authored Oct 15, 2024
1 parent b291757 commit 15d1aac
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs/user_docs/cli/kbcli_cluster_create_elasticsearch.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/cluster/create_subcmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 24 additions & 1 deletion pkg/cmd/cluster/create_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}

Expand Down Expand Up @@ -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 := `
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/cluster/create_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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())
})

Expand Down

0 comments on commit 15d1aac

Please sign in to comment.