From dd3656b4f33d4f869bbe1c38e55820593851e993 Mon Sep 17 00:00:00 2001 From: GLYASAI Date: Tue, 4 Dec 2018 15:20:59 +0800 Subject: [PATCH] [REV] change label request from LabelStruct to LabelsStruct --- api/controller/resources.go | 80 +++++++++---------- api/handler/ServiceHandler.go | 6 +- api/handler/service.go | 141 ++++++++++++++++++++-------------- api/model/model.go | 4 + db/dao/dao.go | 2 + db/mysql/dao/tenants.go | 18 +++++ 6 files changed, 151 insertions(+), 100 deletions(-) diff --git a/api/controller/resources.go b/api/controller/resources.go index f14f9d1bc..af3dde267 100644 --- a/api/controller/resources.go +++ b/api/controller/resources.go @@ -36,7 +36,7 @@ import ( "github.com/go-chi/chi" "github.com/jinzhu/gorm" - validator "github.com/thedevsaddam/govalidator" + "github.com/thedevsaddam/govalidator" "github.com/goodrain/rainbond/api/handler" httputil "github.com/goodrain/rainbond/util/http" @@ -816,20 +816,20 @@ func (t *TenantStruct) AddServiceLabel(w http.ResponseWriter, r *http.Request) { // schema: // "$ref": "#/responses/commandResponse" // description: 统一返回格式 - rules := validator.MapData{ - "label_values": []string{"required"}, - } - data, ok := httputil.ValidatorRequestMapAndErrorResponse(r, w, rules, nil) - if !ok { - return - } - var valueList []string - valueList = append(valueList, data["label_values"].(string)) - serviceID := r.Context().Value(middleware.ContextKey("service_id")).(string) - if err := handler.GetServiceManager().AddLabel("service", serviceID, valueList); err != nil { - httputil.ReturnError(r, w, 500, fmt.Sprintf("add service label error, %v", err)) - return - } + //rules := validator.MapData{ + // "label_values": []string{"required"}, + //} + //data, ok := httputil.ValidatorRequestMapAndErrorResponse(r, w, rules, nil) + //if !ok { + // return + //} + //var valueList []string + //valueList = append(valueList, data["label_values"].(string)) + //serviceID := r.Context().Value(middleware.ContextKey("service_id")).(string) + //if err := handler.GetServiceManager().AddLabel("service", serviceID, valueList); err != nil { + // httputil.ReturnError(r, w, 500, fmt.Sprintf("add service label error, %v", err)) + // return + //} httputil.ReturnSuccess(r, w, nil) } @@ -910,12 +910,12 @@ func (t *TenantStruct) AddNodeLabel(w http.ResponseWriter, r *http.Request) { return } //logrus.Info(labels.Body.LabelValues) - valueList := labels.Body.LabelValues - serviceID := r.Context().Value(middleware.ContextKey("service_id")).(string) - if err := handler.GetServiceManager().AddLabel("node", serviceID, valueList); err != nil { - httputil.ReturnError(r, w, 500, fmt.Sprintf("add node label failure, %v", err)) - return - } + //valueList := labels.Body.LabelValues + //serviceID := r.Context().Value(middleware.ContextKey("service_id")).(string) + //if err := handler.GetServiceManager().AddLabel("node", serviceID, valueList); err != nil { + // httputil.ReturnError(r, w, 500, fmt.Sprintf("add node label failure, %v", err)) + // return + //} httputil.ReturnSuccess(r, w, nil) } @@ -932,7 +932,7 @@ func (t *TenantStruct) Label(w http.ResponseWriter, r *http.Request) { func (t *TenantStruct) AddLabel(w http.ResponseWriter, r *http.Request) { logrus.Debugf("add label") - var req api_model.LabelStruct + var req api_model.LabelsStruct ok := httputil.ValidatorRequestStructAndErrorResponse(r, w, &req, nil) if !ok { return @@ -941,7 +941,7 @@ func (t *TenantStruct) AddLabel(w http.ResponseWriter, r *http.Request) { logrus.Debugf("Request is : %s", string(reqJSON)) serviceID := r.Context().Value(middleware.ContextKey("service_id")).(string) - if err := handler.GetServiceManager().AddLabel(req.LabelKey, serviceID, req.LabelValues); err != nil { + if err := handler.GetServiceManager().AddLabel(&req, serviceID); err != nil { httputil.ReturnError(r, w, 500, fmt.Sprintf("add label error, %v", err)) return } @@ -950,7 +950,7 @@ func (t *TenantStruct) AddLabel(w http.ResponseWriter, r *http.Request) { func (t *TenantStruct) DeleteLabel(w http.ResponseWriter, r *http.Request) { logrus.Debugf("delete label") - var req api_model.LabelStruct + var req api_model.LabelsStruct ok := httputil.ValidatorRequestStructAndErrorResponse(r, w, &req, nil) if !ok { return @@ -959,7 +959,7 @@ func (t *TenantStruct) DeleteLabel(w http.ResponseWriter, r *http.Request) { logrus.Debugf("Request is : %s", string(reqJSON)) serviceID := r.Context().Value(middleware.ContextKey("service_id")).(string) - if err := handler.GetServiceManager().DeleteLabel(req.LabelKey, serviceID, req.LabelValues); err != nil { + if err := handler.GetServiceManager().DeleteLabel(&req, serviceID); err != nil { httputil.ReturnError(r, w, 500, fmt.Sprintf("delete node label failure, %v", err)) return } @@ -968,7 +968,7 @@ func (t *TenantStruct) DeleteLabel(w http.ResponseWriter, r *http.Request) { func (t *TenantStruct) UpdateLabel(w http.ResponseWriter, r *http.Request) { logrus.Debugf("update label") - var req api_model.LabelStruct + var req api_model.LabelsStruct ok := httputil.ValidatorRequestStructAndErrorResponse(r, w, &req, nil) if !ok { return @@ -977,7 +977,7 @@ func (t *TenantStruct) UpdateLabel(w http.ResponseWriter, r *http.Request) { logrus.Debugf("Request is : %s", string(reqJSON)) serviceID := r.Context().Value(middleware.ContextKey("service_id")).(string) - if err := handler.GetServiceManager().UpdateLabel(req.LabelKey, serviceID, req.LabelValues); err != nil { + if err := handler.GetServiceManager().UpdateLabel(&req, serviceID); err != nil { httputil.ReturnError(r, w, 500, fmt.Sprintf("error updating label: %v", err)) return } @@ -1007,19 +1007,19 @@ func (t *TenantStruct) DeleteNodeLabel(w http.ResponseWriter, r *http.Request) { // "$ref": "#/responses/commandResponse" // description: 统一返回格式 - var labels api_model.AddNodeLabelStruct - ok := httputil.ValidatorRequestStructAndErrorResponse(r, w, &labels.Body, nil) - if !ok { - return - } - //logrus.Info(labels.Body.LabelValues) - valueList := labels.Body.LabelValues - serviceID := r.Context().Value(middleware.ContextKey("service_id")).(string) - if err := handler.GetServiceManager().DeleteLabel("node", serviceID, valueList); err != nil { - httputil.ReturnError(r, w, 500, fmt.Sprintf("delete node label failure, %v", err)) - return - } - httputil.ReturnSuccess(r, w, nil) + //var labels api_model.AddNodeLabelStruct + //ok := httputil.ValidatorRequestStructAndErrorResponse(r, w, &labels.Body, nil) + //if !ok { + // return + //} + ////logrus.Info(labels.Body.LabelValues) + //valueList := labels.Body.LabelValues + //serviceID := r.Context().Value(middleware.ContextKey("service_id")).(string) + //if err := handler.GetServiceManager().DeleteLabel("node", serviceID, valueList); err != nil { + // httputil.ReturnError(r, w, 500, fmt.Sprintf("delete node label failure, %v", err)) + // return + //} + //httputil.ReturnSuccess(r, w, nil) } //StatusContainerID StatusContainerID diff --git a/api/handler/ServiceHandler.go b/api/handler/ServiceHandler.go index 225bebbce..16247081e 100644 --- a/api/handler/ServiceHandler.go +++ b/api/handler/ServiceHandler.go @@ -29,9 +29,9 @@ import ( //ServiceHandler service handler type ServiceHandler interface { ServiceBuild(tenantID, serviceID string, r *api_model.BuildServiceStruct) error - AddLabel(kind, serviceID string, valueList []string) error - DeleteLabel(kind, serviceID string, amp []string) error - UpdateLabel(serviceID, kind string, amp []string) error + AddLabel(l *api_model.LabelsStruct, serviceID string) error + DeleteLabel(l *api_model.LabelsStruct, serviceID string) error + UpdateLabel(l *api_model.LabelsStruct, serviceID string) error UpdateServiceLabel(serviceID, value string) error StartStopService(s *api_model.StartStopStruct) error ServiceVertical(v *model.VerticalScalingTaskBody) error diff --git a/api/handler/service.go b/api/handler/service.go index 204f1c049..493bc537f 100644 --- a/api/handler/service.go +++ b/api/handler/service.go @@ -211,70 +211,81 @@ func (s *ServiceAction) buildFromSourceCode(r *api_model.BuildServiceStruct, ser } //AddLabel add labels -func (s *ServiceAction) AddLabel(kind, serviceID string, amp []string) error { - for _, v := range amp { - var labelModel dbmodel.TenantServiceLable - switch kind { - case "service": - labelModel.ServiceID = serviceID - labelModel.LabelKey = core_model.LabelKeyServiceType - v = chekeServiceLabel(v) - labelModel.LabelValue = v - case "node": - labelModel.ServiceID = serviceID - labelModel.LabelKey = v - labelModel.LabelValue = core_model.LabelKeyNodeSelector - default: - labelModel.ServiceID = serviceID - labelModel.LabelKey = kind - labelModel.LabelValue = v - } - if err := db.GetManager().TenantServiceLabelDao().AddModel(&labelModel); err != nil { - return err +func (s *ServiceAction) AddLabel(l *api_model.LabelsStruct, serviceID string) error { + tx := db.GetManager().Begin() + for _, label := range l.Labels { + for _, v := range label.LabelValues { + var labelModel dbmodel.TenantServiceLable + switch label.LabelKey { + case "service": + labelModel.ServiceID = serviceID + labelModel.LabelKey = core_model.LabelKeyServiceType + v = chekeServiceLabel(v) + labelModel.LabelValue = v + case "node": + labelModel.ServiceID = serviceID + labelModel.LabelKey = v + labelModel.LabelValue = core_model.LabelKeyNodeSelector + default: + labelModel.ServiceID = serviceID + labelModel.LabelKey = label.LabelKey + labelModel.LabelValue = v + } + if err := db.GetManager().TenantServiceLabelDaoTransactions(tx).AddModel(&labelModel); err != nil { + return err + } } } + if err := tx.Commit().Error; err != nil { + tx.Rollback() + return err + } return nil } //UpdateLabel updates labels -func (s *ServiceAction) UpdateLabel(serviceID, kind string, amp []string) error { +func (s *ServiceAction) UpdateLabel(l *api_model.LabelsStruct, serviceID string) error { tx := db.GetManager().Begin() - // delete old labels - var err error - switch kind { - case "node": - err = db.GetManager().TenantServiceLabelDaoTransactions(tx).DELTenantServiceLabelsByLabelvaluesAndServiceID(serviceID, amp) - default: - err = db.GetManager().TenantServiceLabelDaoTransactions(tx).DelTenantServiceLabels(serviceID, kind, amp) - } - if err != nil { - logrus.Errorf("error deleting old labels: %v", err) - tx.Rollback() - return err - } - // add new labels - for _, v := range amp { - var labelModel dbmodel.TenantServiceLable - switch kind { - case "service": - labelModel.ServiceID = serviceID - labelModel.LabelKey = core_model.LabelKeyServiceType - v = chekeServiceLabel(v) - labelModel.LabelValue = v + for _, label := range l.Labels { + // delete old labels + var err error + switch label.LabelKey { case "node": - labelModel.ServiceID = serviceID - labelModel.LabelKey = v - labelModel.LabelValue = core_model.LabelKeyNodeSelector + err = db.GetManager().TenantServiceLabelDaoTransactions(tx). + DelTenantServiceLabelsByLabelValuesAndServiceID(serviceID) default: - labelModel.ServiceID = serviceID - labelModel.LabelKey = kind - labelModel.LabelValue = v + err = db.GetManager().TenantServiceLabelDaoTransactions(tx). + DelTenantServiceLabelsByKey(serviceID, label.LabelKey) } - if err := db.GetManager().TenantServiceLabelDaoTransactions(tx).AddModel(&labelModel); err != nil { - logrus.Errorf("error adding new labels: %v", err) + if err != nil { + logrus.Errorf("error deleting old labels: %v", err) tx.Rollback() return err } + // add new labels + for _, v := range label.LabelValues { + var labelModel dbmodel.TenantServiceLable + switch label.LabelKey { + case "service": + labelModel.ServiceID = serviceID + labelModel.LabelKey = core_model.LabelKeyServiceType + v = chekeServiceLabel(v) + labelModel.LabelValue = v + case "node": + labelModel.ServiceID = serviceID + labelModel.LabelKey = v + labelModel.LabelValue = core_model.LabelKeyNodeSelector + default: + labelModel.ServiceID = serviceID + labelModel.LabelKey = label.LabelKey + labelModel.LabelValue = v + } + if err := db.GetManager().TenantServiceLabelDaoTransactions(tx).AddModel(&labelModel); err != nil { + logrus.Errorf("error adding new labels: %v", err) + tx.Rollback() + return err + } + } } if err := tx.Commit().Error; err != nil { tx.Rollback() @@ -284,13 +295,29 @@ func (s *ServiceAction) UpdateLabel(serviceID, kind string, amp []string) error } //DeleteLabel deletes label -func (s *ServiceAction) DeleteLabel(kind, serviceID string, amp []string) error { - switch kind { - case "node": - return db.GetManager().TenantServiceLabelDao().DELTenantServiceLabelsByLabelvaluesAndServiceID(serviceID, amp) - default: - return db.GetManager().TenantServiceLabelDao().DelTenantServiceLabels(serviceID, kind, amp) +func (s *ServiceAction) DeleteLabel(l *api_model.LabelsStruct, serviceID string) error { + tx := db.GetManager().Begin() + var err error + for _, label := range l.Labels { + switch label.LabelKey { + case "node": + err = db.GetManager().TenantServiceLabelDaoTransactions(tx). + DELTenantServiceLabelsByLabelvaluesAndServiceID(serviceID, label.LabelValues) + default: + err = db.GetManager().TenantServiceLabelDaoTransactions(tx). + DelTenantServiceLabels(serviceID, label.LabelKey, label.LabelValues) + } + if err != nil { + logrus.Errorf("error deleting label: %v", err) + tx.Rollback() + return err + } } + if err := tx.Commit().Error; err != nil { + tx.Rollback() + return err + } + return nil } //UpdateServiceLabel UpdateLabel diff --git a/api/model/model.go b/api/model/model.go index 92017e9f0..ccf1584d7 100644 --- a/api/model/model.go +++ b/api/model/model.go @@ -803,6 +803,10 @@ type AddNodeLabelStruct struct { } } +type LabelsStruct struct { + Labels []LabelStruct `json:"labels"` +} + // LabelStruct holds info for adding, updating or deleting label type LabelStruct struct { LabelKey string `json:"label_key" validate:"label_key|required"` diff --git a/db/dao/dao.go b/db/dao/dao.go index 5a37cff91..a9764436c 100644 --- a/db/dao/dao.go +++ b/db/dao/dao.go @@ -245,7 +245,9 @@ type TenantServiceLabelDao interface { GetTenantServiceAffinityLabel(serviceID string) ([]*model.TenantServiceLable, error) GetTenantServiceTypeLabel(serviceID string) (*model.TenantServiceLable, error) DELTenantServiceLabelsByLabelvaluesAndServiceID(serviceID string, labelValues []string) error + DelTenantServiceLabelsByLabelValuesAndServiceID(serviceID string) error DelTenantServiceLabels(serviceID string, labelKey string, labelValues []string) error + DelTenantServiceLabelsByKey(serviceID string, labelKey string) error } //LocalSchedulerDao 本地调度信息 diff --git a/db/mysql/dao/tenants.go b/db/mysql/dao/tenants.go index ee6a28476..780d85fcf 100644 --- a/db/mysql/dao/tenants.go +++ b/db/mysql/dao/tenants.go @@ -1272,6 +1272,15 @@ func (t *ServiceLabelDaoImpl) DELTenantServiceLabelsByLabelvaluesAndServiceID(se return nil } +//DELTenantServiceLabelsByLabelvaluesAndServiceID DELTenantServiceLabelsByLabelvaluesAndServiceID +func (t *ServiceLabelDaoImpl) DelTenantServiceLabelsByLabelValuesAndServiceID(serviceID string) error { + var label model.TenantServiceLable + if err := t.DB.Where("service_id=? and label_value=?", serviceID, model.LabelKeyNodeSelector).Delete(&label).Error; err != nil { + return err + } + return nil +} + //DelTenantServiceLabels deletes labels func (t *ServiceLabelDaoImpl) DelTenantServiceLabels(serviceID string, labelKey string, labelValues []string) error { var label model.TenantServiceLable @@ -1281,3 +1290,12 @@ func (t *ServiceLabelDaoImpl) DelTenantServiceLabels(serviceID string, labelKey } return nil } + +//DelTenantServiceLabelsByKey deletes labels by label key +func (t *ServiceLabelDaoImpl) DelTenantServiceLabelsByKey(serviceID string, labelKey string) error { + var label model.TenantServiceLable + if err := t.DB.Where("service_id=? and label_key=?", serviceID, labelKey).Delete(&label).Error; err != nil { + return err + } + return nil +}