fix wrong memory for service

This commit is contained in:
GLYASAI 2020-06-30 15:08:20 +08:00
parent df1f25206e
commit 15da4546bc
4 changed files with 82 additions and 26 deletions

View File

@ -19,6 +19,7 @@
package handler
import (
"context"
"encoding/json"
"fmt"
"net/http"
@ -35,6 +36,7 @@ import (
mqclient "github.com/goodrain/rainbond/mq/client"
cli "github.com/goodrain/rainbond/node/nodem/client"
"github.com/goodrain/rainbond/worker/client"
"github.com/goodrain/rainbond/worker/server/pb"
)
//TenantAction tenant act
@ -415,10 +417,20 @@ func (t *TenantAction) GetServicesResources(tr *api_model.ServicesResources) (re
closed = append(closed, k)
}
}
resmp, err := db.GetManager().TenantServiceDao().GetServiceMemoryByServiceIDs(running)
if err != nil {
return nil, err
}
for serviceID, item := range resmp {
podNums := t.getPodNums(serviceID)
memory, ok := item["memory"].(int)
if ok {
item["memory"] = memory * podNums
}
}
for _, c := range closed {
resmp[c] = map[string]interface{}{"memory": 0, "cpu": 0}
}
@ -435,6 +447,19 @@ func (t *TenantAction) GetServicesResources(tr *api_model.ServicesResources) (re
return resmp, nil
}
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)
}
//TenantsSum TenantsSum
func (t *TenantAction) TenantsSum() (int, error) {
s, err := db.GetManager().TenantDao().GetALLTenants("")

View File

@ -293,7 +293,7 @@ func (t *TenantServicesDaoImpl) GetServiceMemoryByTenantIDs(tenantIDs []string,
//GetServiceMemoryByServiceIDs get service memory by service ids
func (t *TenantServicesDaoImpl) GetServiceMemoryByServiceIDs(serviceIDs []string) (map[string]map[string]interface{}, error) {
rows, err := t.DB.Raw("select service_id, container_cpu as cpu,container_memory * replicas as memory from tenant_services where service_id in (?)", serviceIDs).Rows()
rows, err := t.DB.Raw("select service_id, container_cpu as cpu, container_memory as memory from tenant_services where service_id in (?)", serviceIDs).Rows()
if err != nil {
return nil, err
}

View File

@ -38,6 +38,7 @@ func (m *MockStorer) EXPECT() *MockStorerMockRecorder {
// Start mocks base method
func (m *MockStorer) Start() error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Start")
ret0, _ := ret[0].(error)
return ret0
@ -45,11 +46,13 @@ func (m *MockStorer) Start() error {
// Start indicates an expected call of Start
func (mr *MockStorerMockRecorder) Start() *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Start", reflect.TypeOf((*MockStorer)(nil).Start))
}
// Ready mocks base method
func (m *MockStorer) Ready() bool {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Ready")
ret0, _ := ret[0].(bool)
return ret0
@ -57,21 +60,25 @@ func (m *MockStorer) Ready() bool {
// Ready indicates an expected call of Ready
func (mr *MockStorerMockRecorder) Ready() *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Ready", reflect.TypeOf((*MockStorer)(nil).Ready))
}
// RegistAppService mocks base method
func (m *MockStorer) RegistAppService(arg0 *v1.AppService) {
m.ctrl.T.Helper()
m.ctrl.Call(m, "RegistAppService", arg0)
}
// RegistAppService indicates an expected call of RegistAppService
func (mr *MockStorerMockRecorder) RegistAppService(arg0 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RegistAppService", reflect.TypeOf((*MockStorer)(nil).RegistAppService), arg0)
}
// GetAppService mocks base method
func (m *MockStorer) GetAppService(serviceID string) *v1.AppService {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "GetAppService", serviceID)
ret0, _ := ret[0].(*v1.AppService)
return ret0
@ -79,11 +86,13 @@ func (m *MockStorer) GetAppService(serviceID string) *v1.AppService {
// GetAppService indicates an expected call of GetAppService
func (mr *MockStorerMockRecorder) GetAppService(serviceID interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetAppService", reflect.TypeOf((*MockStorer)(nil).GetAppService), serviceID)
}
// UpdateGetAppService mocks base method
func (m *MockStorer) UpdateGetAppService(serviceID string) *v1.AppService {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "UpdateGetAppService", serviceID)
ret0, _ := ret[0].(*v1.AppService)
return ret0
@ -91,11 +100,13 @@ func (m *MockStorer) UpdateGetAppService(serviceID string) *v1.AppService {
// UpdateGetAppService indicates an expected call of UpdateGetAppService
func (mr *MockStorerMockRecorder) UpdateGetAppService(serviceID interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateGetAppService", reflect.TypeOf((*MockStorer)(nil).UpdateGetAppService), serviceID)
}
// GetAllAppServices mocks base method
func (m *MockStorer) GetAllAppServices() []*v1.AppService {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "GetAllAppServices")
ret0, _ := ret[0].([]*v1.AppService)
return ret0
@ -103,11 +114,13 @@ func (m *MockStorer) GetAllAppServices() []*v1.AppService {
// GetAllAppServices indicates an expected call of GetAllAppServices
func (mr *MockStorerMockRecorder) GetAllAppServices() *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetAllAppServices", reflect.TypeOf((*MockStorer)(nil).GetAllAppServices))
}
// GetAppServiceStatus mocks base method
func (m *MockStorer) GetAppServiceStatus(serviceID string) string {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "GetAppServiceStatus", serviceID)
ret0, _ := ret[0].(string)
return ret0
@ -115,11 +128,13 @@ func (m *MockStorer) GetAppServiceStatus(serviceID string) string {
// GetAppServiceStatus indicates an expected call of GetAppServiceStatus
func (mr *MockStorerMockRecorder) GetAppServiceStatus(serviceID interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetAppServiceStatus", reflect.TypeOf((*MockStorer)(nil).GetAppServiceStatus), serviceID)
}
// GetAppServicesStatus mocks base method
func (m *MockStorer) GetAppServicesStatus(serviceIDs []string) map[string]string {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "GetAppServicesStatus", serviceIDs)
ret0, _ := ret[0].(map[string]string)
return ret0
@ -127,11 +142,13 @@ func (m *MockStorer) GetAppServicesStatus(serviceIDs []string) map[string]string
// GetAppServicesStatus indicates an expected call of GetAppServicesStatus
func (mr *MockStorerMockRecorder) GetAppServicesStatus(serviceIDs interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetAppServicesStatus", reflect.TypeOf((*MockStorer)(nil).GetAppServicesStatus), serviceIDs)
}
// GetTenantResource mocks base method
func (m *MockStorer) GetTenantResource(tenantID string) *v1.TenantResource {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "GetTenantResource", tenantID)
ret0, _ := ret[0].(*v1.TenantResource)
return ret0
@ -139,11 +156,13 @@ func (m *MockStorer) GetTenantResource(tenantID string) *v1.TenantResource {
// GetTenantResource indicates an expected call of GetTenantResource
func (mr *MockStorerMockRecorder) GetTenantResource(tenantID interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetTenantResource", reflect.TypeOf((*MockStorer)(nil).GetTenantResource), tenantID)
}
// GetTenantRunningApp mocks base method
func (m *MockStorer) GetTenantRunningApp(tenantID string) []*v1.AppService {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "GetTenantRunningApp", tenantID)
ret0, _ := ret[0].([]*v1.AppService)
return ret0
@ -151,11 +170,13 @@ func (m *MockStorer) GetTenantRunningApp(tenantID string) []*v1.AppService {
// GetTenantRunningApp indicates an expected call of GetTenantRunningApp
func (mr *MockStorerMockRecorder) GetTenantRunningApp(tenantID interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetTenantRunningApp", reflect.TypeOf((*MockStorer)(nil).GetTenantRunningApp), tenantID)
}
// GetNeedBillingStatus mocks base method
func (m *MockStorer) GetNeedBillingStatus(serviceIDs []string) map[string]string {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "GetNeedBillingStatus", serviceIDs)
ret0, _ := ret[0].(map[string]string)
return ret0
@ -163,11 +184,13 @@ func (m *MockStorer) GetNeedBillingStatus(serviceIDs []string) map[string]string
// GetNeedBillingStatus indicates an expected call of GetNeedBillingStatus
func (mr *MockStorerMockRecorder) GetNeedBillingStatus(serviceIDs interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetNeedBillingStatus", reflect.TypeOf((*MockStorer)(nil).GetNeedBillingStatus), serviceIDs)
}
// OnDeletes mocks base method
func (m *MockStorer) OnDeletes(obj ...interface{}) {
m.ctrl.T.Helper()
varargs := []interface{}{}
for _, a := range obj {
varargs = append(varargs, a)
@ -177,11 +200,13 @@ func (m *MockStorer) OnDeletes(obj ...interface{}) {
// OnDeletes indicates an expected call of OnDeletes
func (mr *MockStorerMockRecorder) OnDeletes(obj ...interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "OnDeletes", reflect.TypeOf((*MockStorer)(nil).OnDeletes), obj...)
}
// GetPodLister mocks base method
func (m *MockStorer) GetPodLister() v11.PodLister {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "GetPodLister")
ret0, _ := ret[0].(v11.PodLister)
return ret0
@ -189,31 +214,61 @@ func (m *MockStorer) GetPodLister() v11.PodLister {
// GetPodLister indicates an expected call of GetPodLister
func (mr *MockStorerMockRecorder) GetPodLister() *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetPodLister", reflect.TypeOf((*MockStorer)(nil).GetPodLister))
}
// RegistPodUpdateListener mocks base method
func (m *MockStorer) RegistPodUpdateListener(arg0 string, arg1 chan<- *v10.Pod) {
m.ctrl.T.Helper()
m.ctrl.Call(m, "RegistPodUpdateListener", arg0, arg1)
}
// RegistPodUpdateListener indicates an expected call of RegistPodUpdateListener
func (mr *MockStorerMockRecorder) RegistPodUpdateListener(arg0, arg1 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RegistPodUpdateListener", reflect.TypeOf((*MockStorer)(nil).RegistPodUpdateListener), arg0, arg1)
}
// UnRegistPodUpdateListener mocks base method
func (m *MockStorer) UnRegistPodUpdateListener(arg0 string) {
m.ctrl.T.Helper()
m.ctrl.Call(m, "UnRegistPodUpdateListener", arg0)
}
// UnRegistPodUpdateListener indicates an expected call of UnRegistPodUpdateListener
func (mr *MockStorerMockRecorder) UnRegistPodUpdateListener(arg0 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UnRegistPodUpdateListener", reflect.TypeOf((*MockStorer)(nil).UnRegistPodUpdateListener), arg0)
}
// RegisterVolumeTypeListener mocks base method
func (m *MockStorer) RegisterVolumeTypeListener(arg0 string, arg1 chan<- *model.TenantServiceVolumeType) {
m.ctrl.T.Helper()
m.ctrl.Call(m, "RegisterVolumeTypeListener", arg0, arg1)
}
// RegisterVolumeTypeListener indicates an expected call of RegisterVolumeTypeListener
func (mr *MockStorerMockRecorder) RegisterVolumeTypeListener(arg0, arg1 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RegisterVolumeTypeListener", reflect.TypeOf((*MockStorer)(nil).RegisterVolumeTypeListener), arg0, arg1)
}
// UnRegisterVolumeTypeListener mocks base method
func (m *MockStorer) UnRegisterVolumeTypeListener(arg0 string) {
m.ctrl.T.Helper()
m.ctrl.Call(m, "UnRegisterVolumeTypeListener", arg0)
}
// UnRegisterVolumeTypeListener indicates an expected call of UnRegisterVolumeTypeListener
func (mr *MockStorerMockRecorder) UnRegisterVolumeTypeListener(arg0 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UnRegisterVolumeTypeListener", reflect.TypeOf((*MockStorer)(nil).UnRegisterVolumeTypeListener), arg0)
}
// InitOneThirdPartService mocks base method
func (m *MockStorer) InitOneThirdPartService(service *model.TenantServices) error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "InitOneThirdPartService", service)
ret0, _ := ret[0].(error)
return ret0
@ -221,5 +276,6 @@ func (m *MockStorer) InitOneThirdPartService(service *model.TenantServices) erro
// InitOneThirdPartService indicates an expected call of InitOneThirdPartService
func (mr *MockStorerMockRecorder) InitOneThirdPartService(service interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "InitOneThirdPartService", reflect.TypeOf((*MockStorer)(nil).InitOneThirdPartService), service)
}

View File

@ -25,7 +25,6 @@ import (
"time"
"github.com/eapache/channels"
autoscalingv2beta2 "k8s.io/api/autoscaling/v2beta2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
@ -140,30 +139,6 @@ func TestGetAppVolumeStatus(t *testing.T) {
time.Sleep(20 * time.Second)
}
func TestListHPAEvents(t *testing.T) {
c, err := clientcmd.BuildConfigFromFlags("", "/opt/rainbond/etc/kubernetes/kubecfg/admin.kubeconfig")
if err != nil {
t.Fatalf("read kube config file error: %v", err)
}
clientset, err := kubernetes.NewForConfig(c)
if err != nil {
t.Fatalf("create kube api client error: %v", err)
}
a := appRuntimeStore{
clientset: clientset,
}
hpa := &v2beta1.HorizontalPodAutoscaler{
ObjectMeta: metav1.ObjectMeta{
Name: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
Namespace: "bab18e6b1c8640979b91f8dfdd211226",
},
}
if err := a.listHPAEvents(hpa); err != nil {
t.Fatal(err)
}
}
func getStoreForTest(t *testing.T, ocfg *option.Config) Storer {
if ocfg == nil {