[ADD] send apply rules task when change outer port or inner port

This commit is contained in:
GLYASAI 2018-11-27 16:46:05 +08:00
parent 0cc7eca7e7
commit 9bb1a8ff74
8 changed files with 94 additions and 41 deletions

View File

@ -82,7 +82,7 @@ func (g *GatewayStruct) addHTTPRule(w http.ResponseWriter, r *http.Request) {
return
}
h.SendTask(req.HTTPRuleID, "http", g.MQClient)
h.SendTaskGW(req.HTTPRuleID, "http", g.MQClient)
httputil.ReturnSuccess(r, w, "success")
}
@ -131,7 +131,7 @@ func (g *GatewayStruct) updateHTTPRule(w http.ResponseWriter, r *http.Request) {
return
}
h.SendTask(req.HTTPRuleID, "http", g.MQClient)
h.SendTaskGW(req.HTTPRuleID, "http", g.MQClient)
httputil.ReturnSuccess(r, w, "success")
}
@ -153,7 +153,7 @@ func (g *GatewayStruct) deleteHTTPRule(w http.ResponseWriter, r *http.Request) {
return
}
h.SendTask(req.HTTPRuleID, "http", g.MQClient)
h.SendTaskGW(req.HTTPRuleID, "http", g.MQClient)
httputil.ReturnSuccess(r, w, "success")
}
@ -220,7 +220,7 @@ func (g *GatewayStruct) AddTCPRule(w http.ResponseWriter, r *http.Request) {
return
}
h.SendTask(req.TCPRuleID, "tcp", g.MQClient)
h.SendTaskGW(req.TCPRuleID, "tcp", g.MQClient)
httputil.ReturnSuccess(r, w, "success")
}
@ -269,7 +269,7 @@ func (g *GatewayStruct) updateTCPRule(w http.ResponseWriter, r *http.Request) {
return
}
h.SendTask(req.TCPRuleID, "tcp", g.MQClient)
h.SendTaskGW(req.TCPRuleID, "tcp", g.MQClient)
httputil.ReturnSuccess(r, w, "success")
}
@ -291,7 +291,7 @@ func (g *GatewayStruct) deleteTCPRule(w http.ResponseWriter, r *http.Request) {
return
}
h.SendTask(req.TCPRuleID, "tcp", g.MQClient)
h.SendTaskGW(req.TCPRuleID, "tcp", g.MQClient)
httputil.ReturnSuccess(r, w, "success")
}

View File

@ -66,8 +66,14 @@ func GetManager() V2Manager {
//NewManager new manager
func NewManager(conf option.Config, statusCli *client.AppRuntimeSyncClient) (*V2Routes, error) {
mqClient, err := mqclient.NewMqClient(conf.EtcdEndpoint, conf.MQAPI)
if err != nil {
return nil, err
}
var v2r V2Routes
v2r.TenantStruct.StatusCli = statusCli
v2r.TenantStruct.MQClient = mqClient
nodeProxy := proxy.CreateProxy("acp_node", "http", conf.NodeAPI)
discover.GetEndpointDiscover(conf.EtcdEndpoint).AddProject("acp_node", nodeProxy)
v2r.AcpNodeStruct.HTTPProxy = nodeProxy
@ -78,10 +84,6 @@ func NewManager(conf option.Config, statusCli *client.AppRuntimeSyncClient) (*V2
v2r.EntranceStruct.HTTPProxy = entranceProxy
logrus.Debugf("create entrance api proxy success")
mqClient, err := mqclient.NewMqClient(conf.EtcdEndpoint, conf.MQAPI)
if err != nil {
return nil, err
}
v2r.GatewayStruct.MQClient = mqClient
return &v2r, nil
}

View File

@ -29,6 +29,7 @@ import (
"github.com/goodrain/rainbond/api/middleware"
api_model "github.com/goodrain/rainbond/api/model"
dbmodel "github.com/goodrain/rainbond/db/model"
mqclient "github.com/goodrain/rainbond/mq/api/grpc/client"
"github.com/pquerna/ffjson/ffjson"
@ -100,6 +101,7 @@ func (v2 *V2Routes) Version(w http.ResponseWriter, r *http.Request) {
//TenantStruct tenant struct
type TenantStruct struct {
StatusCli *client.AppRuntimeSyncClient
MQClient *mqclient.MQClient
}
//AllTenantResources GetResources
@ -1524,6 +1526,9 @@ func (t *TenantStruct) PortOuterController(w http.ResponseWriter, r *http.Reques
rc["domain"] = mm[0]
rc["port"] = fmt.Sprintf("%v", vsPort.Port)
}
handler.GetServiceManager().SendTaskSA(serviceID, t.MQClient)
httputil.ReturnSuccess(r, w, rc)
}
@ -1573,6 +1578,9 @@ func (t *TenantStruct) PortInnerController(w http.ResponseWriter, r *http.Reques
return
}
}
handler.GetServiceManager().SendTaskSA(serviceID, t.MQClient)
httputil.ReturnSuccess(r, w, nil)
}

View File

@ -24,6 +24,7 @@ import (
"github.com/goodrain/rainbond/builder/exector"
dbmodel "github.com/goodrain/rainbond/db/model"
"github.com/goodrain/rainbond/worker/discover/model"
"github.com/goodrain/rainbond/mq/api/grpc/client"
)
//ServiceHandler service handler
@ -69,4 +70,5 @@ type ServiceHandler interface {
DeleteComplexEnvs(tenantID, serviceAlias, pluginID string) *util.APIHandleError
ServiceCheck(*api_model.ServiceCheckStruct) (string, string, *util.APIHandleError)
GetServiceCheckInfo(uuid string) (*exector.ServiceCheckResult, *util.APIHandleError)
SendTaskSA(serviceID string, mqClient *client.MQClient)
}

View File

@ -19,17 +19,14 @@
package handler
import (
"context"
"fmt"
"github.com/Sirupsen/logrus"
api_db "github.com/goodrain/rainbond/api/db"
apimodel "github.com/goodrain/rainbond/api/model"
"github.com/goodrain/rainbond/db"
"github.com/goodrain/rainbond/db/model"
"github.com/goodrain/rainbond/mq/api/grpc/client"
"github.com/goodrain/rainbond/util"
"github.com/jinzhu/gorm"
"github.com/pquerna/ffjson/ffjson"
"os"
"strconv"
)
@ -465,7 +462,8 @@ func (g *GatewayAction) PortExists(port int) bool {
return g.dbmanager.TenantServiceLBMappingPortDao().PortExists(port)
}
func (g *GatewayAction) SendTask(ruleID string, ruleType string, mqClient *client.MQClient) {
// SendTaskGW sends apply rules task
func (g *GatewayAction) SendTaskGW(ruleID string, ruleType string, mqClient *client.MQClient) {
logrus.Info("sending apply_rule task...")
// get serviceID
var serviceID string
@ -488,33 +486,8 @@ func (g *GatewayAction) SendTask(ruleID string, ruleType string, mqClient *clien
body := make(map[string]interface{})
body["service_id"] = serviceID
body["deploy_version"] = service.DeployVersion
err = g.sendTask(body, "apply_rule", mqClient)
err = sendTask(body, "apply_rule", mqClient)
if err != nil {
logrus.Errorf("Unexpected error occurred while sending task: %v", err)
}
}
func (g *GatewayAction) sendTask(body map[string]interface{}, taskType string, mqClient *client.MQClient) error {
bodyJ, err := ffjson.Marshal(body)
if err != nil {
return err
}
bs := &api_db.BuildTaskStruct{
TaskType: taskType,
TaskBody: bodyJ,
User: "define",
}
eq, errEq := api_db.BuildTaskBuild(bs)
if errEq != nil {
logrus.Errorf("build equeue stop request error, %v", errEq)
return errEq
}
ctx, cancel := context.WithCancel(context.Background())
_, err = mqClient.Enqueue(ctx, eq)
cancel()
if err != nil {
logrus.Errorf("equque mq error, %v", err)
return err
}
return nil
}

View File

@ -41,5 +41,5 @@ type GatewayHandler interface {
GetAvailablePort() (int, error)
PortExists(port int) bool
SendTask(ruleID string, ruleType string, mqClient *client.MQClient)
SendTaskGW(ruleID string, ruleType string, mqClient *client.MQClient)
}

52
api/handler/send_task.go Normal file
View File

@ -0,0 +1,52 @@
// 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 (
"context"
"github.com/Sirupsen/logrus"
api_db "github.com/goodrain/rainbond/api/db"
"github.com/goodrain/rainbond/mq/api/grpc/client"
"github.com/pquerna/ffjson/ffjson"
)
func sendTask(body map[string]interface{}, taskType string, mqClient *client.MQClient) error {
bodyJ, err := ffjson.Marshal(body)
if err != nil {
return err
}
bs := &api_db.BuildTaskStruct{
TaskType: taskType,
TaskBody: bodyJ,
User: "define",
}
eq, errEq := api_db.BuildTaskBuild(bs)
if errEq != nil {
logrus.Errorf("build equeue stop request error, %v", errEq)
return errEq
}
ctx, cancel := context.WithCancel(context.Background())
_, err = mqClient.Enqueue(ctx, eq)
cancel()
if err != nil {
logrus.Errorf("equque mq error, %v", err)
return err
}
return nil
}

View File

@ -36,6 +36,7 @@ import (
"github.com/pquerna/ffjson/ffjson"
gclient "github.com/goodrain/rainbond/mq/api/grpc/client"
api_db "github.com/goodrain/rainbond/api/db"
api_model "github.com/goodrain/rainbond/api/model"
"github.com/goodrain/rainbond/api/util"
@ -1657,3 +1658,18 @@ func chekeServiceLabel(v string) string {
}
return v
}
// SendTaskSA sends apply rules task
func (s *ServiceAction) SendTaskSA(serviceID string, mqClient *gclient.MQClient) {
service, err := db.GetManager().TenantServiceDao().GetServiceByID(serviceID)
if err != nil {
logrus.Errorf("Unexpected error occurred while getting Service by ServiceID(%s): %v", serviceID, err)
}
body := make(map[string]interface{})
body["service_id"] = serviceID
body["deploy_version"] = service.DeployVersion
err = sendTask(body, "apply_rule", mqClient)
if err != nil {
logrus.Errorf("Unexpected error occurred while sending task: %v", err)
}
}