Merge pull request #59832 from shyamjvs/fix-fake-docker-client-ip-collision

Automatic merge from submit-queue. If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

Fake docker-client assigns random IPs to containers

Fixes https://github.com/kubernetes/kubernetes/issues/59823

/cc @wojtek-t @Random-Liu
This commit is contained in:
Kubernetes Submit Queue 2018-02-14 07:08:24 -08:00 committed by GitHub
commit a129c0f984
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 12 deletions

View File

@ -19,6 +19,7 @@ package dockershim
import ( import (
"errors" "errors"
"fmt" "fmt"
"math/rand"
"net" "net"
"testing" "testing"
"time" "time"
@ -96,10 +97,8 @@ func TestSandboxStatus(t *testing.T) {
labels := map[string]string{"label": "foobar1"} labels := map[string]string{"label": "foobar1"}
annotations := map[string]string{"annotation": "abc"} annotations := map[string]string{"annotation": "abc"}
config := makeSandboxConfigWithLabelsAndAnnotations("foo", "bar", "1", 0, labels, annotations) config := makeSandboxConfigWithLabelsAndAnnotations("foo", "bar", "1", 0, labels, annotations)
r := rand.New(rand.NewSource(0)).Uint32()
// TODO: The following variables depend on the internal podIP := fmt.Sprintf("10.%d.%d.%d", byte(r>>16), byte(r>>8), byte(r))
// implementation of FakeDockerClient, and should be fixed.
fakeIP := "2.3.4.5"
state := runtimeapi.PodSandboxState_SANDBOX_READY state := runtimeapi.PodSandboxState_SANDBOX_READY
ct := int64(0) ct := int64(0)
@ -107,7 +106,7 @@ func TestSandboxStatus(t *testing.T) {
State: state, State: state,
CreatedAt: ct, CreatedAt: ct,
Metadata: config.Metadata, Metadata: config.Metadata,
Network: &runtimeapi.PodSandboxNetworkStatus{Ip: fakeIP}, Network: &runtimeapi.PodSandboxNetworkStatus{Ip: podIP},
Linux: &runtimeapi.LinuxPodSandboxStatus{ Linux: &runtimeapi.LinuxPodSandboxStatus{
Namespaces: &runtimeapi.Namespace{ Namespaces: &runtimeapi.Namespace{
Options: &runtimeapi.NamespaceOption{ Options: &runtimeapi.NamespaceOption{
@ -160,10 +159,8 @@ func TestSandboxStatus(t *testing.T) {
func TestSandboxStatusAfterRestart(t *testing.T) { func TestSandboxStatusAfterRestart(t *testing.T) {
ds, _, fClock := newTestDockerService() ds, _, fClock := newTestDockerService()
config := makeSandboxConfig("foo", "bar", "1", 0) config := makeSandboxConfig("foo", "bar", "1", 0)
r := rand.New(rand.NewSource(0)).Uint32()
// TODO: The following variables depend on the internal podIP := fmt.Sprintf("10.%d.%d.%d", byte(r>>16), byte(r>>8), byte(r))
// implementation of FakeDockerClient, and should be fixed.
fakeIP := "2.3.4.5"
state := runtimeapi.PodSandboxState_SANDBOX_READY state := runtimeapi.PodSandboxState_SANDBOX_READY
ct := int64(0) ct := int64(0)
@ -171,7 +168,7 @@ func TestSandboxStatusAfterRestart(t *testing.T) {
State: state, State: state,
CreatedAt: ct, CreatedAt: ct,
Metadata: config.Metadata, Metadata: config.Metadata,
Network: &runtimeapi.PodSandboxNetworkStatus{Ip: fakeIP}, Network: &runtimeapi.PodSandboxNetworkStatus{Ip: podIP},
Linux: &runtimeapi.LinuxPodSandboxStatus{ Linux: &runtimeapi.LinuxPodSandboxStatus{
Namespaces: &runtimeapi.Namespace{ Namespaces: &runtimeapi.Namespace{
Options: &runtimeapi.NamespaceOption{ Options: &runtimeapi.NamespaceOption{

View File

@ -18,6 +18,7 @@ package dockershim
import ( import (
"errors" "errors"
"math/rand"
"testing" "testing"
"time" "time"
@ -44,7 +45,7 @@ func newTestNetworkPlugin(t *testing.T) *nettest.MockNetworkPlugin {
func newTestDockerService() (*dockerService, *libdocker.FakeDockerClient, *clock.FakeClock) { func newTestDockerService() (*dockerService, *libdocker.FakeDockerClient, *clock.FakeClock) {
fakeClock := clock.NewFakeClock(time.Time{}) fakeClock := clock.NewFakeClock(time.Time{})
c := libdocker.NewFakeDockerClient().WithClock(fakeClock).WithVersion("1.11.2", "1.23") c := libdocker.NewFakeDockerClient().WithClock(fakeClock).WithVersion("1.11.2", "1.23").WithRandSource(rand.NewSource(0))
pm := network.NewPluginManager(&network.NoopNetworkPlugin{}) pm := network.NewPluginManager(&network.NoopNetworkPlugin{})
return &dockerService{ return &dockerService{
client: c, client: c,

View File

@ -61,6 +61,7 @@ type FakeDockerClient struct {
called []calledDetail called []calledDetail
pulled []string pulled []string
EnableTrace bool EnableTrace bool
RandGenerator *rand.Rand
// Created, Started, Stopped and Removed all contain container docker ID // Created, Started, Stopped and Removed all contain container docker ID
Created []string Created []string
@ -99,6 +100,7 @@ func NewFakeDockerClient() *FakeDockerClient {
EnableTrace: true, EnableTrace: true,
ImageInspects: make(map[string]*dockertypes.ImageInspect), ImageInspects: make(map[string]*dockertypes.ImageInspect),
ImageIDsNeedingAuth: make(map[string]dockertypes.AuthConfig), ImageIDsNeedingAuth: make(map[string]dockertypes.AuthConfig),
RandGenerator: rand.New(rand.NewSource(time.Now().UnixNano())),
} }
} }
@ -123,6 +125,13 @@ func (f *FakeDockerClient) WithTraceDisabled() *FakeDockerClient {
return f return f
} }
func (f *FakeDockerClient) WithRandSource(source rand.Source) *FakeDockerClient {
f.Lock()
defer f.Unlock()
f.RandGenerator = rand.New(source)
return f
}
func (f *FakeDockerClient) appendCalled(callDetail calledDetail) { func (f *FakeDockerClient) appendCalled(callDetail calledDetail) {
if f.EnableTrace { if f.EnableTrace {
f.called = append(f.called, callDetail) f.called = append(f.called, callDetail)
@ -597,7 +606,8 @@ func (f *FakeDockerClient) StartContainer(id string) error {
container.State.Running = true container.State.Running = true
container.State.Pid = os.Getpid() container.State.Pid = os.Getpid()
container.State.StartedAt = dockerTimestampToString(timestamp) container.State.StartedAt = dockerTimestampToString(timestamp)
container.NetworkSettings.IPAddress = "2.3.4.5" r := f.RandGenerator.Uint32()
container.NetworkSettings.IPAddress = fmt.Sprintf("10.%d.%d.%d", byte(r>>16), byte(r>>8), byte(r))
f.ContainerMap[id] = container f.ContainerMap[id] = container
f.updateContainerStatus(id, StatusRunningPrefix) f.updateContainerStatus(id, StatusRunningPrefix)
f.normalSleep(200, 50, 50) f.normalSleep(200, 50, 50)