[ADD] add test code

This commit is contained in:
zhoujunhao 2018-06-12 15:02:55 +08:00
parent e7429e4d7a
commit 19975ce62a
3 changed files with 101 additions and 17 deletions

View File

@ -22,6 +22,7 @@ func NewCheanManager(kubeclient *kubernetes.Clientset) *CheanManager {
func (c *CheanManager) Start() {
logrus.Info("clean 开始工作...")
go c.Run()
go c.cleanStatefulset()
}
@ -68,7 +69,9 @@ func (c *CheanManager) Run() {
}
for _, v := range Namespaces1.Items {
if len(v.Name) != 32{
continue
}
nameList = append(nameList, v.Name)
}
fmt.Println(len(nameList), nameList[0], nameList[2])
@ -82,23 +85,95 @@ func (c *CheanManager) Run() {
diffList := SliceDiff(nameList,allList)
fmt.Println(diffList)
StatefulSets, err := c.kubeclient.StatefulSets("824b2e9dcc4d461a852ddea20369d377").List(meta_v1.ListOptions{})
ReplicationControllers, err := c.kubeclient.ReplicationControllers("c69c40ecedae41ca9fbb6c3cec0926f2").List(meta_v1.ListOptions{})
for _,v := range diffList {
err := c.kubeclient.Namespaces().Delete(v,&meta_v1.DeleteOptions{})
if err != nil{
fmt.Println("删除错误",err)
}
break
}
for _,v:=range StatefulSets.Items{
fmt.Println(v.Name)
fmt.Println(v.Labels)
}
err2 := c.kubeclient.StatefulSets("824b2e9dcc4d461a852ddea20369d377").Delete("grd1b4e0",meta_v1.NewDeleteOptions(0))
fmt.Println("----------------------")
for _,v:=range ReplicationControllers.Items{
fmt.Println(v.Name)
fmt.Println(v.Labels)
}
if err2!=nil{
fmt.Println("错误",err2)
}
}
func (c *CheanManager) cleanStatefulset(){
StatefulSetsMap := make(map[string][]string)
ReplicationControllersMap := make(map[string][]string)
StadeleteList := make([]string,0,20)
RepdeleteList := make([]string,0,20)
//StatefulSets, _ := c.kubeclient.StatefulSets("824b2e9dcc4d461a852ddea20369d377").List(meta_v1.ListOptions{})
//ReplicationControllers, _ := c.kubeclient.ReplicationControllers("c69c40ecedae41ca9fbb6c3cec0926f2").List(meta_v1.ListOptions{})
//
//for _,v:=range StatefulSets.Items{
// fmt.Println(v.Name)
// fmt.Println(v.Labels)
//}
//err2 := c.kubeclient.StatefulSets("824b2e9dcc4d461a852ddea20369d377").Delete("grd1b4e0",meta_v1.NewDeleteOptions(0))
//
//fmt.Println("----------------------")
//for _,v:=range ReplicationControllers.Items{
// fmt.Println(v.Name)
// fmt.Println(v.Labels)
//}
//if err2!=nil{
// fmt.Println("错误",err2)
//}
//
isDeleteList,err := db.GetManager().K8sDeployReplicationDao().GetK8sDeployReplicationByIsDelete(true)
if err!= nil{
logrus.Error(err)
}
for _,v := range isDeleteList {
if v.ReplicationType == "statefulset"{
if _,ok := StatefulSetsMap[v.TenantID];ok{
StatefulSetsMap[v.TenantID] = append(StatefulSetsMap[v.TenantID], v.ReplicationID)
}else {
StatefulSetsMap[v.TenantID] = []string{v.ReplicationID}
}
}
if v.ReplicationType == "replicationcontroller"{
if _,ok := ReplicationControllersMap[v.TenantID];ok{
ReplicationControllersMap[v.TenantID] = append(StatefulSetsMap[v.TenantID], v.ReplicationID)
}else {
ReplicationControllersMap[v.TenantID] = []string{v.ReplicationID}
}
}
}
for k,valuse := range StatefulSetsMap{
StatefulSetsList,err := c.kubeclient.StatefulSets(k).List(meta_v1.ListOptions{})
if err != nil{
logrus.Error("错误3",err)
}
for _,v := range StatefulSetsList.Items{
if !InSlice(v.Name,valuse){
StadeleteList = append(StadeleteList, v.Name)
}
}
}
for k,valuse := range ReplicationControllersMap{
ReplicationControllersList,err := c.kubeclient.ReplicationControllers(k).List(meta_v1.ListOptions{})
if err != nil{
logrus.Error("错误4",err)
}
for _,v := range ReplicationControllersList.Items{
if !InSlice(v.Name,valuse){
RepdeleteList = append(RepdeleteList, v.Name)
}
}
}
fmt.Println("StadeleteList",StadeleteList)
fmt.Println("RepdeleteList",RepdeleteList)
}

View File

@ -271,6 +271,7 @@ type K8sDeployReplicationDao interface {
DeleteK8sDeployReplicationByService(serviceID string) error
GetReplications() ([]*model.K8sDeployReplication, error)
BeachDelete([]uint) error
GetK8sDeployReplicationByIsDelete(isDelete bool) ([]*model.K8sDeployReplication, error)
}
//K8sPodDao pod info dao

View File

@ -223,6 +223,14 @@ func (t *K8sDeployReplicationDaoImpl) DeleteK8sDeployReplicationByService(servic
return nil
}
func (t *K8sDeployReplicationDaoImpl) GetK8sDeployReplicationByIsDelete(isDelete bool) ([]*model.K8sDeployReplication, error) {
var deploy []*model.K8sDeployReplication
if err := t.DB.Model(&deploy).Where("is_delete=?", isDelete).Find(&deploy).Error; err != nil {
return nil, err
}
return deploy, nil
}
//K8sPodDaoImpl k8s pod dao
type K8sPodDaoImpl struct {
DB *gorm.DB