Skip to content

Commit

Permalink
Merge pull request kubernetes#59832 from shyamjvs/fix-fake-docker-cli…
Browse files Browse the repository at this point in the history
…ent-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 kubernetes#59823

/cc @wojtek-t @Random-Liu
  • Loading branch information
Kubernetes Submit Queue authored Feb 14, 2018
2 parents 0dda5c8 + 517301d commit a129c0f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
17 changes: 7 additions & 10 deletions pkg/kubelet/dockershim/docker_sandbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package dockershim
import (
"errors"
"fmt"
"math/rand"
"net"
"testing"
"time"
Expand Down Expand Up @@ -96,18 +97,16 @@ func TestSandboxStatus(t *testing.T) {
labels := map[string]string{"label": "foobar1"}
annotations := map[string]string{"annotation": "abc"}
config := makeSandboxConfigWithLabelsAndAnnotations("foo", "bar", "1", 0, labels, annotations)

// TODO: The following variables depend on the internal
// implementation of FakeDockerClient, and should be fixed.
fakeIP := "2.3.4.5"
r := rand.New(rand.NewSource(0)).Uint32()
podIP := fmt.Sprintf("10.%d.%d.%d", byte(r>>16), byte(r>>8), byte(r))

state := runtimeapi.PodSandboxState_SANDBOX_READY
ct := int64(0)
expected := &runtimeapi.PodSandboxStatus{
State: state,
CreatedAt: ct,
Metadata: config.Metadata,
Network: &runtimeapi.PodSandboxNetworkStatus{Ip: fakeIP},
Network: &runtimeapi.PodSandboxNetworkStatus{Ip: podIP},
Linux: &runtimeapi.LinuxPodSandboxStatus{
Namespaces: &runtimeapi.Namespace{
Options: &runtimeapi.NamespaceOption{
Expand Down Expand Up @@ -160,18 +159,16 @@ func TestSandboxStatus(t *testing.T) {
func TestSandboxStatusAfterRestart(t *testing.T) {
ds, _, fClock := newTestDockerService()
config := makeSandboxConfig("foo", "bar", "1", 0)

// TODO: The following variables depend on the internal
// implementation of FakeDockerClient, and should be fixed.
fakeIP := "2.3.4.5"
r := rand.New(rand.NewSource(0)).Uint32()
podIP := fmt.Sprintf("10.%d.%d.%d", byte(r>>16), byte(r>>8), byte(r))

state := runtimeapi.PodSandboxState_SANDBOX_READY
ct := int64(0)
expected := &runtimeapi.PodSandboxStatus{
State: state,
CreatedAt: ct,
Metadata: config.Metadata,
Network: &runtimeapi.PodSandboxNetworkStatus{Ip: fakeIP},
Network: &runtimeapi.PodSandboxNetworkStatus{Ip: podIP},
Linux: &runtimeapi.LinuxPodSandboxStatus{
Namespaces: &runtimeapi.Namespace{
Options: &runtimeapi.NamespaceOption{
Expand Down
3 changes: 2 additions & 1 deletion pkg/kubelet/dockershim/docker_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package dockershim

import (
"errors"
"math/rand"
"testing"
"time"

Expand All @@ -44,7 +45,7 @@ func newTestNetworkPlugin(t *testing.T) *nettest.MockNetworkPlugin {

func newTestDockerService() (*dockerService, *libdocker.FakeDockerClient, *clock.FakeClock) {
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{})
return &dockerService{
client: c,
Expand Down
12 changes: 11 additions & 1 deletion pkg/kubelet/dockershim/libdocker/fake_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type FakeDockerClient struct {
called []calledDetail
pulled []string
EnableTrace bool
RandGenerator *rand.Rand

// Created, Started, Stopped and Removed all contain container docker ID
Created []string
Expand Down Expand Up @@ -99,6 +100,7 @@ func NewFakeDockerClient() *FakeDockerClient {
EnableTrace: true,
ImageInspects: make(map[string]*dockertypes.ImageInspect),
ImageIDsNeedingAuth: make(map[string]dockertypes.AuthConfig),
RandGenerator: rand.New(rand.NewSource(time.Now().UnixNano())),
}
}

Expand All @@ -123,6 +125,13 @@ func (f *FakeDockerClient) WithTraceDisabled() *FakeDockerClient {
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) {
if f.EnableTrace {
f.called = append(f.called, callDetail)
Expand Down Expand Up @@ -597,7 +606,8 @@ func (f *FakeDockerClient) StartContainer(id string) error {
container.State.Running = true
container.State.Pid = os.Getpid()
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.updateContainerStatus(id, StatusRunningPrefix)
f.normalSleep(200, 50, 50)
Expand Down

0 comments on commit a129c0f

Please sign in to comment.