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

feat: add and delete master or worker nodes with different ssh settings #3525

Merged
merged 4 commits into from
Jul 24, 2023
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
9 changes: 7 additions & 2 deletions cmd/sealos/cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,25 @@ add to nodes :
add to default cluster:
sealos add --masters x.x.x.x --nodes x.x.x.x
sealos add --masters x.x.x.x-x.x.x.y --nodes x.x.x.x-x.x.x.y

add with different ssh setting:
sealos add --masters x.x.x.x --nodes x.x.x.x --passwd your_diff_passwd
Please note that the masters and nodes added in one command should have the save password.
`

// addCmd represents the delete command
// addCmd represents the add command
func newAddCmd() *cobra.Command {
addArgs := &apply.ScaleArgs{
Cluster: &apply.Cluster{},
SSH: &apply.SSH{},
}
var addCmd = &cobra.Command{
Use: "add",
Short: "Add nodes into cluster",
Args: cobra.NoArgs,
Example: exampleAdd,
RunE: func(cmd *cobra.Command, args []string) error {
applier, err := apply.NewScaleApplierFromArgs(addArgs, "add")
applier, err := apply.NewScaleApplierFromArgs(cmd, addArgs)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/sealos/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func newDeleteCmd() *cobra.Command {
if err := processor.ConfirmDeleteNodes(); err != nil {
return err
}
applier, err := apply.NewScaleApplierFromArgs(deleteArgs, "delete")
applier, err := apply.NewScaleApplierFromArgs(cmd, deleteArgs)
if err != nil {
return err
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/apply/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,13 @@ func (arg *ResetArgs) RegisterFlags(fs *pflag.FlagSet) {

type ScaleArgs struct {
*Cluster
*SSH
}

func (arg *ScaleArgs) RegisterFlags(fs *pflag.FlagSet, verb, action string) {
arg.Cluster.RegisterFlags(fs, verb, action)
// delete cmd does not support setting ssh, it reads from clusterfile
if arg.SSH != nil {
arg.SSH.RegisterFlags(fs)
}
}
3 changes: 3 additions & 0 deletions pkg/apply/args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,15 @@ func TestParseScaleArgsFlagsCorrect(t *testing.T) {
{
[]string{
"--masters", "10.74.22.22:22", "--nodes", "10.74.22.44:22", "--cluster", "default",
"-u", "root", "-p", "passwd", "--port", "22",
},
&ScaleArgs{
Cluster: &Cluster{},
SSH: &SSH{},
},
&ScaleArgs{
Cluster: &Cluster{Masters: "10.74.22.22:22", Nodes: "10.74.22.44:22", ClusterName: "default"},
SSH: &SSH{User: "root", Password: "passwd", Port: 22, Pk: path.Join(constants.GetHomeDir(), ".ssh", "id_rsa")},
},
},
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/apply/processor/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (d *DeleteProcessor) PreProcess(cluster *v2.Cluster) error {
func (d *DeleteProcessor) UndoBootstrap(cluster *v2.Cluster) error {
logger.Info("Executing pipeline Bootstrap in DeleteProcessor")
hosts := append(cluster.GetMasterIPAndPortList(), cluster.GetNodeIPAndPortList()...)
bs := bootstrap.New(cluster)
bs := bootstrap.New(d.ClusterFile.GetCluster())
return bs.Delete(hosts...)
}

Expand All @@ -96,7 +96,7 @@ func (d *DeleteProcessor) UnMountRootfs(cluster *v2.Cluster) error {
if err != nil {
return err
}
return fs.UnMountRootfs(cluster, hosts)
return fs.UnMountRootfs(d.ClusterFile.GetCluster(), hosts)
}

func (d *DeleteProcessor) UnMountImage(cluster *v2.Cluster) error {
Expand Down
15 changes: 10 additions & 5 deletions pkg/apply/processor/scale.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (c ScaleProcessor) UnMountRootfs(cluster *v2.Cluster) error {
if err != nil {
return err
}
return fs.UnMountRootfs(cluster, hosts)
return fs.UnMountRootfs(c.ClusterFile.GetCluster(), hosts)
}

func (c *ScaleProcessor) JoinCheck(cluster *v2.Cluster) error {
Expand Down Expand Up @@ -184,11 +184,16 @@ func (c *ScaleProcessor) preProcess(cluster *v2.Cluster) error {
if err = SyncClusterStatus(cluster, c.Buildah, false); err != nil {
return err
}
runTime, err := runtime.NewDefaultRuntime(cluster, c.ClusterFile.GetKubeadmConfig())
var rt runtime.Interface
if c.IsScaleUp {
rt, err = runtime.NewDefaultRuntimeWithCurrentCluster(cluster, c.ClusterFile.GetKubeadmConfig())
} else {
rt, err = runtime.NewDefaultRuntimeWithCurrentCluster(c.ClusterFile.GetCluster(), c.ClusterFile.GetKubeadmConfig())
}
if err != nil {
return fmt.Errorf("failed to init runtime: %v", err)
}
c.Runtime = runTime
c.Runtime = rt

return err
}
Expand Down Expand Up @@ -258,10 +263,10 @@ func (c *ScaleProcessor) Bootstrap(cluster *v2.Cluster) error {
return bs.Apply(hosts...)
}

func (c *ScaleProcessor) UndoBootstrap(cluster *v2.Cluster) error {
func (c *ScaleProcessor) UndoBootstrap(_ *v2.Cluster) error {
logger.Info("Executing pipeline UndoBootstrap in ScaleProcessor")
hosts := append(c.MastersToDelete, c.NodesToDelete...)
bs := bootstrap.New(cluster)
bs := bootstrap.New(c.ClusterFile.GetCluster())
return bs.Delete(hosts...)
}

Expand Down
87 changes: 67 additions & 20 deletions pkg/apply/scale.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,44 +20,44 @@
"strconv"
"strings"

"github.com/labring/sealos/pkg/constants"
"github.com/labring/sealos/pkg/ssh"
fileutil "github.com/labring/sealos/pkg/utils/file"
"github.com/labring/sealos/pkg/utils/iputils"
strings2 "github.com/labring/sealos/pkg/utils/strings"

"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/util/sets"

"github.com/labring/sealos/pkg/apply/applydrivers"
"github.com/labring/sealos/pkg/clusterfile"
"github.com/labring/sealos/pkg/constants"
"github.com/labring/sealos/pkg/ssh"
v2 "github.com/labring/sealos/pkg/types/v1beta1"
fileutil "github.com/labring/sealos/pkg/utils/file"
"github.com/labring/sealos/pkg/utils/iputils"
strings2 "github.com/labring/sealos/pkg/utils/strings"
)

// NewScaleApplierFromArgs will filter ip list from command parameters.
func NewScaleApplierFromArgs(scaleArgs *ScaleArgs, flag string) (applydrivers.Interface, error) {
func NewScaleApplierFromArgs(cmd *cobra.Command, scaleArgs *ScaleArgs) (applydrivers.Interface, error) {

Check warning on line 37 in pkg/apply/scale.go

View check run for this annotation

Codecov / codecov/patch

pkg/apply/scale.go#L37

Added line #L37 was not covered by tests
var cluster *v2.Cluster
var curr *v2.Cluster
clusterPath := constants.Clusterfile(scaleArgs.Cluster.ClusterName)

Check warning on line 40 in pkg/apply/scale.go

View check run for this annotation

Codecov / codecov/patch

pkg/apply/scale.go#L40

Added line #L40 was not covered by tests
if !fileutil.IsExist(clusterPath) {
cluster = initCluster(scaleArgs.Cluster.ClusterName)
curr = cluster
} else {
clusterFile := clusterfile.NewClusterFile(clusterPath)
err := clusterFile.Process()
if err != nil {
return nil, err
}
cluster = clusterFile.GetCluster()
curr = clusterFile.GetCluster().DeepCopy()
}

curr := cluster.DeepCopy()

Check warning on line 53 in pkg/apply/scale.go

View check run for this annotation

Codecov / codecov/patch

pkg/apply/scale.go#L52-L53

Added lines #L52 - L53 were not covered by tests
if scaleArgs.Cluster.Nodes == "" && scaleArgs.Cluster.Masters == "" {
return nil, fmt.Errorf("the node or master parameter was not committed")
}
var err error
switch flag {
switch cmd.Name() {

Check warning on line 58 in pkg/apply/scale.go

View check run for this annotation

Codecov / codecov/patch

pkg/apply/scale.go#L58

Added line #L58 was not covered by tests
case "add":
err = Join(cluster, scaleArgs)
err = verifyAndSetNodes(cmd, cluster, scaleArgs)

Check warning on line 60 in pkg/apply/scale.go

View check run for this annotation

Codecov / codecov/patch

pkg/apply/scale.go#L60

Added line #L60 was not covered by tests
case "delete":
err = Delete(cluster, scaleArgs)
}
Expand All @@ -68,14 +68,52 @@
return applydrivers.NewDefaultScaleApplier(curr, cluster)
}

func Join(cluster *v2.Cluster, scalingArgs *ScaleArgs) error {
return joinNodes(cluster, scalingArgs)
func getSSHFromCommand(cmd *cobra.Command) *v2.SSH {
var (
ret = &v2.SSH{}
fs = cmd.Flags()
changed bool
)
if flagChanged(cmd, "user") {
ret.User, _ = fs.GetString("user")
changed = true
}

Check warning on line 80 in pkg/apply/scale.go

View check run for this annotation

Codecov / codecov/patch

pkg/apply/scale.go#L78-L80

Added lines #L78 - L80 were not covered by tests
if flagChanged(cmd, "passwd") {
ret.Passwd, _ = fs.GetString("passwd")
changed = true
}

Check warning on line 84 in pkg/apply/scale.go

View check run for this annotation

Codecov / codecov/patch

pkg/apply/scale.go#L82-L84

Added lines #L82 - L84 were not covered by tests
if flagChanged(cmd, "pk") {
ret.Pk, _ = fs.GetString("pk")
changed = true
}

Check warning on line 88 in pkg/apply/scale.go

View check run for this annotation

Codecov / codecov/patch

pkg/apply/scale.go#L86-L88

Added lines #L86 - L88 were not covered by tests
if flagChanged(cmd, "pk-passwd") {
ret.PkPasswd, _ = fs.GetString("pk-passwd")
changed = true
}

Check warning on line 92 in pkg/apply/scale.go

View check run for this annotation

Codecov / codecov/patch

pkg/apply/scale.go#L90-L92

Added lines #L90 - L92 were not covered by tests
if flagChanged(cmd, "port") {
ret.Port, _ = fs.GetUint16("port")
changed = true
}

Check warning on line 96 in pkg/apply/scale.go

View check run for this annotation

Codecov / codecov/patch

pkg/apply/scale.go#L94-L96

Added lines #L94 - L96 were not covered by tests
if changed {
return ret
}

Check warning on line 99 in pkg/apply/scale.go

View check run for this annotation

Codecov / codecov/patch

pkg/apply/scale.go#L98-L99

Added lines #L98 - L99 were not covered by tests
return nil
}

func flagChanged(cmd *cobra.Command, name string) bool {
if cmd != nil {
if fs := cmd.Flag(name); fs != nil && fs.Changed {
return true
}

Check warning on line 107 in pkg/apply/scale.go

View check run for this annotation

Codecov / codecov/patch

pkg/apply/scale.go#L106-L107

Added lines #L106 - L107 were not covered by tests
}
return false
}

func joinNodes(cluster *v2.Cluster, scaleArgs *ScaleArgs) error {
func verifyAndSetNodes(cmd *cobra.Command, cluster *v2.Cluster, scaleArgs *ScaleArgs) error {
if err := PreProcessIPList(scaleArgs.Cluster); err != nil {
return err
}

masters, nodes := scaleArgs.Cluster.Masters, scaleArgs.Cluster.Nodes
if len(masters) > 0 {
if err := validateIPList(masters); err != nil {
Expand All @@ -94,7 +132,7 @@
var hasMaster bool
// check duplicate
alreadyIn := sets.NewString()
// add already add masters and nodes
// add already joined masters and nodes
for i := range cluster.Spec.Hosts {
h := cluster.Spec.Hosts[i]
if strings2.InList(v2.MASTER, h.Roles) {
Expand All @@ -106,11 +144,14 @@
IPS: ips,
Roles: h.Roles,
Env: h.Env,
SSH: h.SSH,
})
}
if !hasMaster {
return fmt.Errorf("`master` role not found, due to Clusterfile may have been corrupted?")
}
override := getSSHFromCommand(cmd)

getHostFunc := func(sliceStr string, role string, exclude []string) (*v2.Host, error) {
ss := strings.Split(sliceStr, ",")
addrs := make([]string, 0)
Expand All @@ -128,13 +169,19 @@
}
}
if len(addrs) > 0 {
clusterSSH := cluster.GetSSH()
sshClient := ssh.NewSSHClient(&clusterSSH, true)
global := cluster.Spec.SSH.DeepCopy()
ssh.OverSSHConfig(global, override)

return &v2.Host{
sshClient := ssh.NewSSHClient(global, true)

host := &v2.Host{
IPS: addrs,
Roles: []string{role, GetHostArch(sshClient, addrs[0])},
}, nil
}
if override != nil {
host.SSH = override
}

Check warning on line 183 in pkg/apply/scale.go

View check run for this annotation

Codecov / codecov/patch

pkg/apply/scale.go#L182-L183

Added lines #L182 - L183 were not covered by tests
return host, nil
}
return nil, nil
}
Expand Down
Loading
Loading