This commit is contained in:
barnett 2018-12-04 16:06:22 +08:00
commit e596450a83
8 changed files with 139 additions and 251 deletions

View File

@ -65,8 +65,6 @@ type ServiceInterface interface {
Pods(w http.ResponseWriter, r *http.Request)
VolumeDependency(w http.ResponseWriter, r *http.Request)
Probe(w http.ResponseWriter, r *http.Request)
ServiceLabel(w http.ResponseWriter, r *http.Request)
NodeLabel(w http.ResponseWriter, r *http.Request)
Label(w http.ResponseWriter, r *http.Request)
Share(w http.ResponseWriter, r *http.Request)
ShareResult(w http.ResponseWriter, r *http.Request)

View File

@ -198,13 +198,6 @@ func (v2 *V2) serviceRouter() chi.Router {
r.Put("/probe", controller.GetManager().Probe)
r.Delete("/probe", controller.GetManager().Probe)
//应用标签 增 删 (source)
r.Post("/service-label", controller.GetManager().ServiceLabel)
r.Put("/service-label", controller.GetManager().ServiceLabel)
//节点标签 增 删
r.Post("/node-label", controller.GetManager().NodeLabel)
r.Delete("/node-label", controller.GetManager().NodeLabel)
// label
r.Post("/label", controller.GetManager().Label)
r.Put("/label", controller.GetManager().Label)
r.Delete("/label", controller.GetManager().Label)

View File

@ -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"
@ -784,141 +784,6 @@ func (t *TenantStruct) StatusServiceList(w http.ResponseWriter, r *http.Request)
httputil.ReturnSuccess(r, w, info)
}
//ServiceLabel label
func (t *TenantStruct) ServiceLabel(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "PUT":
t.UpdateServiceLabel(w, r)
case "POST":
t.AddServiceLabel(w, r)
}
}
//AddServiceLabel AddServiceLabel
func (t *TenantStruct) AddServiceLabel(w http.ResponseWriter, r *http.Request) {
// swagger:operation POST /v2/tenants/{tenant_name}/services/{service_alias}/service-label v2 addServiceLabel
//
// 添加应用标签
//
// add service label
//
// ---
// consumes:
// - application/json
// - application/x-protobuf
//
// produces:
// - application/json
// - application/xml
//
// responses:
// default:
// 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
}
httputil.ReturnSuccess(r, w, nil)
}
//UpdateServiceLabel UpdateServiceLabel
func (t *TenantStruct) UpdateServiceLabel(w http.ResponseWriter, r *http.Request) {
// swagger:operation PUT /v2/tenants/{tenant_name}/services/{service_alias}/service-label v2 updateServiceLabel
//
// 更新应用标签
//
// delete service label
//
// ---
// consumes:
// - application/json
// - application/x-protobuf
//
// produces:
// - application/json
// - application/xml
//
// responses:
// default:
// schema:
// "$ref": "#/responses/commandResponse"
// description: 统一返回格式
rules := validator.MapData{
"label_values": []string{"required"},
}
data, ok := httputil.ValidatorRequestMapAndErrorResponse(r, w, rules, nil)
if !ok {
return
}
value := data["label_values"].(string)
serviceID := r.Context().Value(middleware.ContextKey("service_id")).(string)
if err := handler.GetServiceManager().UpdateServiceLabel(serviceID, value); err != nil {
httputil.ReturnError(r, w, 500, fmt.Sprintf("update service label error, %v", err))
return
}
httputil.ReturnSuccess(r, w, nil)
}
//NodeLabel label
func (t *TenantStruct) NodeLabel(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "DELETE":
t.DeleteNodeLabel(w, r)
case "POST":
t.AddNodeLabel(w, r)
}
}
//AddNodeLabel AddNodeLabel
func (t *TenantStruct) AddNodeLabel(w http.ResponseWriter, r *http.Request) {
// swagger:operation POST /v2/tenants/{tenant_name}/services/{service_alias}/node-label v2 addNodeLabel
//
// 添加节点标签
//
// add node label
//
// ---
// consumes:
// - application/json
// - application/x-protobuf
//
// produces:
// - application/json
// - application/xml
//
// responses:
// default:
// schema:
// "$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().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)
}
func (t *TenantStruct) Label(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "DELETE":
@ -932,7 +797,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 +806,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 +815,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 +824,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 +833,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,50 +842,13 @@ 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
}
httputil.ReturnSuccess(r, w, nil)
}
//DeleteNodeLabel DeleteLabel
func (t *TenantStruct) DeleteNodeLabel(w http.ResponseWriter, r *http.Request) {
// swagger:operation DELETE /v2/tenants/{tenant_name}/services/{service_alias}/node-label v2 deleteNodeLabel
//
// 删除节点标签
//
// delete node label
//
// ---
// consumes:
// - application/json
// - application/x-protobuf
//
// produces:
// - application/json
// - application/xml
//
// responses:
// default:
// schema:
// "$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)
}
//StatusContainerID StatusContainerID
func (t *TenantStruct) StatusContainerID(w http.ResponseWriter, r *http.Request) {

View File

@ -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

View File

@ -212,67 +212,25 @@ 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
}
}
return nil
}
//UpdateLabel updates labels
func (s *ServiceAction) UpdateLabel(serviceID, kind string, amp []string) error {
func (s *ServiceAction) AddLabel(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 {
for _, label := range l.Labels {
var labelModel dbmodel.TenantServiceLable
switch kind {
case "service":
switch label.LabelKey {
case core_model.LabelKeyServiceType:
labelModel.ServiceID = serviceID
labelModel.LabelKey = core_model.LabelKeyServiceType
v = chekeServiceLabel(v)
labelModel.LabelValue = v
case "node":
labelModel.LabelValue = chekeServiceLabel(label.LabelValue)
case core_model.LabelKeyNodeSelector:
labelModel.ServiceID = serviceID
labelModel.LabelKey = v
labelModel.LabelKey = label.LabelValue
labelModel.LabelValue = core_model.LabelKeyNodeSelector
default:
labelModel.ServiceID = serviceID
labelModel.LabelKey = kind
labelModel.LabelValue = v
labelModel.LabelKey = label.LabelKey
labelModel.LabelValue = label.LabelValue
}
if err := db.GetManager().TenantServiceLabelDaoTransactions(tx).AddModel(&labelModel); err != nil {
logrus.Errorf("error adding new labels: %v", err)
tx.Rollback()
return err
}
@ -284,14 +242,79 @@ func (s *ServiceAction) UpdateLabel(serviceID, kind string, amp []string) error
return nil
}
//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)
//UpdateLabel updates labels
func (s *ServiceAction) UpdateLabel(l *api_model.LabelsStruct, serviceID string) error {
tx := db.GetManager().Begin()
for _, label := range l.Labels {
// delete old labels
var err error
switch label.LabelKey {
case core_model.LabelKeyNodeSelector:
err = db.GetManager().TenantServiceLabelDaoTransactions(tx).
DelTenantServiceLabelsByLabelValuesAndServiceID(serviceID)
default:
err = db.GetManager().TenantServiceLabelDaoTransactions(tx).
DelTenantServiceLabelsByKey(serviceID, label.LabelKey)
}
if err != nil {
logrus.Errorf("error deleting old labels: %v", err)
tx.Rollback()
return err
}
// add new labels
var labelModel dbmodel.TenantServiceLable
switch label.LabelKey {
case core_model.LabelKeyServiceType:
labelModel.ServiceID = serviceID
labelModel.LabelKey = core_model.LabelKeyServiceType
labelModel.LabelValue = chekeServiceLabel(label.LabelValue)
case core_model.LabelKeyNodeSelector:
labelModel.ServiceID = serviceID
labelModel.LabelKey = label.LabelValue
labelModel.LabelValue = core_model.LabelKeyNodeSelector
default:
labelModel.ServiceID = serviceID
labelModel.LabelKey = label.LabelKey
labelModel.LabelValue = label.LabelValue
}
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()
return err
}
return nil
}
//DeleteLabel deletes label
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 core_model.LabelKeyNodeSelector:
err = db.GetManager().TenantServiceLabelDaoTransactions(tx).
DelTenantServiceLabelByLabelValueAndServiceID(serviceID, label.LabelValue)
default:
err = db.GetManager().TenantServiceLabelDaoTransactions(tx).
DelTenantServiceLabelsByKeyValueServiceID(serviceID, label.LabelKey, label.LabelValue)
}
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

View File

@ -803,10 +803,14 @@ 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"`
LabelValues []string `json:"label_values" validate:"label_key|required"`
LabelKey string `json:"label_key" validate:"label_key|required"`
LabelValue string `json:"label_value" validate:"label_value|required"`
}
//GetSingleServiceInfoStruct GetSingleServiceInfoStruct

View File

@ -245,7 +245,11 @@ type TenantServiceLabelDao interface {
GetTenantServiceAffinityLabel(serviceID string) ([]*model.TenantServiceLable, error)
GetTenantServiceTypeLabel(serviceID string) (*model.TenantServiceLable, error)
DELTenantServiceLabelsByLabelvaluesAndServiceID(serviceID string, labelValues []string) error
DelTenantServiceLabelByLabelValueAndServiceID(serviceID string, labelValue string) error
DelTenantServiceLabelsByLabelValuesAndServiceID(serviceID string) error
DelTenantServiceLabels(serviceID string, labelKey string, labelValues []string) error
DelTenantServiceLabelsByKey(serviceID string, labelKey string) error
DelTenantServiceLabelsByKeyValueServiceID(serviceID string, labelKey string, labelValue string) error
}
//LocalSchedulerDao 本地调度信息

View File

@ -1272,6 +1272,25 @@ func (t *ServiceLabelDaoImpl) DELTenantServiceLabelsByLabelvaluesAndServiceID(se
return nil
}
// DelTenantServiceLabelByLabelValueAndServiceID -
func (t *ServiceLabelDaoImpl) DelTenantServiceLabelByLabelValueAndServiceID(serviceID string, labelValue string) error {
var label model.TenantServiceLable
if err := t.DB.Where("service_id=? and label_value=? and label_key=?", serviceID,
model.LabelKeyNodeSelector, labelValue).Delete(&label).Error; err != nil {
return err
}
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 +1300,22 @@ func (t *ServiceLabelDaoImpl) DelTenantServiceLabels(serviceID string, labelKey
}
return nil
}
//DelTenantServiceLabels deletes labels
func (t *ServiceLabelDaoImpl) DelTenantServiceLabelsByKeyValueServiceID(serviceID string, labelKey string, labelValue string) error {
var label model.TenantServiceLable
if err := t.DB.Where("service_id=? and label_key=? and label_value=?", serviceID, labelKey,
labelValue).Delete(&label).Error; err != nil {
return err
}
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
}