mirror of https://github.com/k3s-io/k3s
[kube-proxy] Harden change tracker and proxiers for unmatched IP versions
parent
2ae45e9f1b
commit
95cde4fb98
|
@ -29,6 +29,7 @@ import (
|
||||||
"k8s.io/client-go/tools/record"
|
"k8s.io/client-go/tools/record"
|
||||||
api "k8s.io/kubernetes/pkg/apis/core"
|
api "k8s.io/kubernetes/pkg/apis/core"
|
||||||
utilproxy "k8s.io/kubernetes/pkg/proxy/util"
|
utilproxy "k8s.io/kubernetes/pkg/proxy/util"
|
||||||
|
utilnet "k8s.io/kubernetes/pkg/util/net"
|
||||||
)
|
)
|
||||||
|
|
||||||
// EndpointInfoCommon contains common endpoint information.
|
// EndpointInfoCommon contains common endpoint information.
|
||||||
|
@ -206,6 +207,13 @@ func (ect *EndpointChangeTracker) endpointsToEndpointsMap(endpoints *api.Endpoin
|
||||||
glog.Warningf("ignoring invalid endpoint port %s with empty host", port.Name)
|
glog.Warningf("ignoring invalid endpoint port %s with empty host", port.Name)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
// Filter out the incorrect IP version case.
|
||||||
|
if ect.isIPv6Mode != nil && utilnet.IsIPv6String(addr.IP) != *ect.isIPv6Mode {
|
||||||
|
// Emit event on the corresponding service which had a different
|
||||||
|
// IP version than the endpoint.
|
||||||
|
utilproxy.LogAndEmitIncorrectIPVersionEvent(ect.recorder, "endpoints", addr.IP, endpoints.Name, endpoints.Namespace, "")
|
||||||
|
continue
|
||||||
|
}
|
||||||
isLocal := addr.NodeName != nil && *addr.NodeName == ect.hostname
|
isLocal := addr.NodeName != nil && *addr.NodeName == ect.hostname
|
||||||
epInfoCommon := newEndpointInfoCommon(addr.IP, int(port.Port), isLocal)
|
epInfoCommon := newEndpointInfoCommon(addr.IP, int(port.Port), isLocal)
|
||||||
if ect.customizeEndpointInfo != nil {
|
if ect.customizeEndpointInfo != nil {
|
||||||
|
|
|
@ -309,6 +309,8 @@ func NewProxier(ipt utiliptables.Interface,
|
||||||
|
|
||||||
if len(clusterCIDR) == 0 {
|
if len(clusterCIDR) == 0 {
|
||||||
glog.Warningf("clusterCIDR not specified, unable to distinguish between internal and external traffic")
|
glog.Warningf("clusterCIDR not specified, unable to distinguish between internal and external traffic")
|
||||||
|
} else if utilnet.IsIPv6CIDR(clusterCIDR) != ipt.IsIpv6() {
|
||||||
|
return nil, fmt.Errorf("clusterCIDR %s has incorrect IP version: expect isIPv6=%t", clusterCIDR, ipt.IsIpv6())
|
||||||
}
|
}
|
||||||
|
|
||||||
healthChecker := healthcheck.NewServer(hostname, recorder, nil, nil) // use default implementations of deps
|
healthChecker := healthcheck.NewServer(hostname, recorder, nil, nil) // use default implementations of deps
|
||||||
|
|
|
@ -296,6 +296,8 @@ func NewProxier(ipt utiliptables.Interface,
|
||||||
|
|
||||||
if len(clusterCIDR) == 0 {
|
if len(clusterCIDR) == 0 {
|
||||||
glog.Warningf("clusterCIDR not specified, unable to distinguish between internal and external traffic")
|
glog.Warningf("clusterCIDR not specified, unable to distinguish between internal and external traffic")
|
||||||
|
} else if utilnet.IsIPv6CIDR(clusterCIDR) != isIPv6 {
|
||||||
|
return nil, fmt.Errorf("clusterCIDR %s has incorrect IP version: expect isIPv6=%t", clusterCIDR, isIPv6)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(scheduler) == 0 {
|
if len(scheduler) == 0 {
|
||||||
|
|
|
@ -20,6 +20,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
|
@ -31,6 +32,7 @@ import (
|
||||||
api "k8s.io/kubernetes/pkg/apis/core"
|
api "k8s.io/kubernetes/pkg/apis/core"
|
||||||
"k8s.io/kubernetes/pkg/apis/core/helper"
|
"k8s.io/kubernetes/pkg/apis/core/helper"
|
||||||
utilproxy "k8s.io/kubernetes/pkg/proxy/util"
|
utilproxy "k8s.io/kubernetes/pkg/proxy/util"
|
||||||
|
utilnet "k8s.io/kubernetes/pkg/util/net"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ServiceInfoCommon contains common service information.
|
// ServiceInfoCommon contains common service information.
|
||||||
|
@ -92,10 +94,23 @@ func (sct *ServiceChangeTracker) newServiceInfoCommon(port *api.ServicePort, ser
|
||||||
OnlyNodeLocalEndpoints: onlyNodeLocalEndpoints,
|
OnlyNodeLocalEndpoints: onlyNodeLocalEndpoints,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if sct.isIPv6Mode == nil {
|
||||||
info.ExternalIPs = make([]string, len(service.Spec.ExternalIPs))
|
info.ExternalIPs = make([]string, len(service.Spec.ExternalIPs))
|
||||||
info.LoadBalancerSourceRanges = make([]string, len(service.Spec.LoadBalancerSourceRanges))
|
info.LoadBalancerSourceRanges = make([]string, len(service.Spec.LoadBalancerSourceRanges))
|
||||||
copy(info.LoadBalancerSourceRanges, service.Spec.LoadBalancerSourceRanges)
|
copy(info.LoadBalancerSourceRanges, service.Spec.LoadBalancerSourceRanges)
|
||||||
copy(info.ExternalIPs, service.Spec.ExternalIPs)
|
copy(info.ExternalIPs, service.Spec.ExternalIPs)
|
||||||
|
} else {
|
||||||
|
// Filter out the incorrect IP version case.
|
||||||
|
var incorrectIPs []string
|
||||||
|
info.ExternalIPs, incorrectIPs = utilnet.FilterIncorrectIPVersion(service.Spec.ExternalIPs, *sct.isIPv6Mode)
|
||||||
|
if len(incorrectIPs) > 0 {
|
||||||
|
utilproxy.LogAndEmitIncorrectIPVersionEvent(sct.recorder, "externalIPs", strings.Join(incorrectIPs, ","), service.Namespace, service.Name, service.UID)
|
||||||
|
}
|
||||||
|
info.LoadBalancerSourceRanges, incorrectIPs = utilnet.FilterIncorrectCIDRVersion(service.Spec.LoadBalancerSourceRanges, *sct.isIPv6Mode)
|
||||||
|
if len(incorrectIPs) > 0 {
|
||||||
|
utilproxy.LogAndEmitIncorrectIPVersionEvent(sct.recorder, "loadBalancerSourceRanges", strings.Join(incorrectIPs, ","), service.Namespace, service.Name, service.UID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if apiservice.NeedsHealthCheck(service) {
|
if apiservice.NeedsHealthCheck(service) {
|
||||||
p := service.Spec.HealthCheckNodePort
|
p := service.Spec.HealthCheckNodePort
|
||||||
|
@ -221,6 +236,14 @@ func (sct *ServiceChangeTracker) serviceToServiceMap(service *api.Service) Servi
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(service.Spec.ClusterIP) != 0 {
|
||||||
|
// Filter out the incorrect IP version case.
|
||||||
|
if sct.isIPv6Mode != nil && utilnet.IsIPv6String(service.Spec.ClusterIP) != *sct.isIPv6Mode {
|
||||||
|
utilproxy.LogAndEmitIncorrectIPVersionEvent(sct.recorder, "clusterIP", service.Spec.ClusterIP, service.Namespace, service.Name, service.UID)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
serviceMap := make(ServiceMap)
|
serviceMap := make(ServiceMap)
|
||||||
for i := range service.Spec.Ports {
|
for i := range service.Spec.Ports {
|
||||||
servicePort := &service.Spec.Ports[i]
|
servicePort := &service.Spec.Ports[i]
|
||||||
|
|
Loading…
Reference in New Issue