[ADD] add test code

This commit is contained in:
zhoujunhao 2018-06-12 18:00:25 +08:00
parent 6875926e47
commit 9686c3739e

View File

@ -3,9 +3,10 @@ package clean
import (
"k8s.io/client-go/kubernetes"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"fmt"
"github.com/Sirupsen/logrus"
"github.com/goodrain/rainbond/db"
"time"
"fmt"
)
type CheanManager struct {
@ -20,10 +21,10 @@ func NewCheanManager(kubeclient *kubernetes.Clientset) *CheanManager {
}
func (c *CheanManager) Start() {
logrus.Info("clean 开始工作...")
go c.Run()
go c.cleanStatefulset()
go c.cleanService()
logrus.Info("clean up module starts....")
go c.DeleteNamespaces()
//go c.cleanStatefulset()
//go c.cleanService()
}
// InSlice checks given string in string slice or not.
@ -47,39 +48,41 @@ func SliceDiff(slice1, slice2 []string) (diffslice []string) {
}
// SliceIntersect returns slice that are present in all the slice1 and slice2.
func SliceIntersect(slice1, slice2 []string) (diffslice []string) {
for _, v := range slice1 {
if InSlice(v, slice2) {
diffslice = append(diffslice, v)
func SliceIntersect(slice1, slice2 *[]string) (Intersectslice []string) {
for _, v := range *slice1 {
if InSlice(v, *slice2) {
Intersectslice = append(Intersectslice, v)
}
}
return
}
func (c *CheanManager) Run() {
func (c *CheanManager) cleanNamespaces() (*[]string) {
nameList := make([]string, 0, 200)
allList := make([]string, 0, 300)
Namespaces1, err := c.kubeclient.CoreV1().Namespaces().List(meta_v1.ListOptions{})
Namespaces, err := c.kubeclient.CoreV1().Namespaces().List(meta_v1.ListOptions{})
if err != nil {
fmt.Println(err)
logrus.Error(err)
}
for _, v := range Namespaces1.Items {
for _, v := range Namespaces.Items {
if len(v.Name) != 32 {
continue
}
nameList = append(nameList, v.Name)
}
fmt.Println(len(nameList), nameList[0], nameList[2])
AllTenantsList, err := db.GetManager().TenantDao().GetALLTenants()
if err != nil {
logrus.Error(err)
}
for _, v := range AllTenantsList {
allList = append(allList, v.UUID)
}
diffList := SliceDiff(nameList, allList)
fmt.Println(diffList)
return &diffList
//for _, v := range diffList {
// err := c.kubeclient.Namespaces().Delete(v, &meta_v1.DeleteOptions{})
@ -87,12 +90,12 @@ func (c *CheanManager) Run() {
// fmt.Println(err)
// }
//
// logrus.Info("删除成功:", v)
// logrus.Info("delete namespaces success:", v)
//}
}
func (c *CheanManager) cleanStatefulset() {
func (c *CheanManager) cleanStatefulset() (*map[string]string,*map[string]string) {
StatefulSetsMap := make(map[string][]string)
ReplicationControllersMap := make(map[string][]string)
@ -147,27 +150,24 @@ func (c *CheanManager) cleanStatefulset() {
}
}
fmt.Println("StadeleteList", StadeleteMap)
fmt.Println("RepdeleteList", RepdeleteMap)
return &StadeleteMap, &RepdeleteMap
for k,v:=range StadeleteMap{
if err := c.kubeclient.StatefulSets(k).Delete(v,&meta_v1.DeleteOptions{});err!=nil{
logrus.Error(err)
}
}
for k,v := range RepdeleteMap{
if err:=c.kubeclient.ReplicationControllers(k).Delete(v,&meta_v1.DeleteOptions{});err!=nil{
logrus.Error(err)
}
}
fmt.Println("结束")
//for k, v := range StadeleteMap {
// if err := c.kubeclient.StatefulSets(k).Delete(v, &meta_v1.DeleteOptions{}); err != nil {
// logrus.Error(err)
// }
//
//}
//
//for k, v := range RepdeleteMap {
// if err := c.kubeclient.ReplicationControllers(k).Delete(v, &meta_v1.DeleteOptions{}); err != nil {
// logrus.Error(err)
// }
//}
}
func (c *CheanManager) cleanService() {
func (c *CheanManager) cleanService() *map[string]string {
ServivesMap := make(map[string][]string)
ServivesDeleteMap := make(map[string]string)
@ -192,23 +192,33 @@ func (c *CheanManager) cleanService() {
logrus.Error(err)
}
for _, v := range ServicesList.Items {
fmt.Println("xxx",v.Namespace,v.Name)
if !InSlice(v.Name, valuse) {
ServivesDeleteMap[k] = v.Name
}
}
}
fmt.Println(ServivesMap)
fmt.Println(ServivesDeleteMap)
for k, v := range ServivesDeleteMap {
err := c.kubeclient.Services(k).Delete(v, &meta_v1.DeleteOptions{})
if err!=nil {
logrus.Error(err)
}
logrus.Info("删除service成功",k,v)
break
}
return &ServivesDeleteMap
//
//for k, v := range ServivesDeleteMap {
// err := c.kubeclient.Services(k).Delete(v, &meta_v1.DeleteOptions{})
// if err != nil {
// logrus.Error(err)
// }
// logrus.Info("delete service success", v)
//}
}
func (c *CheanManager) DeleteNamespaces(){
diffList :=c.cleanNamespaces()
fmt.Println(diffList)
time.AfterFunc(time.Second *10, func() {
newdiffList :=c.cleanNamespaces()
deleteList := SliceIntersect(newdiffList,diffList)
fmt.Println("delete:",deleteList)
})
}