diff --git a/pkg/controlplane/cache.go b/pkg/controlplane/cache.go index dbcd6837..c0b037d4 100644 --- a/pkg/controlplane/cache.go +++ b/pkg/controlplane/cache.go @@ -42,7 +42,7 @@ type Rule struct { PortMap map[int32]int32 } -func (a *Virtual) To() ( +func (a *Virtual) To(enableIPv6 bool) ( listeners []types.Resource, clusters []types.Resource, routes []types.Resource, @@ -56,7 +56,13 @@ func (a *Virtual) To() ( var rr []*route.Route for _, rule := range a.Rules { - for _, ip := range []string{rule.LocalTunIPv4, rule.LocalTunIPv6} { + var ips []string + if enableIPv6 { + ips = []string{rule.LocalTunIPv4, rule.LocalTunIPv6} + } else { + ips = []string{rule.LocalTunIPv4} + } + for _, ip := range ips { clusterName := fmt.Sprintf("%s_%v", ip, rule.PortMap[port.ContainerPort]) clusters = append(clusters, ToCluster(clusterName)) endpoints = append(endpoints, ToEndPoint(clusterName, ip, rule.PortMap[port.ContainerPort])) diff --git a/pkg/controlplane/processor.go b/pkg/controlplane/processor.go index 494cd6d3..5180a688 100644 --- a/pkg/controlplane/processor.go +++ b/pkg/controlplane/processor.go @@ -17,6 +17,8 @@ import ( log "github.com/sirupsen/logrus" utilcache "k8s.io/apimachinery/pkg/util/cache" "sigs.k8s.io/yaml" + + "github.com/wencaiwulue/kubevpn/v2/pkg/util" ) type Processor struct { @@ -50,6 +52,7 @@ func (p *Processor) ProcessFile(file NotifyMessage) error { p.logger.Errorf("error parsing yaml file: %+v", err) return err } + enableIPv6, _ := util.DetectSupportIPv6() for _, config := range configList { if len(config.Uid) == 0 { continue @@ -62,7 +65,7 @@ func (p *Processor) ProcessFile(file NotifyMessage) error { } p.logger.Debugf("update config, version %d, config %v", p.version, config) - listeners, clusters, routes, endpoints := config.To() + listeners, clusters, routes, endpoints := config.To(enableIPv6) resources := map[resource.Type][]types.Resource{ resource.ListenerType: listeners, // listeners resource.RouteType: routes, // routes diff --git a/pkg/util/net.go b/pkg/util/net.go index f2bf6dd0..a86a5ad4 100644 --- a/pkg/util/net.go +++ b/pkg/util/net.go @@ -7,6 +7,9 @@ import ( "math" "math/rand" "net" + "os" + "strconv" + "strings" "time" "github.com/cilium/ipam/service/allocator" @@ -240,3 +243,15 @@ func GenICMPPacketIPv6(src net.IP, dst net.IP) ([]byte, error) { } return buf.Bytes(), nil } + +func DetectSupportIPv6() (bool, error) { + content, err := os.ReadFile("/proc/sys/net/ipv6/conf/all/disable_ipv6") + if err != nil { + return false, err + } + disableIPv6, err := strconv.Atoi(strings.TrimSpace(string(content))) + if err != nil { + return false, err + } + return disableIPv6 == 0, nil +}