Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions server/controller/genesis/common/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ const (
SYNC_TYPE_FORMAT = "%d-%s-%s" // orgID-type-vtapKey
)

const (
DEFAULT_IGNORE_NIC_REGEX = "^(kube-ipvs)"
)

const (
CONFIG_DB_MYSQL = "mysql"
CONFIG_DB_REDIS = "redis"
Expand Down
62 changes: 62 additions & 0 deletions server/controller/genesis/common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"sort"
"strconv"
"strings"
"sync"
"time"

simplejson "github.com/bitly/go-simplejson"
Expand Down Expand Up @@ -559,3 +560,64 @@ func IsAgentInterestedHost(aType agent.AgentType) bool {
}
return false
}

func IPsToPrefixes(orgID int, ips []string) []netaddr.IPPrefix {
var prefixes []netaddr.IPPrefix
for _, ip := range ips {
net, err := netaddr.ParseIP(ip)
if err == nil {
var bit uint8
switch {
case net.Is4():
bit = 32
case net.Is6():
bit = 128
}
ipPrefix, err := net.Prefix(bit)
if err != nil {
log.Warningf("ip convert to ip prefix error: %s", err.Error(), logger.NewORGPrefix(orgID))
continue
}
prefixes = append(prefixes, ipPrefix)
} else {
ipRange, err := netaddr.ParseIPPrefix(ip)
if err != nil {
ipRangeSlice, err := netaddr.ParseIPRange(ip)
if err != nil {
log.Errorf("parse ip ranges error: %s", err.Error(), logger.NewORGPrefix(orgID))
continue
}
prefixes = append(prefixes, ipRangeSlice.Prefixes()...)
} else {
prefixes = append(prefixes, ipRange)
}
}
}
return prefixes
}

type CoolDownFunc struct {
mu sync.Mutex
lastExecMap map[int]time.Time
cooldown time.Duration
}

func NewCoolDownFunc(cooldown time.Duration) *CoolDownFunc {
return &CoolDownFunc{
lastExecMap: make(map[int]time.Time),
cooldown: cooldown,
}
}

func (c *CoolDownFunc) Call(orgID int, f func()) bool {
c.mu.Lock()
defer c.mu.Unlock()

now := time.Now()
if time.Since(c.lastExecMap[orgID]) < c.cooldown {
return false
}
c.lastExecMap[orgID] = now
f()
return true
}
22 changes: 17 additions & 5 deletions server/controller/genesis/store/sync/mysql/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,9 @@ func (g *GenesisSync) Start() {
vStorage := NewSyncStorage(g.ctx, g.config.GenesisCfg, sDataChan)
vStorage.Start()

minuteCD := common.NewCoolDownFunc(time.Minute)
updaterMap := map[int]*updater.GenesisSyncRpcUpdater{}
genesisSyncDataByVtap := map[string]common.GenesisSyncDataResponse{}
vUpdater := updater.NewGenesisSyncRpcUpdater(g.config.GenesisCfg)
for {
genesisSyncData := common.GenesisSyncDataResponse{}
info := g.queue.Get().(common.VIFRPCMessage)
Expand All @@ -417,6 +418,17 @@ func (g *GenesisSync) Start() {

log.Debugf("sync received (%s) vtap_id (%v) type (%v) workload resource enabled (%t) received (%s)", info.Peer, info.VtapID, info.MessageType, info.WorkloadResourceEnabled, info.Message, logger.NewORGPrefix(info.ORGID))

vUpdater, ok := updaterMap[info.ORGID]
if !ok {
vUpdater = updater.NewGenesisSyncRpcUpdater(info.ORGID)
updaterMap[info.ORGID] = vUpdater
}

// load config when first time receive sync message and every minute after
minuteCD.Call(info.ORGID, func() {
vUpdater.LoadConfig(g.config.GenesisCfg)
})

vtap := fmt.Sprintf("%d%d", info.ORGID, info.VtapID)
if info.MessageType == common.TYPE_RENEW {
if info.VtapID != 0 {
Expand All @@ -433,13 +445,13 @@ func (g *GenesisSync) Start() {
agentType := info.Message.GetAgentType()
switch agentType {
case agent.AgentType_TT_PHYSICAL_MACHINE:
genesisSyncData = vUpdater.UnmarshalWorkloadProtobuf(info.ORGID, info.TeamID, info.VtapID, info.Peer, common.DEVICE_TYPE_PHYSICAL_MACHINE, info.WorkloadResourceEnabled, info.Message)
genesisSyncData = vUpdater.UnmarshalWorkloadProtobuf(info.TeamID, info.VtapID, info.Peer, common.DEVICE_TYPE_PHYSICAL_MACHINE, info.WorkloadResourceEnabled, info.Message)
case agent.AgentType_TT_PUBLIC_CLOUD:
genesisSyncData = vUpdater.UnmarshalWorkloadProtobuf(info.ORGID, info.TeamID, info.VtapID, info.Peer, common.DEVICE_TYPE_PUBLIC_CLOUD, info.WorkloadResourceEnabled, info.Message)
genesisSyncData = vUpdater.UnmarshalWorkloadProtobuf(info.TeamID, info.VtapID, info.Peer, common.DEVICE_TYPE_PUBLIC_CLOUD, info.WorkloadResourceEnabled, info.Message)
case agent.AgentType_TT_HOST_POD, agent.AgentType_TT_VM_POD, agent.AgentType_TT_K8S_SIDECAR:
genesisSyncData = vUpdater.UnmarshalKubernetesProtobuf(info.ORGID, info.TeamID, info.VtapID, info.Peer, info.WorkloadResourceEnabled, info.Message)
genesisSyncData = vUpdater.UnmarshalKubernetesProtobuf(info.TeamID, info.VtapID, info.Peer, info.WorkloadResourceEnabled, info.Message)
default:
genesisSyncData = vUpdater.UnmarshalProtobuf(info.ORGID, info.TeamID, info.VtapID, info.Peer, info.Message)
genesisSyncData = vUpdater.UnmarshalProtobuf(info.TeamID, info.VtapID, info.Peer, info.Message)
}

if info.VtapID != 0 {
Expand Down
Loading
Loading