Rainbond/api/handler/tenant.go

692 lines
20 KiB
Go
Raw Permalink Normal View History

2018-03-14 14:12:26 +08:00
// Copyright (C) 2014-2018 Goodrain Co., Ltd.
2017-11-07 11:40:44 +08:00
// RAINBOND, Application Management Platform
2018-03-14 14:33:31 +08:00
2017-11-07 11:40:44 +08:00
// 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.
2018-03-14 14:33:31 +08:00
2017-11-07 11:40:44 +08:00
// 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.
2018-03-14 14:33:31 +08:00
2017-11-07 11:40:44 +08:00
// 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 (
2020-06-30 15:08:20 +08:00
"context"
2017-11-07 11:40:44 +08:00
"fmt"
"k8s.io/apimachinery/pkg/fields"
2019-08-01 17:19:22 +08:00
"sort"
2019-01-11 09:44:28 +08:00
"strings"
2020-06-17 12:57:41 +08:00
"time"
2017-11-07 11:40:44 +08:00
2020-09-24 21:09:00 +08:00
"github.com/goodrain/rainbond/api/client/prometheus"
2021-05-07 21:41:09 +08:00
"github.com/goodrain/rainbond/api/model"
api_model "github.com/goodrain/rainbond/api/model"
2019-01-11 09:44:28 +08:00
"github.com/goodrain/rainbond/api/util"
2021-05-07 21:41:09 +08:00
"github.com/goodrain/rainbond/api/util/bcode"
"github.com/goodrain/rainbond/cmd/api/option"
"github.com/goodrain/rainbond/db"
dbmodel "github.com/goodrain/rainbond/db/model"
2019-11-05 14:27:54 +08:00
mqclient "github.com/goodrain/rainbond/mq/client"
2021-05-07 21:41:09 +08:00
"github.com/goodrain/rainbond/pkg/apis/rainbond/v1alpha1"
rutil "github.com/goodrain/rainbond/util"
2019-01-11 09:44:28 +08:00
"github.com/goodrain/rainbond/worker/client"
2020-06-30 15:08:20 +08:00
"github.com/goodrain/rainbond/worker/server/pb"
2021-05-07 21:41:09 +08:00
"github.com/pkg/errors"
2020-09-24 21:09:00 +08:00
"github.com/sirupsen/logrus"
2021-05-07 21:41:09 +08:00
corev1 "k8s.io/api/core/v1"
2020-06-17 12:57:41 +08:00
v1 "k8s.io/api/core/v1"
2021-05-07 21:41:09 +08:00
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
2020-06-17 12:57:41 +08:00
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2021-05-07 21:41:09 +08:00
"k8s.io/apimachinery/pkg/types"
2020-06-17 12:57:41 +08:00
"k8s.io/client-go/kubernetes"
2021-05-07 21:41:09 +08:00
k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
2017-11-07 11:40:44 +08:00
)
//TenantAction tenant act
type TenantAction struct {
2020-06-17 12:57:41 +08:00
MQClient mqclient.MQClient
statusCli *client.AppRuntimeSyncClient
OptCfg *option.Config
kubeClient *kubernetes.Clientset
cacheClusterResourceStats *ClusterResourceStats
cacheTime time.Time
2020-09-24 21:09:00 +08:00
prometheusCli prometheus.Interface
2021-05-07 21:41:09 +08:00
k8sClient k8sclient.Client
2022-09-26 17:01:48 +08:00
resources map[string]k8sclient.Object
2017-11-07 11:40:44 +08:00
}
//CreateTenManager create Manger
2019-11-05 14:27:54 +08:00
func CreateTenManager(mqc mqclient.MQClient, statusCli *client.AppRuntimeSyncClient,
2021-05-07 21:41:09 +08:00
optCfg *option.Config,
kubeClient *kubernetes.Clientset,
prometheusCli prometheus.Interface,
k8sClient k8sclient.Client) *TenantAction {
2022-09-26 17:01:48 +08:00
resources := map[string]k8sclient.Object{
2021-05-07 21:41:09 +08:00
"helmApp": &v1alpha1.HelmApp{},
"service": &corev1.Service{},
}
2017-11-07 11:40:44 +08:00
return &TenantAction{
2020-09-24 21:09:00 +08:00
MQClient: mqc,
statusCli: statusCli,
OptCfg: optCfg,
kubeClient: kubeClient,
prometheusCli: prometheusCli,
2021-05-07 21:41:09 +08:00
k8sClient: k8sClient,
resources: resources,
2018-03-04 22:48:50 +08:00
}
2017-11-07 11:40:44 +08:00
}
2019-08-01 17:19:22 +08:00
//BindTenantsResource query tenant resource used and sort
func (t *TenantAction) BindTenantsResource(source []*dbmodel.Tenants) api_model.TenantList {
var list api_model.TenantList
var resources = make(map[string]*pb.TenantResource, len(source))
if len(source) == 1 {
re, err := t.statusCli.GetTenantResource(source[0].UUID)
if err != nil {
logrus.Errorf("get tenant %s resource failure %s", source[0].UUID, err.Error())
}
if re != nil {
resources[source[0].UUID] = re
}
} else {
res, err := t.statusCli.GetAllTenantResource()
if err != nil {
logrus.Errorf("get all tenant resource failure %s", err.Error())
}
if res != nil {
resources = res.Resources
}
}
2019-08-01 17:19:22 +08:00
for i, ten := range source {
var item = &api_model.TenantAndResource{
Tenants: *source[i],
}
re := resources[ten.UUID]
2019-08-01 17:19:22 +08:00
if re != nil {
item.CPULimit = re.CpuLimit
item.CPURequest = re.CpuRequest
item.MemoryLimit = re.MemoryLimit
item.MemoryRequest = re.MemoryRequest
item.RunningAppNum = re.RunningAppNum
item.RunningAppInternalNum = re.RunningAppInternalNum
item.RunningAppThirdNum = re.RunningAppThirdNum
2022-05-29 18:02:34 +08:00
item.RunningApplications = re.RunningApplications
2019-08-01 17:19:22 +08:00
}
list.Add(item)
}
2023-02-27 19:36:49 +08:00
sort.SliceStable(list, func(i, j int) bool {
if list[i].MemoryRequest > list[j].MemoryRequest {
return true
}
return false
})
2019-08-01 17:19:22 +08:00
return list
}
2017-11-07 11:40:44 +08:00
//GetTenants get tenants
2019-08-01 17:19:22 +08:00
func (t *TenantAction) GetTenants(query string) ([]*dbmodel.Tenants, error) {
tenants, err := db.GetManager().TenantDao().GetALLTenants(query)
2017-11-07 11:40:44 +08:00
if err != nil {
return nil, err
}
2017-12-07 12:08:25 +08:00
return tenants, err
2017-11-07 11:40:44 +08:00
}
2018-04-26 13:04:29 +08:00
//GetTenantsByEid GetTenantsByEid
2019-08-01 17:19:22 +08:00
func (t *TenantAction) GetTenantsByEid(eid, query string) ([]*dbmodel.Tenants, error) {
tenants, err := db.GetManager().TenantDao().GetTenantByEid(eid, query)
2018-03-23 10:48:12 +08:00
if err != nil {
return nil, err
}
return tenants, err
}
2019-08-01 17:19:22 +08:00
//UpdateTenant update tenant info
func (t *TenantAction) UpdateTenant(tenant *dbmodel.Tenants) error {
return db.GetManager().TenantDao().UpdateModel(tenant)
2017-12-15 16:49:24 +08:00
}
2019-11-05 11:43:10 +08:00
// DeleteTenant deletes tenant based on the given tenantID.
//
// tenant can only be deleted without service or plugin
2021-07-01 16:17:46 +08:00
func (t *TenantAction) DeleteTenant(ctx context.Context, tenantID string) error {
2019-11-05 11:43:10 +08:00
// check if there are still services
services, err := db.GetManager().TenantServiceDao().ListServicesByTenantID(tenantID)
if err != nil {
return err
}
if len(services) > 0 {
2020-08-16 20:13:09 +08:00
for _, service := range services {
2021-07-01 16:17:46 +08:00
GetServiceManager().TransServieToDelete(ctx, tenantID, service.ServiceID)
2020-08-16 20:13:09 +08:00
}
2019-11-05 11:43:10 +08:00
}
// check if there are still plugins
plugins, err := db.GetManager().TenantPluginDao().ListByTenantID(tenantID)
if err != nil {
return err
}
if len(plugins) > 0 {
2020-08-16 20:13:09 +08:00
for _, plugin := range plugins {
GetPluginManager().DeletePluginAct(plugin.PluginID, tenantID)
}
2019-11-05 11:43:10 +08:00
}
2019-11-05 14:27:54 +08:00
tenant, err := db.GetManager().TenantDao().GetTenantByUUID(tenantID)
if err != nil {
return err
}
2020-04-27 17:54:45 +08:00
oldStatus := tenant.Status
var rollback = func() {
tenant.Status = oldStatus
_ = db.GetManager().TenantDao().UpdateModel(tenant)
}
2019-11-05 14:27:54 +08:00
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 {
2020-04-27 17:54:45 +08:00
rollback()
2019-11-05 14:27:54 +08:00
logrus.Error("send task 'delete tenant'", err)
return err
}
return nil
2019-11-05 11:43:10 +08:00
}
//TotalMemCPU StatsMemCPU
2018-01-02 18:02:08 +08:00
func (t *TenantAction) TotalMemCPU(services []*dbmodel.TenantServices) (*api_model.StatsInfo, error) {
2017-12-06 15:26:21 +08:00
cpus := 0
mem := 0
for _, service := range services {
logrus.Debugf("service is %d, cpus is %d, mem is %v", service.ID, service.ContainerCPU, service.ContainerMemory)
2017-12-06 15:26:21 +08:00
cpus += service.ContainerCPU
mem += service.ContainerMemory
}
si := &api_model.StatsInfo{
CPU: cpus,
MEM: mem,
}
return si, nil
}
2017-12-07 12:08:25 +08:00
//GetTenantsName get tenants name
func (t *TenantAction) GetTenantsName() ([]string, error) {
2019-08-01 17:19:22 +08:00
tenants, err := db.GetManager().TenantDao().GetALLTenants("")
2017-12-07 12:08:25 +08:00
if err != nil {
return nil, err
}
var result []string
for _, v := range tenants {
result = append(result, strings.ToLower(v.Name))
2017-12-07 12:08:25 +08:00
}
return result, err
}
//GetTenantsByName get tenants
2017-12-07 12:08:25 +08:00
func (t *TenantAction) GetTenantsByName(name string) (*dbmodel.Tenants, error) {
tenant, err := db.GetManager().TenantDao().GetTenantIDByName(name)
if err != nil {
return nil, err
}
return tenant, err
}
2018-01-02 18:02:08 +08:00
//GetTenantsByUUID get tenants
2017-12-15 16:49:24 +08:00
func (t *TenantAction) GetTenantsByUUID(uuid string) (*dbmodel.Tenants, error) {
tenant, err := db.GetManager().TenantDao().GetTenantByUUID(uuid)
if err != nil {
return nil, err
}
return tenant, err
}
2017-11-07 11:40:44 +08:00
//StatsMemCPU StatsMemCPU
func (t *TenantAction) StatsMemCPU(services []*dbmodel.TenantServices) (*api_model.StatsInfo, error) {
cpus := 0
mem := 0
for _, service := range services {
status := t.statusCli.GetStatus(service.ServiceID)
if t.statusCli.IsClosedStatus(status) {
2017-11-07 11:40:44 +08:00
continue
}
cpus += service.ContainerCPU
mem += service.ContainerMemory
}
si := &api_model.StatsInfo{
CPU: cpus,
MEM: mem,
}
return si, nil
}
// QueryResult contains result data for a query.
type QueryResult struct {
Data struct {
Type string `json:"resultType"`
Result []map[string]interface{} `json:"result"`
} `json:"data"`
Status string `json:"status"`
}
//GetTenantsResources Gets the resource usage of the specified tenant.
2021-04-01 19:08:57 +08:00
func (t *TenantAction) GetTenantsResources(ctx context.Context, tr *api_model.TenantResources) (map[string]map[string]interface{}, error) {
ids, err := db.GetManager().TenantDao().GetTenantIDsByNames(tr.Body.TenantNames)
if err != nil {
return nil, err
}
2018-11-13 13:41:20 +08:00
limits, err := db.GetManager().TenantDao().GetTenantLimitsByNames(tr.Body.TenantNames)
if err != nil {
return nil, err
}
services, err := db.GetManager().TenantServiceDao().GetServicesByTenantIDs(ids)
if err != nil {
return nil, err
}
2018-11-17 18:34:46 +08:00
var serviceTenantCount = make(map[string]int, len(ids))
for _, s := range services {
2018-11-17 18:34:46 +08:00
serviceTenantCount[s.TenantID]++
}
// get cluster resources
2021-04-01 19:08:57 +08:00
clusterStats, err := t.GetAllocatableResources(ctx)
if err != nil {
return nil, fmt.Errorf("error getting allocatalbe cpu and memory: %v", err)
}
var result = make(map[string]map[string]interface{}, len(ids))
var resources = make(map[string]*pb.TenantResource, len(ids))
if len(ids) == 1 {
re, err := t.statusCli.GetTenantResource(ids[0])
if err != nil {
logrus.Errorf("get tenant %s resource failure %s", ids[0], err.Error())
}
if re != nil {
resources[ids[0]] = re
}
} else {
res, err := t.statusCli.GetAllTenantResource()
if err != nil {
logrus.Errorf("get all tenant resource failure %s", err.Error())
}
if res != nil {
resources = res.Resources
}
}
for _, tenantID := range ids {
var limitMemory int64
if l, ok := limits[tenantID]; ok && l != 0 {
limitMemory = int64(l)
} else {
limitMemory = clusterStats.AllMemory
}
result[tenantID] = map[string]interface{}{
"tenant_id": tenantID,
"limit_memory": limitMemory,
"limit_cpu": clusterStats.AllCPU,
"service_total_num": serviceTenantCount[tenantID],
"disk": 0,
"service_running_num": 0,
"cpu": 0,
"memory": 0,
2022-05-29 18:02:34 +08:00
"app_running_num": 0,
}
tr, _ := resources[tenantID]
if tr != nil {
result[tenantID]["service_running_num"] = tr.RunningAppNum
result[tenantID]["cpu"] = tr.CpuRequest
result[tenantID]["memory"] = tr.MemoryRequest
2022-05-29 18:02:34 +08:00
result[tenantID]["app_running_num"] = tr.RunningApplications
}
}
//query disk used in prometheus
query := fmt.Sprintf(`sum(app_resource_appfs{tenant_id=~"%s"}) by(tenant_id)`, strings.Join(ids, "|"))
2020-09-24 21:09:00 +08:00
metric := t.prometheusCli.GetMetric(query, time.Now())
for _, mv := range metric.MetricData.MetricValues {
var tenantID = mv.Metadata["tenant_id"]
var disk int
if mv.Sample != nil {
disk = int(mv.Sample.Value() / 1024)
}
2020-09-24 21:09:00 +08:00
if tenantID != "" {
2021-08-02 13:57:03 +08:00
result[tenantID]["disk"] = disk
}
}
return result, nil
}
//TenantResourceStats tenant resource stats
type TenantResourceStats struct {
2022-05-29 18:02:34 +08:00
TenantID string `json:"tenant_id,omitempty"`
CPURequest int64 `json:"cpu_request,omitempty"`
CPULimit int64 `json:"cpu_limit,omitempty"`
MemoryRequest int64 `json:"memory_request,omitempty"`
MemoryLimit int64 `json:"memory_limit,omitempty"`
RunningAppNum int64 `json:"running_app_num"`
UnscdCPUReq int64 `json:"unscd_cpu_req,omitempty"`
UnscdCPULimit int64 `json:"unscd_cpu_limit,omitempty"`
UnscdMemoryReq int64 `json:"unscd_memory_req,omitempty"`
UnscdMemoryLimit int64 `json:"unscd_memory_limit,omitempty"`
RunningApplications int64 `json:"running_applications"`
}
//GetTenantResource get tenant resource
func (t *TenantAction) GetTenantResource(tenantID string) (ts TenantResourceStats, err error) {
tr, err := t.statusCli.GetTenantResource(tenantID)
if err != nil {
return ts, err
}
ts.TenantID = tenantID
ts.CPULimit = tr.CpuLimit
ts.CPURequest = tr.CpuRequest
ts.MemoryLimit = tr.MemoryLimit
ts.MemoryRequest = tr.MemoryRequest
ts.RunningAppNum = tr.RunningAppNum
2022-05-29 18:02:34 +08:00
ts.RunningApplications = tr.RunningApplications
return
}
// PodResourceInformation -
type PodResourceInformation struct {
NodeName string
ServiceID string
AppID string
Memory int64
ResourceVersion string
CPU int64
StorageEphemeral int64
}
//ClusterResourceStats cluster resource stats
type ClusterResourceStats struct {
AllCPU int64
AllMemory int64
RequestCPU int64
RequestMemory int64
NodePods []PodResourceInformation
AllPods int64
}
2021-04-01 19:08:57 +08:00
func (t *TenantAction) initClusterResource(ctx context.Context) error {
2020-06-17 12:57:41 +08:00
if t.cacheClusterResourceStats == nil || t.cacheTime.Add(time.Minute*3).Before(time.Now()) {
var crs ClusterResourceStats
2021-04-01 19:08:57 +08:00
nodes, err := t.kubeClient.CoreV1().Nodes().List(ctx, metav1.ListOptions{})
if err != nil {
2020-06-17 12:57:41 +08:00
logrus.Errorf("get cluster nodes failure %s", err.Error())
return err
}
usedNodeList := make([]v1.Node, len(nodes.Items))
for i, node := range nodes.Items {
2020-08-12 13:39:33 +08:00
// check if node contains taints
if containsTaints(&node) {
logrus.Debugf("[GetClusterInfo] node(%s) contains NoSchedule taints", node.GetName())
continue
}
usedNodeList[i] = node
2020-06-17 12:57:41 +08:00
for _, c := range node.Status.Conditions {
if c.Type == v1.NodeReady && c.Status != v1.ConditionTrue {
continue
}
}
2020-06-17 12:57:41 +08:00
crs.AllMemory += node.Status.Allocatable.Memory().Value() / (1024 * 1024)
2020-08-13 14:53:55 +08:00
crs.AllCPU += node.Status.Allocatable.Cpu().MilliValue()
}
var nodePodsList []PodResourceInformation
for i := range usedNodeList {
node := usedNodeList[i]
time.Sleep(50 * time.Microsecond)
podList, err := t.kubeClient.CoreV1().Pods(metav1.NamespaceAll).List(ctx, metav1.ListOptions{
FieldSelector: fields.SelectorFromSet(fields.Set{"spec.nodeName": node.Name}).String()})
if err != nil {
logrus.Errorf("get node %v pods error:%v", node.Name, err)
continue
}
crs.AllPods += int64(len(podList.Items))
for _, pod := range podList.Items {
var nodePod PodResourceInformation
nodePod.NodeName = node.Name
if componentID, ok := pod.Labels["service_id"]; ok {
nodePod.ServiceID = componentID
}
if appID, ok := pod.Labels["app_id"]; ok {
nodePod.AppID = appID
}
nodePod.ResourceVersion = pod.ResourceVersion
for _, c := range pod.Spec.Containers {
nodePod.Memory += c.Resources.Requests.Memory().Value()
nodePod.CPU += c.Resources.Requests.Cpu().MilliValue()
nodePod.StorageEphemeral += c.Resources.Requests.StorageEphemeral().Value()
}
nodePodsList = append(nodePodsList, nodePod)
}
}
crs.NodePods = nodePodsList
2020-06-17 12:57:41 +08:00
t.cacheClusterResourceStats = &crs
t.cacheTime = time.Now()
}
return nil
}
// GetAllocatableResources returns allocatable cpu and memory (MB)
2021-04-01 19:08:57 +08:00
func (t *TenantAction) GetAllocatableResources(ctx context.Context) (*ClusterResourceStats, error) {
var crs ClusterResourceStats
2021-04-01 19:08:57 +08:00
if t.initClusterResource(ctx) != nil {
return &crs, nil
}
ts, err := t.statusCli.GetAllTenantResource()
2019-09-03 14:13:09 +08:00
if err != nil {
logrus.Errorf("get tenant resource failure %s", err.Error())
}
re := t.cacheClusterResourceStats
2019-09-03 14:13:09 +08:00
if ts != nil {
crs.RequestCPU = 0
crs.RequestMemory = 0
for _, re := range ts.Resources {
crs.RequestCPU += re.CpuRequest
crs.RequestMemory += re.MemoryRequest
}
2019-09-03 14:13:09 +08:00
}
return re, nil
}
//GetServicesResources Gets the resource usage of the specified service.
func (t *TenantAction) GetServicesResources(tr *api_model.ServicesResources) (re map[string]map[string]interface{}, err error) {
status := t.statusCli.GetStatuss(strings.Join(tr.Body.ServiceIDs, ","))
var running, closed []string
for k, v := range status {
if !t.statusCli.IsClosedStatus(v) {
running = append(running, k)
} else {
closed = append(closed, k)
}
}
2020-06-30 15:08:20 +08:00
2020-11-24 14:59:27 +08:00
podList, err := t.statusCli.GetMultiServicePods(running)
if err != nil {
return nil, err
}
2020-06-30 15:08:20 +08:00
2020-11-24 14:59:27 +08:00
res := make(map[string]map[string]interface{})
for serviceID, item := range podList.ServicePods {
pods := item.NewPods
pods = append(pods, item.OldPods...)
var memory, cpu int64
for _, pod := range pods {
for _, c := range pod.Containers {
memory += c.MemoryRequest
2020-11-24 14:59:27 +08:00
cpu += c.CpuRequest
}
2020-06-30 15:08:20 +08:00
}
2020-11-25 16:39:38 +08:00
res[serviceID] = map[string]interface{}{"memory": memory / 1024 / 1024, "cpu": cpu}
2020-06-30 15:08:20 +08:00
}
for _, c := range closed {
2020-11-24 14:59:27 +08:00
res[c] = map[string]interface{}{"memory": 0, "cpu": 0}
}
2020-11-24 14:59:27 +08:00
2020-09-28 16:43:27 +08:00
disks := GetServicesDiskDeprecated(tr.Body.ServiceIDs, t.prometheusCli)
2019-01-25 23:34:12 +08:00
for serviceID, disk := range disks {
2020-11-24 14:59:27 +08:00
if _, ok := res[serviceID]; ok {
res[serviceID]["disk"] = disk / 1024
2018-07-16 19:06:57 +08:00
} else {
2020-11-24 14:59:27 +08:00
res[serviceID] = make(map[string]interface{})
res[serviceID]["disk"] = disk / 1024
}
}
2020-11-24 14:59:27 +08:00
return res, nil
2017-11-07 11:40:44 +08:00
}
2020-06-30 15:08:20 +08:00
func (t *TenantAction) getPodNums(serviceID string) int {
pods, err := t.statusCli.GetAppPods(context.TODO(), &pb.ServiceRequest{
ServiceId: serviceID,
})
if err != nil {
logrus.Warningf("get app pods: %v", err)
return 0
}
return len(pods.OldPods) + len(pods.NewPods)
}
2017-11-07 11:40:44 +08:00
//TenantsSum TenantsSum
func (t *TenantAction) TenantsSum() (int, error) {
2019-08-01 17:19:22 +08:00
s, err := db.GetManager().TenantDao().GetALLTenants("")
2017-11-07 11:40:44 +08:00
if err != nil {
return 0, err
}
return len(s), nil
}
2018-01-02 18:02:08 +08:00
//GetProtocols GetProtocols
func (t *TenantAction) GetProtocols() ([]*dbmodel.RegionProcotols, *util.APIHandleError) {
2021-03-17 20:44:47 +08:00
return []*dbmodel.RegionProcotols{
{
ProtocolGroup: "http",
ProtocolChild: "http",
APIVersion: "v2",
IsSupport: true,
},
{
ProtocolGroup: "http",
ProtocolChild: "grpc",
APIVersion: "v2",
IsSupport: true,
}, {
ProtocolGroup: "stream",
ProtocolChild: "tcp",
APIVersion: "v2",
IsSupport: true,
}, {
ProtocolGroup: "stream",
ProtocolChild: "udp",
APIVersion: "v2",
IsSupport: true,
}, {
ProtocolGroup: "stream",
ProtocolChild: "mysql",
APIVersion: "v2",
IsSupport: true,
},
}, nil
2018-01-02 18:02:08 +08:00
}
2018-01-18 13:45:24 +08:00
//TransPlugins TransPlugins
2018-03-04 22:48:50 +08:00
func (t *TenantAction) TransPlugins(tenantID, tenantName, fromTenant string, pluginList []string) *util.APIHandleError {
2018-01-18 13:45:24 +08:00
tenantInfo, err := db.GetManager().TenantDao().GetTenantIDByName(fromTenant)
if err != nil {
return util.CreateAPIHandleErrorFromDBError("get tenant infos", err)
}
goodrainID := tenantInfo.UUID
tx := db.GetManager().Begin()
2019-06-20 13:59:19 +08:00
defer func() {
if r := recover(); r != nil {
logrus.Errorf("Unexpected panic occurred, rollback transaction: %v", r)
tx.Rollback()
}
}()
2018-01-18 13:45:24 +08:00
for _, p := range pluginList {
pluginInfo, err := db.GetManager().TenantPluginDao().GetPluginByID(p, goodrainID)
if err != nil {
tx.Rollback()
return util.CreateAPIHandleErrorFromDBError("get plugin infos", err)
}
pluginInfo.TenantID = tenantID
pluginInfo.Domain = tenantName
pluginInfo.ID = 0
err = db.GetManager().TenantPluginDaoTransactions(tx).AddModel(pluginInfo)
if err != nil {
if !strings.Contains(err.Error(), "is exist") {
tx.Rollback()
return util.CreateAPIHandleErrorFromDBError("add plugin Info", err)
}
}
}
if err := tx.Commit().Error; err != nil {
return util.CreateAPIHandleErrorFromDBError("trans plugins infos", err)
}
return nil
}
// GetServicesStatus returns a list of service status matching ids.
func (t *TenantAction) GetServicesStatus(ids string) map[string]string {
return t.statusCli.GetStatuss(ids)
}
//IsClosedStatus checks if the status is closed status.
func (t *TenantAction) IsClosedStatus(status string) bool {
return t.statusCli.IsClosedStatus(status)
}
//GetClusterResource get cluster resource
2021-04-01 19:08:57 +08:00
func (t *TenantAction) GetClusterResource(ctx context.Context) *ClusterResourceStats {
if t.initClusterResource(ctx) != nil {
return nil
}
return t.cacheClusterResourceStats
}
2021-05-07 21:41:09 +08:00
2021-05-12 11:39:38 +08:00
// CheckResourceName checks resource name.
func (t *TenantAction) CheckResourceName(ctx context.Context, namespace string, req *model.CheckResourceNameReq) (*model.CheckResourceNameResp, error) {
obj, ok := t.resources[req.Type]
2021-05-07 21:41:09 +08:00
if !ok {
return nil, bcode.NewBadRequest("unsupported resource: " + req.Type)
}
nctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
retries := 3
for i := 0; i < retries; i++ {
2021-05-12 11:39:38 +08:00
if err := t.k8sClient.Get(nctx, types.NamespacedName{Namespace: namespace, Name: req.Name}, obj); err != nil {
2021-05-07 21:41:09 +08:00
if k8sErrors.IsNotFound(err) {
break
}
return nil, errors.Wrap(err, "ensure app name")
}
req.Name += "-" + rutil.NewUUID()[:5]
}
return &model.CheckResourceNameResp{
Name: req.Name,
}, nil
}