Skip to content

Commit

Permalink
Merge pull request #158 from YTGhost/fix-nat-type-empty
Browse files Browse the repository at this point in the history
fix: avoid additional logic when NATTraversal is disabled
  • Loading branch information
njucjc authored Jan 15, 2024
2 parents ef4b2ed + 0b399c1 commit 7003f50
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
8 changes: 5 additions & 3 deletions pkg/engine/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ func (t *TunnelEngine) processNextWorkItem() bool {

func (t *TunnelEngine) handler(gw *v1beta1.Gateway) error {
klog.Info(utils.FormatRavenEngine("update raven l3 tunnel config for gateway %s", gw.GetName()))
if err := t.checkNatCapability(); err != nil {
return err
if t.config.Tunnel.NATTraversal {
if err := t.checkNatCapability(); err != nil {
return err
}
}

err := t.initDriver()
Expand Down Expand Up @@ -97,7 +99,7 @@ func (t *TunnelEngine) initDriver() error {
}

if t.tunnelHandler == nil {
t.tunnelHandler = tunnelengine.NewTunnelHandler(t.nodeName, t.config.Tunnel.ForwardNodeIP, t.client, t.routeDriver, t.vpnDriver)
t.tunnelHandler = tunnelengine.NewTunnelHandler(t.nodeName, t.config.Tunnel.ForwardNodeIP, t.config.Tunnel.NATTraversal, t.client, t.routeDriver, t.vpnDriver)
}
return nil
}
Expand Down
24 changes: 14 additions & 10 deletions pkg/tunnelengine/tunnelagent.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
type TunnelHandler struct {
nodeName string
forwardNodeIP bool
natTraversal bool
ownGateway *v1beta1.Gateway

ravenClient client.Client
Expand All @@ -52,10 +53,11 @@ type TunnelHandler struct {
lastSeenNetwork *types.Network
}

func NewTunnelHandler(nodeName string, forwardNodeIP bool, client client.Client, routeDriver routedriver.Driver, vpnDriver vpndriver.Driver) *TunnelHandler {
func NewTunnelHandler(nodeName string, forwardNodeIP bool, natTraversal bool, client client.Client, routeDriver routedriver.Driver, vpnDriver vpndriver.Driver) *TunnelHandler {
return &TunnelHandler{
nodeName: nodeName,
forwardNodeIP: forwardNodeIP,
natTraversal: natTraversal,
ravenClient: client,
routeDriver: routeDriver,
vpnDriver: vpnDriver,
Expand Down Expand Up @@ -87,13 +89,13 @@ func (c *TunnelHandler) Handler() error {
// try to update public IP if empty.
gw := &gws.Items[i]
if ep := getTunnelActiveEndpoints(gw); ep != nil {
if ep.PublicIP == "" || ep.NATType == "" || ep.PublicPort == 0 && ep.NATType != utils.NATSymmetric {
if ep.PublicIP == "" || c.natTraversal && (ep.NATType == "" || ep.PublicPort == 0 && ep.NATType != utils.NATSymmetric) {
if ep.PublicIP == "" {
if err := c.configGatewayPublicIP(gw); err != nil {
klog.ErrorS(err, "error config gateway public ip", "gateway", klog.KObj(gw))
}
}
if ep.NATType == "" || ep.PublicPort == 0 && ep.NATType != utils.NATSymmetric {
if c.natTraversal && (ep.NATType == "" || ep.PublicPort == 0 && ep.NATType != utils.NATSymmetric) {
if err := c.configGatewayStunInfo(gw); err != nil {
klog.ErrorS(err, "error config gateway stun info", "gateway", klog.KObj(gw))
}
Expand Down Expand Up @@ -214,13 +216,15 @@ func (c *TunnelHandler) shouldHandleGateway(gateway *v1beta1.Gateway) bool {
klog.InfoS("no public IP for gateway, waiting for sync", "gateway", klog.KObj(gateway))
return false
}
if ep.NATType == "" {
klog.InfoS("no nat type for gateway, waiting for sync", "gateway", klog.KObj(gateway))
return false
}
if ep.NATType != utils.NATSymmetric && ep.PublicPort == 0 {
klog.InfoS("no public port for gateway, waiting for sync", "gateway", klog.KObj(gateway))
return false
if c.natTraversal {
if ep.NATType == "" {
klog.InfoS("no nat type for gateway, waiting for sync", "gateway", klog.KObj(gateway))
return false
}
if ep.NATType != utils.NATSymmetric && ep.PublicPort == 0 {
klog.InfoS("no public port for gateway, waiting for sync", "gateway", klog.KObj(gateway))
return false
}
}
if c.ownGateway == nil {
klog.InfoS(fmt.Sprintf("no own gateway for node %s, skip it", c.nodeName), "gateway", klog.KObj(gateway))
Expand Down

0 comments on commit 7003f50

Please sign in to comment.