Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve NAT handling in kubernetes environments" #30962

Closed
wants to merge 2 commits into from
Closed
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
18 changes: 17 additions & 1 deletion p2p/nat/nat.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"errors"
"fmt"
"net"
"os"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -133,7 +134,13 @@ func Map(m Interface, c <-chan struct{}, protocol string, extport, intport int,
// Mapping operations will not return an error but won't actually do anything.
type ExtIP net.IP

func (n ExtIP) ExternalIP() (net.IP, error) { return net.IP(n), nil }
func (n ExtIP) ExternalIP() (net.IP, error) {
if IsKubernetesEnvironment() {
return net.IP(n), nil
Copy link
Contributor

@ucwong ucwong Dec 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

en .. I think it is the same of return, whetherIsKubernetesEnvironment or not in this PR

}
return net.IP(n), nil
}

func (n ExtIP) String() string { return fmt.Sprintf("ExtIP(%v)", net.IP(n)) }

// These do nothing.
Expand Down Expand Up @@ -240,3 +247,12 @@ func (n *autodisc) wait() error {
}
return nil
}

// IsKubernetesEnvironment detects if the process is running inside Kubernetes cluster
// by checking for the presence of standard K8s environment variables.
func IsKubernetesEnvironment() bool {
if _, exists := os.LookupEnv("KUBERNETES_SERVICE_HOST"); exists {
return true
}
return false
}
21 changes: 20 additions & 1 deletion p2p/nat/nat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ package nat

import (
"net"
"os"
"testing"
"time"
)

// This test checks that autodisc doesn't hang and returns
// This test ensures that autodisc doesn't hang and returns
// consistent results when multiple goroutines call its methods
// concurrently.
func TestAutoDiscRace(t *testing.T) {
Expand Down Expand Up @@ -61,3 +62,21 @@ func TestAutoDiscRace(t *testing.T) {
}
}
}

func TestExtIPInKubernetes(t *testing.T) {
// Setting up the test environment
os.Setenv("KUBERNETES_SERVICE_HOST", "10.0.0.1")
defer os.Unsetenv("KUBERNETES_SERVICE_HOST")

testIP := net.ParseIP("192.0.2.1")
extIP := ExtIP(testIP)

// Verify that the IP is returned unchanged in Kubernetes
ip, err := extIP.ExternalIP()
if err != nil {
t.Fatalf("ExternalIP failed: %v", err)
}
if !ip.Equal(testIP) {
t.Errorf("wrong IP: got %v, want %v", ip, testIP)
}
}