Rainbond/api/handler/gateway_action.go

984 lines
28 KiB
Go
Raw Normal View History

2018-11-21 15:04:03 +08:00
// RAINBOND, Application Management Platform
// Copyright (C) 2014-2017 Goodrain Co., Ltd.
// 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 (
2019-09-18 17:56:52 +08:00
"context"
2018-11-21 17:15:40 +08:00
"fmt"
2018-12-04 13:43:15 +08:00
"os"
2019-09-18 17:56:52 +08:00
"sort"
2018-12-04 13:43:15 +08:00
"strconv"
"strings"
2019-09-18 17:56:52 +08:00
"time"
2018-12-04 13:43:15 +08:00
2019-09-18 17:56:52 +08:00
"github.com/coreos/etcd/clientv3"
2018-11-21 15:04:03 +08:00
apimodel "github.com/goodrain/rainbond/api/model"
2021-05-20 00:01:37 +08:00
"github.com/goodrain/rainbond/api/util/bcode"
2018-11-21 15:04:03 +08:00
"github.com/goodrain/rainbond/db"
"github.com/goodrain/rainbond/db/model"
2018-12-04 13:43:15 +08:00
"github.com/goodrain/rainbond/mq/client"
2018-11-21 15:04:03 +08:00
"github.com/goodrain/rainbond/util"
"github.com/jinzhu/gorm"
2021-04-23 14:30:46 +08:00
"github.com/pkg/errors"
2020-11-25 16:39:38 +08:00
"github.com/sirupsen/logrus"
2018-11-21 15:04:03 +08:00
)
2018-11-26 02:19:08 +08:00
// GatewayAction -
2018-11-21 15:04:03 +08:00
type GatewayAction struct {
dbmanager db.Manager
2018-12-04 13:43:15 +08:00
mqclient client.MQClient
2019-09-18 17:56:52 +08:00
etcdCli *clientv3.Client
2018-11-21 15:04:03 +08:00
}
2018-11-26 02:19:08 +08:00
//CreateGatewayManager creates gateway manager.
2019-09-18 17:56:52 +08:00
func CreateGatewayManager(dbmanager db.Manager, mqclient client.MQClient, etcdCli *clientv3.Client) *GatewayAction {
2018-11-21 15:04:03 +08:00
return &GatewayAction{
dbmanager: dbmanager,
2018-12-04 13:43:15 +08:00
mqclient: mqclient,
2019-09-18 17:56:52 +08:00
etcdCli: etcdCli,
2018-11-21 15:04:03 +08:00
}
}
2018-11-26 02:19:08 +08:00
// AddHTTPRule adds http rule to db if it doesn't exists.
func (g *GatewayAction) AddHTTPRule(req *apimodel.AddHTTPRuleStruct) error {
2021-06-23 17:23:46 +08:00
return db.GetManager().DB().Transaction(func(tx *gorm.DB) error {
if err := g.CreateHTTPRule(tx, req); err != nil {
return err
}
2021-06-23 17:23:46 +08:00
// Effective immediately
err := g.SendTaskDeprecated(map[string]interface{}{
"service_id": req.ServiceID,
"action": "add-http-rule",
"limit": map[string]string{"domain": req.Domain},
})
if err != nil {
return fmt.Errorf("send http rule task: %v", err)
}
return nil
})
}
2021-05-25 22:50:26 +08:00
// CreateHTTPRule Create http rules through transactions
func (g *GatewayAction) CreateHTTPRule(tx *gorm.DB, req *apimodel.AddHTTPRuleStruct) error {
2018-11-23 20:39:34 +08:00
httpRule := &model.HTTPRule{
UUID: req.HTTPRuleID,
2018-11-22 10:38:55 +08:00
ServiceID: req.ServiceID,
ContainerPort: req.ContainerPort,
Domain: req.Domain,
2019-03-27 21:20:06 +08:00
Path: func() string {
if !strings.HasPrefix(req.Path, "/") {
return "/" + req.Path
}
return req.Path
}(),
2018-11-22 10:38:55 +08:00
Header: req.Header,
Cookie: req.Cookie,
2018-12-04 17:11:15 +08:00
Weight: req.Weight,
2018-11-22 10:38:55 +08:00
IP: req.IP,
CertificateID: req.CertificateID,
2018-11-22 09:34:00 +08:00
}
if err := db.GetManager().HTTPRuleDaoTransactions(tx).AddModel(httpRule); err != nil {
2020-05-27 17:04:20 +08:00
return fmt.Errorf("create http rule: %v", err)
2018-11-22 09:34:00 +08:00
}
if strings.Replace(req.CertificateID, " ", "", -1) != "" {
2018-11-29 11:46:46 +08:00
cert := &model.Certificate{
UUID: req.CertificateID,
CertificateName: fmt.Sprintf("cert-%s", util.NewUUID()[0:8]),
Certificate: req.Certificate,
PrivateKey: req.PrivateKey,
}
2018-11-29 18:12:26 +08:00
if err := db.GetManager().CertificateDaoTransactions(tx).AddOrUpdate(cert); err != nil {
2020-05-27 17:04:20 +08:00
return fmt.Errorf("create or update http rule: %v", err)
2018-11-29 11:46:46 +08:00
}
2018-11-22 09:34:00 +08:00
}
for _, ruleExtension := range req.RuleExtensions {
re := &model.RuleExtension{
2018-11-22 10:38:55 +08:00
UUID: util.NewUUID(),
2018-11-22 09:34:00 +08:00
RuleID: httpRule.UUID,
2018-11-22 10:38:55 +08:00
Key: ruleExtension.Key,
Value: ruleExtension.Value,
2018-11-22 09:34:00 +08:00
}
if err := db.GetManager().RuleExtensionDaoTransactions(tx).AddModel(re); err != nil {
2020-05-27 17:04:20 +08:00
return fmt.Errorf("create rule extensions: %v", err)
2018-11-22 09:34:00 +08:00
}
}
return nil
2018-11-21 15:04:03 +08:00
}
2018-11-26 02:19:08 +08:00
// UpdateHTTPRule updates http rule
func (g *GatewayAction) UpdateHTTPRule(req *apimodel.UpdateHTTPRuleStruct) error {
2018-11-22 10:38:55 +08:00
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()
}
}()
rule, err := g.dbmanager.HTTPRuleDaoTransactions(tx).GetHTTPRuleByID(req.HTTPRuleID)
2018-11-21 17:15:40 +08:00
if err != nil {
2018-11-22 09:34:00 +08:00
tx.Rollback()
return err
2018-11-21 17:15:40 +08:00
}
2019-11-12 20:23:46 +08:00
if rule == nil || rule.UUID == "" { // rule won't be nil
2018-11-22 09:34:00 +08:00
tx.Rollback()
return fmt.Errorf("HTTPRule dosen't exist based on uuid(%s)", req.HTTPRuleID)
2018-11-21 17:15:40 +08:00
}
if strings.Replace(req.CertificateID, " ", "", -1) != "" {
// add new certificate
cert := &model.Certificate{
2020-05-21 20:39:10 +08:00
UUID: req.CertificateID,
Certificate: req.Certificate,
PrivateKey: req.PrivateKey,
}
2018-11-29 18:12:26 +08:00
if err := g.dbmanager.CertificateDaoTransactions(tx).AddOrUpdate(cert); err != nil {
tx.Rollback()
return err
}
rule.CertificateID = req.CertificateID
2019-11-12 20:23:46 +08:00
} else {
rule.CertificateID = ""
}
if len(req.RuleExtensions) > 0 {
// delete old RuleExtensions
if err := g.dbmanager.RuleExtensionDaoTransactions(tx).DeleteRuleExtensionByRuleID(rule.UUID); err != nil {
2018-11-22 09:34:00 +08:00
tx.Rollback()
return err
2018-11-22 09:34:00 +08:00
}
// add new rule extensions
for _, ruleExtension := range req.RuleExtensions {
re := &model.RuleExtension{
UUID: util.NewUUID(),
RuleID: rule.UUID,
Key: ruleExtension.Key,
Value: ruleExtension.Value,
}
if err := db.GetManager().RuleExtensionDaoTransactions(tx).AddModel(re); err != nil {
tx.Rollback()
return err
}
}
2018-11-22 09:34:00 +08:00
}
// update http rule
if req.ServiceID != "" {
rule.ServiceID = req.ServiceID
}
if req.ContainerPort != 0 {
rule.ContainerPort = req.ContainerPort
}
if req.Domain != "" {
rule.Domain = req.Domain
}
2019-03-27 21:20:06 +08:00
rule.Path = func() string {
if !strings.HasPrefix(req.Path, "/") {
return "/" + req.Path
}
return req.Path
}()
2019-03-13 20:06:11 +08:00
rule.Header = req.Header
rule.Cookie = req.Cookie
rule.Weight = req.Weight
if req.IP != "" {
rule.IP = req.IP
}
if err := db.GetManager().HTTPRuleDaoTransactions(tx).UpdateModel(rule); err != nil {
2018-11-22 09:34:00 +08:00
tx.Rollback()
return err
2018-11-22 09:34:00 +08:00
}
// end transaction
if err := tx.Commit().Error; err != nil {
tx.Rollback()
return err
2018-11-22 09:34:00 +08:00
}
2021-04-23 14:30:46 +08:00
if err := g.SendTaskDeprecated(map[string]interface{}{
"service_id": rule.ServiceID,
"action": "update-http-rule",
"limit": map[string]string{"domain": req.Domain},
}); err != nil {
logrus.Errorf("send runtime message about gateway failure %s", err.Error())
}
return nil
2018-11-21 17:15:40 +08:00
}
2018-11-26 02:19:08 +08:00
// DeleteHTTPRule deletes http rule, including certificate and rule extensions
func (g *GatewayAction) DeleteHTTPRule(req *apimodel.DeleteHTTPRuleStruct) error {
2018-11-21 17:57:48 +08:00
// begin transaction
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-11-21 17:57:48 +08:00
// delete http rule
httpRule, err := g.dbmanager.HTTPRuleDaoTransactions(tx).GetHTTPRuleByID(req.HTTPRuleID)
2018-11-21 17:57:48 +08:00
if err != nil {
tx.Rollback()
return err
2018-11-21 17:57:48 +08:00
}
2020-03-10 23:46:20 +08:00
svcID := httpRule.ServiceID
if err := g.dbmanager.HTTPRuleDaoTransactions(tx).DeleteHTTPRuleByID(httpRule.UUID); err != nil {
2018-11-22 09:34:00 +08:00
tx.Rollback()
return err
2018-11-22 09:34:00 +08:00
}
2020-03-10 23:46:20 +08:00
2018-11-21 17:57:48 +08:00
// delete rule extension
if err := g.dbmanager.RuleExtensionDaoTransactions(tx).DeleteRuleExtensionByRuleID(httpRule.UUID); err != nil {
tx.Rollback()
return err
2018-11-21 17:57:48 +08:00
}
// end transaction
if err := tx.Commit().Error; err != nil {
return err
2018-11-21 17:57:48 +08:00
}
2021-04-23 14:30:46 +08:00
if err := g.SendTaskDeprecated(map[string]interface{}{
"service_id": svcID,
"action": "delete-http-rule",
"limit": map[string]string{"domain": httpRule.Domain},
}); err != nil {
logrus.Errorf("send runtime message about gateway failure %s", err.Error())
}
return nil
2018-11-21 17:57:48 +08:00
}
// DeleteHTTPRuleByServiceIDWithTransaction deletes http rule, including certificate and rule extensions
func (g *GatewayAction) DeleteHTTPRuleByServiceIDWithTransaction(sid string, tx *gorm.DB) error {
// delete http rule
rules, err := g.dbmanager.HTTPRuleDaoTransactions(tx).ListByServiceID(sid)
if err != nil {
return err
}
for _, rule := range rules {
if err := g.dbmanager.RuleExtensionDaoTransactions(tx).DeleteRuleExtensionByRuleID(rule.UUID); err != nil {
return err
}
if err := g.dbmanager.GwRuleConfigDaoTransactions(tx).DeleteByRuleID(rule.UUID); err != nil {
return err
}
if err := g.dbmanager.HTTPRuleDaoTransactions(tx).DeleteHTTPRuleByID(rule.UUID); err != nil {
return err
}
}
return nil
}
2018-11-21 17:15:40 +08:00
// AddCertificate adds certificate to db if it doesn't exists
func (g *GatewayAction) AddCertificate(req *apimodel.AddHTTPRuleStruct, tx *gorm.DB) error {
2018-11-21 17:15:40 +08:00
cert := &model.Certificate{
2018-11-21 17:57:48 +08:00
UUID: req.CertificateID,
CertificateName: fmt.Sprintf("cert-%s", util.NewUUID()[0:8]),
2018-11-21 17:57:48 +08:00
Certificate: req.Certificate,
PrivateKey: req.PrivateKey,
2018-11-21 17:15:40 +08:00
}
return g.dbmanager.CertificateDaoTransactions(tx).AddModel(cert)
}
2018-11-26 02:19:08 +08:00
// UpdateCertificate updates certificate for http rule
func (g *GatewayAction) UpdateCertificate(req apimodel.AddHTTPRuleStruct, httpRule *model.HTTPRule,
2018-11-21 19:51:40 +08:00
tx *gorm.DB) error {
2018-11-21 17:15:40 +08:00
// delete old certificate
cert, err := g.dbmanager.CertificateDaoTransactions(tx).GetCertificateByID(req.CertificateID)
if err != nil {
return err
}
if cert == nil {
2021-06-23 17:23:46 +08:00
return fmt.Errorf("certificate doesn't exist based on certificateID(%s)", req.CertificateID)
2018-11-21 17:15:40 +08:00
}
cert.CertificateName = fmt.Sprintf("cert-%s", util.NewUUID()[0:8])
2018-11-21 17:15:40 +08:00
cert.Certificate = req.Certificate
cert.PrivateKey = req.PrivateKey
return g.dbmanager.CertificateDaoTransactions(tx).UpdateModel(cert)
2018-11-21 15:04:03 +08:00
}
2018-11-26 02:19:08 +08:00
// AddTCPRule adds tcp rule.
func (g *GatewayAction) AddTCPRule(req *apimodel.AddTCPRuleStruct) error {
2021-06-23 17:23:46 +08:00
return g.dbmanager.DB().Transaction(func(tx *gorm.DB) error {
if err := g.CreateTCPRule(tx, req); err != nil {
return err
}
err := g.SendTaskDeprecated(map[string]interface{}{
"service_id": req.ServiceID,
"action": "add-tcp-rule",
"limit": map[string]string{"tcp-address": fmt.Sprintf("%s:%d", req.IP, req.Port)},
})
if err != nil {
return fmt.Errorf("send tcp rule task: %v", err)
}
return nil
})
}
2021-05-25 22:50:26 +08:00
// CreateTCPRule Create tcp rules through transactions
func (g *GatewayAction) CreateTCPRule(tx *gorm.DB, req *apimodel.AddTCPRuleStruct) error {
// add tcp rule
2018-11-23 20:39:34 +08:00
tcpRule := &model.TCPRule{
UUID: req.TCPRuleID,
2018-11-22 10:38:55 +08:00
ServiceID: req.ServiceID,
ContainerPort: req.ContainerPort,
IP: req.IP,
Port: req.Port,
2018-11-21 19:51:40 +08:00
}
if err := g.dbmanager.TCPRuleDaoTransactions(tx).AddModel(tcpRule); err != nil {
return err
2018-11-21 19:51:40 +08:00
}
// add rule extensions
for _, ruleExtension := range req.RuleExtensions {
re := &model.RuleExtension{
2018-11-21 20:14:13 +08:00
UUID: util.NewUUID(),
RuleID: tcpRule.UUID,
Value: ruleExtension.Value,
}
if err := g.dbmanager.RuleExtensionDaoTransactions(tx).AddModel(re); err != nil {
return err
2018-11-21 20:14:13 +08:00
}
}
return nil
2018-11-21 20:14:13 +08:00
}
2018-11-26 02:19:08 +08:00
// UpdateTCPRule updates a tcp rule
func (g *GatewayAction) UpdateTCPRule(req *apimodel.UpdateTCPRuleStruct, minPort int) error {
2018-11-21 20:14:13 +08:00
// begin transaction
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-11-21 20:14:13 +08:00
// get old tcp rule
tcpRule, err := g.dbmanager.TCPRuleDaoTransactions(tx).GetTCPRuleByID(req.TCPRuleID)
2018-11-21 20:14:13 +08:00
if err != nil {
tx.Rollback()
return err
2018-11-21 20:14:13 +08:00
}
if len(req.RuleExtensions) > 0 {
// delete old rule extensions
if err := g.dbmanager.RuleExtensionDaoTransactions(tx).DeleteRuleExtensionByRuleID(tcpRule.UUID); err != nil {
logrus.Debugf("TCP rule id: %s;error delete rule extension: %v", tcpRule.UUID, err)
tx.Rollback()
return err
}
// add new rule extensions
for _, ruleExtension := range req.RuleExtensions {
re := &model.RuleExtension{
UUID: util.NewUUID(),
RuleID: tcpRule.UUID,
Value: ruleExtension.Value,
}
if err := g.dbmanager.RuleExtensionDaoTransactions(tx).AddModel(re); err != nil {
tx.Rollback()
logrus.Debugf("TCP rule id: %s;error add rule extension: %v", tcpRule.UUID, err)
return err
}
}
2018-11-21 20:14:13 +08:00
}
// update tcp rule
if req.ContainerPort != 0 {
tcpRule.ContainerPort = req.ContainerPort
}
2018-12-19 13:53:11 +08:00
if req.IP != "" {
tcpRule.IP = req.IP
2018-12-18 20:55:12 +08:00
}
2019-09-18 17:56:52 +08:00
tcpRule.Port = req.Port
if req.ServiceID != "" {
tcpRule.ServiceID = req.ServiceID
}
if err := g.dbmanager.TCPRuleDaoTransactions(tx).UpdateModel(tcpRule); err != nil {
logrus.Debugf("TCP rule id: %s;error updating tcp rule: %v", tcpRule.UUID, err)
tx.Rollback()
return err
2018-11-21 19:51:40 +08:00
}
// end transaction
if err := tx.Commit().Error; err != nil {
2018-11-21 20:14:13 +08:00
tx.Rollback()
logrus.Debugf("TCP rule id: %s;error end transaction %v", tcpRule.UUID, err)
return err
}
2021-04-23 14:30:46 +08:00
if err := g.SendTaskDeprecated(map[string]interface{}{
"service_id": tcpRule.ServiceID,
"action": "update-tcp-rule",
"limit": map[string]string{"tcp-address": fmt.Sprintf("%s:%d", tcpRule.IP, tcpRule.Port)},
}); err != nil {
logrus.Errorf("send runtime message about gateway failure %s", err.Error())
2018-11-21 19:51:40 +08:00
}
return nil
2018-11-21 19:51:40 +08:00
}
2018-11-26 02:19:08 +08:00
// DeleteTCPRule deletes a tcp rule
func (g *GatewayAction) DeleteTCPRule(req *apimodel.DeleteTCPRuleStruct) error {
2018-11-21 20:33:37 +08:00
// begin transaction
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()
}
}()
tcpRule, err := db.GetManager().TCPRuleDaoTransactions(tx).GetTCPRuleByID(req.TCPRuleID)
2018-11-21 20:33:37 +08:00
if err != nil {
tx.Rollback()
return err
2018-11-21 20:33:37 +08:00
}
// delete rule extensions
if err := db.GetManager().RuleExtensionDaoTransactions(tx).DeleteRuleExtensionByRuleID(tcpRule.UUID); err != nil {
tx.Rollback()
return err
2018-11-21 20:33:37 +08:00
}
// delete tcp rule
if err := db.GetManager().TCPRuleDaoTransactions(tx).DeleteByID(tcpRule.UUID); err != nil {
2018-11-21 20:33:37 +08:00
tx.Rollback()
return err
2018-11-21 20:33:37 +08:00
}
// delete LBMappingPort
err = db.GetManager().TenantServiceLBMappingPortDaoTransactions(tx).DELServiceLBMappingPortByServiceIDAndPort(
tcpRule.ServiceID, tcpRule.Port)
if err != nil {
tx.Rollback()
return err
}
2018-11-21 20:33:37 +08:00
// end transaction
if err := tx.Commit().Error; err != nil {
tx.Rollback()
return err
}
2021-04-23 14:30:46 +08:00
if err := g.SendTaskDeprecated(map[string]interface{}{
"service_id": tcpRule.ServiceID,
"action": "delete-tcp-rule",
"limit": map[string]string{"tcp-address": fmt.Sprintf("%s:%d", tcpRule.IP, tcpRule.Port)},
}); err != nil {
logrus.Errorf("send runtime message about gateway failure %s", err.Error())
2018-11-21 20:33:37 +08:00
}
return nil
2018-11-21 20:33:37 +08:00
}
// DeleteTCPRuleByServiceIDWithTransaction deletes a tcp rule
func (g *GatewayAction) DeleteTCPRuleByServiceIDWithTransaction(sid string, tx *gorm.DB) error {
rules, err := db.GetManager().TCPRuleDaoTransactions(tx).GetTCPRuleByServiceID(sid)
if err != nil {
return err
}
for _, rule := range rules {
// delete rule extensions
if err := db.GetManager().RuleExtensionDaoTransactions(tx).DeleteRuleExtensionByRuleID(rule.UUID); err != nil {
return err
}
// delete tcp rule
if err := db.GetManager().TCPRuleDaoTransactions(tx).DeleteByID(rule.UUID); err != nil {
return err
}
}
return nil
}
2018-11-21 17:15:40 +08:00
// AddRuleExtensions adds rule extensions to db if any of they doesn't exists
2018-11-21 19:51:40 +08:00
func (g *GatewayAction) AddRuleExtensions(ruleID string, ruleExtensions []*apimodel.RuleExtensionStruct,
tx *gorm.DB) error {
2018-11-21 15:04:03 +08:00
for _, ruleExtension := range ruleExtensions {
re := &model.RuleExtension{
UUID: util.NewUUID(),
RuleID: ruleID,
Value: ruleExtension.Value,
}
err := g.dbmanager.RuleExtensionDaoTransactions(tx).AddModel(re)
if err != nil {
return err
}
}
return nil
}
2018-11-26 02:19:08 +08:00
// GetAvailablePort returns a available port
2021-05-26 18:19:15 +08:00
func (g *GatewayAction) GetAvailablePort(ip string, lock bool) (int, error) {
2019-09-18 17:56:52 +08:00
roles, err := g.dbmanager.TCPRuleDao().GetUsedPortsByIP(ip)
2018-11-26 02:19:08 +08:00
if err != nil {
return 0, err
}
var ports []int
2019-09-18 17:56:52 +08:00
for _, p := range roles {
2018-11-26 02:19:08 +08:00
ports = append(ports, p.Port)
}
2021-07-03 17:18:45 +08:00
resp, err := clientv3.KV(g.etcdCli).Get(context.TODO(), "/rainbond/gateway/lockports", clientv3.WithPrefix())
if err != nil {
logrus.Info("get lock ports failed")
}
for _, etcdValue := range resp.Kvs {
port, err := strconv.Atoi(string(etcdValue.Value))
if err != nil {
continue
2021-05-26 18:14:08 +08:00
}
2021-07-03 17:18:45 +08:00
ports = append(ports, port)
2021-05-26 18:14:08 +08:00
}
2019-09-18 17:56:52 +08:00
port := selectAvailablePort(ports)
if port != 0 {
2021-05-26 18:19:15 +08:00
if lock {
2021-07-03 17:18:45 +08:00
lease := clientv3.NewLease(g.etcdCli)
leaseResp, err := lease.Grant(context.Background(), 120)
if err != nil {
logrus.Info("set lease failed")
return port, nil
}
lockPortKey := fmt.Sprintf("/rainbond/gateway/lockports/%d", port)
_, err = g.etcdCli.Put(context.Background(), lockPortKey, fmt.Sprintf("%d", port), clientv3.WithLease(leaseResp.ID))
if err != nil {
logrus.Infof("set lock port key %s failed", lockPortKey)
return port, nil
}
2021-05-26 21:40:42 +08:00
logrus.Infof("select gateway port %d, lock it 2 min", port)
2021-05-26 18:19:15 +08:00
}
2019-09-18 17:56:52 +08:00
return port, nil
}
return 0, fmt.Errorf("no more lb port can be use with ip %s", ip)
}
func selectAvailablePort(used []int) int {
maxPort, _ := strconv.Atoi(os.Getenv("MAX_LB_PORT"))
minPort, _ := strconv.Atoi(os.Getenv("MIN_LB_PORT"))
2018-11-26 02:19:08 +08:00
if minPort == 0 {
2019-09-18 17:56:52 +08:00
minPort = 10000
2018-11-26 02:19:08 +08:00
}
if maxPort == 0 {
2019-09-18 17:56:52 +08:00
maxPort = 65535
2018-11-26 02:19:08 +08:00
}
2019-10-21 16:10:36 +08:00
if len(used) == 0 {
return minPort
}
sort.Ints(used)
selectPort := used[len(used)-1] + 1
if selectPort < minPort {
selectPort = minPort
2018-11-26 02:19:08 +08:00
}
//顺序分配端口
if selectPort <= maxPort {
2019-09-18 17:56:52 +08:00
return selectPort
2018-11-26 02:19:08 +08:00
}
//捡漏以前端口
selectPort = minPort
2019-09-18 17:56:52 +08:00
for _, p := range used {
2018-11-26 02:19:08 +08:00
if p == selectPort {
selectPort = selectPort + 1
continue
}
if p > selectPort {
2019-09-18 17:56:52 +08:00
return selectPort
2018-11-26 02:19:08 +08:00
}
selectPort = selectPort + 1
}
if selectPort <= maxPort {
2019-09-18 17:56:52 +08:00
return selectPort
2018-11-26 02:19:08 +08:00
}
2019-09-18 17:56:52 +08:00
return 0
2018-11-26 02:19:08 +08:00
}
2019-09-18 17:56:52 +08:00
// TCPIPPortExists returns if the port exists
func (g *GatewayAction) TCPIPPortExists(host string, port int) bool {
roles, _ := db.GetManager().TCPRuleDao().GetUsedPortsByIP(host)
for _, role := range roles {
if role.Port == port {
return true
}
}
return false
}
2018-11-26 18:08:16 +08:00
2021-05-12 11:39:38 +08:00
// SendTaskDeprecated sends apply rules task
2021-04-23 14:30:46 +08:00
func (g *GatewayAction) SendTaskDeprecated(in map[string]interface{}) error {
2019-03-12 09:59:09 +08:00
sid := in["service_id"].(string)
service, err := db.GetManager().TenantServiceDao().GetServiceByID(sid)
2018-11-26 18:08:16 +08:00
if err != nil {
2021-06-23 17:23:46 +08:00
return fmt.Errorf("unexpected error occurred while getting Service by ServiceID(%s): %v", sid, err)
2018-11-26 18:08:16 +08:00
}
body := make(map[string]interface{})
body["deploy_version"] = service.DeployVersion
2019-03-12 09:59:09 +08:00
for k, v := range in {
body[k] = v
}
2018-12-04 13:43:15 +08:00
err = g.mqclient.SendBuilderTopic(client.TaskStruct{
2018-12-04 18:08:51 +08:00
Topic: client.WorkerTopic,
2018-12-04 13:43:15 +08:00
TaskType: "apply_rule",
TaskBody: body,
})
2018-11-26 18:08:16 +08:00
if err != nil {
2021-06-23 17:23:46 +08:00
return fmt.Errorf("unexpected error occurred while sending task: %v", err)
2018-11-26 18:08:16 +08:00
}
2018-12-04 13:43:15 +08:00
return nil
2018-11-26 18:08:16 +08:00
}
2021-04-23 14:30:46 +08:00
// SendTask sends apply rules task
func (g *GatewayAction) SendTask(task *ComponentIngressTask) error {
err := g.mqclient.SendBuilderTopic(client.TaskStruct{
Topic: client.WorkerTopic,
TaskType: "apply_rule",
TaskBody: task,
})
if err != nil {
return errors.WithMessage(err, "send gateway task")
2018-11-26 18:08:16 +08:00
}
2018-12-04 13:43:15 +08:00
return nil
2018-11-26 18:08:16 +08:00
}
// RuleConfig -
func (g *GatewayAction) RuleConfig(req *apimodel.RuleConfigReq) error {
var configs []*model.GwRuleConfig
// TODO: use reflect to read the field of req, huangrh
configs = append(configs, &model.GwRuleConfig{
RuleID: req.RuleID,
2019-03-13 20:06:11 +08:00
Key: "proxy-connect-timeout",
Value: strconv.Itoa(req.Body.ProxyConnectTimeout),
})
configs = append(configs, &model.GwRuleConfig{
RuleID: req.RuleID,
2019-03-13 20:06:11 +08:00
Key: "proxy-send-timeout",
Value: strconv.Itoa(req.Body.ProxySendTimeout),
})
configs = append(configs, &model.GwRuleConfig{
RuleID: req.RuleID,
2019-03-13 20:06:11 +08:00
Key: "proxy-read-timeout",
Value: strconv.Itoa(req.Body.ProxyReadTimeout),
})
configs = append(configs, &model.GwRuleConfig{
RuleID: req.RuleID,
2019-03-13 20:06:11 +08:00
Key: "proxy-body-size",
Value: strconv.Itoa(req.Body.ProxyBodySize),
})
configs = append(configs, &model.GwRuleConfig{
RuleID: req.RuleID,
Key: "proxy-buffer-size",
Value: strconv.Itoa(req.Body.ProxyBufferSize) + "k",
})
configs = append(configs, &model.GwRuleConfig{
RuleID: req.RuleID,
Key: "proxy-buffer-numbers",
Value: strconv.Itoa(req.Body.ProxyBufferNumbers),
})
2021-05-27 18:16:13 +08:00
configs = append(configs, &model.GwRuleConfig{
RuleID: req.RuleID,
Key: "proxy-buffering",
Value: req.Body.ProxyBuffering,
})
setheaders := make(map[string]string)
for _, item := range req.Body.SetHeaders {
2019-06-20 13:59:19 +08:00
if strings.TrimSpace(item.Key) == "" {
2019-03-27 12:17:32 +08:00
continue
}
if strings.TrimSpace(item.Value) == "" {
item.Value = "empty"
}
// filter same key
setheaders["set-header-"+item.Key] = item.Value
}
for k, v := range setheaders {
configs = append(configs, &model.GwRuleConfig{
RuleID: req.RuleID,
Key: k,
Value: v,
})
2019-03-11 01:28:37 +08:00
}
rule, err := g.dbmanager.HTTPRuleDao().GetHTTPRuleByID(req.RuleID)
if err != nil {
return err
}
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()
}
}()
if err := g.dbmanager.GwRuleConfigDaoTransactions(tx).DeleteByRuleID(req.RuleID); err != nil {
tx.Rollback()
2019-03-11 01:28:37 +08:00
return err
}
for _, cfg := range configs {
if err := g.dbmanager.GwRuleConfigDaoTransactions(tx).AddModel(cfg); err != nil {
tx.Rollback()
return err
}
2019-03-11 01:28:37 +08:00
}
if err := tx.Commit().Error; err != nil {
tx.Rollback()
return err
}
2021-04-23 14:30:46 +08:00
if err := g.SendTaskDeprecated(map[string]interface{}{
"service_id": req.ServiceID,
"action": "update-rule-config",
"event_id": req.EventID,
"limit": map[string]string{"domain": rule.Domain},
}); err != nil {
logrus.Errorf("send runtime message about gateway failure %s", err.Error())
}
2019-03-11 01:28:37 +08:00
return nil
2019-03-13 20:06:11 +08:00
}
2019-09-18 17:56:52 +08:00
// UpdCertificate -
func (g *GatewayAction) UpdCertificate(req *apimodel.UpdCertificateReq) error {
cert, err := db.GetManager().CertificateDao().GetCertificateByID(req.CertificateID)
if err != nil {
msg := "retrieve certificate: %v"
return fmt.Errorf(msg, err)
}
2020-01-02 13:55:32 +08:00
if cert == nil {
// cert do not exists in region db, create it
cert = &model.Certificate{
UUID: req.CertificateID,
CertificateName: req.CertificateName,
Certificate: req.Certificate,
PrivateKey: req.PrivateKey,
}
if err := db.GetManager().CertificateDao().AddModel(cert); err != nil {
msg := "update cert error :%s"
return fmt.Errorf(msg, err.Error())
}
return nil
}
cert.CertificateName = req.CertificateName
cert.Certificate = req.Certificate
cert.PrivateKey = req.PrivateKey
if err := db.GetManager().CertificateDao().UpdateModel(cert); err != nil {
msg := "update certificate: %v"
return fmt.Errorf(msg, err)
}
// list related http rules
rules, err := g.ListHTTPRulesByCertID(req.CertificateID)
if err != nil {
msg := "certificate id: %s; list http rules: %v"
return fmt.Errorf(msg, req.CertificateID, err)
}
for _, rule := range rules {
eventID := util.NewUUID()
2021-04-23 14:30:46 +08:00
if err := g.SendTaskDeprecated(map[string]interface{}{
"service_id": rule.ServiceID,
"action": "update-rule-config",
"event_id": eventID,
"limit": map[string]string{"domain": rule.Domain},
}); err != nil {
logrus.Warningf("send runtime message about gateway failure %v", err)
}
}
return nil
}
// ListHTTPRulesByCertID -
func (g *GatewayAction) ListHTTPRulesByCertID(certID string) ([]*model.HTTPRule, error) {
return db.GetManager().HTTPRuleDao().ListByCertID(certID)
}
2019-09-18 17:56:52 +08:00
//IPAndAvailablePort ip and advice available port
type IPAndAvailablePort struct {
IP string `json:"ip"`
AvailablePort int `json:"available_port"`
}
//GetGatewayIPs get all gateway node ips
func (g *GatewayAction) GetGatewayIPs() []IPAndAvailablePort {
2021-05-26 18:19:15 +08:00
defaultAvailablePort, _ := g.GetAvailablePort("0.0.0.0", false)
2021-03-17 20:44:47 +08:00
defaultIps := []IPAndAvailablePort{{
2019-09-18 17:56:52 +08:00
IP: "0.0.0.0",
AvailablePort: defaultAvailablePort,
}}
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
res, err := clientv3.NewKV(g.etcdCli).Get(ctx, "/rainbond/gateway/ips", clientv3.WithPrefix())
if err != nil {
return defaultIps
}
gatewayIps := []string{}
for _, v := range res.Kvs {
gatewayIps = append(gatewayIps, string(v.Value))
}
sort.Strings(gatewayIps)
for _, v := range gatewayIps {
2021-05-26 18:19:15 +08:00
availablePort, _ := g.GetAvailablePort(v, false)
2019-09-18 17:56:52 +08:00
defaultIps = append(defaultIps, IPAndAvailablePort{
IP: v,
AvailablePort: availablePort,
})
}
return defaultIps
}
2021-05-20 00:01:37 +08:00
// DeleteIngressRulesByComponentPort deletes ingress rules, including http rules and tcp rules, based on the given componentID and port.
func (g *GatewayAction) DeleteIngressRulesByComponentPort(tx *gorm.DB, componentID string, port int) error {
httpRuleIDs, err := g.listHTTPRuleIDs(componentID, port)
if err != nil {
return err
}
// delete rule configs
if err := db.GetManager().GwRuleConfigDaoTransactions(tx).DeleteByRuleIDs(httpRuleIDs); err != nil {
return err
}
// delete rule extentions
if err := db.GetManager().RuleExtensionDaoTransactions(tx).DeleteByRuleIDs(httpRuleIDs); err != nil {
return err
}
// delete http rules
if err := db.GetManager().HTTPRuleDaoTransactions(tx).DeleteByComponentPort(componentID, port); err != nil {
if !errors.Is(err, bcode.ErrIngressHTTPRuleNotFound) {
return err
}
}
// delete tcp rules
if err := db.GetManager().TCPRuleDaoTransactions(tx).DeleteByComponentPort(componentID, port); err != nil {
if !errors.Is(err, bcode.ErrIngressTCPRuleNotFound) {
return err
}
}
return nil
}
func (g *GatewayAction) listHTTPRuleIDs(componentID string, port int) ([]string, error) {
httpRules, err := db.GetManager().HTTPRuleDao().ListByComponentPort(componentID, port)
if err != nil {
return nil, err
}
var ruleIDs []string
for _, rule := range httpRules {
ruleIDs = append(ruleIDs, rule.UUID)
}
return ruleIDs, nil
}
// SyncHTTPRules -
func (g *GatewayAction) SyncHTTPRules(tx *gorm.DB, components []*apimodel.Component) error {
var (
2021-07-30 16:17:33 +08:00
componentIDs []string
httpRules []*model.HTTPRule
ruleExtensions []*model.RuleExtension
)
for _, component := range components {
2021-06-08 09:43:30 +08:00
if component.HTTPRules == nil {
continue
}
componentIDs = append(componentIDs, component.ComponentBase.ComponentID)
for _, httpRule := range component.HTTPRules {
httpRules = append(httpRules, httpRule.DbModel(component.ComponentBase.ComponentID))
2021-07-30 16:17:33 +08:00
for _, ext := range httpRule.RuleExtensions {
ruleExtensions = append(ruleExtensions, &model.RuleExtension{
UUID: util.NewUUID(),
RuleID: httpRule.HTTPRuleID,
Key: ext.Key,
Value: ext.Value,
})
}
}
}
2021-07-30 16:17:33 +08:00
if err := g.syncRuleExtensions(tx, httpRules, ruleExtensions); err != nil {
return err
}
if err := db.GetManager().HTTPRuleDaoTransactions(tx).DeleteByComponentIDs(componentIDs); err != nil {
return err
}
return db.GetManager().HTTPRuleDaoTransactions(tx).CreateOrUpdateHTTPRuleInBatch(httpRules)
}
2021-07-30 16:17:33 +08:00
func (g *GatewayAction) syncRuleExtensions(tx *gorm.DB, httpRules []*model.HTTPRule, exts []*model.RuleExtension) error {
var ruleIDs []string
for _, hr := range httpRules {
ruleIDs = append(ruleIDs, hr.UUID)
}
if err := db.GetManager().RuleExtensionDaoTransactions(tx).DeleteByRuleIDs(ruleIDs); err != nil {
return err
}
return db.GetManager().RuleExtensionDaoTransactions(tx).CreateOrUpdateRuleExtensionsInBatch(exts)
}
// SyncTCPRules -
func (g *GatewayAction) SyncTCPRules(tx *gorm.DB, components []*apimodel.Component) error {
var (
componentIDs []string
2021-06-08 09:43:30 +08:00
tcpRules []*model.TCPRule
)
for _, component := range components {
2021-06-08 09:43:30 +08:00
if component.TCPRules == nil {
continue
}
componentIDs = append(componentIDs, component.ComponentBase.ComponentID)
for _, tcpRule := range component.TCPRules {
tcpRules = append(tcpRules, tcpRule.DbModel(component.ComponentBase.ComponentID))
}
}
if err := db.GetManager().TCPRuleDaoTransactions(tx).DeleteByComponentIDs(componentIDs); err != nil {
return err
}
return db.GetManager().TCPRuleDaoTransactions(tx).CreateOrUpdateTCPRuleInBatch(tcpRules)
}
2021-07-30 17:53:45 +08:00
// SyncRuleConfigs -
func (g *GatewayAction) SyncRuleConfigs(tx *gorm.DB, components []*apimodel.Component) error {
var configs []*model.GwRuleConfig
var componentIDs []string
for _, component := range components {
componentIDs = append(componentIDs, component.ComponentBase.ComponentID)
if len(component.HTTPRuleConfigs) == 0 {
continue
}
for _, httpRuleConfig := range component.HTTPRuleConfigs {
configs = append(configs, httpRuleConfig.DbModel()...)
}
}
// http rule ids
rules, err := db.GetManager().HTTPRuleDao().ListByComponentIDs(componentIDs)
if err != nil {
return err
}
var ruleIDs []string
for _, rule := range rules {
ruleIDs = append(ruleIDs, rule.UUID)
}
if err := db.GetManager().GwRuleConfigDaoTransactions(tx).DeleteByRuleIDs(ruleIDs); err != nil {
return err
}
return db.GetManager().GwRuleConfigDaoTransactions(tx).CreateOrUpdateGwRuleConfigsInBatch(configs)
}