mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-01 07:47:56 +00:00
Merge pull request #14227 from gmarek/fake_docker
Fix 'ContainersMap' feature in fake-docker-client.
This commit is contained in:
commit
2c92672827
@ -23,6 +23,7 @@ import (
|
|||||||
"reflect"
|
"reflect"
|
||||||
"sort"
|
"sort"
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
docker "github.com/fsouza/go-dockerclient"
|
docker "github.com/fsouza/go-dockerclient"
|
||||||
|
|
||||||
@ -31,6 +32,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// FakeDockerClient is a simple fake docker client, so that kubelet can be run for testing without requiring a real docker setup.
|
// FakeDockerClient is a simple fake docker client, so that kubelet can be run for testing without requiring a real docker setup.
|
||||||
|
// TODO: create a proper constructor for FakeDockerClient, so we won't need to check if ContainerMap is not nil.
|
||||||
type FakeDockerClient struct {
|
type FakeDockerClient struct {
|
||||||
sync.Mutex
|
sync.Mutex
|
||||||
ContainerList []docker.APIContainers
|
ContainerList []docker.APIContainers
|
||||||
@ -186,7 +188,12 @@ func (f *FakeDockerClient) CreateContainer(c docker.CreateContainerOptions) (*do
|
|||||||
// Docker likes to add a '/', so copy that behavior.
|
// Docker likes to add a '/', so copy that behavior.
|
||||||
name := "/" + c.Name
|
name := "/" + c.Name
|
||||||
f.ContainerList = append(f.ContainerList, docker.APIContainers{ID: name, Names: []string{name}, Image: c.Config.Image})
|
f.ContainerList = append(f.ContainerList, docker.APIContainers{ID: name, Names: []string{name}, Image: c.Config.Image})
|
||||||
return &docker.Container{ID: name}, nil
|
container := docker.Container{ID: name, Name: name, Config: c.Config}
|
||||||
|
if f.ContainerMap != nil {
|
||||||
|
containerCopy := container
|
||||||
|
f.ContainerMap[name] = &containerCopy
|
||||||
|
}
|
||||||
|
return &container, nil
|
||||||
}
|
}
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -208,9 +215,24 @@ func (f *FakeDockerClient) StartContainer(id string, hostConfig *docker.HostConf
|
|||||||
State: docker.State{
|
State: docker.State{
|
||||||
Running: true,
|
Running: true,
|
||||||
Pid: os.Getpid(),
|
Pid: os.Getpid(),
|
||||||
|
StartedAt: time.Now(),
|
||||||
},
|
},
|
||||||
NetworkSettings: &docker.NetworkSettings{IPAddress: "1.2.3.4"},
|
NetworkSettings: &docker.NetworkSettings{IPAddress: "1.2.3.4"},
|
||||||
}
|
}
|
||||||
|
if f.ContainerMap != nil {
|
||||||
|
container, ok := f.ContainerMap[id]
|
||||||
|
if !ok {
|
||||||
|
container = &docker.Container{ID: id, Name: id}
|
||||||
|
}
|
||||||
|
container.HostConfig = hostConfig
|
||||||
|
container.State = docker.State{
|
||||||
|
Running: true,
|
||||||
|
Pid: os.Getpid(),
|
||||||
|
StartedAt: time.Now(),
|
||||||
|
}
|
||||||
|
container.NetworkSettings = &docker.NetworkSettings{IPAddress: "2.3.4.5"}
|
||||||
|
f.ContainerMap[id] = container
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -233,6 +255,25 @@ func (f *FakeDockerClient) StopContainer(id string, timeout uint) error {
|
|||||||
newList = append(newList, container)
|
newList = append(newList, container)
|
||||||
}
|
}
|
||||||
f.ContainerList = newList
|
f.ContainerList = newList
|
||||||
|
if f.ContainerMap != nil {
|
||||||
|
container, ok := f.ContainerMap[id]
|
||||||
|
if !ok {
|
||||||
|
container = &docker.Container{
|
||||||
|
ID: id,
|
||||||
|
Name: id,
|
||||||
|
State: docker.State{
|
||||||
|
Running: false,
|
||||||
|
StartedAt: time.Now().Add(-time.Second),
|
||||||
|
FinishedAt: time.Now(),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
container.State.FinishedAt = time.Now()
|
||||||
|
container.State.Running = false
|
||||||
|
}
|
||||||
|
f.ContainerMap[id] = container
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -245,6 +286,9 @@ func (f *FakeDockerClient) RemoveContainer(opts docker.RemoveContainerOptions) e
|
|||||||
if err == nil {
|
if err == nil {
|
||||||
f.Removed = append(f.Removed, opts.ID)
|
f.Removed = append(f.Removed, opts.ID)
|
||||||
}
|
}
|
||||||
|
if f.ContainerMap != nil {
|
||||||
|
delete(f.ContainerMap, opts.ID)
|
||||||
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user