Skip to content

Commit

Permalink
feat: add and delete master or worker nodes with different ssh settings
Browse files Browse the repository at this point in the history
Signed-off-by: yangxg <yangxggo@163.com>
  • Loading branch information
yangxggo committed Jul 18, 2023
1 parent 13b2a29 commit ccb6314
Show file tree
Hide file tree
Showing 23 changed files with 338 additions and 87 deletions.
7 changes: 6 additions & 1 deletion cmd/sealos/cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,17 @@ 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",
Expand Down
2 changes: 1 addition & 1 deletion cmd/sealos/cmd/cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func newCertCmd() *cobra.Command {
if err = cf.Process(); err != nil {
return err
}
r, err := runtime.NewDefaultRuntime(cluster, cf.GetKubeadmConfig())
r, err := runtime.NewDefaultRuntime(cluster, cf.GetCluster(), cf.GetKubeadmConfig())
if err != nil {
return fmt.Errorf("get default runtime failed, %v", err)
}
Expand Down
1 change: 1 addition & 0 deletions cmd/sealos/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Please note that sealos will delete your master if the --masters parameter is sp
func newDeleteCmd() *cobra.Command {
deleteArgs := &apply.ScaleArgs{
Cluster: &apply.Cluster{},
SSH: &apply.SSH{},
}
var deleteCmd = &cobra.Command{
Use: "delete",
Expand Down
4 changes: 4 additions & 0 deletions pkg/apply/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,12 @@ func (arg *ResetArgs) RegisterFlags(fs *pflag.FlagSet) {

type ScaleArgs struct {
*Cluster
*SSH
fs *pflag.FlagSet
}

func (arg *ScaleArgs) RegisterFlags(fs *pflag.FlagSet, verb, action string) {
arg.Cluster.RegisterFlags(fs, verb, action)
arg.SSH.RegisterFlags(fs)
arg.fs = 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
2 changes: 1 addition & 1 deletion pkg/apply/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func NewClusterFromGenArgs(imageNames []string, args *RunArgs) ([]byte, error) {
return nil, fmt.Errorf("input first image %s is not kubernetes image", imageNames)
}
cluster.Status.Mounts = append(cluster.Status.Mounts, *img)
rtInterface, err := runtime.NewDefaultRuntime(cluster, &runtime.KubeadmConfig{})
rtInterface, err := runtime.NewDefaultRuntime(cluster, nil, &runtime.KubeadmConfig{})

Check warning on line 54 in pkg/apply/gen.go

View check run for this annotation

Codecov / codecov/patch

pkg/apply/gen.go#L54

Added line #L54 was not covered by tests
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/apply/processor/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (c *CreateProcessor) preProcess(cluster *v2.Cluster) error {
if err := MountClusterImages(c.Buildah, cluster, false); err != nil {
return err
}
runTime, err := runtime.NewDefaultRuntime(cluster, c.ClusterFile.GetKubeadmConfig())
runTime, err := runtime.NewDefaultRuntime(cluster, c.ClusterFile.GetCluster(), c.ClusterFile.GetKubeadmConfig())
if err != nil {
return fmt.Errorf("failed to init runtime, %v", err)
}
Expand Down Expand Up @@ -118,7 +118,7 @@ func (c *CreateProcessor) MountRootfs(cluster *v2.Cluster) error {
if err != nil {
return err
}
return fs.MountRootfs(cluster, hosts)
return fs.MountRootfs(cluster, c.ClusterFile.GetCluster(), hosts)
}

func (c *CreateProcessor) MirrorRegistry(cluster *v2.Cluster) error {
Expand All @@ -129,7 +129,7 @@ func (c *CreateProcessor) MirrorRegistry(cluster *v2.Cluster) error {
func (c *CreateProcessor) Bootstrap(cluster *v2.Cluster) error {
logger.Info("Executing pipeline Bootstrap in CreateProcessor")
hosts := append(cluster.GetMasterIPAndPortList(), cluster.GetNodeIPAndPortList()...)
bs := bootstrap.New(cluster)
bs := bootstrap.New(cluster, c.ClusterFile.GetCluster())
return bs.Apply(hosts...)
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/apply/processor/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ 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(cluster, d.ClusterFile.GetCluster())
return bs.Delete(hosts...)
}

func (d *DeleteProcessor) Reset(cluster *v2.Cluster) error {
runTime, err := runtime.NewDefaultRuntime(cluster, d.ClusterFile.GetKubeadmConfig())
runTime, err := runtime.NewDefaultRuntime(cluster, d.ClusterFile.GetCluster(), d.ClusterFile.GetKubeadmConfig())
if err != nil {
return fmt.Errorf("failed to delete runtime, %v", err)
}
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(cluster, d.ClusterFile.GetCluster(), hosts)
}

func (d *DeleteProcessor) UnMountImage(cluster *v2.Cluster) error {
Expand Down
4 changes: 2 additions & 2 deletions pkg/apply/processor/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func (c *InstallProcessor) PreProcess(cluster *v2.Cluster) error {
cluster.SetMountImage(mount)
c.NewMounts = append(c.NewMounts, *mount)
}
runtime, err := runtime.NewDefaultRuntime(cluster, c.ClusterFile.GetKubeadmConfig())
runtime, err := runtime.NewDefaultRuntime(cluster, c.ClusterFile.GetCluster(), c.ClusterFile.GetKubeadmConfig())
if err != nil {
return fmt.Errorf("failed to init runtime, %v", err)
}
Expand Down Expand Up @@ -237,7 +237,7 @@ func (c *InstallProcessor) MountRootfs(cluster *v2.Cluster) error {
if err != nil {
return err
}
return fs.MountRootfs(cluster, hosts)
return fs.MountRootfs(cluster, c.ClusterFile.GetCluster(), hosts)
}

func (c *InstallProcessor) MirrorRegistry(cluster *v2.Cluster) error {
Expand Down
14 changes: 7 additions & 7 deletions pkg/apply/processor/scale.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ import (
)

type ScaleProcessor struct {
ClusterFile clusterfile.Interface
Runtime runtime.Interface
ClusterFile clusterfile.Interface // current
Runtime runtime.Interface // disered
Buildah buildah.Interface
pullImages []string
MastersToJoin []string
Expand Down 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(cluster, c.ClusterFile.GetCluster(), hosts)
}

func (c *ScaleProcessor) JoinCheck(cluster *v2.Cluster) error {
Expand Down Expand Up @@ -184,7 +184,7 @@ 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())
runTime, err := runtime.NewDefaultRuntime(cluster, c.ClusterFile.GetCluster(), c.ClusterFile.GetKubeadmConfig())
if err != nil {
return fmt.Errorf("failed to init runtime: %v", err)
}
Expand Down Expand Up @@ -238,7 +238,7 @@ func (c *ScaleProcessor) MountRootfs(cluster *v2.Cluster) error {
if err != nil {
return err
}
return fs.MountRootfs(cluster, hosts)
return fs.MountRootfs(cluster, c.ClusterFile.GetCluster(), hosts)
}

func filterNoneApplicationMounts(images []v2.MountImage) []v2.MountImage {
Expand All @@ -254,14 +254,14 @@ func filterNoneApplicationMounts(images []v2.MountImage) []v2.MountImage {
func (c *ScaleProcessor) Bootstrap(cluster *v2.Cluster) error {
logger.Info("Executing pipeline Bootstrap in ScaleProcessor")
hosts := append(c.MastersToJoin, c.NodesToJoin...)
bs := bootstrap.New(cluster)
bs := bootstrap.New(cluster, c.ClusterFile.GetCluster())
return bs.Apply(hosts...)
}

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

Expand Down
47 changes: 36 additions & 11 deletions pkg/apply/scale.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ import (
"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"

"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"
"github.com/labring/sealos/pkg/utils/logger"
strings2 "github.com/labring/sealos/pkg/utils/strings"
)

// NewScaleApplierFromArgs will filter ip list from command parameters.
Expand Down Expand Up @@ -106,12 +106,13 @@ func joinNodes(cluster *v2.Cluster, scaleArgs *ScaleArgs) error {
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?")
}
getHostFunc := func(sliceStr string, role string, exclude []string) (*v2.Host, error) {
getHostFunc := func(sliceStr string, role string, exclude []string, scaleSshConfig *SSH) (*v2.Host, error) {
ss := strings.Split(sliceStr, ",")
addrs := make([]string, 0)
for _, s := range ss {
Expand All @@ -129,22 +130,46 @@ func joinNodes(cluster *v2.Cluster, scaleArgs *ScaleArgs) error {
}
if len(addrs) > 0 {
clusterSSH := cluster.GetSSH()

var override bool
var overrideSSH v2.SSH

if scaleSshConfig != nil {
overrideSSH.User = scaleSshConfig.User
overrideSSH.Passwd = scaleSshConfig.Password
overrideSSH.PkName = clusterSSH.PkName
overrideSSH.PkData = clusterSSH.PkData
overrideSSH.Pk = scaleSshConfig.Pk
overrideSSH.PkPasswd = scaleSshConfig.PkPassword
overrideSSH.Port = scaleSshConfig.Port

if clusterSSH != overrideSSH {
logger.Info("scale '%s' nodes '%q' with different ssh settings: %+v", role, sliceStr, scaleSshConfig)
clusterSSH = overrideSSH
override = true
}
}

sshClient := ssh.NewSSHClient(&clusterSSH, true)

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

if mastersToAdded, err := getHostFunc(masters, v2.MASTER, cluster.GetMasterIPAndPortList()); err != nil {
if mastersToAdded, err := getHostFunc(masters, v2.MASTER, cluster.GetMasterIPAndPortList(), scaleArgs.SSH); err != nil {
return err
} else if mastersToAdded != nil {
hosts = append(hosts, *mastersToAdded)
}
if nodesToAdded, err := getHostFunc(nodes, v2.NODE, cluster.GetNodeIPAndPortList()); err != nil {
if nodesToAdded, err := getHostFunc(nodes, v2.NODE, cluster.GetNodeIPAndPortList(), scaleArgs.SSH); err != nil {
return err
} else if nodesToAdded != nil {
hosts = append(hosts, *nodesToAdded)
Expand Down
82 changes: 82 additions & 0 deletions pkg/apply/scale_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,88 @@ func TestJoin(t *testing.T) {
},
wantErr: false,
},
{
name: "add masters with different password",
args: args{
cluster: &v2.Cluster{
TypeMeta: metav1.TypeMeta{},
ObjectMeta: metav1.ObjectMeta{},
Spec: v2.ClusterSpec{
Image: nil,
SSH: v2.SSH{
User: "root",
Passwd: "Fanux#123",
Port: 22,
},
Hosts: []v2.Host{
{
IPS: []string{"192.168.16.99:22", "192.168.16.98:22", "192.168.16.97:22"},
Roles: []string{v2.MASTER},
},
{
IPS: []string{"192.168.16.1:22", "192.168.16.2:22", "192.168.16.3:22", "192.168.16.4:22"},
Roles: []string{v2.NODE},
},
},
},
Status: v2.ClusterStatus{},
},
scalingArgs: &ScaleArgs{
Cluster: &Cluster{
Masters: "192.168.16.5:22",
Nodes: "",
ClusterName: "",
},
SSH: &SSH{
User: "root",
Password: "Fanux@1234",
Port: 22,
},
},
},
wantErr: false,
},
{
name: "add nodes with different password",
args: args{
cluster: &v2.Cluster{
TypeMeta: metav1.TypeMeta{},
ObjectMeta: metav1.ObjectMeta{},
Spec: v2.ClusterSpec{
Image: nil,
SSH: v2.SSH{
User: "root",
Passwd: "Fanux#123",
Port: 22,
},
Hosts: []v2.Host{
{
IPS: []string{"192.168.16.99:22", "192.168.16.98:22", "192.168.16.97:22"},
Roles: []string{v2.MASTER},
},
{
IPS: []string{"192.168.16.1:22", "192.168.16.2:22", "192.168.16.3:22", "192.168.16.4:22"},
Roles: []string{v2.NODE},
},
},
},
Status: v2.ClusterStatus{},
},
scalingArgs: &ScaleArgs{
Cluster: &Cluster{
Masters: "",
Nodes: "192.168.16.90:22",
ClusterName: "",
},
SSH: &SSH{
User: "root",
Password: "Fanux@1234",
Port: 22,
},
},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit ccb6314

Please sign in to comment.