[ADD] add test code

This commit is contained in:
zhoujunhao 2018-06-13 14:52:45 +08:00
parent e80d6cba78
commit 865397a083
3 changed files with 420 additions and 254 deletions

View File

@ -11,88 +11,307 @@ import (
"fmt"
)
type CheanUp interface {
//Resource should be clean resource
type Resource interface {
IsTimeout() bool
DeleteResources(map[string]string)
DeleteResources() error
IsClean() bool
Name() string
Type() string
}
type tenantServiceResource struct {
manager *Manager
id string
namespaces string
createTime time.Time
}
type rcResource struct {
manager *Manager
id string
namespaces string
createTime time.Time
}
type statefulResource struct {
manager *Manager
id string
namespaces string
createTime time.Time
}
type deploymentResource struct {
manager *Manager
id string
namespaces string
createTime time.Time
}
type k8sServiceResource struct {
manager *Manager
id string
namespaces string
createTime time.Time
}
func (k *k8sServiceResource) IsTimeout() bool {
now := time.Now()
if now.After(k.createTime.Add(time.Second * 0)) {
return true
}
return false
}
type CheanManager struct {
ctx context.Context
kubeclient *kubernetes.Clientset
data map[string]string
period time.Time
genre string
func (k *k8sServiceResource) DeleteResources() error {
if err := k.manager.kubeclient.Services(k.namespaces).Delete(k.id, &meta_v1.DeleteOptions{}); err != nil {
logrus.Error(err)
return err
} else {
logrus.Info("delete k8sServiceResource success", k.id)
return nil
}
}
func (k *k8sServiceResource) IsClean() bool {
isNotExist := db.GetManager().K8sServiceDao().K8sServiceIsExist(k.namespaces, k.id)
var TaskSlice = make([]*CheanManager, 0, 100)
if isNotExist {
return true
} else {
return false
}
}
func NewCheanManager(ctx context.Context, kubeclient *kubernetes.Clientset) *CheanManager {
m := &CheanManager{
ctx: ctx,
kubeclient: kubeclient,
func (k *k8sServiceResource) Name() string {
return k.id
}
func (k *k8sServiceResource) Type() string {
return "k8sService"
}
func queryK8sServiceResource(m *Manager) []Resource {
ServivesMap := make(map[string][]string)
services, err := db.GetManager().K8sServiceDao().GetAllK8sService()
if err != nil {
logrus.Error(err)
}
return m
}
for _, v := range services {
func (c *CheanManager) Start() {
logrus.Info("clean up module starts....")
c.CollectingTasks()
fmt.Println("TaskSlice",TaskSlice)
c.PerformTasks()
}
// InSlice checks given string in string slice or not.
func InSlice(v string, sl []string) bool {
for _, vv := range sl {
if vv == v {
return true
if _, ok := ServivesMap[v.TenantID]; ok {
ServivesMap[v.TenantID] = append(ServivesMap[v.TenantID], v.K8sServiceID)
} else {
ServivesMap[v.TenantID] = []string{v.K8sServiceID}
}
}
for k, valuse := range ServivesMap {
ServicesList, err := m.kubeclient.Services(k).List(meta_v1.ListOptions{})
if err != nil {
logrus.Error(err)
}
for _, v := range ServicesList.Items {
if !InSlice(v.Name, valuse) {
s := &k8sServiceResource{
createTime: time.Now(),
namespaces: k,
id: v.Name,
}
m.waiting = append(m.waiting, s)
}
}
}
return nil
}
func (d *deploymentResource) IsTimeout() bool {
now := time.Now()
if now.After(d.createTime.Add(time.Second * 0)) {
return true
}
return false
}
func (d *deploymentResource) DeleteResources() error {
if err := d.manager.kubeclient.AppsV1beta1().Deployments(d.namespaces).Delete(d.id, &meta_v1.DeleteOptions{}); err != nil {
logrus.Error(err)
return err
} else {
logrus.Info("delete deployment success", d.id)
return nil
}
}
func (d *deploymentResource) IsClean() bool {
isNotExist := db.GetManager().K8sDeployReplicationDao().GetK8sDeployReplicationIsExist(d.namespaces, "statefulset", d.id, false)
if isNotExist {
return true
} else {
return false
}
}
func (d *deploymentResource) Name() string {
return d.id
}
func (d *deploymentResource) Type() string {
return "deployment"
}
func queryDeploymentResource(m *Manager) []Resource {
DeploymentMap := make(map[string][]string)
DeleteList, err := db.GetManager().K8sDeployReplicationDao().GetK8sDeployReplicationByIsDelete("deployment", true)
if err != nil {
logrus.Error(err)
}
for _, v := range DeleteList {
if _, ok := DeploymentMap[v.TenantID]; ok {
DeploymentMap[v.TenantID] = append(DeploymentMap[v.TenantID], v.ReplicationID)
} else {
DeploymentMap[v.TenantID] = []string{v.ReplicationID}
}
}
for k, valuse := range DeploymentMap {
DeploymentList, err := m.kubeclient.AppsV1beta1().Deployments(k).List(meta_v1.ListOptions{})
if err != nil {
logrus.Error(err)
}
for _, v := range DeploymentList.Items {
if InSlice(v.Name, valuse) {
s := &deploymentResource{
createTime: time.Now(),
namespaces: k,
id: v.Name,
}
m.waiting = append(m.waiting, s)
}
}
}
return nil
}
func (s *statefulResource) IsTimeout() bool {
now := time.Now()
if now.After(s.createTime.Add(time.Second * 0)) {
return true
}
return false
}
func (s *statefulResource) DeleteResources() error {
if err := s.manager.kubeclient.StatefulSets(s.namespaces).Delete(s.id, &meta_v1.DeleteOptions{}); err != nil {
logrus.Error(err)
return err
} else {
logrus.Info("delete statefulset success", s.id)
return nil
}
}
func (s *statefulResource) IsClean() bool {
isNotExist := db.GetManager().K8sDeployReplicationDao().GetK8sDeployReplicationIsExist(s.namespaces, "statefulset", s.id, false)
if isNotExist {
return true
} else {
return false
}
}
func (s *statefulResource) Name() string {
return s.id
}
func (s *statefulResource) Type() string {
return "statefulset"
}
func queryStatefulResource(m *Manager) []Resource {
StatefulSetsMap := make(map[string][]string)
DeleteList, err := db.GetManager().K8sDeployReplicationDao().GetK8sDeployReplicationByIsDelete("statefulset", true)
if err != nil {
logrus.Error(err)
}
for _, v := range DeleteList {
if _, ok := StatefulSetsMap[v.TenantID]; ok {
StatefulSetsMap[v.TenantID] = append(StatefulSetsMap[v.TenantID], v.ReplicationID)
} else {
StatefulSetsMap[v.TenantID] = []string{v.ReplicationID}
}
}
for k, valuse := range StatefulSetsMap {
StatefulSetsList, err := m.kubeclient.StatefulSets(k).List(meta_v1.ListOptions{})
if err != nil {
logrus.Error(err)
}
for _, v := range StatefulSetsList.Items {
if InSlice(v.Name, valuse) {
s := &statefulResource{
createTime: time.Now(),
namespaces: k,
id: v.Name,
}
m.waiting = append(m.waiting, s)
}
}
}
return nil
}
func (t *tenantServiceResource) IsTimeout() bool {
now := time.Now()
if now.After(t.createTime.Add(time.Second * 0)) {
return true
}
return false
}
// SliceDiff returns diff slice of slice1 - slice2.
func SliceDiff(slice1, slice2 []string) (diffSlice []string) {
for _, v := range slice1 {
if !InSlice(v, slice2) {
diffSlice = append(diffSlice, v)
}
}
return
func (t *tenantServiceResource) DeleteResources() error {
//if err := t.manager.kubeclient.Namespaces().Delete(t.namespaces, &meta_v1.DeleteOptions{}); err != nil {
// logrus.Error(err)
// return err
//} else {
// logrus.Info("delete namespaces success", t.namespaces)
// return nil
//}
fmt.Println("删除", t.id, t.namespaces)
return nil
}
// SliceIntersect returns slice that are present in all the slice1 and slice2.
func SliceIntersect(slice1, slice2 []string) (IntersectSlice []string) {
for _, v := range slice1 {
if InSlice(v, slice2) {
IntersectSlice = append(IntersectSlice, v)
}
func (t *tenantServiceResource) IsClean() bool {
isNotExist := db.GetManager().TenantDao().GetTenantByUUIDIsExist(t.namespaces)
fmt.Println("isNotExist", isNotExist)
if isNotExist {
return true
}
return
return false
}
func MapIntersect(map1, map2 map[string]string) (IntersectMap map[string]string) {
intersectMap := make(map[string]string)
for k, v := range map2 {
v2, ok := map1[k]
if ok {
if v == v2 {
intersectMap[k] = v
}
}
}
return intersectMap
func (t *tenantServiceResource) Name() string {
return t.id
}
func (c *CheanManager) cleanNamespaces() {
func (t *tenantServiceResource) Type() string {
return "namespaces"
}
func queryTenantServiceResource(m *Manager) []Resource {
nameList := make([]string, 0, 200)
allList := make([]string, 0, 300)
diffMap := make(map[string]string)
Namespaces, err := c.kubeclient.CoreV1().Namespaces().List(meta_v1.ListOptions{})
Namespaces, err := m.kubeclient.CoreV1().Namespaces().List(meta_v1.ListOptions{})
if err != nil {
logrus.Error(err)
}
@ -115,216 +334,163 @@ func (c *CheanManager) cleanNamespaces() {
diffList := SliceDiff(nameList, allList)
for _, v := range diffList {
diffMap[v] = v
s := &tenantServiceResource{
createTime: time.Now(),
id: v,
namespaces: v,
}
m.waiting = append(m.waiting, s)
}
fmt.Println("diffMap:",diffMap)
TaskSlice = append(TaskSlice, &CheanManager{
data: diffMap,
period: time.Now(),
genre: "namespaces",
})
fmt.Println("1结束")
return nil
}
func (c *CheanManager) cleanStaAndRep() {
StatefulSetsMap := make(map[string][]string)
ReplicationControllersMap := make(map[string][]string)
StadeleteMap := make(map[string]string)
RepdeleteMap := make(map[string]string)
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(ReplicationControllersMap[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(err)
}
for _, v := range StatefulSetsList.Items {
if InSlice(v.Name, valuse) {
StadeleteMap[k] = v.Name
}
}
}
for k, valuse := range ReplicationControllersMap {
ReplicationControllersList, err := c.kubeclient.ReplicationControllers(k).List(meta_v1.ListOptions{})
if err != nil {
logrus.Error(err)
}
for _, v := range ReplicationControllersList.Items {
if InSlice(v.Name, valuse) {
RepdeleteMap[k] = v.Name
}
}
}
fmt.Println("statefulset",StadeleteMap)
TaskSlice = append(TaskSlice, &CheanManager{
data: StadeleteMap,
period: time.Now(),
genre: "statefulset",
})
fmt.Println("2结束")
fmt.Println("replicationcontroller",RepdeleteMap)
TaskSlice = append(TaskSlice, &CheanManager{
data: RepdeleteMap,
period: time.Now(),
genre: "replicationcontroller",
})
fmt.Println("3结束")
}
func (c *CheanManager) cleanService() {
ServivesMap := make(map[string][]string)
ServivesDeleteMap := make(map[string]string)
services, err := db.GetManager().K8sServiceDao().GetAllK8sService()
if err != nil {
logrus.Error(err)
}
for _, v := range services {
if _, ok := ServivesMap[v.TenantID]; ok {
ServivesMap[v.TenantID] = append(ServivesMap[v.TenantID], v.K8sServiceID)
} else {
ServivesMap[v.TenantID] = []string{v.K8sServiceID}
}
}
for k, valuse := range ServivesMap {
ServicesList, err := c.kubeclient.Services(k).List(meta_v1.ListOptions{})
if err != nil {
logrus.Error(err)
}
for _, v := range ServicesList.Items {
if !InSlice(v.Name, valuse) {
ServivesDeleteMap[k] = v.Name
}
}
}
fmt.Println("services",ServivesDeleteMap)
TaskSlice = append(TaskSlice, &CheanManager{
data: ServivesDeleteMap,
period: time.Now(),
genre: "services",
})
fmt.Println("4结束")
}
func (c *CheanManager) IsTimeout() bool {
func (r *rcResource) IsTimeout() bool {
now := time.Now()
if now.After(c.period.Add(time.Second *0)) {
if now.After(r.createTime.Add(time.Second * 0)) {
return true
}
return false
}
func (c *CheanManager) DeleteResources(deleteMap map[string]string) {
if c.genre == "namespaces" {
for _, v := range deleteMap {
isExist := db.GetManager().TenantDao().GetTenantByUUIDIsExist(v)
fmt.Println("isExist",isExist)
if isExist {
if err := c.kubeclient.Namespaces().Delete(v, &meta_v1.DeleteOptions{}); err != nil {
logrus.Error(err)
} else {
logrus.Info("delete namespaces success", v)
}
}
}
}
if c.genre == "statefulset" {
for k, v := range deleteMap {
isExist := db.GetManager().K8sDeployReplicationDao().GetK8sDeployReplicationIsExist(k, "statefulset", v, false)
if isExist {
if err := c.kubeclient.StatefulSets(k).Delete(v, &meta_v1.DeleteOptions{}); err != nil {
logrus.Error(err)
} else {
logrus.Info("delete statefulset success", v)
}
}
}
}
if c.genre == "replicationcontroller" {
for k, v := range deleteMap {
isExist := db.GetManager().K8sDeployReplicationDao().GetK8sDeployReplicationIsExist(k, "replicationcontroller", v, false)
if isExist {
if err := c.kubeclient.ReplicationControllers(k).Delete(v, &meta_v1.DeleteOptions{}); err != nil {
logrus.Error(err)
} else {
logrus.Info("delete replicationcontroller success", v)
}
}
}
}
if c.genre == "services" {
for k, v := range deleteMap {
isExist := db.GetManager().K8sServiceDao().K8sServiceIsExist(k, v)
if isExist {
if err := c.kubeclient.Services(k).Delete(v, &meta_v1.DeleteOptions{}); err != nil {
logrus.Error(err)
} else {
logrus.Info("delete service success", v)
}
}
}
func (r *rcResource) DeleteResources() error {
if err := r.manager.kubeclient.ReplicationControllers(r.namespaces).Delete(r.id, &meta_v1.DeleteOptions{}); err != nil {
logrus.Error(err)
return err
} else {
logrus.Info("delete replicationcontroller success", r.id)
return nil
}
}
func (c *CheanManager) CollectingTasks() {
run := func() { util.Exec(c.ctx, func() error {
c.cleanNamespaces()
c.cleanStaAndRep()
c.cleanService()
return nil
}, time.Minute*24)}
func (r *rcResource) IsClean() bool {
isNotExist := db.GetManager().K8sDeployReplicationDao().GetK8sDeployReplicationIsExist(r.namespaces, "replicationcontroller", r.id, false)
if isNotExist {
return true
} else {
return false
}
}
func (r *rcResource) Name() string {
return r.id
}
func (r *rcResource) Type() string {
return "replicationcontroller"
}
func queryRcResource(m *Manager) []Resource {
ReplicationControllersMap := make(map[string][]string)
DeleteList, err := db.GetManager().K8sDeployReplicationDao().GetK8sDeployReplicationByIsDelete("replicationcontroller", true)
if err != nil {
logrus.Error(err)
}
for _, v := range DeleteList {
if _, ok := ReplicationControllersMap[v.TenantID]; ok {
ReplicationControllersMap[v.TenantID] = append(ReplicationControllersMap[v.TenantID], v.ReplicationID)
} else {
ReplicationControllersMap[v.TenantID] = []string{v.ReplicationID}
}
}
for k, valuse := range ReplicationControllersMap {
ReplicationControllersList, err := m.kubeclient.ReplicationControllers(k).List(meta_v1.ListOptions{})
if err != nil {
logrus.Error(err)
}
for _, v := range ReplicationControllersList.Items {
if InSlice(v.Name, valuse) {
s := &rcResource{
namespaces: k,
id: v.Name,
createTime: time.Now(),
}
m.waiting = append(m.waiting, s)
}
}
}
return nil
}
type Manager struct {
ctx context.Context
kubeclient *kubernetes.Clientset
waiting []Resource
queryResource []func(*Manager) []Resource
}
func NewManager(ctx context.Context, kubeclient *kubernetes.Clientset) *Manager {
m := &Manager{
ctx: ctx,
kubeclient: kubeclient,
}
queryResource := []func(*Manager) []Resource{
queryRcResource,
queryTenantServiceResource,
queryStatefulResource,
queryDeploymentResource,
queryK8sServiceResource,
}
m.queryResource = queryResource
return m
}
func (m *Manager) Start() {
logrus.Info("clean up module starts....")
m.CollectingTasks()
m.PerformTasks()
}
// InSlice checks given string in string slice or not.
func InSlice(v string, sl []string) bool {
for _, vv := range sl {
if vv == v {
return true
}
}
return false
}
// SliceDiff returns diff slice of slice1 - slice2.
func SliceDiff(slice1, slice2 []string) (diffSlice []string) {
for _, v := range slice1 {
if !InSlice(v, slice2) {
diffSlice = append(diffSlice, v)
}
}
return
}
func (m *Manager) CollectingTasks() {
run := func() {
util.Exec(m.ctx, func() error {
for _, v := range m.queryResource {
v(m)
}
return nil
}, time.Minute*24)
}
go run()
}
func (c *CheanManager) PerformTasks() {
run := func() {util.Exec(c.ctx, func() error {
fmt.Println("长度:",len(TaskSlice))
for _, v := range TaskSlice {
if v.IsTimeout() {
v.DeleteResources(v.data)
func (m *Manager) PerformTasks() {
run := func() {
util.Exec(m.ctx, func() error {
fmt.Println("长度", m.waiting)
for _, v := range m.waiting {
if v.IsTimeout() {
if v.IsClean() {
v.DeleteResources()
}
}
}
}
return nil
}, time.Minute*12)}
fmt.Println("结束")
m.waiting = nil
return nil
}, time.Minute*12)
}
go run()
}

View File

@ -274,7 +274,7 @@ type K8sDeployReplicationDao interface {
DeleteK8sDeployReplicationByService(serviceID string) error
GetReplications() ([]*model.K8sDeployReplication, error)
BeachDelete([]uint) error
GetK8sDeployReplicationByIsDelete(isDelete bool) ([]*model.K8sDeployReplication, error)
GetK8sDeployReplicationByIsDelete(rcType string, isDelete bool) ([]*model.K8sDeployReplication, error)
GetK8sDeployReplicationIsExist(tenantId string, RcType string, RcId string, isDelete bool) (IsExist bool)
}

View File

@ -239,9 +239,9 @@ func (t *K8sDeployReplicationDaoImpl) DeleteK8sDeployReplicationByService(servic
return nil
}
func (t *K8sDeployReplicationDaoImpl) GetK8sDeployReplicationByIsDelete(isDelete bool) ([]*model.K8sDeployReplication, error) {
func (t *K8sDeployReplicationDaoImpl) GetK8sDeployReplicationByIsDelete(rcType string, isDelete bool) ([]*model.K8sDeployReplication, error) {
var deploy []*model.K8sDeployReplication
if err := t.DB.Model(&deploy).Where("is_delete=?", isDelete).Find(&deploy).Error; err != nil {
if err := t.DB.Model(&deploy).Where("rc_type=? AND is_delete=?",rcType, isDelete).Find(&deploy).Error; err != nil {
return nil, err
}
return deploy, nil