Skip to content

Commit

Permalink
refactor clusterOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
sophon-zt committed Jun 9, 2023
1 parent 8a5e924 commit ae269f6
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ function set_network() {
sysctl_set_keyvalue "net.bridge.bridge-nf-call-arptables" "1"
sysctl_set_keyvalue "net.bridge.bridge-nf-call-ip6tables" "1"
sysctl_set_keyvalue "net.bridge.bridge-nf-call-iptables" "1"
# for node port
sysctl_set_keyvalue "net.ipv4.ip_local_reserved_ports" "30000-32767"

echo
Expand Down Expand Up @@ -132,10 +133,11 @@ install_ipvs
install_hosts
common_os_setting

# for es/opensearch
sysctl_set_keyvalue "vm.max_map_count" "262144"
sysctl_set_keyvalue "vm.swappiness" "1"
# for kubeblocks
sysctl_set_keyvalue "fs.inotify.max_user_watches" "524288"
sysctl_set_keyvalue "fs.inotify.max_user_instances" "524288"
sysctl_set_keyvalue "kernel.pid_max" "65535"
sysctl -p

# Make sure the iptables utility doesn't use the nftables backend.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ spec:
- name: {{ .Name }}
address: {{ .Address }}
internalAddress: {{ .InternalAddress }}
user: {{ $.User }}
password: {{ $.Password }}
privateKey: {{ $.PrivateKey | quote }}
user: {{ $.User.Name }}
password: {{ $.User.Password }}
privateKey: {{ $.User.PrivateKey | quote }}
timeout: {{ $.Timeout }}
{{- end }}
roleGroups:
Expand Down
92 changes: 56 additions & 36 deletions internal/cli/cmd/infrastructure/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"strings"

"github.com/StudioSol/set"
"github.com/apecloud/kubeblocks/internal/cli/cmd/infrastructure/builder"
"github.com/apecloud/kubeblocks/internal/cli/printer"
"github.com/apecloud/kubeblocks/internal/cli/util/prompt"
kubekeyapiv1alpha2 "github.com/kubesphere/kubekey/v3/cmd/kk/apis/kubekey/v1alpha2"
Expand All @@ -41,33 +42,30 @@ import (
)

type clusterOptions struct {
types.Cluster
IOStreams genericclioptions.IOStreams

clusterName string
nodes []string
cluster types.Cluster

timeout int64
userName string
password string
privateKey string
privateKeyPath string
clusterConfig string
clusterName string
nodes []string
timeout int64
}

func buildCommonFlags(cmd *cobra.Command, o *clusterOptions) {
cmd.Flags().StringVarP(&o.clusterConfig, "config", "c", "", "Specify infra cluster config file. [option]")
cmd.Flags().StringVarP(&o.clusterName, "name", "", "", "Specify kubernetes cluster name")
cmd.Flags().StringSliceVarP(&o.nodes, "nodes", "", nil, "List of machines on which kubernetes is installed. [require]")

// for user
cmd.Flags().StringVarP(&o.userName, "user", "u", "", "Specify the account to access the remote server. [require]")
cmd.Flags().StringVarP(&o.User.Name, "user", "u", "", "Specify the account to access the remote server. [require]")
cmd.Flags().Int64VarP(&o.timeout, "timeout", "t", 30, "Specify the ssh timeout.[option]")
cmd.Flags().StringVarP(&o.password, "password", "p", "", "Specify the password for the account to execute sudo. [option]")
cmd.Flags().StringVarP(&o.privateKey, "private-key", "", "", "The PrimaryKey for ssh to the remote machine. [option]")
cmd.Flags().StringVarP(&o.privateKeyPath, "private-key-path", "", "", "Specify the file PrimaryKeyPath of ssh to the remote machine. default ~/.ssh/id_rsa.")
cmd.Flags().StringVarP(&o.User.Password, "password", "p", "", "Specify the password for the account to execute sudo. [option]")
cmd.Flags().StringVarP(&o.User.PrivateKey, "private-key", "", "", "The PrimaryKey for ssh to the remote machine. [option]")
cmd.Flags().StringVarP(&o.User.PrivateKeyPath, "private-key-path", "", "", "Specify the file PrimaryKeyPath of ssh to the remote machine. default ~/.ssh/id_rsa.")

cmd.Flags().StringSliceVarP(&o.cluster.ETCD, "etcd", "", nil, "Specify etcd nodes")
cmd.Flags().StringSliceVarP(&o.cluster.Master, "master", "", nil, "Specify master nodes")
cmd.Flags().StringSliceVarP(&o.cluster.Worker, "worker", "", nil, "Specify worker nodes")
cmd.Flags().StringSliceVarP(&o.ETCD, "etcd", "", nil, "Specify etcd nodes")
cmd.Flags().StringSliceVarP(&o.Master, "master", "", nil, "Specify master nodes")
cmd.Flags().StringSliceVarP(&o.Worker, "worker", "", nil, "Specify worker nodes")
}

func (o *clusterOptions) Complete() error {
Expand All @@ -76,30 +74,34 @@ func (o *clusterOptions) Complete() error {
fmt.Printf("The cluster name is not set, auto generate cluster name: %s\n", o.clusterName)
}

if o.userName == "" {
if o.clusterConfig != "" {
return o.validateClusterConfig(o.clusterConfig)
}

if o.User.Name == "" {
currentUser, err := user.Current()
if err != nil {
return err
}
o.userName = currentUser.Username
fmt.Printf("The user is not set, use current user %s\n", o.userName)
o.User.Name = currentUser.Username
fmt.Printf("The user is not set, use current user %s\n", o.User.Name)
}
if o.privateKey == "" {
if o.User.PrivateKey == "" {
home, err := os.UserHomeDir()
if err != nil {
return err
}
if o.privateKeyPath == "" && o.password == "" {
o.privateKeyPath = filepath.Join(home, ".ssh", "id_rsa")
if o.User.PrivateKeyPath == "" && o.User.Password == "" {
o.User.PrivateKeyPath = filepath.Join(home, ".ssh", "id_rsa")
}
if strings.HasPrefix(o.privateKeyPath, "~/") {
o.privateKeyPath = filepath.Join(home, o.privateKeyPath[2:])
if strings.HasPrefix(o.User.PrivateKeyPath, "~/") {
o.User.PrivateKeyPath = filepath.Join(home, o.User.PrivateKeyPath[2:])
}
}
if len(o.nodes) == 0 {
return cfgcore.MakeError("The list of machines where kubernetes is installed must be specified.")
}
o.cluster.Nodes = make([]types.ClusterNode, len(o.nodes))
o.Nodes = make([]types.ClusterNode, len(o.nodes))
for i, node := range o.nodes {
fields := strings.SplitN(node, ":", 3)
if len(fields) < 2 {
Expand All @@ -113,14 +115,14 @@ func (o *clusterOptions) Complete() error {
if len(fields) == 3 {
n.InternalAddress = fields[2]
}
o.cluster.Nodes[i] = n
o.Nodes[i] = n
}
return nil
}

func (o *clusterOptions) Validate() error {
checkFn := func(n string) bool {
for _, node := range o.cluster.Nodes {
for _, node := range o.Nodes {
if node.Name == n {
return true
}
Expand All @@ -140,29 +142,29 @@ func (o *clusterOptions) Validate() error {
}
return nil
}
if o.userName == "" {
if o.User.Name == "" {
return cfgcore.MakeError("user name is empty")
}
if o.privateKey == "" && o.privateKeyPath != "" {
if _, err := os.Stat(o.privateKeyPath); err != nil {
if o.User.PrivateKey == "" && o.User.PrivateKeyPath != "" {
if _, err := os.Stat(o.User.PrivateKeyPath); err != nil {
return err
}
b, err := os.ReadFile(o.privateKeyPath)
b, err := os.ReadFile(o.User.PrivateKeyPath)
if err != nil {
return err
}
o.privateKey = string(b)
o.User.PrivateKey = string(b)
}
if len(o.cluster.ETCD) == 0 || len(o.cluster.Master) == 0 || len(o.cluster.Worker) == 0 {
if len(o.ETCD) == 0 || len(o.Master) == 0 || len(o.Worker) == 0 {
return cfgcore.MakeError("etcd, master or worker is empty")
}
if err := validateNodes(o.cluster.ETCD); err != nil {
if err := validateNodes(o.ETCD); err != nil {
return err
}
if err := validateNodes(o.cluster.Master); err != nil {
if err := validateNodes(o.Master); err != nil {
return err
}
if err := validateNodes(o.cluster.Worker); err != nil {
if err := validateNodes(o.Worker); err != nil {
return err
}
return nil
Expand Down Expand Up @@ -202,3 +204,21 @@ func (o *clusterOptions) confirm(promptStr string) (bool, error) {
}
return strings.ToLower(input) == yesStr, nil
}

func (o *clusterOptions) validateClusterConfig(configFile string) error {
_, err := os.Stat(configFile)
if err != nil {
return err
}
b, err := os.ReadFile(configFile)
if err != nil {
return err
}

c, err := builder.BuildResourceFromYaml(o.Cluster, string(b))
if err != nil {
return err
}
o.Cluster = *c
return nil
}
12 changes: 5 additions & 7 deletions internal/cli/cmd/infrastructure/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,14 @@ type deleteOptions struct {
func (o *deleteOptions) Run() error {
cluster, err := createClusterWithOptions(&gotemplate.TplValues{
builtinClusterNameObject: o.clusterName,
builtinUserObject: o.userName,
builtinClusterVersionObject: "0.0.0",
builtinPasswordObject: o.password,
builtinPrivateKeyObject: o.privateKey,
builtinHostsObject: o.cluster.Nodes,
builtinUserObject: o.User,
builtinHostsObject: o.Nodes,
builtinTimeoutObject: o.timeout,
builtinRoleGroupsObject: gotemplate.TplValues{
common.ETCD: o.cluster.ETCD,
common.Master: o.cluster.Master,
common.Worker: o.cluster.Worker,
common.ETCD: o.ETCD,
common.Master: o.Master,
common.Worker: o.Worker,
},
})
if err != nil {
Expand Down
14 changes: 5 additions & 9 deletions internal/cli/cmd/infrastructure/loader_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ const (
builtinClusterVersionObject = "Version"
builtinCRITypeObject = "CRIType"
builtinUserObject = "User"
builtinPasswordObject = "Password"
builtinPrivateKeyObject = "PrivateKey"
builtinHostsObject = "Hosts"
builtinRoleGroupsObject = "RoleGroups"
)
Expand All @@ -60,15 +58,13 @@ func buildTemplateParams(o *createOptions) *gotemplate.TplValues {
builtinClusterNameObject: o.clusterName,
builtinClusterVersionObject: o.version.KubernetesVersion,
builtinCRITypeObject: o.criType,
builtinUserObject: o.userName,
builtinPasswordObject: o.password,
builtinPrivateKeyObject: o.privateKey,
builtinHostsObject: o.cluster.Nodes,
builtinUserObject: o.User,
builtinHostsObject: o.Nodes,
builtinTimeoutObject: o.timeout,
builtinRoleGroupsObject: gotemplate.TplValues{
common.ETCD: o.cluster.ETCD,
common.Master: o.cluster.Master,
common.Worker: o.cluster.Worker,
common.ETCD: o.ETCD,
common.Master: o.Master,
common.Worker: o.Worker,
},
}
}
8 changes: 5 additions & 3 deletions internal/cli/cmd/infrastructure/loader_adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ func TestCreateClusterWithOptions(t *testing.T) {
criType: string(container.ContainerdType),
clusterOptions: clusterOptions{
clusterName: "for_test",
userName: "test",
password: "test",
cluster: types.Cluster{
Cluster: types.Cluster{
Nodes: []types.ClusterNode{
{
Name: "node1",
Expand All @@ -55,6 +53,10 @@ func TestCreateClusterWithOptions(t *testing.T) {
InternalAddress: "127.0.0.2",
},
},
User: types.ClusterUser{
Name: "test",
Password: "test",
},
ETCD: []string{"node1"},
Master: []string{"node1"},
Worker: []string{"node2"},
Expand Down
7 changes: 4 additions & 3 deletions internal/cli/cmd/infrastructure/types/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ type InfraVersionInfo struct {
}

type Cluster struct {
User ClusterUser
Nodes []ClusterNode
User ClusterUser `json:"user"`
Nodes []ClusterNode `json:"nodes"`

ETCD []string `json:"etcd"`
Master []string `json:"master"`
Expand All @@ -50,5 +50,6 @@ type ClusterUser struct {
// sudo password
Password string `json:"password"`
// ssh privateKey
PrivateKey string `json:"privateKey"`
PrivateKey string `json:"privateKey"`
PrivateKeyPath string `json:"privateKeyPath"`
}
71 changes: 0 additions & 71 deletions internal/cli/create/template/kubekey_cluster_template.cue

This file was deleted.

0 comments on commit ae269f6

Please sign in to comment.