Merge branch 'master' into unhealthcluster

This commit is contained in:
凡羊羊 2019-11-06 18:41:07 +08:00
commit d88bafc901
12 changed files with 1144 additions and 18 deletions

View File

@ -546,7 +546,23 @@ func (t *TenantStruct) GetTenants(w http.ResponseWriter, r *http.Request) {
//DeleteTenant DeleteTenant //DeleteTenant DeleteTenant
func (t *TenantStruct) DeleteTenant(w http.ResponseWriter, r *http.Request) { func (t *TenantStruct) DeleteTenant(w http.ResponseWriter, r *http.Request) {
httputil.ReturnError(r, w, 400, "this rainbond version can not support delete tenant") tenantID := r.Context().Value(middleware.ContextKey("tenant_id")).(string)
if err := handler.GetTenantManager().DeleteTenant(tenantID); err != nil {
if err == handler.ErrTenantStillHasServices || err == handler.ErrTenantStillHasPlugins {
httputil.ReturnError(r, w, 400, err.Error())
return
}
if err == gorm.ErrRecordNotFound {
httputil.ReturnError(r, w, 404, err.Error())
return
}
httputil.ReturnError(r, w, 500, fmt.Sprintf("delete tenant: %v", err))
return
}
httputil.ReturnSuccess(r, w, nil)
} }
//UpdateTenant UpdateTenant //UpdateTenant UpdateTenant

View File

@ -32,23 +32,23 @@ import (
"github.com/goodrain/rainbond/cmd/api/option" "github.com/goodrain/rainbond/cmd/api/option"
"github.com/goodrain/rainbond/db" "github.com/goodrain/rainbond/db"
dbmodel "github.com/goodrain/rainbond/db/model" dbmodel "github.com/goodrain/rainbond/db/model"
"github.com/goodrain/rainbond/mq/api/grpc/pb" mqclient "github.com/goodrain/rainbond/mq/client"
cli "github.com/goodrain/rainbond/node/nodem/client" cli "github.com/goodrain/rainbond/node/nodem/client"
"github.com/goodrain/rainbond/worker/client" "github.com/goodrain/rainbond/worker/client"
) )
//TenantAction tenant act //TenantAction tenant act
type TenantAction struct { type TenantAction struct {
MQClient pb.TaskQueueClient MQClient mqclient.MQClient
statusCli *client.AppRuntimeSyncClient statusCli *client.AppRuntimeSyncClient
OptCfg *option.Config OptCfg *option.Config
} }
//CreateTenManager create Manger //CreateTenManager create Manger
func CreateTenManager(MQClient pb.TaskQueueClient, statusCli *client.AppRuntimeSyncClient, func CreateTenManager(mqc mqclient.MQClient, statusCli *client.AppRuntimeSyncClient,
optCfg *option.Config) *TenantAction { optCfg *option.Config) *TenantAction {
return &TenantAction{ return &TenantAction{
MQClient: MQClient, MQClient: mqc,
statusCli: statusCli, statusCli: statusCli,
OptCfg: optCfg, OptCfg: optCfg,
} }
@ -104,6 +104,53 @@ func (t *TenantAction) UpdateTenant(tenant *dbmodel.Tenants) error {
return db.GetManager().TenantDao().UpdateModel(tenant) return db.GetManager().TenantDao().UpdateModel(tenant)
} }
// DeleteTenant deletes tenant based on the given tenantID.
//
// tenant can only be deleted without service or plugin
func (t *TenantAction) DeleteTenant(tenantID string) error {
// check if there are still services
services, err := db.GetManager().TenantServiceDao().ListServicesByTenantID(tenantID)
if err != nil {
return err
}
if len(services) > 0 {
return ErrTenantStillHasServices
}
// check if there are still plugins
plugins, err := db.GetManager().TenantPluginDao().ListByTenantID(tenantID)
if err != nil {
return err
}
if len(plugins) > 0 {
return ErrTenantStillHasPlugins
}
tenant, err := db.GetManager().TenantDao().GetTenantByUUID(tenantID)
if err != nil {
return err
}
tenant.Status = dbmodel.TenantStatusDeleting.String()
if err := db.GetManager().TenantDao().UpdateModel(tenant); err != nil {
return err
}
// delete namespace in k8s
err = t.MQClient.SendBuilderTopic(mqclient.TaskStruct{
TaskType: "delete_tenant",
Topic: mqclient.WorkerTopic,
TaskBody: map[string]string{
"tenant_id": tenantID,
},
})
if err != nil {
logrus.Error("send task 'delete tenant'", err)
return err
}
return nil
}
//TotalMemCPU StatsMemCPU //TotalMemCPU StatsMemCPU
func (t *TenantAction) TotalMemCPU(services []*dbmodel.TenantServices) (*api_model.StatsInfo, error) { func (t *TenantAction) TotalMemCPU(services []*dbmodel.TenantServices) (*api_model.StatsInfo, error) {
cpus := 0 cpus := 0

View File

@ -44,4 +44,5 @@ type TenantHandler interface {
IsClosedStatus(status string) bool IsClosedStatus(status string) bool
BindTenantsResource(source []*dbmodel.Tenants) api_model.TenantList BindTenantsResource(source []*dbmodel.Tenants) api_model.TenantList
UpdateTenant(*dbmodel.Tenants) error UpdateTenant(*dbmodel.Tenants) error
DeleteTenant(tenantID string) error
} }

28
api/handler/types.go Normal file
View File

@ -0,0 +1,28 @@
// Copyright (C) 2014-2018 Goodrain Co., Ltd.
// RAINBOND, Application Management Platform
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version. For any non-GPL usage of Rainbond,
// one or multiple Commercial Licenses authorized by Goodrain Co., Ltd.
// must be obtained first.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package handler
import "errors"
var (
// ErrTenantStillHasServices -
ErrTenantStillHasServices = errors.New("tenant still has services")
// ErrTenantStillHasPlugins -
ErrTenantStillHasPlugins = errors.New("tenant still has plugins")
)

View File

@ -52,6 +52,7 @@ type TenantDao interface {
GetTenantIDsByNames(names []string) ([]string, error) GetTenantIDsByNames(names []string) ([]string, error)
GetTenantLimitsByNames(names []string) (map[string]int, error) GetTenantLimitsByNames(names []string) (map[string]int, error)
GetTenantByUUIDIsExist(uuid string) bool GetTenantByUUIDIsExist(uuid string) bool
DelByTenantID(tenantID string) error
} }
//AppDao tenant dao //AppDao tenant dao
@ -87,6 +88,7 @@ type TenantServiceDao interface {
GetAllServicesID() ([]*model.TenantServices, error) GetAllServicesID() ([]*model.TenantServices, error)
UpdateDeployVersion(serviceID, deployversion string) error UpdateDeployVersion(serviceID, deployversion string) error
ListThirdPartyServices() ([]*model.TenantServices, error) ListThirdPartyServices() ([]*model.TenantServices, error)
ListServicesByTenantID(tenantID string) ([]*model.TenantServices, error)
} }
//TenantServiceDeleteDao TenantServiceDeleteDao //TenantServiceDeleteDao TenantServiceDeleteDao
@ -119,6 +121,7 @@ type TenantPluginDao interface {
DeletePluginByID(pluginID, tenantID string) error DeletePluginByID(pluginID, tenantID string) error
GetPluginsByTenantID(tenantID string) ([]*model.TenantPlugin, error) GetPluginsByTenantID(tenantID string) ([]*model.TenantPlugin, error)
ListByIDs(ids []string) ([]*model.TenantPlugin, error) ListByIDs(ids []string) ([]*model.TenantPlugin, error)
ListByTenantID(tenantID string) ([]*model.TenantPlugin, error)
} }
//TenantPluginDefaultENVDao TenantPluginDefaultENVDao //TenantPluginDefaultENVDao TenantPluginDefaultENVDao

View File

@ -41,6 +41,22 @@ type Interface interface {
TableName() string TableName() string
} }
// TenantStatus -
type TenantStatus string
var (
// TenantStatusNormal -
TenantStatusNormal TenantStatus = "normal"
// TenantStatusDeleting -
TenantStatusDeleting TenantStatus = "deleting"
// TenantStatusDeleteFailed -
TenantStatusDeleteFailed TenantStatus = "delete_failed"
)
func (t TenantStatus) String() string {
return string(t)
}
//Tenants 租户信息 //Tenants 租户信息
type Tenants struct { type Tenants struct {
Model Model
@ -48,6 +64,7 @@ type Tenants struct {
UUID string `gorm:"column:uuid;size:33;unique_index"` UUID string `gorm:"column:uuid;size:33;unique_index"`
EID string `gorm:"column:eid"` EID string `gorm:"column:eid"`
LimitMemory int `gorm:"column:limit_memory"` LimitMemory int `gorm:"column:limit_memory"`
Status string `gorm:"column:status;default:'normal'"`
} }
//TableName 返回租户表名称 //TableName 返回租户表名称

View File

@ -95,6 +95,16 @@ func (t *PluginDaoImpl) GetPluginsByTenantID(tenantID string) ([]*model.TenantPl
return plugins, nil return plugins, nil
} }
// ListByTenantID -
func (t *PluginDaoImpl) ListByTenantID(tenantID string) ([]*model.TenantPlugin, error) {
var plugins []*model.TenantPlugin
if err := t.DB.Where("tenant_id=?", tenantID).Find(&plugins).Error; err != nil {
return nil, err
}
return plugins, nil
}
//PluginDefaultENVDaoImpl PluginDefaultENVDaoImpl //PluginDefaultENVDaoImpl PluginDefaultENVDaoImpl
type PluginDefaultENVDaoImpl struct { type PluginDefaultENVDaoImpl struct {
DB *gorm.DB DB *gorm.DB

View File

@ -149,9 +149,8 @@ func (t *TenantDaoImpl) GetTenantLimitsByNames(names []string) (limit map[string
return return
} }
//GetALLTenants GetALLTenants // GetPagedTenants -
func (t *TenantDaoImpl) GetPagedTenants(offset, len int) ([]*model.Tenants, error) { func (t *TenantDaoImpl) GetPagedTenants(offset, len int) ([]*model.Tenants, error) {
var tenants []*model.Tenants var tenants []*model.Tenants
if err := t.DB.Find(&tenants).Group("").Error; err != nil { if err := t.DB.Find(&tenants).Group("").Error; err != nil {
return nil, err return nil, err
@ -159,6 +158,15 @@ func (t *TenantDaoImpl) GetPagedTenants(offset, len int) ([]*model.Tenants, erro
return tenants, nil return tenants, nil
} }
// DelByTenantID -
func (t *TenantDaoImpl) DelByTenantID(tenantID string) error {
if err := t.DB.Where("uuid=?", tenantID).Delete(&model.Tenants{}).Error; err != nil {
return err
}
return nil
}
//TenantServicesDaoImpl 租户应用dao //TenantServicesDaoImpl 租户应用dao
type TenantServicesDaoImpl struct { type TenantServicesDaoImpl struct {
DB *gorm.DB DB *gorm.DB
@ -176,6 +184,16 @@ func (t *TenantServicesDaoImpl) GetAllServicesID() ([]*model.TenantServices, err
return services, nil return services, nil
} }
// ListServicesByTenantID -
func (t *TenantServicesDaoImpl) ListServicesByTenantID(tenantID string) ([]*model.TenantServices, error) {
var services []*model.TenantServices
if err := t.DB.Where("tenant_id=?", tenantID).Find(&services).Error; err != nil {
return nil, err
}
return services, nil
}
//UpdateDeployVersion update service current deploy version //UpdateDeployVersion update service current deploy version
func (t *TenantServicesDaoImpl) UpdateDeployVersion(serviceID, deployversion string) error { func (t *TenantServicesDaoImpl) UpdateDeployVersion(serviceID, deployversion string) error {
if err := t.DB.Exec("update tenant_services set deploy_version=? where service_id=?", deployversion, serviceID).Error; err != nil { if err := t.DB.Exec("update tenant_services set deploy_version=? where service_id=?", deployversion, serviceID).Error; err != nil {
@ -216,7 +234,7 @@ func (t *TenantServicesDaoImpl) GetServiceByID(serviceID string) (*model.TenantS
return &service, nil return &service, nil
} }
//GetServiceByID 获取服务通过服务别名 //GetServiceByServiceAlias 获取服务通过服务别名
func (t *TenantServicesDaoImpl) GetServiceByServiceAlias(serviceAlias string) (*model.TenantServices, error) { func (t *TenantServicesDaoImpl) GetServiceByServiceAlias(serviceAlias string) (*model.TenantServices, error) {
var service model.TenantServices var service model.TenantServices
if err := t.DB.Where("service_alias=?", serviceAlias).Find(&service).Error; err != nil { if err := t.DB.Where("service_alias=?", serviceAlias).Find(&service).Error; err != nil {
@ -441,7 +459,7 @@ func (t *TenantServicesDaoImpl) DeleteServiceByServiceID(serviceID string) error
return nil return nil
} }
// ListThirdPartyService lists all third party services // ListThirdPartyServices lists all third party services
func (t *TenantServicesDaoImpl) ListThirdPartyServices() ([]*model.TenantServices, error) { func (t *TenantServicesDaoImpl) ListThirdPartyServices() ([]*model.TenantServices, error) {
var res []*model.TenantServices var res []*model.TenantServices
if err := t.DB.Where("kind=?", model.ServiceKindThirdParty.String()).Find(&res).Error; err != nil { if err := t.DB.Where("kind=?", model.ServiceKindThirdParty.String()).Find(&res).Error; err != nil {
@ -476,6 +494,7 @@ func (t *TenantServicesDeleteImpl) UpdateModel(mo model.Interface) error {
return nil return nil
} }
// GetTenantServicesDeleteByCreateTime -
func (t *TenantServicesDeleteImpl) GetTenantServicesDeleteByCreateTime(createTime time.Time) ([]*model.TenantServicesDelete, error) { func (t *TenantServicesDeleteImpl) GetTenantServicesDeleteByCreateTime(createTime time.Time) ([]*model.TenantServicesDelete, error) {
var ServiceDel []*model.TenantServicesDelete var ServiceDel []*model.TenantServicesDelete
if err := t.DB.Where("create_time < ?", createTime).Find(&ServiceDel).Error; err != nil { if err := t.DB.Where("create_time < ?", createTime).Find(&ServiceDel).Error; err != nil {
@ -487,6 +506,7 @@ func (t *TenantServicesDeleteImpl) GetTenantServicesDeleteByCreateTime(createTim
return ServiceDel, nil return ServiceDel, nil
} }
// DeleteTenantServicesDelete -
func (t *TenantServicesDeleteImpl) DeleteTenantServicesDelete(record *model.TenantServicesDelete) error { func (t *TenantServicesDeleteImpl) DeleteTenantServicesDelete(record *model.TenantServicesDelete) error {
if err := t.DB.Delete(record).Error; err != nil { if err := t.DB.Delete(record).Error; err != nil {
return err return err
@ -584,7 +604,7 @@ func (t *TenantServicesPortDaoImpl) GetPort(serviceID string, port int) (*model.
return &oldPort, nil return &oldPort, nil
} }
// GetEnablePort returns opened ports. // GetOpenedPorts returns opened ports.
func (t *TenantServicesPortDaoImpl) GetOpenedPorts(serviceID string) ([]*model.TenantServicesPort, error) { func (t *TenantServicesPortDaoImpl) GetOpenedPorts(serviceID string) ([]*model.TenantServicesPort, error) {
var ports []*model.TenantServicesPort var ports []*model.TenantServicesPort
if err := t.DB.Where("service_id = ? and (is_inner_service=1 or is_outer_service=1)", serviceID). if err := t.DB.Where("service_id = ? and (is_inner_service=1 or is_outer_service=1)", serviceID).
@ -1411,7 +1431,7 @@ func (t *ServiceLabelDaoImpl) GetTenantServiceTypeLabel(serviceID string) (*mode
return &label, nil return &label, nil
} }
//DELTenantServiceLabelsByLabelvaluesAndServiceID DELTenantServiceLabelsByLabelvaluesAndServiceID //DelTenantServiceLabelsByLabelValuesAndServiceID DELTenantServiceLabelsByLabelvaluesAndServiceID
func (t *ServiceLabelDaoImpl) DelTenantServiceLabelsByLabelValuesAndServiceID(serviceID string) error { func (t *ServiceLabelDaoImpl) DelTenantServiceLabelsByLabelValuesAndServiceID(serviceID string) error {
var label model.TenantServiceLable var label model.TenantServiceLable
if err := t.DB.Where("service_id=? and label_value=?", serviceID, model.LabelKeyNodeSelector).Delete(&label).Error; err != nil { if err := t.DB.Where("service_id=? and label_value=?", serviceID, model.LabelKeyNodeSelector).Delete(&label).Error; err != nil {
@ -1420,7 +1440,7 @@ func (t *ServiceLabelDaoImpl) DelTenantServiceLabelsByLabelValuesAndServiceID(se
return nil return nil
} }
//DelTenantServiceLabels deletes labels //DelTenantServiceLabelsByServiceIDKeyValue deletes labels
func (t *ServiceLabelDaoImpl) DelTenantServiceLabelsByServiceIDKeyValue(serviceID string, labelKey string, func (t *ServiceLabelDaoImpl) DelTenantServiceLabelsByServiceIDKeyValue(serviceID string, labelKey string,
labelValue string) error { labelValue string) error {
var label model.TenantServiceLable var label model.TenantServiceLable

918
util/convert_types.go Normal file
View File

@ -0,0 +1,918 @@
package util
import "time"
// String returns a pointer to the string value passed in.
func String(v string) *string {
return &v
}
// StringValue returns the value of the string pointer passed in or
// "" if the pointer is nil.
func StringValue(v *string) string {
if v != nil {
return *v
}
return ""
}
// StringSlice converts a slice of string values into a slice of
// string pointers
func StringSlice(src []string) []*string {
dst := make([]*string, len(src))
for i := 0; i < len(src); i++ {
dst[i] = &(src[i])
}
return dst
}
// StringValueSlice converts a slice of string pointers into a slice of
// string values
func StringValueSlice(src []*string) []string {
dst := make([]string, len(src))
for i := 0; i < len(src); i++ {
if src[i] != nil {
dst[i] = *(src[i])
}
}
return dst
}
// StringMap converts a string map of string values into a string
// map of string pointers
func StringMap(src map[string]string) map[string]*string {
dst := make(map[string]*string)
for k, val := range src {
v := val
dst[k] = &v
}
return dst
}
// StringValueMap converts a string map of string pointers into a string
// map of string values
func StringValueMap(src map[string]*string) map[string]string {
dst := make(map[string]string)
for k, val := range src {
if val != nil {
dst[k] = *val
}
}
return dst
}
// Bool returns a pointer to the bool value passed in.
func Bool(v bool) *bool {
return &v
}
// BoolValue returns the value of the bool pointer passed in or
// false if the pointer is nil.
func BoolValue(v *bool) bool {
if v != nil {
return *v
}
return false
}
// BoolSlice converts a slice of bool values into a slice of
// bool pointers
func BoolSlice(src []bool) []*bool {
dst := make([]*bool, len(src))
for i := 0; i < len(src); i++ {
dst[i] = &(src[i])
}
return dst
}
// BoolValueSlice converts a slice of bool pointers into a slice of
// bool values
func BoolValueSlice(src []*bool) []bool {
dst := make([]bool, len(src))
for i := 0; i < len(src); i++ {
if src[i] != nil {
dst[i] = *(src[i])
}
}
return dst
}
// BoolMap converts a string map of bool values into a string
// map of bool pointers
func BoolMap(src map[string]bool) map[string]*bool {
dst := make(map[string]*bool)
for k, val := range src {
v := val
dst[k] = &v
}
return dst
}
// BoolValueMap converts a string map of bool pointers into a string
// map of bool values
func BoolValueMap(src map[string]*bool) map[string]bool {
dst := make(map[string]bool)
for k, val := range src {
if val != nil {
dst[k] = *val
}
}
return dst
}
// Int returns a pointer to the int value passed in.
func Int(v int) *int {
return &v
}
// IntValue returns the value of the int pointer passed in or
// 0 if the pointer is nil.
func IntValue(v *int) int {
if v != nil {
return *v
}
return 0
}
// IntSlice converts a slice of int values into a slice of
// int pointers
func IntSlice(src []int) []*int {
dst := make([]*int, len(src))
for i := 0; i < len(src); i++ {
dst[i] = &(src[i])
}
return dst
}
// IntValueSlice converts a slice of int pointers into a slice of
// int values
func IntValueSlice(src []*int) []int {
dst := make([]int, len(src))
for i := 0; i < len(src); i++ {
if src[i] != nil {
dst[i] = *(src[i])
}
}
return dst
}
// IntMap converts a string map of int values into a string
// map of int pointers
func IntMap(src map[string]int) map[string]*int {
dst := make(map[string]*int)
for k, val := range src {
v := val
dst[k] = &v
}
return dst
}
// IntValueMap converts a string map of int pointers into a string
// map of int values
func IntValueMap(src map[string]*int) map[string]int {
dst := make(map[string]int)
for k, val := range src {
if val != nil {
dst[k] = *val
}
}
return dst
}
// Uint returns a pointer to the uint value passed in.
func Uint(v uint) *uint {
return &v
}
// UintValue returns the value of the uint pointer passed in or
// 0 if the pointer is nil.
func UintValue(v *uint) uint {
if v != nil {
return *v
}
return 0
}
// UintSlice converts a slice of uint values uinto a slice of
// uint pointers
func UintSlice(src []uint) []*uint {
dst := make([]*uint, len(src))
for i := 0; i < len(src); i++ {
dst[i] = &(src[i])
}
return dst
}
// UintValueSlice converts a slice of uint pointers uinto a slice of
// uint values
func UintValueSlice(src []*uint) []uint {
dst := make([]uint, len(src))
for i := 0; i < len(src); i++ {
if src[i] != nil {
dst[i] = *(src[i])
}
}
return dst
}
// UintMap converts a string map of uint values uinto a string
// map of uint pointers
func UintMap(src map[string]uint) map[string]*uint {
dst := make(map[string]*uint)
for k, val := range src {
v := val
dst[k] = &v
}
return dst
}
// UintValueMap converts a string map of uint pointers uinto a string
// map of uint values
func UintValueMap(src map[string]*uint) map[string]uint {
dst := make(map[string]uint)
for k, val := range src {
if val != nil {
dst[k] = *val
}
}
return dst
}
// Int8 returns a pointer to the int8 value passed in.
func Int8(v int8) *int8 {
return &v
}
// Int8Value returns the value of the int8 pointer passed in or
// 0 if the pointer is nil.
func Int8Value(v *int8) int8 {
if v != nil {
return *v
}
return 0
}
// Int8Slice converts a slice of int8 values into a slice of
// int8 pointers
func Int8Slice(src []int8) []*int8 {
dst := make([]*int8, len(src))
for i := 0; i < len(src); i++ {
dst[i] = &(src[i])
}
return dst
}
// Int8ValueSlice converts a slice of int8 pointers into a slice of
// int8 values
func Int8ValueSlice(src []*int8) []int8 {
dst := make([]int8, len(src))
for i := 0; i < len(src); i++ {
if src[i] != nil {
dst[i] = *(src[i])
}
}
return dst
}
// Int8Map converts a string map of int8 values into a string
// map of int8 pointers
func Int8Map(src map[string]int8) map[string]*int8 {
dst := make(map[string]*int8)
for k, val := range src {
v := val
dst[k] = &v
}
return dst
}
// Int8ValueMap converts a string map of int8 pointers into a string
// map of int8 values
func Int8ValueMap(src map[string]*int8) map[string]int8 {
dst := make(map[string]int8)
for k, val := range src {
if val != nil {
dst[k] = *val
}
}
return dst
}
// Int16 returns a pointer to the int16 value passed in.
func Int16(v int16) *int16 {
return &v
}
// Int16Value returns the value of the int16 pointer passed in or
// 0 if the pointer is nil.
func Int16Value(v *int16) int16 {
if v != nil {
return *v
}
return 0
}
// Int16Slice converts a slice of int16 values into a slice of
// int16 pointers
func Int16Slice(src []int16) []*int16 {
dst := make([]*int16, len(src))
for i := 0; i < len(src); i++ {
dst[i] = &(src[i])
}
return dst
}
// Int16ValueSlice converts a slice of int16 pointers into a slice of
// int16 values
func Int16ValueSlice(src []*int16) []int16 {
dst := make([]int16, len(src))
for i := 0; i < len(src); i++ {
if src[i] != nil {
dst[i] = *(src[i])
}
}
return dst
}
// Int16Map converts a string map of int16 values into a string
// map of int16 pointers
func Int16Map(src map[string]int16) map[string]*int16 {
dst := make(map[string]*int16)
for k, val := range src {
v := val
dst[k] = &v
}
return dst
}
// Int16ValueMap converts a string map of int16 pointers into a string
// map of int16 values
func Int16ValueMap(src map[string]*int16) map[string]int16 {
dst := make(map[string]int16)
for k, val := range src {
if val != nil {
dst[k] = *val
}
}
return dst
}
// Int32 returns a pointer to the int32 value passed in.
func Int32(v int32) *int32 {
return &v
}
// Int32Value returns the value of the int32 pointer passed in or
// 0 if the pointer is nil.
func Int32Value(v *int32) int32 {
if v != nil {
return *v
}
return 0
}
// Int32Slice converts a slice of int32 values into a slice of
// int32 pointers
func Int32Slice(src []int32) []*int32 {
dst := make([]*int32, len(src))
for i := 0; i < len(src); i++ {
dst[i] = &(src[i])
}
return dst
}
// Int32ValueSlice converts a slice of int32 pointers into a slice of
// int32 values
func Int32ValueSlice(src []*int32) []int32 {
dst := make([]int32, len(src))
for i := 0; i < len(src); i++ {
if src[i] != nil {
dst[i] = *(src[i])
}
}
return dst
}
// Int32Map converts a string map of int32 values into a string
// map of int32 pointers
func Int32Map(src map[string]int32) map[string]*int32 {
dst := make(map[string]*int32)
for k, val := range src {
v := val
dst[k] = &v
}
return dst
}
// Int32ValueMap converts a string map of int32 pointers into a string
// map of int32 values
func Int32ValueMap(src map[string]*int32) map[string]int32 {
dst := make(map[string]int32)
for k, val := range src {
if val != nil {
dst[k] = *val
}
}
return dst
}
// Int64 returns a pointer to the int64 value passed in.
func Int64(v int64) *int64 {
return &v
}
// Int64Value returns the value of the int64 pointer passed in or
// 0 if the pointer is nil.
func Int64Value(v *int64) int64 {
if v != nil {
return *v
}
return 0
}
// Int64Slice converts a slice of int64 values into a slice of
// int64 pointers
func Int64Slice(src []int64) []*int64 {
dst := make([]*int64, len(src))
for i := 0; i < len(src); i++ {
dst[i] = &(src[i])
}
return dst
}
// Int64ValueSlice converts a slice of int64 pointers into a slice of
// int64 values
func Int64ValueSlice(src []*int64) []int64 {
dst := make([]int64, len(src))
for i := 0; i < len(src); i++ {
if src[i] != nil {
dst[i] = *(src[i])
}
}
return dst
}
// Int64Map converts a string map of int64 values into a string
// map of int64 pointers
func Int64Map(src map[string]int64) map[string]*int64 {
dst := make(map[string]*int64)
for k, val := range src {
v := val
dst[k] = &v
}
return dst
}
// Int64ValueMap converts a string map of int64 pointers into a string
// map of int64 values
func Int64ValueMap(src map[string]*int64) map[string]int64 {
dst := make(map[string]int64)
for k, val := range src {
if val != nil {
dst[k] = *val
}
}
return dst
}
// Uint8 returns a pointer to the uint8 value passed in.
func Uint8(v uint8) *uint8 {
return &v
}
// Uint8Value returns the value of the uint8 pointer passed in or
// 0 if the pointer is nil.
func Uint8Value(v *uint8) uint8 {
if v != nil {
return *v
}
return 0
}
// Uint8Slice converts a slice of uint8 values into a slice of
// uint8 pointers
func Uint8Slice(src []uint8) []*uint8 {
dst := make([]*uint8, len(src))
for i := 0; i < len(src); i++ {
dst[i] = &(src[i])
}
return dst
}
// Uint8ValueSlice converts a slice of uint8 pointers into a slice of
// uint8 values
func Uint8ValueSlice(src []*uint8) []uint8 {
dst := make([]uint8, len(src))
for i := 0; i < len(src); i++ {
if src[i] != nil {
dst[i] = *(src[i])
}
}
return dst
}
// Uint8Map converts a string map of uint8 values into a string
// map of uint8 pointers
func Uint8Map(src map[string]uint8) map[string]*uint8 {
dst := make(map[string]*uint8)
for k, val := range src {
v := val
dst[k] = &v
}
return dst
}
// Uint8ValueMap converts a string map of uint8 pointers into a string
// map of uint8 values
func Uint8ValueMap(src map[string]*uint8) map[string]uint8 {
dst := make(map[string]uint8)
for k, val := range src {
if val != nil {
dst[k] = *val
}
}
return dst
}
// Uint16 returns a pointer to the uint16 value passed in.
func Uint16(v uint16) *uint16 {
return &v
}
// Uint16Value returns the value of the uint16 pointer passed in or
// 0 if the pointer is nil.
func Uint16Value(v *uint16) uint16 {
if v != nil {
return *v
}
return 0
}
// Uint16Slice converts a slice of uint16 values into a slice of
// uint16 pointers
func Uint16Slice(src []uint16) []*uint16 {
dst := make([]*uint16, len(src))
for i := 0; i < len(src); i++ {
dst[i] = &(src[i])
}
return dst
}
// Uint16ValueSlice converts a slice of uint16 pointers into a slice of
// uint16 values
func Uint16ValueSlice(src []*uint16) []uint16 {
dst := make([]uint16, len(src))
for i := 0; i < len(src); i++ {
if src[i] != nil {
dst[i] = *(src[i])
}
}
return dst
}
// Uint16Map converts a string map of uint16 values into a string
// map of uint16 pointers
func Uint16Map(src map[string]uint16) map[string]*uint16 {
dst := make(map[string]*uint16)
for k, val := range src {
v := val
dst[k] = &v
}
return dst
}
// Uint16ValueMap converts a string map of uint16 pointers into a string
// map of uint16 values
func Uint16ValueMap(src map[string]*uint16) map[string]uint16 {
dst := make(map[string]uint16)
for k, val := range src {
if val != nil {
dst[k] = *val
}
}
return dst
}
// Uint32 returns a pointer to the uint32 value passed in.
func Uint32(v uint32) *uint32 {
return &v
}
// Uint32Value returns the value of the uint32 pointer passed in or
// 0 if the pointer is nil.
func Uint32Value(v *uint32) uint32 {
if v != nil {
return *v
}
return 0
}
// Uint32Slice converts a slice of uint32 values into a slice of
// uint32 pointers
func Uint32Slice(src []uint32) []*uint32 {
dst := make([]*uint32, len(src))
for i := 0; i < len(src); i++ {
dst[i] = &(src[i])
}
return dst
}
// Uint32ValueSlice converts a slice of uint32 pointers into a slice of
// uint32 values
func Uint32ValueSlice(src []*uint32) []uint32 {
dst := make([]uint32, len(src))
for i := 0; i < len(src); i++ {
if src[i] != nil {
dst[i] = *(src[i])
}
}
return dst
}
// Uint32Map converts a string map of uint32 values into a string
// map of uint32 pointers
func Uint32Map(src map[string]uint32) map[string]*uint32 {
dst := make(map[string]*uint32)
for k, val := range src {
v := val
dst[k] = &v
}
return dst
}
// Uint32ValueMap converts a string map of uint32 pointers into a string
// map of uint32 values
func Uint32ValueMap(src map[string]*uint32) map[string]uint32 {
dst := make(map[string]uint32)
for k, val := range src {
if val != nil {
dst[k] = *val
}
}
return dst
}
// Uint64 returns a pointer to the uint64 value passed in.
func Uint64(v uint64) *uint64 {
return &v
}
// Uint64Value returns the value of the uint64 pointer passed in or
// 0 if the pointer is nil.
func Uint64Value(v *uint64) uint64 {
if v != nil {
return *v
}
return 0
}
// Uint64Slice converts a slice of uint64 values into a slice of
// uint64 pointers
func Uint64Slice(src []uint64) []*uint64 {
dst := make([]*uint64, len(src))
for i := 0; i < len(src); i++ {
dst[i] = &(src[i])
}
return dst
}
// Uint64ValueSlice converts a slice of uint64 pointers into a slice of
// uint64 values
func Uint64ValueSlice(src []*uint64) []uint64 {
dst := make([]uint64, len(src))
for i := 0; i < len(src); i++ {
if src[i] != nil {
dst[i] = *(src[i])
}
}
return dst
}
// Uint64Map converts a string map of uint64 values into a string
// map of uint64 pointers
func Uint64Map(src map[string]uint64) map[string]*uint64 {
dst := make(map[string]*uint64)
for k, val := range src {
v := val
dst[k] = &v
}
return dst
}
// Uint64ValueMap converts a string map of uint64 pointers into a string
// map of uint64 values
func Uint64ValueMap(src map[string]*uint64) map[string]uint64 {
dst := make(map[string]uint64)
for k, val := range src {
if val != nil {
dst[k] = *val
}
}
return dst
}
// Float32 returns a pointer to the float32 value passed in.
func Float32(v float32) *float32 {
return &v
}
// Float32Value returns the value of the float32 pointer passed in or
// 0 if the pointer is nil.
func Float32Value(v *float32) float32 {
if v != nil {
return *v
}
return 0
}
// Float32Slice converts a slice of float32 values into a slice of
// float32 pointers
func Float32Slice(src []float32) []*float32 {
dst := make([]*float32, len(src))
for i := 0; i < len(src); i++ {
dst[i] = &(src[i])
}
return dst
}
// Float32ValueSlice converts a slice of float32 pointers into a slice of
// float32 values
func Float32ValueSlice(src []*float32) []float32 {
dst := make([]float32, len(src))
for i := 0; i < len(src); i++ {
if src[i] != nil {
dst[i] = *(src[i])
}
}
return dst
}
// Float32Map converts a string map of float32 values into a string
// map of float32 pointers
func Float32Map(src map[string]float32) map[string]*float32 {
dst := make(map[string]*float32)
for k, val := range src {
v := val
dst[k] = &v
}
return dst
}
// Float32ValueMap converts a string map of float32 pointers into a string
// map of float32 values
func Float32ValueMap(src map[string]*float32) map[string]float32 {
dst := make(map[string]float32)
for k, val := range src {
if val != nil {
dst[k] = *val
}
}
return dst
}
// Float64 returns a pointer to the float64 value passed in.
func Float64(v float64) *float64 {
return &v
}
// Float64Value returns the value of the float64 pointer passed in or
// 0 if the pointer is nil.
func Float64Value(v *float64) float64 {
if v != nil {
return *v
}
return 0
}
// Float64Slice converts a slice of float64 values into a slice of
// float64 pointers
func Float64Slice(src []float64) []*float64 {
dst := make([]*float64, len(src))
for i := 0; i < len(src); i++ {
dst[i] = &(src[i])
}
return dst
}
// Float64ValueSlice converts a slice of float64 pointers into a slice of
// float64 values
func Float64ValueSlice(src []*float64) []float64 {
dst := make([]float64, len(src))
for i := 0; i < len(src); i++ {
if src[i] != nil {
dst[i] = *(src[i])
}
}
return dst
}
// Float64Map converts a string map of float64 values into a string
// map of float64 pointers
func Float64Map(src map[string]float64) map[string]*float64 {
dst := make(map[string]*float64)
for k, val := range src {
v := val
dst[k] = &v
}
return dst
}
// Float64ValueMap converts a string map of float64 pointers into a string
// map of float64 values
func Float64ValueMap(src map[string]*float64) map[string]float64 {
dst := make(map[string]float64)
for k, val := range src {
if val != nil {
dst[k] = *val
}
}
return dst
}
// Time returns a pointer to the time.Time value passed in.
func Time(v time.Time) *time.Time {
return &v
}
// TimeValue returns the value of the time.Time pointer passed in or
// time.Time{} if the pointer is nil.
func TimeValue(v *time.Time) time.Time {
if v != nil {
return *v
}
return time.Time{}
}
// SecondsTimeValue converts an int64 pointer to a time.Time value
// representing seconds since Epoch or time.Time{} if the pointer is nil.
func SecondsTimeValue(v *int64) time.Time {
if v != nil {
return time.Unix((*v / 1000), 0)
}
return time.Time{}
}
// MillisecondsTimeValue converts an int64 pointer to a time.Time value
// representing milliseconds sinch Epoch or time.Time{} if the pointer is nil.
func MillisecondsTimeValue(v *int64) time.Time {
if v != nil {
return time.Unix(0, (*v * 1000000))
}
return time.Time{}
}
// TimeUnixMilli returns a Unix timestamp in milliseconds from "January 1, 1970 UTC".
// The result is undefined if the Unix time cannot be represented by an int64.
// Which includes calling TimeUnixMilli on a zero Time is undefined.
//
// This utility is useful for service API's such as CloudWatch Logs which require
// their unix time values to be in milliseconds.
//
// See Go stdlib https://golang.org/pkg/time/#Time.UnixNano for more information.
func TimeUnixMilli(t time.Time) int64 {
return t.UnixNano() / int64(time.Millisecond/time.Nanosecond)
}
// TimeSlice converts a slice of time.Time values into a slice of
// time.Time pointers
func TimeSlice(src []time.Time) []*time.Time {
dst := make([]*time.Time, len(src))
for i := 0; i < len(src); i++ {
dst[i] = &(src[i])
}
return dst
}
// TimeValueSlice converts a slice of time.Time pointers into a slice of
// time.Time values
func TimeValueSlice(src []*time.Time) []time.Time {
dst := make([]time.Time, len(src))
for i := 0; i < len(src); i++ {
if src[i] != nil {
dst[i] = *(src[i])
}
}
return dst
}
// TimeMap converts a string map of time.Time values into a string
// map of time.Time pointers
func TimeMap(src map[string]time.Time) map[string]*time.Time {
dst := make(map[string]*time.Time)
for k, val := range src {
v := val
dst[k] = &v
}
return dst
}
// TimeValueMap converts a string map of time.Time pointers into a string
// map of time.Time values
func TimeValueMap(src map[string]*time.Time) map[string]time.Time {
dst := make(map[string]time.Time)
for k, val := range src {
if val != nil {
dst[k] = *val
}
}
return dst
}

View File

@ -54,18 +54,18 @@ type TaskManager struct {
} }
//NewTaskManager return *TaskManager //NewTaskManager return *TaskManager
func NewTaskManager(c option.Config, func NewTaskManager(cfg option.Config,
store store.Storer, store store.Storer,
controllermanager *controller.Manager, controllermanager *controller.Manager,
startCh *channels.RingChannel) *TaskManager { startCh *channels.RingChannel) *TaskManager {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
handleManager := handle.NewManager(ctx, c, store, controllermanager, startCh) handleManager := handle.NewManager(ctx, cfg, store, controllermanager, startCh)
healthStatus["status"] = "health" healthStatus["status"] = "health"
healthStatus["info"] = "worker service health" healthStatus["info"] = "worker service health"
return &TaskManager{ return &TaskManager{
ctx: ctx, ctx: ctx,
cancel: cancel, cancel: cancel,
config: c, config: cfg,
handleManager: handleManager, handleManager: handleManager,
} }
} }
@ -122,8 +122,10 @@ func (t *TaskManager) Do() {
} }
rc := t.handleManager.AnalystToExec(transData) rc := t.handleManager.AnalystToExec(transData)
if rc != nil && rc != handle.ErrCallback { if rc != nil && rc != handle.ErrCallback {
logrus.Errorf("analyst to exet: %v", rc)
TaskError++ TaskError++
} else if rc != nil && rc == handle.ErrCallback { } else if rc != nil && rc == handle.ErrCallback {
logrus.Errorf("err callback; analyst to exet: %v", rc)
ctx, cancel := context.WithCancel(t.ctx) ctx, cancel := context.WithCancel(t.ctx)
reply, err := t.client.Enqueue(ctx, &pb.EnqueueRequest{ reply, err := t.client.Enqueue(ctx, &pb.EnqueueRequest{
Topic: client.WorkerTopic, Topic: client.WorkerTopic,

View File

@ -144,6 +144,13 @@ func NewTaskBody(taskType string, body []byte) TaskBody {
return nil return nil
} }
return b return b
case "delete_tenant":
b := &DeleteTenantTaskBody{}
err := ffjson.Unmarshal(body, &b)
if err != nil {
return nil
}
return b
default: default:
return DefaultTaskBody{} return DefaultTaskBody{}
} }
@ -172,6 +179,8 @@ func CreateTaskBody(taskType string) TaskBody {
return VerticalScalingTaskBody{} return VerticalScalingTaskBody{}
case "apply_plugin_config": case "apply_plugin_config":
return ApplyPluginConfigTaskBody{} return ApplyPluginConfigTaskBody{}
case "delete_tenant":
return DeleteTenantTaskBody{}
default: default:
return DefaultTaskBody{} return DefaultTaskBody{}
} }
@ -310,5 +319,10 @@ type GroupStopTaskBody struct {
Strategy []string `json:"strategy"` Strategy []string `json:"strategy"`
} }
// DeleteTenantTaskBody -
type DeleteTenantTaskBody struct {
TenantID string `json:"tenant_id"`
}
//DefaultTaskBody 默认操作任务主体 //DefaultTaskBody 默认操作任务主体
type DefaultTaskBody map[string]interface{} type DefaultTaskBody map[string]interface{}

View File

@ -26,10 +26,14 @@ import (
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
"github.com/eapache/channels" "github.com/eapache/channels"
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/goodrain/rainbond/cmd/worker/option" "github.com/goodrain/rainbond/cmd/worker/option"
"github.com/goodrain/rainbond/db" "github.com/goodrain/rainbond/db"
dbmodel "github.com/goodrain/rainbond/db/model" dbmodel "github.com/goodrain/rainbond/db/model"
"github.com/goodrain/rainbond/event" "github.com/goodrain/rainbond/event"
"github.com/goodrain/rainbond/util"
"github.com/goodrain/rainbond/worker/appm/controller" "github.com/goodrain/rainbond/worker/appm/controller"
"github.com/goodrain/rainbond/worker/appm/conversion" "github.com/goodrain/rainbond/worker/appm/conversion"
"github.com/goodrain/rainbond/worker/appm/store" "github.com/goodrain/rainbond/worker/appm/store"
@ -40,7 +44,7 @@ import (
//Manager manager //Manager manager
type Manager struct { type Manager struct {
ctx context.Context ctx context.Context
c option.Config cfg option.Config
store store.Storer store store.Storer
dbmanager db.Manager dbmanager db.Manager
controllerManager *controller.Manager controllerManager *controller.Manager
@ -57,7 +61,7 @@ func NewManager(ctx context.Context,
return &Manager{ return &Manager{
ctx: ctx, ctx: ctx,
c: config, cfg: config,
dbmanager: db.GetManager(), dbmanager: db.GetManager(),
store: store, store: store,
controllerManager: controllerManager, controllerManager: controllerManager,
@ -69,7 +73,7 @@ func NewManager(ctx context.Context,
var ErrCallback = fmt.Errorf("callback task to mq") var ErrCallback = fmt.Errorf("callback task to mq")
func (m *Manager) checkCount() bool { func (m *Manager) checkCount() bool {
if m.controllerManager.GetControllerSize() > m.c.MaxTasks { if m.controllerManager.GetControllerSize() > m.cfg.MaxTasks {
return true return true
} }
return false return false
@ -112,6 +116,9 @@ func (m *Manager) AnalystToExec(task *model.Task) error {
case "apply_plugin_config": case "apply_plugin_config":
logrus.Info("start a 'apply_plugin_config' task worker") logrus.Info("start a 'apply_plugin_config' task worker")
return m.applyPluginConfig(task) return m.applyPluginConfig(task)
case "delete_tenant":
logrus.Info("start a 'delete_tenant' task worker")
return m.deleteTenant(task)
default: default:
logrus.Warning("task can not execute because no type is identified") logrus.Warning("task can not execute because no type is identified")
return nil return nil
@ -433,3 +440,46 @@ func (m *Manager) applyPluginConfig(task *model.Task) error {
} }
return nil return nil
} }
func (m *Manager) deleteTenant(task *model.Task) (err error) {
body, ok := task.Body.(*model.DeleteTenantTaskBody)
if !ok {
logrus.Errorf("can't convert %s to *model.DeleteTenantTaskBody", reflect.TypeOf(task.Body))
err = fmt.Errorf("can't convert %s to *model.DeleteTenantTaskBody", reflect.TypeOf(task.Body))
return
}
defer func() {
if err == nil {
return
}
logrus.Errorf("failed to delete tenant: %v", err)
var tenant *dbmodel.Tenants
tenant, err = db.GetManager().TenantDao().GetTenantByUUID(body.TenantID)
if err != nil {
err = fmt.Errorf("tenant id: %s; find tenant: %v", err)
return
}
tenant.Status = dbmodel.TenantStatusDeleteFailed.String()
err := db.GetManager().TenantDao().UpdateModel(tenant)
if err != nil {
err = fmt.Errorf("update tenant_status to '%s': %v", tenant.Status, err)
return
}
}()
if err = m.cfg.KubeClient.CoreV1().Namespaces().Delete(body.TenantID, &metav1.DeleteOptions{
GracePeriodSeconds: util.Int64(0),
}); err != nil && !k8sErrors.IsNotFound(err) {
err = fmt.Errorf("delete namespace: %v", err)
return
}
err = db.GetManager().TenantDao().DelByTenantID(body.TenantID)
if err != nil {
err = fmt.Errorf("delete tenant: %v", err)
return
}
return
}