Skip to content

Commit

Permalink
feat: intercommunication between subclusters
Browse files Browse the repository at this point in the history
Signed-off-by: wangyizhi1 <[email protected]>
  • Loading branch information
wangyizhi1 committed Aug 31, 2023
1 parent d5548df commit eba9ea3
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
1 change: 1 addition & 0 deletions pkg/constants/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ const (

ALL_ZERO_MAC = "00:00:00:00:00:00"

IPTablesPreRoutingChain = "PREROUTING"
IPTablesPostRoutingChain = "POSTROUTING"
)
62 changes: 62 additions & 0 deletions pkg/network-manager/handlers/globalmap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package handlers

import (
"fmt"
"net"

"k8s.io/klog/v2"

"github.com/kosmos.io/kosmos/pkg/apis/clusterlink/v1alpha1"
"github.com/kosmos.io/kosmos/pkg/constants"
"github.com/kosmos.io/kosmos/pkg/network-manager/helpers"
)

type GlobalMap struct {
Next
}

func (h *GlobalMap) Do(c *Context) (err error) {
gwNodes := c.Filter.GetGatewayNodes()
epNodes := c.Filter.GetEndpointNodes()

nodes := append(gwNodes, epNodes...)

for _, n := range nodes {
cluster := c.Filter.GetClusterByName(n.Spec.ClusterName)
globalMap := cluster.Spec.GlobalCIDRsMap

if len(globalMap) > 0 {
for src, dst := range cluster.Spec.GlobalCIDRsMap {
ipType := helpers.GetIPType(src)

var vxBridge string
if ipType == helpers.IPV6 {
vxBridge = constants.VXLAN_BRIDGE_NAME_6
} else if ipType == helpers.IPV4 {
vxBridge = constants.VXLAN_BRIDGE_NAME
}

// todo in-cluster globalIP access
c.Results[n.Name].Iptables = append(c.Results[n.Name].Iptables, v1alpha1.Iptables{
Table: "nat",
Chain: constants.IPTablesPreRoutingChain,
Rule: fmt.Sprintf("-d %s -i %s -j NETMAP --to %s", dst, vxBridge, src),
})

_, dstIP, err := net.ParseCIDR(dst)
if err != nil {
klog.Errorf("globalmap: invalid dstIP, err: %v", err)
continue
}

c.Results[n.Name].Iptables = append(c.Results[n.Name].Iptables, v1alpha1.Iptables{
Table: "nat",
Chain: constants.IPTablesPostRoutingChain,
Rule: fmt.Sprintf("-s %s -o %s -j SNAT --to-source %s", src, vxBridge, dstIP.IP),
})
}
}
}

return nil
}
3 changes: 2 additions & 1 deletion pkg/network-manager/network_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ func (n *Manager) CalculateNetworkConfigs(clusters []v1alpha1.Cluster, clusterNo
SetNext(&handlers.PodRoutes{}).
SetNext(&handlers.VxLocalMacCache{}).
SetNext(&handlers.VxBridgeMacCache{}).
SetNext(&handlers.HostNetwork{})
SetNext(&handlers.HostNetwork{}).
SetNext(&handlers.GlobalMap{})

if err := rootHandler.Run(c); err != nil {
return nil, fmt.Errorf("filed to calculate network config, err: %v", err)
Expand Down

0 comments on commit eba9ea3

Please sign in to comment.