Skip to content

Commit

Permalink
feature(devlop): fix clean feature
Browse files Browse the repository at this point in the history
  • Loading branch information
cuisongliu committed Mar 13, 2020
1 parent 9e404fa commit f852076
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 47 deletions.
20 changes: 6 additions & 14 deletions cmd/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,21 @@ var cleanCmd = &cobra.Command{
Short: "Simplest way to clean your kubernets HA cluster",
Long: `sealos clean`,
Run: func(cmd *cobra.Command, args []string) {
beforeNodes:=install.ParseIPs(install.NodeIPs)
deleteNodes := install.ParseIPs(install.NodeIPs)
deleteMasters := install.ParseIPs(install.MasterIPs)
c := &install.SealConfig{}
c.Load("")
install.BuildClean(beforeNodes)
if len(beforeNodes) > 0{
var resultNodes []string
//去掉共同数据添加数据到结果集中去
for _,node:=range c.Nodes{
if !install.StrSliceContains(beforeNodes,node){
resultNodes = append(resultNodes,node)
}
}
install.NodeIPs = resultNodes
c.Dump("")
}
install.BuildClean(deleteNodes, deleteMasters)
c.Dump("")
},
}

func init() {
rootCmd.AddCommand(cleanCmd)

// Here you will define your flags and configuration settings.
cleanCmd.Flags().StringSliceVar(&install.NodeIPs, "node", []string{}, "kubernetes multi-nodes ex. 192.168.0.5-192.168.0.5")
cleanCmd.Flags().StringSliceVar(&install.NodeIPs, "node", []string{}, "clean node ips.kubernetes multi-nodes ex. 192.168.0.5-192.168.0.5")
cleanCmd.Flags().StringSliceVar(&install.MasterIPs, "master", []string{}, "clean master ips.kubernetes multi-nodes ex. 192.168.0.5-192.168.0.5")

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
Expand Down
2 changes: 1 addition & 1 deletion cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func init() {
rootCmd.AddCommand(initCmd)

// Here you will define your flags and configuration settings.
initCmd.Flags().StringVar(&install.SSHConfig.PkFile, "user", "root", "servers user name for ssh")
initCmd.Flags().StringVar(&install.SSHConfig.User, "user", "root", "servers user name for ssh")
initCmd.Flags().StringVar(&install.SSHConfig.Password, "passwd", "", "password for ssh")
initCmd.Flags().StringVar(&install.SSHConfig.PkFile, "pk", "/root/.ssh/id_rsa", "private key for ssh")

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/cuisongliu/sshcmd v1.4.2
github.com/fanux/sealgate v0.0.4
github.com/mitchellh/go-homedir v1.1.0
github.com/pkg/errors v0.8.1
github.com/spf13/cobra v0.0.5
github.com/spf13/viper v1.5.0
github.com/wonderivan/logger v1.0.0
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5y
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/gofuzz v0.0.0-20161122191042-44d81051d367/go.mod h1:HP5RmnzzSNb993RKQDq4+1A4ia9nllfqcQFTQJedwGI=
github.com/google/gofuzz v1.0.0 h1:A8PeW59pxE9IoFRqBp37U+mSNaQoZ46F1f0f863XSXw=
Expand Down
86 changes: 74 additions & 12 deletions install/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,100 @@ package install

import (
"fmt"
"github.com/fanux/sealos/ipvs"
"github.com/wonderivan/logger"
"strings"
"sync"
)

//BuildClean is
func BuildClean(beforeNodes []string) {
func BuildClean(deleteNodes, deleteMasters []string) {
i := &SealosInstaller{}
var hosts []string
if len(beforeNodes) == 0 {
masters := ParseIPs(MasterIPs)
nodes := ParseIPs(NodeIPs)
//1. 删除masters
if len(deleteMasters) != 0 {
//只清除masters
i.Masters = deleteMasters
}
//2. 删除nodes
if len(deleteNodes) != 0 {
//只清除nodes
i.Nodes = deleteNodes
}
if len(deleteNodes) == 0 && len(deleteMasters) == 0 {
// 所有master节点
masters := ParseIPs(MasterIPs)
i.Masters = masters
// 所有node节点
nodes := ParseIPs(NodeIPs)
hosts = append(masters, nodes...)
} else {
hosts = beforeNodes
i.Nodes = nodes
}
i.Hosts = hosts
i.CheckValid()
i.Clean()
}

//CleanCluster is
func (s *SealosInstaller) Clean() {
var wg sync.WaitGroup
for _, host := range s.Hosts {
//s 是要删除的数据
//全局是当前的数据
if len(s.Masters) > 0 {
//1. 先删除master
for _, master := range s.Masters {
wg.Add(1)
go func(master string) {
defer wg.Done()
cleanMaster(master)
}(master)
}
}
if len(s.Nodes) > 0 {
//2. 再删除nodes
for _, node := range s.Nodes {
wg.Add(1)
go func(node string) {
defer wg.Done()
cleanNode(node)
}(node)
}
}
wg.Wait()
}

func cleanNode(node string) {
clean(node)
logger.Debug("clean node in master")
hostname := SSHConfig.CmdToString(node, "cat /etc/hostname")
cmd := "kubectl delete node %s || true"
if len(MasterIPs) > 0 {
SSHConfig.Cmd(MasterIPs[0], fmt.Sprintf(cmd, node))
SSHConfig.Cmd(MasterIPs[0], fmt.Sprintf(cmd, strings.TrimSpace(hostname)))
}
//remove node
NodeIPs = SliceRemoveStr(NodeIPs, node)
}

func cleanMaster(master string) {
clean(master)
logger.Debug("clean node in master")
MasterIPs = SliceRemoveStr(MasterIPs, master)
hostname := SSHConfig.CmdToString(master, "cat /etc/hostname")
cmd := "kubectl delete node %s || true"
if len(MasterIPs) > 0 {
SSHConfig.Cmd(MasterIPs[0], fmt.Sprintf(cmd, master))
SSHConfig.Cmd(MasterIPs[0], fmt.Sprintf(cmd, strings.TrimSpace(hostname)))
}
//清空所有的nodes的数据
yaml := ipvs.LvsStaticPodYaml(VIP, MasterIPs, "")
var wg sync.WaitGroup
for _, node := range NodeIPs {
wg.Add(1)
go func(node string) {
defer wg.Done()
clean(node)
}(host)
SSHConfig.Cmd(node, "echo \""+yaml+"\" > /etc/kubernetes/manifests/kube-sealyun-lvscare.yaml")
}(node)
}
wg.Wait()

}

func clean(host string) {
Expand Down
8 changes: 4 additions & 4 deletions install/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,13 +240,13 @@ func ParseIPs(ips []string) []string {
return hosts
}

func StrSliceContains(ss []string, s string) bool {
func SliceRemoveStr(ss []string, s string) (result []string) {
for _, v := range ss {
if v == s {
return true
if v != s {
result = append(result, v)
}
}
return false
return
}

func UrlGetMd5(downloadUrl string) string {
Expand Down
6 changes: 6 additions & 0 deletions install/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,9 @@ func TestUrlGetMd5(t *testing.T) {
aa := UrlGetMd5("https://sealyun.oss-cn-beijing.aliyuncs.com/37374d999dbadb788ef0461844a70151-1.16.0/kube1.16.0.tar.gz")
t.Log(aa)
}

func TestSliceRemoveStr(t *testing.T) {
ss := []string{"aa", "bb", "cc"}
aa := SliceRemoveStr(ss, "bb")
t.Log(aa)
}
33 changes: 17 additions & 16 deletions ipvs/lvscare.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/scheme"
)

const defaultImage = "fanux/lvscare:latest"

// return lvscare static pod yaml
Expand All @@ -18,29 +19,29 @@ func LvsStaticPodYaml(vip string, masters []string, image string) string {
if vip == "" || len(masters) == 0 {
return ""
}
args := []string{"care","--vs",vip+":6443","--health-path", "/healthz","--health-schem","https"}
for _,m := range masters {
args = append(args,"--rs")
args = append(args, m + ":6443")
args := []string{"care", "--vs", vip + ":6443", "--health-path", "/healthz", "--health-schem", "https"}
for _, m := range masters {
args = append(args, "--rs")
args = append(args, m+":6443")
}
flag := true
pod := ComponentPod(v1.Container{
Name: "kube-sealyun-lvscare",
Image: image,
Command: []string{"/usr/bin/lvscare"},
Args: args,
ImagePullPolicy: v1.PullIfNotPresent,
SecurityContext: &v1.SecurityContext{Privileged:&flag},
pod := componentPod(v1.Container{
Name: "kube-sealyun-lvscare",
Image: image,
Command: []string{"/usr/bin/lvscare"},
Args: args,
ImagePullPolicy: v1.PullIfNotPresent,
SecurityContext: &v1.SecurityContext{Privileged: &flag},
})
yaml,err := PodToYaml(pod)
yaml, err := podToYaml(pod)
if err != nil {
logger.Error("decode lvscare static pod yaml failed %s",err)
logger.Error("decode lvscare static pod yaml failed %s", err)
return ""
}
return string(yaml)
}

func PodToYaml(pod v1.Pod)([]byte,error) {
func podToYaml(pod v1.Pod) ([]byte, error) {
codecs := scheme.Codecs
gv := v1.SchemeGroupVersion
const mediaType = runtime.ContentTypeYAML
Expand All @@ -53,8 +54,8 @@ func PodToYaml(pod v1.Pod)([]byte,error) {
return runtime.Encode(encoder, &pod)
}

// ComponentPod returns a Pod object from the container and volume specifications
func ComponentPod(container v1.Container) v1.Pod {
// componentPod returns a Pod object from the container and volume specifications
func componentPod(container v1.Container) v1.Pod {
return v1.Pod{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Expand Down

0 comments on commit f852076

Please sign in to comment.