Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimization: Categorize cmd modules by type #1680

Merged
merged 6 commits into from
Sep 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions cmd/sealer/cmd/alpha/cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,20 @@ import (
"github.com/sealerio/sealer/pkg/clusterfile"
)

var altNames []string

var longCertCmdDescription = `This command will add the new domain or IP address in cert to update cluster API server.

sealer has some default domain and IP in the cert process builtin: localhost,outbound IP address and some DNS domain which is strongly related to the apiserver CertSANs configured by kubeadm.yml.

You need to restart your API server manually after using sealer alpha cert. Then, you can using cmd "openssl x509 -noout -text -in apiserver.crt" to check the cert details.
`

var exampleForCertCmd = `
The following command will generate new api server cert and key for all control-plane certificates:

sealer alpha cert --alt-names 39.105.169.253,sealer.cool
`

var altNames []string

func NewCertCmd() *cobra.Command {
certCmd := &cobra.Command{
Use: "cert",
Expand Down
8 changes: 3 additions & 5 deletions cmd/sealer/cmd/alpha/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,10 @@ import (
)

var (
clusterName string
roles []string
clusterName string
roles []string
longExecCmdDescription = `Using sealer builtin ssh client to run shell command on the node filtered by cluster and cluster role. it is convenient for cluster administrator to do quick investigate`
)

var longExecCmdDescription = `Using sealer builtin ssh client to run shell command on the node filtered by cluster and cluster role. it is convenient for cluster administrator to do quick investigate`

var exampleForExecCmd = `
Exec the default cluster node:
sealer alpha exec "cat /etc/hosts"
Expand Down
1 change: 0 additions & 1 deletion cmd/sealer/cmd/alpha/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ var exampleForGenCmd = `The following command will generate Clusterfile used by

sealer alpha gen --passwd xxxx --image kubernetes:v1.19.8
`

var longGenCmdDescription = `Sealer will call kubernetes API to get masters and nodes IP info, then generate a Clusterfile. and also pull a ClusterImage which matches the kubernetes version.

Then you can use any sealer command to manage the cluster like:
Expand Down
5 changes: 2 additions & 3 deletions cmd/sealer/cmd/alpha/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@ import (
"github.com/spf13/cobra"
)

var upgradeClusterName string

var exampleForUpgradeCmd = `The following command will upgrade the current cluster to kubernetes:v1.19.9
sealer alpha upgrade kubernetes:v1.19.9
`

var longUpgradeCmdDescription = `Sealer upgrade command will upgrade the current cluster to the specified version with the ClusterImage using kubeadm upgrade
`

var upgradeClusterName string

// NewUpgradeCmd implement the sealer upgrade command
func NewUpgradeCmd() *cobra.Command {
upgradeCmd := &cobra.Command{
Expand Down
39 changes: 20 additions & 19 deletions cmd/sealer/cmd/apply.go → cmd/sealer/cmd/cluster/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd
package cluster

import (
"github.com/sealerio/sealer/pkg/runtime/kubernetes"
Expand All @@ -23,26 +23,27 @@ import (

var clusterFile string

// applyCmd represents the apply command
var applyCmd = &cobra.Command{
Use: "apply",
Short: "apply a Kubernetes cluster via specified Clusterfile",
Long: `apply command is used to apply a Kubernetes cluster via specified Clusterfile.
var longNewApplyCmdDescription = `apply command is used to apply a Kubernetes cluster via specified Clusterfile.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const

If the Clusterfile is applied first time, Kubernetes cluster will be created. Otherwise, sealer
will apply the diff change of current Clusterfile and the original one.`,
Example: `sealer apply -f Clusterfile`,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
applier, err := apply.NewApplierFromFile(clusterFile)
if err != nil {
return err
}
return applier.Apply()
},
}
will apply the diff change of current Clusterfile and the original one.`

func init() {
rootCmd.AddCommand(applyCmd)
// NewApplyCmd applyCmd represents the apply command
func NewApplyCmd() *cobra.Command {
applyCmd := &cobra.Command{
Use: "apply",
Short: "apply a Kubernetes cluster via specified Clusterfile",
Long: longNewApplyCmdDescription,
Example: `sealer apply -f Clusterfile`,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
applier, err := apply.NewApplierFromFile(clusterFile)
if err != nil {
return err
}
return applier.Apply()
},
}
applyCmd.Flags().StringVarP(&clusterFile, "Clusterfile", "f", "Clusterfile", "Clusterfile path to apply a Kubernetes cluster")
applyCmd.Flags().BoolVar(&kubernetes.ForceDelete, "force", false, "force to delete the specified cluster if set true")
return applyCmd
}
48 changes: 25 additions & 23 deletions cmd/sealer/cmd/check.go → cmd/sealer/cmd/cluster/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd
package cluster

import (
"fmt"
Expand All @@ -29,29 +29,31 @@ type CheckArgs struct {

var checkArgs *CheckArgs

// pushCmd represents the push command
var checkCmd = &cobra.Command{
Use: "check",
Short: "check the state of cluster",
Long: `check command is used to check status of the cluster, including node status
, service status and pod status.`,
Example: `sealer check --pre or sealer check --post`,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
if checkArgs.Pre && checkArgs.Post {
return fmt.Errorf("don't allow to set two flags --pre and --post")
}
list := []checker.Interface{checker.NewNodeChecker(), checker.NewSvcChecker(), checker.NewPodChecker()}
if checkArgs.Pre {
return checker.RunCheckList(list, nil, checker.PhasePre)
}
return checker.RunCheckList(list, nil, checker.PhasePost)
},
}

func init() {
var longNewCheckCmdDescription = `check command is used to check status of the cluster, including node status
, service status and pod status.`

// NewCheckCmd pushCmd represents the push command
func NewCheckCmd() *cobra.Command {
checkCmd := &cobra.Command{
Use: "check",
Short: "check the state of cluster",
Long: longNewCheckCmdDescription,
Example: `sealer check --pre
sealer check --post`,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
if checkArgs.Pre && checkArgs.Post {
return fmt.Errorf("don't allow to set two flags --pre and --post")
}
list := []checker.Interface{checker.NewNodeChecker(), checker.NewSvcChecker(), checker.NewPodChecker()}
if checkArgs.Pre {
return checker.RunCheckList(list, nil, checker.PhasePre)
}
return checker.RunCheckList(list, nil, checker.PhasePost)
},
}
checkArgs = &CheckArgs{}
rootCmd.AddCommand(checkCmd)
checkCmd.Flags().BoolVar(&checkArgs.Pre, "pre", false, "Check dependencies before cluster creation")
checkCmd.Flags().BoolVar(&checkArgs.Post, "post", false, "Check the status of the cluster after it is created")
return checkCmd
}
23 changes: 23 additions & 0 deletions cmd/sealer/cmd/cluster/cluster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright © 2021 Alibaba Group Holding Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cluster

import "github.com/spf13/cobra"

func NewClusterCommands() []*cobra.Command {
var clusterCommands []*cobra.Command
clusterCommands = append(clusterCommands, NewApplyCmd(), NewCheckCmd(), NewDeleteCmd(), NewJoinCmd(), NewRunCmd())
return clusterCommands
}
97 changes: 50 additions & 47 deletions cmd/sealer/cmd/delete.go → cmd/sealer/cmd/cluster/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd
package cluster

import (
"fmt"
Expand All @@ -32,72 +32,75 @@ var (
deleteClusterName string
)

// deleteCmd represents the delete command
var deleteCmd = &cobra.Command{
Use: "delete",
Short: "delete an existing cluster",
Long: `delete command is used to delete part or all of existing cluster.
User can delete cluster by explicitly specifying node IP, Clusterfile, or cluster name.`,
Args: cobra.NoArgs,
Example: `
var longDeleteCmdDescription = `delete command is used to delete part or all of existing cluster.
User can delete cluster by explicitly specifying node IP, Clusterfile, or cluster name.`

var exampleForDeleteCmd = `
delete default cluster:
sealer delete --masters x.x.x.x --nodes x.x.x.x
sealer delete --masters x.x.x.x-x.x.x.y --nodes x.x.x.x-x.x.x.y
delete all:
sealer delete --all [--force]
sealer delete -f /root/.sealer/mycluster/Clusterfile [--force]
sealer delete -c my-cluster [--force]
`,
RunE: func(cmd *cobra.Command, args []string) error {
all, err := cmd.Flags().GetBool("all")
if err != nil {
return err
}
if deleteClusterName == "" && deleteClusterFile == "" {
if !all && deleteArgs.Masters == "" && deleteArgs.Nodes == "" {
return fmt.Errorf("the delete parameter needs to be set")
}
deleteClusterName, err = clusterfile.GetDefaultClusterName()
if err == clusterfile.ErrClusterNotExist {
fmt.Println("Find no exist cluster, skip delete")
return nil
}
`

// NewDeleteCmd deleteCmd represents the delete command
func NewDeleteCmd() *cobra.Command {
deleteCmd := &cobra.Command{
Use: "delete",
Short: "delete an existing cluster",
Long: longDeleteCmdDescription,
Args: cobra.NoArgs,
Example: exampleForDeleteCmd,
RunE: func(cmd *cobra.Command, args []string) error {
all, err := cmd.Flags().GetBool("all")
if err != nil {
return err
}
deleteClusterFile = common.GetClusterWorkClusterfile(deleteClusterName)
} else if deleteClusterName != "" && deleteClusterFile != "" {
tmpClusterfile := common.GetClusterWorkClusterfile(deleteClusterName)
if tmpClusterfile != deleteClusterFile {
return fmt.Errorf("arguments error:%s and %s refer to different clusters", deleteClusterFile, tmpClusterfile)
if deleteClusterName == "" && deleteClusterFile == "" {
if !all && deleteArgs.Masters == "" && deleteArgs.Nodes == "" {
return fmt.Errorf("the delete parameter needs to be set")
}
deleteClusterName, err = clusterfile.GetDefaultClusterName()
if err == clusterfile.ErrClusterNotExist {
fmt.Println("Find no exist cluster, skip delete")
return nil
}
if err != nil {
return err
}
deleteClusterFile = common.GetClusterWorkClusterfile(deleteClusterName)
} else if deleteClusterName != "" && deleteClusterFile != "" {
tmpClusterfile := common.GetClusterWorkClusterfile(deleteClusterName)
if tmpClusterfile != deleteClusterFile {
return fmt.Errorf("arguments error:%s and %s refer to different clusters", deleteClusterFile, tmpClusterfile)
}
} else if deleteClusterFile == "" {
deleteClusterFile = common.GetClusterWorkClusterfile(deleteClusterName)
}
if deleteArgs.Nodes != "" || deleteArgs.Masters != "" {
applier, err := apply.NewScaleApplierFromArgs(deleteClusterFile, deleteArgs, common.DeleteSubCmd)
if err != nil {
return err
}
return applier.Apply()
}
} else if deleteClusterFile == "" {
deleteClusterFile = common.GetClusterWorkClusterfile(deleteClusterName)
}
if deleteArgs.Nodes != "" || deleteArgs.Masters != "" {
applier, err := apply.NewScaleApplierFromArgs(deleteClusterFile, deleteArgs, common.DeleteSubCmd)

applier, err := apply.NewApplierFromFile(deleteClusterFile)
if err != nil {
return err
}
return applier.Apply()
}

applier, err := apply.NewApplierFromFile(deleteClusterFile)
if err != nil {
return err
}
return applier.Delete()
},
}

func init() {
return applier.Delete()
},
}
deleteArgs = &apply.Args{}
rootCmd.AddCommand(deleteCmd)
deleteCmd.Flags().StringVarP(&deleteArgs.Masters, "masters", "m", "", "reduce Count or IPList to masters")
deleteCmd.Flags().StringVarP(&deleteArgs.Nodes, "nodes", "n", "", "reduce Count or IPList to nodes")
deleteCmd.Flags().StringVarP(&deleteClusterFile, "Clusterfile", "f", "", "delete a kubernetes cluster with Clusterfile Annotations")
deleteCmd.Flags().StringVarP(&deleteClusterName, "cluster", "c", "", "delete a kubernetes cluster with cluster name")
deleteCmd.Flags().StringSliceVarP(&deleteArgs.CustomEnv, "env", "e", []string{}, "set custom environment variables")
deleteCmd.Flags().BoolVar(&kubernetes.ForceDelete, "force", false, "We also can input an --force flag to delete cluster by force")
deleteCmd.Flags().BoolP("all", "a", false, "this flags is for delete nodes, if this is true, empty all node ip")
return deleteCmd
}
Loading