-
Notifications
You must be signed in to change notification settings - Fork 38.6k
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
Add Windows support to kube-proxy #36079
Merged
k8s-github-robot
merged 4 commits into
kubernetes:master
from
apprenda:windows_kube_proxy
Nov 9, 2016
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package(default_visibility = ["//visibility:public"]) | ||
|
||
licenses(["notice"]) | ||
|
||
load( | ||
"@io_bazel_rules_go//go:def.bzl", | ||
"go_binary", | ||
"go_library", | ||
"go_test", | ||
"cgo_library", | ||
) | ||
|
||
go_library( | ||
name = "go_default_library", | ||
srcs = [ | ||
"loadbalancer.go", | ||
"port_allocator.go", | ||
"proxier.go", | ||
"proxysocket.go", | ||
"roundrobin.go", | ||
"udp_server.go", | ||
], | ||
tags = ["automanaged"], | ||
deps = [ | ||
"//pkg/api:go_default_library", | ||
"//pkg/proxy:go_default_library", | ||
"//pkg/types:go_default_library", | ||
"//pkg/util/errors:go_default_library", | ||
"//pkg/util/net:go_default_library", | ||
"//pkg/util/netsh:go_default_library", | ||
"//pkg/util/runtime:go_default_library", | ||
"//pkg/util/slice:go_default_library", | ||
"//pkg/util/wait:go_default_library", | ||
"//vendor:github.com/golang/glog", | ||
], | ||
) | ||
|
||
go_test( | ||
name = "go_default_test", | ||
srcs = [ | ||
"port_allocator_test.go", | ||
"proxier_test.go", | ||
"roundrobin_test.go", | ||
], | ||
library = "go_default_library", | ||
tags = ["automanaged"], | ||
deps = [ | ||
"//pkg/api:go_default_library", | ||
"//pkg/proxy:go_default_library", | ||
"//pkg/types:go_default_library", | ||
"//pkg/util/net:go_default_library", | ||
"//pkg/util/netsh/testing:go_default_library", | ||
"//pkg/util/runtime:go_default_library", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
Copyright 2016 The Kubernetes Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package winuserspace | ||
|
||
import ( | ||
"net" | ||
|
||
"k8s.io/kubernetes/pkg/api" | ||
"k8s.io/kubernetes/pkg/proxy" | ||
) | ||
|
||
// LoadBalancer is an interface for distributing incoming requests to service endpoints. | ||
type LoadBalancer interface { | ||
// NextEndpoint returns the endpoint to handle a request for the given | ||
// service-port and source address. | ||
NextEndpoint(service proxy.ServicePortName, srcAddr net.Addr, sessionAffinityReset bool) (string, error) | ||
NewService(service proxy.ServicePortName, sessionAffinityType api.ServiceAffinity, stickyMaxAgeMinutes int) error | ||
DeleteService(service proxy.ServicePortName) | ||
CleanupStaleStickySessions(service proxy.ServicePortName) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
/* | ||
Copyright 2016 The Kubernetes Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package winuserspace | ||
|
||
import ( | ||
"errors" | ||
"math/big" | ||
"math/rand" | ||
"sync" | ||
"time" | ||
|
||
"k8s.io/kubernetes/pkg/util/net" | ||
"k8s.io/kubernetes/pkg/util/wait" | ||
) | ||
|
||
var ( | ||
errPortRangeNoPortsRemaining = errors.New("port allocation failed; there are no remaining ports left to allocate in the accepted range") | ||
) | ||
|
||
type PortAllocator interface { | ||
AllocateNext() (int, error) | ||
Release(int) | ||
} | ||
|
||
// randomAllocator is a PortAllocator implementation that allocates random ports, yielding | ||
// a port value of 0 for every call to AllocateNext(). | ||
type randomAllocator struct{} | ||
|
||
// AllocateNext always returns 0 | ||
func (r *randomAllocator) AllocateNext() (int, error) { | ||
return 0, nil | ||
} | ||
|
||
// Release is a noop | ||
func (r *randomAllocator) Release(_ int) { | ||
// noop | ||
} | ||
|
||
// newPortAllocator builds PortAllocator for a given PortRange. If the PortRange is empty | ||
// then a random port allocator is returned; otherwise, a new range-based allocator | ||
// is returned. | ||
func newPortAllocator(r net.PortRange) PortAllocator { | ||
if r.Base == 0 { | ||
return &randomAllocator{} | ||
} | ||
return newPortRangeAllocator(r) | ||
} | ||
|
||
const ( | ||
portsBufSize = 16 | ||
nextFreePortCooldown = 500 * time.Millisecond | ||
allocateNextTimeout = 1 * time.Second | ||
) | ||
|
||
type rangeAllocator struct { | ||
net.PortRange | ||
ports chan int | ||
used big.Int | ||
lock sync.Mutex | ||
rand *rand.Rand | ||
} | ||
|
||
func newPortRangeAllocator(r net.PortRange) PortAllocator { | ||
if r.Base == 0 || r.Size == 0 { | ||
panic("illegal argument: may not specify an empty port range") | ||
} | ||
ra := &rangeAllocator{ | ||
PortRange: r, | ||
ports: make(chan int, portsBufSize), | ||
rand: rand.New(rand.NewSource(time.Now().UnixNano())), | ||
} | ||
go wait.Until(func() { ra.fillPorts(wait.NeverStop) }, nextFreePortCooldown, wait.NeverStop) | ||
return ra | ||
} | ||
|
||
// fillPorts loops, always searching for the next free port and, if found, fills the ports buffer with it. | ||
// this func blocks until either there are no remaining free ports, or else the stopCh chan is closed. | ||
func (r *rangeAllocator) fillPorts(stopCh <-chan struct{}) { | ||
for { | ||
port := r.nextFreePort() | ||
if port == -1 { | ||
return | ||
} | ||
select { | ||
case <-stopCh: | ||
return | ||
case r.ports <- port: | ||
} | ||
} | ||
} | ||
|
||
// nextFreePort finds a free port, first picking a random port. if that port is already in use | ||
// then the port range is scanned sequentially until either a port is found or the scan completes | ||
// unsuccessfully. an unsuccessful scan returns a port of -1. | ||
func (r *rangeAllocator) nextFreePort() int { | ||
r.lock.Lock() | ||
defer r.lock.Unlock() | ||
|
||
// choose random port | ||
j := r.rand.Intn(r.Size) | ||
if b := r.used.Bit(j); b == 0 { | ||
r.used.SetBit(&r.used, j, 1) | ||
return j + r.Base | ||
} | ||
|
||
// search sequentially | ||
for i := j + 1; i < r.Size; i++ { | ||
if b := r.used.Bit(i); b == 0 { | ||
r.used.SetBit(&r.used, i, 1) | ||
return i + r.Base | ||
} | ||
} | ||
for i := 0; i < j; i++ { | ||
if b := r.used.Bit(i); b == 0 { | ||
r.used.SetBit(&r.used, i, 1) | ||
return i + r.Base | ||
} | ||
} | ||
return -1 | ||
} | ||
|
||
func (r *rangeAllocator) AllocateNext() (port int, err error) { | ||
select { | ||
case port = <-r.ports: | ||
case <-time.After(allocateNextTimeout): | ||
err = errPortRangeNoPortsRemaining | ||
} | ||
return | ||
} | ||
|
||
func (r *rangeAllocator) Release(port int) { | ||
port -= r.Base | ||
if port < 0 || port >= r.Size { | ||
return | ||
} | ||
r.lock.Lock() | ||
defer r.lock.Unlock() | ||
r.used.SetBit(&r.used, port, 0) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dislike all these checks. Can we find a way to do this more abstractly. Maybe something like
platformParams := createPlatformParams()
and make that exist in distinct _windows and _linux files?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@thockin right now, there's only three of such checks and we can't see a quick and clear way for us to add this in time to have it in 1.5. Again, and as above, we'll have this in consideration for the next release.
Also, this may even go away as Windows Containers evolve.