Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

Commit a995586

Browse files
committed
Use userspace proxier when namespace is without network
1 parent d054268 commit a995586

File tree

6 files changed

+133
-52
lines changed

6 files changed

+133
-52
lines changed

cmd/kube-proxy/app/server.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ func NewProxyServerDefault(config *options.ProxyServerConfig) (*ProxyServer, err
217217
userspace.CleanupLeftovers(iptInterface)
218218
case proxyModeHaproxy:
219219
glog.V(2).Info("Using pod-buildin-haproxy proxy.")
220-
proxierBuildin, err := haproxy.NewProxier(config.SyncPeriod)
220+
proxierBuildin, err := haproxy.NewProxier(config.SyncPeriod, client)
221221
if err != nil {
222222
glog.Fatalf("Unable to create proxier: %v", err)
223223
}
@@ -227,7 +227,7 @@ func NewProxyServerDefault(config *options.ProxyServerConfig) (*ProxyServer, err
227227
glog.V(2).Info("Using userspace Proxier.")
228228
// This is a proxy.LoadBalancer which NewProxier needs but has methods we don't need for
229229
// our config.EndpointsConfigHandler.
230-
loadBalancer := userspace.NewLoadBalancerRR()
230+
loadBalancer := userspace.NewLoadBalancerRR(client, false)
231231
// set EndpointsConfigHandler to our loadBalancer
232232
endpointsHandler = loadBalancer
233233

@@ -238,6 +238,8 @@ func NewProxyServerDefault(config *options.ProxyServerConfig) (*ProxyServer, err
238238
*utilnet.ParsePortRangeOrDie(config.PortRange),
239239
config.IPTablesSyncPeriod.Duration,
240240
config.UDPIdleTimeout.Duration,
241+
client,
242+
false,
241243
)
242244
if err != nil {
243245
glog.Fatalf("Unable to create proxier: %v", err)
@@ -266,7 +268,16 @@ func NewProxyServerDefault(config *options.ProxyServerConfig) (*ProxyServer, err
266268
loadBalancer := userspace.NewLoadBalancerRR(client, true)
267269
endpointsConfig.RegisterHandler(loadBalancer)
268270

269-
proxierUserspace, err := userspace.NewProxier(loadBalancer, config.BindAddress, iptInterface, config.PortRange, config.IptablesSyncPeriod, config.UDPIdleTimeout, client, true)
271+
proxierUserspace, err := userspace.NewProxier(
272+
loadBalancer,
273+
net.ParseIP(config.BindAddress),
274+
iptInterface,
275+
*utilnet.ParsePortRangeOrDie(config.PortRange),
276+
config.IPTablesSyncPeriod.Duration,
277+
config.UDPIdleTimeout.Duration,
278+
client,
279+
true,
280+
)
270281
if err != nil {
271282
glog.Fatalf("Unable to create proxier: %v", err)
272283
}

pkg/proxy/haproxy/proxier.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/davecgh/go-spew/spew"
2626
"github.com/golang/glog"
2727
"k8s.io/kubernetes/pkg/api"
28+
kubeclient "k8s.io/kubernetes/pkg/client/unversioned"
2829
"k8s.io/kubernetes/pkg/kubelet/hyper"
2930
"k8s.io/kubernetes/pkg/proxy"
3031
"k8s.io/kubernetes/pkg/types"
@@ -58,6 +59,7 @@ type Proxier struct {
5859
serviceMap map[proxy.ServicePortName]*serviceInfo
5960
portsMap map[localPort]closeable
6061
hyperClient *hyper.HyperClient
62+
kubeClient *kubeclient.Client
6163
haveReceivedServiceUpdate bool // true once we've seen an OnServiceUpdate event
6264
haveReceivedEndpointsUpdate bool // true once we've seen an OnEndpointsUpdate event
6365

@@ -85,7 +87,7 @@ type closeable interface {
8587
var _ proxy.ProxyProvider = &Proxier{}
8688

8789
// NewProxier returns a new Proxier given an pod-buildin-haproxy Interface instance.
88-
func NewProxier(syncPeriod time.Duration) (*Proxier, error) {
90+
func NewProxier(syncPeriod time.Duration, kubeClient *kubeclient.Client) (*Proxier, error) {
8991
client := hyper.NewHyperClient()
9092
_, err := client.Version()
9193
if err != nil {
@@ -98,6 +100,7 @@ func NewProxier(syncPeriod time.Duration) (*Proxier, error) {
98100
portsMap: make(map[localPort]closeable),
99101
syncPeriod: syncPeriod,
100102
hyperClient: client,
103+
kubeClient: kubeClient,
101104
}, nil
102105
}
103106

@@ -162,6 +165,19 @@ func (proxier *Proxier) OnServiceUpdate(allServices []api.Service) {
162165

163166
for i := range allServices {
164167
service := &allServices[i]
168+
169+
// Check if namespace is configured with network
170+
namespace, err := proxier.kubeClient.Namespaces().Get(service.Namespace)
171+
if err != nil {
172+
glog.Warningf("Get namespace error: %v", err)
173+
continue
174+
}
175+
if namespace.Spec.Network == "" {
176+
// Only process namespaces with network
177+
// Namespaces without network will be processed by userspace proxier
178+
continue
179+
}
180+
165181
svcName := types.NamespacedName{
166182
Namespace: service.Namespace,
167183
Name: service.Name,
@@ -233,6 +249,18 @@ func (proxier *Proxier) OnEndpointsUpdate(allEndpoints []api.Endpoints) {
233249
for i := range allEndpoints {
234250
svcEndpoints := &allEndpoints[i]
235251

252+
// Check is namespace is configured with network
253+
namespace, err := proxier.kubeClient.Namespaces().Get(svcEndpoints.Namespace)
254+
if err != nil {
255+
glog.Warningf("Get namespace error: %v", err)
256+
continue
257+
}
258+
if namespace.Spec.Network == "" {
259+
// Only process namespaces with network
260+
// Namespaces without network will be processed by userspace proxier
261+
continue
262+
}
263+
236264
// We need to build a map of portname -> all ip:ports for that
237265
// portname. Explode Endpoints.Subsets[*] into this structure.
238266
portsToEndpoints := map[string][]hostPortPair{}

pkg/proxy/userspace/proxier.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727

2828
"github.com/golang/glog"
2929
"k8s.io/kubernetes/pkg/api"
30+
kubeclient "k8s.io/kubernetes/pkg/client/unversioned"
3031
"k8s.io/kubernetes/pkg/proxy"
3132
"k8s.io/kubernetes/pkg/types"
3233
utilnet "k8s.io/kubernetes/pkg/util/net"
@@ -95,6 +96,8 @@ type Proxier struct {
9596
iptables iptables.Interface
9697
hostIP net.IP
9798
proxyPorts PortAllocator
99+
kubeClient *kubeclient.Client
100+
withHaproxier bool
98101
}
99102

100103
// assert Proxier is a ProxyProvider
@@ -139,7 +142,7 @@ func IsProxyLocked(err error) bool {
139142
// if iptables fails to update or acquire the initial lock. Once a proxier is
140143
// created, it will keep iptables up to date in the background and will not
141144
// terminate if a particular iptables call fails.
142-
func NewProxier(loadBalancer LoadBalancer, listenIP net.IP, iptables iptables.Interface, pr utilnet.PortRange, syncPeriod, udpIdleTimeout time.Duration) (*Proxier, error) {
145+
func NewProxier(loadBalancer LoadBalancer, listenIP net.IP, iptables iptables.Interface, pr utilnet.PortRange, syncPeriod, udpIdleTimeout time.Duration, kubeClient *kubeclient.Client, withHaproxier bool) (*Proxier, error) {
143146
if listenIP.Equal(localhostIPv4) || listenIP.Equal(localhostIPv6) {
144147
return nil, ErrProxyOnLocalhost
145148
}
@@ -157,10 +160,14 @@ func NewProxier(loadBalancer LoadBalancer, listenIP net.IP, iptables iptables.In
157160
proxyPorts := newPortAllocator(pr)
158161

159162
glog.V(2).Infof("Setting proxy IP to %v and initializing iptables", hostIP)
160-
return createProxier(loadBalancer, listenIP, iptables, hostIP, proxyPorts, syncPeriod, udpIdleTimeout)
163+
return createProxier(loadBalancer, listenIP, iptables, hostIP, proxyPorts, syncPeriod, udpIdleTimeout, kubeClient, withHaproxier)
161164
}
162165

163-
func createProxier(loadBalancer LoadBalancer, listenIP net.IP, iptables iptables.Interface, hostIP net.IP, proxyPorts PortAllocator, syncPeriod, udpIdleTimeout time.Duration) (*Proxier, error) {
166+
func setRLimit(limit uint64) error {
167+
return syscall.Setrlimit(syscall.RLIMIT_NOFILE, &syscall.Rlimit{Max: limit, Cur: limit})
168+
}
169+
170+
func createProxier(loadBalancer LoadBalancer, listenIP net.IP, iptables iptables.Interface, hostIP net.IP, proxyPorts PortAllocator, syncPeriod, udpIdleTimeout time.Duration, kubeClient *kubeclient.Client, withHaproxier bool) (*Proxier, error) {
164171
// convenient to pass nil for tests..
165172
if proxyPorts == nil {
166173
proxyPorts = newPortAllocator(utilnet.PortRange{})
@@ -184,6 +191,8 @@ func createProxier(loadBalancer LoadBalancer, listenIP net.IP, iptables iptables
184191
iptables: iptables,
185192
hostIP: hostIP,
186193
proxyPorts: proxyPorts,
194+
kubeClient: kubeClient,
195+
withHaproxier: withHaproxier,
187196
}, nil
188197
}
189198

@@ -377,6 +386,20 @@ func (proxier *Proxier) OnServiceUpdate(services []api.Service) {
377386
for i := range services {
378387
service := &services[i]
379388

389+
// Check if namespace is configured with network
390+
if proxier.withHaproxier {
391+
namespace, err := proxier.kubeClient.Namespaces().Get(service.Namespace)
392+
if err != nil {
393+
glog.Warningf("Get namespace error: %v", err)
394+
continue
395+
}
396+
if namespace.Spec.Network != "" {
397+
// Only process namespaces without network
398+
// Namespaces with network will be processed by haproxy proxier
399+
continue
400+
}
401+
}
402+
380403
// if ClusterIP is "None" or empty, skip proxying
381404
if !api.IsServiceIPSet(service) {
382405
glog.V(3).Infof("Skipping service %s due to clusterIP = %q", types.NamespacedName{Namespace: service.Namespace, Name: service.Name}, service.Spec.ClusterIP)

0 commit comments

Comments
 (0)