Skip to content

Commit

Permalink
add fdb for flannel which is needed 4 connectivity
Browse files Browse the repository at this point in the history
  • Loading branch information
Niklaus-xie committed Dec 5, 2022
1 parent c3b3868 commit 819d938
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 29 deletions.
31 changes: 25 additions & 6 deletions controllers/v1_controller.go
Expand Up @@ -95,6 +95,11 @@ func (r *NodeReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.
ocfgs := map[string]interface{}{}
ncfgs := map[string]interface{}{}

oIpToMacV4 := map[string]string{}
// oIpToMacV6 := map[string]string{}
nIpToMacV4 := map[string]string{}
// nIpToMacV6 := map[string]string{}

var obj v1.Node
zlog := log.FromContext(ctx)
zlog.V(1).Info("resource event: " + req.NamespacedName.String())
Expand Down Expand Up @@ -137,6 +142,13 @@ func (r *NodeReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.
}
}

if pkg.ActiveSIGs.Mode == "flannel" {
oIpToMacV4, _ = k8s.NodeCache.AllIpToMac()
if ocfgs, err = pkg.ParseFdbsFrom(pkg.ActiveSIGs.VxlanTunnelName, oIpToMacV4); err != nil {
return ctrl.Result{}, err
}
}

k8s.NodeCache.Set(obj.DeepCopy())

if pkg.ActiveSIGs.Mode == "calico" {
Expand All @@ -145,14 +157,21 @@ func (r *NodeReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.
return ctrl.Result{}, err
}

pkg.PendingDeploys <- pkg.DeployRequest{
Meta: fmt.Sprintf("refreshing neighs for node '%s'", req.Name),
From: &ocfgs,
To: &ncfgs,
StatusFunc: func() {},
Partition: "Common",
}

if pkg.ActiveSIGs.Mode == "flannel" {
nIpToMacV4, _ = k8s.NodeCache.AllIpToMac()
if ncfgs, err = pkg.ParseFdbsFrom(pkg.ActiveSIGs.VxlanTunnelName, nIpToMacV4); err != nil {
return ctrl.Result{}, err
}
}
pkg.PendingDeploys <- pkg.DeployRequest{
Meta: fmt.Sprintf("refreshing for request '%s'", req.Name),
From: &ocfgs,
To: &ncfgs,
StatusFunc: func() {},
Partition: "Common",
}
}
return ctrl.Result{}, nil
}
Expand Down
21 changes: 21 additions & 0 deletions k8s/k8s.go
Expand Up @@ -127,3 +127,24 @@ func (ns *Nodes) AllIpAddresses() []string {
}
return rlt
}

func (ns *Nodes) AllIpToMac() (map[string]string, map[string]string) {
NodeCache.mutex <- true
defer func() { <-NodeCache.mutex }()

rlt4 := map[string]string{}
rlt6 := map[string]string{}

for _, n := range ns.Items {
if len(n.IpAddr) > 0 && len(n.MacAddr) > 0 {
slog.Infof("n.IpAddress is %s:", n.IpAddr)
rlt4[n.IpAddr] = n.MacAddr
}
if len(n.IpAddrV6) > 0 && len(n.MacAddrV6) > 0 {
slog.Infof("n.IpAddrV6 is %s:", n.IpAddrV6)
rlt6[n.IpAddrV6] = n.MacAddrV6
}

}
return rlt4, rlt6
}
7 changes: 4 additions & 3 deletions main.go
Expand Up @@ -92,13 +92,13 @@ func main() {
flag.StringVar(&credsDir, "credentials-directory", "", "Optional, directory that contains the BIG-IP username,"+
"password, and/or url files. To be used instead of username, password, and/or url arguments.")
flag.StringVar(&controllerName, "controller-name", "f5.io/gateway-controller-name", "This controller name.")
flag.StringVar(&mode, "mode", "flannel", "if set to calico or flannel, will make some related configs onto bigip automatically.")
flag.StringVar(&mode, "mode", "", "if set to calico or flannel, will make some related configs onto bigip automatically.")
flag.StringVar(&vxlanProfileName, "vxlan-profile-name", "fl-vxlan", "vxlan profile name on bigip.")
flag.StringVar(&vxlanPort, "vxlan-port", "8472", "port number in the vxlan profile.")
flag.StringVar(&vxlanTunnelName, "vxlan-tunnel-name", "fl-vxlan", "vxlan tunnel name on bigip.")
flag.StringVar(&vxlanLocalAddress, "vxlan-local-address", "9.4.4.4", "local address in the vxlan tunnel. e.g. 192.168.2.100")
flag.StringVar(&vxlanLocalAddress, "vxlan-local-address", "", "local address in the vxlan tunnel. e.g. 192.168.2.100")
flag.StringVar(&selfIpName, "selfip-name", "flannel-self", "flannel selfip name.")
flag.StringVar(&selfIpAddress, "selfip-address", "5.5.5.5/28", "flannel selfip ip address. e.g. 192.168.1.100/24")
flag.StringVar(&selfIpAddress, "selfip-address", "", "flannel selfip ip address. e.g. 192.168.1.100/24")

opts := zap.Options{
Development: true,
Expand All @@ -108,6 +108,7 @@ func main() {

pkg.ActiveSIGs.ControllerName = controllerName
pkg.ActiveSIGs.Mode = mode
pkg.ActiveSIGs.VxlanTunnelName = vxlanTunnelName

if (len(bigipUrl) == 0 || len(bigipUsername) == 0 ||
len(bigipPassword) == 0) && len(credsDir) == 0 {
Expand Down
23 changes: 23 additions & 0 deletions pkg/parser.go
Expand Up @@ -625,3 +625,26 @@ func ParseNeighsFrom(routerName, localAs, remoteAs string, addresses []string) (
"": rlt,
}, nil
}

func ParseFdbsFrom(tunnelName string, iPToMac map[string]string) (map[string]interface{}, error) {
rlt := map[string]interface{}{}

rlt["net/fdb/tunnel/"+tunnelName] = map[string]interface{}{
"records": []interface{}{},
}

fmtrecords := []interface{}{}
for ip, mac := range iPToMac {
slog.Infof("parse ip address: %s and mac: %s", ip, mac)
fmtrecords = append(fmtrecords, map[string]string{
"name": mac,
"endpoint": ip,
})
}

rlt["net/fdb/tunnel/"+tunnelName].(map[string]interface{})["records"] = fmtrecords

return map[string]interface{}{
"": rlt,
}, nil
}
21 changes: 11 additions & 10 deletions pkg/types.go
Expand Up @@ -22,16 +22,17 @@ type ParseRequest struct {
}

type SIGCache struct {
mutex sync.RWMutex
SyncedAtStart bool
ControllerName string
Mode string
Gateway map[string]*gatewayv1beta1.Gateway
HTTPRoute map[string]*gatewayv1beta1.HTTPRoute
Endpoints map[string]*v1.Endpoints
Service map[string]*v1.Service
GatewayClasses map[string]*gatewayv1beta1.GatewayClass
Bigip *f5_bigip.BIGIP
mutex sync.RWMutex
SyncedAtStart bool
ControllerName string
Mode string
VxlanTunnelName string
Gateway map[string]*gatewayv1beta1.Gateway
HTTPRoute map[string]*gatewayv1beta1.HTTPRoute
Endpoints map[string]*v1.Endpoints
Service map[string]*v1.Service
GatewayClasses map[string]*gatewayv1beta1.GatewayClass
Bigip *f5_bigip.BIGIP
// Node map[string]*v1.Node
}

Expand Down
21 changes: 11 additions & 10 deletions pkg/utils.go
Expand Up @@ -24,16 +24,17 @@ func init() {
PendingParses = make(chan ParseRequest, 16)
slog = utils.SetupLog("", "debug")
ActiveSIGs = &SIGCache{
mutex: sync.RWMutex{},
SyncedAtStart: false,
ControllerName: "",
Mode: "",
Gateway: map[string]*gatewayv1beta1.Gateway{},
HTTPRoute: map[string]*gatewayv1beta1.HTTPRoute{},
Endpoints: map[string]*v1.Endpoints{},
Service: map[string]*v1.Service{},
GatewayClasses: map[string]*gatewayv1beta1.GatewayClass{},
Bigip: nil,
mutex: sync.RWMutex{},
SyncedAtStart: false,
ControllerName: "",
Mode: "",
VxlanTunnelName: "",
Gateway: map[string]*gatewayv1beta1.Gateway{},
HTTPRoute: map[string]*gatewayv1beta1.HTTPRoute{},
Endpoints: map[string]*v1.Endpoints{},
Service: map[string]*v1.Service{},
GatewayClasses: map[string]*gatewayv1beta1.GatewayClass{},
Bigip: nil,
// Node: map[string]*v1.Node{},
}
}
Expand Down

0 comments on commit 819d938

Please sign in to comment.