Cleaning up container ID handling inside kubelet

This commit is contained in:
Justin Huff 2014-06-25 16:24:20 -07:00
parent 3432cf6c53
commit fe0066d2e4
5 changed files with 129 additions and 262 deletions

View File

@ -59,13 +59,13 @@ func (f *FakeDockerClient) CreateContainer(c docker.CreateContainerOptions) (*do
func (f *FakeDockerClient) StartContainer(id string, hostConfig *docker.HostConfig) error { func (f *FakeDockerClient) StartContainer(id string, hostConfig *docker.HostConfig) error {
f.appendCall("start") f.appendCall("start")
return nil return f.err
} }
func (f *FakeDockerClient) StopContainer(id string, timeout uint) error { func (f *FakeDockerClient) StopContainer(id string, timeout uint) error {
f.appendCall("stop") f.appendCall("stop")
f.stopped = append(f.stopped, id) f.stopped = append(f.stopped, id)
return nil return f.err
} }
type FakeDockerPuller struct { type FakeDockerPuller struct {

View File

@ -169,64 +169,37 @@ func (kl *Kubelet) LogEvent(event *api.Event) error {
return err return err
} }
// Does this container exist on this host? Returns true if so, and the name under which the container is running. // Return a map of docker containers that we manage. The map key is the docker container ID
// Returns an error if one occurs. func (kl *Kubelet) getDockerContainers() (map[string]docker.APIContainers, error) {
func (kl *Kubelet) ContainerExists(manifest *api.ContainerManifest, container *api.Container) (exists bool, foundName string, err error) { result := map[string]docker.APIContainers{}
containers, err := kl.ListContainers() containerList, err := kl.DockerClient.ListContainers(docker.ListContainersOptions{})
if err != nil { if err != nil {
return false, "", err return result, err
} }
for _, name := range containers { for _, value := range containerList {
manifestId, containerName := dockerNameToManifestAndContainer(name) // Skip containers that we didn't create to allow users to manually
// spin up their own containers if they want.
if !strings.HasPrefix(value.Names[0], "/"+containerNamePrefix+"--") {
continue
}
result[value.ID] = value
}
return result, err
}
// Return Docker's container ID for a manifest's container. Returns an empty string if it doesn't exist
func (kl *Kubelet) getContainerId(manifest *api.ContainerManifest, container *api.Container) (string, error) {
dockerContainers, err := kl.getDockerContainers()
if err != nil {
return "", err
}
for id, dockerContainer := range dockerContainers {
manifestId, containerName := dockerNameToManifestAndContainer(dockerContainer.Names[0])
if manifestId == manifest.Id && containerName == container.Name { if manifestId == manifest.Id && containerName == container.Name {
// TODO(bburns) : This leads to an extra list. Convert this to use the returned ID and a straight call return id, nil
// to inspect
data, err := kl.GetContainerByName(name)
return data != nil, name, err
} }
} }
return false, "", nil return "", nil
}
// GetContainerID looks at the list of containers on the machine and returns the ID of the container whose name
// matches 'name'. It returns the name of the container, or empty string, if the container isn't found.
// it returns true if the container is found, false otherwise, and any error that occurs.
func (kl *Kubelet) GetContainerID(name string) (string, bool, error) {
containerList, err := kl.DockerClient.ListContainers(docker.ListContainersOptions{})
if err != nil {
return "", false, err
}
for _, value := range containerList {
if strings.Contains(value.Names[0], name) {
return value.ID, true, nil
}
}
return "", false, nil
}
// Get a container by name.
// returns the container data from Docker, or an error if one exists.
func (kl *Kubelet) GetContainerByName(name string) (*docker.Container, error) {
id, found, err := kl.GetContainerID(name)
if err != nil {
return nil, err
}
if !found {
return nil, nil
}
return kl.DockerClient.InspectContainer(id)
}
func (kl *Kubelet) ListContainers() ([]string, error) {
result := []string{}
containerList, err := kl.DockerClient.ListContainers(docker.ListContainersOptions{})
if err != nil {
return result, err
}
for _, value := range containerList {
result = append(result, value.Names[0])
}
return result, err
} }
type dockerPuller struct{} type dockerPuller struct{}
@ -346,15 +319,14 @@ func makePortsAndBindings(container *api.Container) (map[docker.Port]struct{}, m
return exposedPorts, portBindings return exposedPorts, portBindings
} }
func (kl *Kubelet) RunContainer(manifest *api.ContainerManifest, container *api.Container, netMode string) (name string, err error) { // Run a single container from a manifest. Returns the docker container ID
name = manifestAndContainerToDockerName(manifest, container) func (kl *Kubelet) runContainer(manifest *api.ContainerManifest, container *api.Container, netMode string) (id string, err error) {
envVariables := makeEnvironmentVariables(container) envVariables := makeEnvironmentVariables(container)
volumes, binds := makeVolumesAndBinds(container) volumes, binds := makeVolumesAndBinds(container)
exposedPorts, portBindings := makePortsAndBindings(container) exposedPorts, portBindings := makePortsAndBindings(container)
opts := docker.CreateContainerOptions{ opts := docker.CreateContainerOptions{
Name: name, Name: manifestAndContainerToDockerName(manifest, container),
Config: &docker.Config{ Config: &docker.Config{
Image: container.Image, Image: container.Image,
ExposedPorts: exposedPorts, ExposedPorts: exposedPorts,
@ -368,25 +340,18 @@ func (kl *Kubelet) RunContainer(manifest *api.ContainerManifest, container *api.
if err != nil { if err != nil {
return "", err return "", err
} }
return name, kl.DockerClient.StartContainer(dockerContainer.ID, &docker.HostConfig{ err = kl.DockerClient.StartContainer(dockerContainer.ID, &docker.HostConfig{
PortBindings: portBindings, PortBindings: portBindings,
Binds: binds, Binds: binds,
NetworkMode: netMode, NetworkMode: netMode,
}) })
return dockerContainer.ID, err
} }
func (kl *Kubelet) KillContainer(name string) error { // Kill a docker container
id, found, err := kl.GetContainerID(name) func (kl *Kubelet) killContainer(container docker.APIContainers) error {
if err != nil { err := kl.DockerClient.StopContainer(container.ID, 10)
return err manifestId, containerName := dockerNameToManifestAndContainer(container.Names[0])
}
if !found {
// This is weird, but not an error, so yell and then return nil
glog.Infof("Couldn't find container: %s", name)
return nil
}
err = kl.DockerClient.StopContainer(id, 10)
manifestId, containerName := dockerNameToManifestAndContainer(name)
kl.LogEvent(&api.Event{ kl.LogEvent(&api.Event{
Event: "STOP", Event: "STOP",
Manifest: &api.ContainerManifest{ Manifest: &api.ContainerManifest{
@ -642,19 +607,12 @@ func (kl *Kubelet) WatchEtcd(watchChannel <-chan *etcd.Response, updateChannel c
const networkContainerName = "net" const networkContainerName = "net"
func (kl *Kubelet) networkContainerExists(manifest *api.ContainerManifest) (string, bool, error) { // Return the docker ID for a manifest's network container. Returns an empty string if it doesn't exist
pods, err := kl.ListContainers() func (kl *Kubelet) getNetworkContainerId(manifest *api.ContainerManifest) (string, error) {
if err != nil { return kl.getContainerId(manifest, &api.Container{Name: networkContainerName})
return "", false, err
}
for _, name := range pods {
if strings.Contains(name, containerNamePrefix+"--"+networkContainerName+"--"+escapeDash(manifest.Id)+"--") {
return name, true, nil
}
}
return "", false, nil
} }
// Create a network container for a manifest. Returns the docker container ID of the newly created container
func (kl *Kubelet) createNetworkContainer(manifest *api.ContainerManifest) (string, error) { func (kl *Kubelet) createNetworkContainer(manifest *api.ContainerManifest) (string, error) {
var ports []api.Port var ports []api.Port
// Docker only exports ports from the network container. Let's // Docker only exports ports from the network container. Let's
@ -669,73 +627,69 @@ func (kl *Kubelet) createNetworkContainer(manifest *api.ContainerManifest) (stri
Ports: ports, Ports: ports,
} }
kl.DockerPuller.Pull("busybox") kl.DockerPuller.Pull("busybox")
return kl.RunContainer(manifest, container, "") return kl.runContainer(manifest, container, "")
} }
// Sync the configured list of containers (desired state) with the host current state // Sync the configured list of containers (desired state) with the host current state
func (kl *Kubelet) SyncManifests(config []api.ContainerManifest) error { func (kl *Kubelet) SyncManifests(config []api.ContainerManifest) error {
glog.Infof("Desired: %#v", config) glog.Infof("Desired: %#v", config)
var err error var err error
desired := map[string]bool{} dockerIdsToKeep := map[string]bool{}
// Check for any containers that need starting
for _, manifest := range config { for _, manifest := range config {
netName, exists, err := kl.networkContainerExists(&manifest) // Make sure we have a network container
netId, err := kl.getNetworkContainerId(&manifest)
if err != nil { if err != nil {
glog.Errorf("Failed to introspect network container. (%#v) Skipping container %s", err, manifest.Id) glog.Errorf("Failed to introspect network container. (%#v) Skipping container %s", err, manifest.Id)
continue continue
} }
if !exists { if netId == "" {
glog.Infof("Network container doesn't exist, creating") glog.Infof("Network container doesn't exist, creating")
netName, err = kl.createNetworkContainer(&manifest) netId, err = kl.createNetworkContainer(&manifest)
if err != nil { if err != nil {
glog.Errorf("Failed to create network container: %#v", err) glog.Errorf("Failed to create network container: %#v Skipping container %s", err, manifest.Id)
continue
} }
// Docker list prefixes '/' for some reason, so let's do that...
netName = "/" + netName
} }
desired[netName] = true dockerIdsToKeep[netId] = true
for _, element := range manifest.Containers {
var exists bool for _, container := range manifest.Containers {
exists, actualName, err := kl.ContainerExists(&manifest, &element) containerId, err := kl.getContainerId(&manifest, &container)
if err != nil { if err != nil {
glog.Errorf("Error detecting container: %#v skipping.", err) glog.Errorf("Error detecting container: %#v skipping.", err)
continue continue
} }
if !exists { if containerId == "" {
glog.Infof("%#v doesn't exist, creating", element) glog.Infof("%#v doesn't exist, creating", container)
kl.DockerPuller.Pull(element.Image) kl.DockerPuller.Pull(container.Image)
if err != nil { if err != nil {
glog.Errorf("Error pulling container: %#v", err) glog.Errorf("Error pulling container: %#v", err)
continue continue
} }
// netName has the '/' prefix, so slice it off containerId, err = kl.runContainer(&manifest, &container, "container:"+netId)
networkContainer := netName[1:]
actualName, err = kl.RunContainer(&manifest, &element, "container:"+networkContainer)
// For some reason, list gives back names that start with '/'
actualName = "/" + actualName
if err != nil { if err != nil {
// TODO(bburns) : Perhaps blacklist a container after N failures? // TODO(bburns) : Perhaps blacklist a container after N failures?
glog.Errorf("Error creating container: %#v", err) glog.Errorf("Error creating container: %#v", err)
desired[actualName] = true
continue continue
} }
} else { } else {
glog.V(1).Infof("%#v exists as %v", element.Name, actualName) glog.V(1).Infof("%#v exists as %v", container.Name, containerId)
} }
desired[actualName] = true dockerIdsToKeep[containerId] = true
} }
} }
existingContainers, _ := kl.ListContainers()
glog.Infof("Existing: %#v Desired: %#v", existingContainers, desired) // Kill any containers we don't need
for _, container := range existingContainers { existingContainers, err := kl.getDockerContainers()
// Skip containers that we didn't create to allow users to manually if err != nil {
// spin up their own containers if they want. glog.Errorf("Error listing containers: %#v", err)
if !strings.HasPrefix(container, "/"+containerNamePrefix+"--") { return err
continue
} }
if !desired[container] { for id, container := range existingContainers {
glog.Infof("Killing: %s", container) if !dockerIdsToKeep[id] {
err = kl.KillContainer(container) glog.Infof("Killing: %s", id)
err = kl.killContainer(container)
if err != nil { if err != nil {
glog.Errorf("Error killing container: %#v", err) glog.Errorf("Error killing container: %#v", err)
} }
@ -772,8 +726,31 @@ func (kl *Kubelet) RunSyncLoop(updateChannel <-chan manifestUpdate, handler Sync
} }
} }
// getContainerIdFromName looks at the list of containers on the machine and returns the ID of the container whose name
// matches 'name'. It returns the name of the container, or empty string, if the container isn't found.
// it returns true if the container is found, false otherwise, and any error that occurs.
// TODO: This functions exists to support GetContainerInfo and GetContainerStats
// It should be removed once those two functions start taking proper pod.IDs
func (kl *Kubelet) getContainerIdFromName(name string) (string, bool, error) {
containerList, err := kl.DockerClient.ListContainers(docker.ListContainersOptions{})
if err != nil {
return "", false, err
}
for _, value := range containerList {
if strings.Contains(value.Names[0], name) {
return value.ID, true, nil
}
}
return "", false, nil
}
// Returns docker info for a container
func (kl *Kubelet) GetContainerInfo(name string) (string, error) { func (kl *Kubelet) GetContainerInfo(name string) (string, error) {
info, err := kl.DockerClient.InspectContainer(name) dockerId, found, err := kl.getContainerIdFromName(name)
if err != nil || !found {
return "{}", err
}
info, err := kl.DockerClient.InspectContainer(dockerId)
if err != nil { if err != nil {
return "{}", err return "{}", err
} }
@ -781,16 +758,17 @@ func (kl *Kubelet) GetContainerInfo(name string) (string, error) {
return string(data), err return string(data), err
} }
//Returns stats (from Cadvisor) for a container
func (kl *Kubelet) GetContainerStats(name string) (*api.ContainerStats, error) { func (kl *Kubelet) GetContainerStats(name string) (*api.ContainerStats, error) {
if kl.CadvisorClient == nil { if kl.CadvisorClient == nil {
return nil, nil return nil, nil
} }
id, found, err := kl.GetContainerID(name) dockerId, found, err := kl.getContainerIdFromName(name)
if err != nil || !found { if err != nil || !found {
return nil, err return nil, err
} }
info, err := kl.CadvisorClient.ContainerInfo(fmt.Sprintf("/docker/%v", id)) info, err := kl.CadvisorClient.ContainerInfo(fmt.Sprintf("/docker/%v", dockerId))
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -35,7 +35,6 @@ type KubeletServer struct {
// kubeletInterface contains all the kubelet methods required by the server. // kubeletInterface contains all the kubelet methods required by the server.
// For testablitiy. // For testablitiy.
type kubeletInterface interface { type kubeletInterface interface {
GetContainerID(name string) (string, bool, error)
GetContainerStats(name string) (*api.ContainerStats, error) GetContainerStats(name string) (*api.ContainerStats, error)
GetContainerInfo(name string) (string, error) GetContainerInfo(name string) (string, error)
} }
@ -78,6 +77,7 @@ func (s *KubeletServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
s.UpdateChannel <- manifestUpdate{httpServerSource, manifests} s.UpdateChannel <- manifestUpdate{httpServerSource, manifests}
} }
case u.Path == "/containerStats": case u.Path == "/containerStats":
// NOTE: The master appears to pass a Pod.ID
container := u.Query().Get("container") container := u.Query().Get("container")
if len(container) == 0 { if len(container) == 0 {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
@ -105,27 +105,23 @@ func (s *KubeletServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
w.Header().Add("Content-type", "application/json") w.Header().Add("Content-type", "application/json")
w.Write(data) w.Write(data)
case u.Path == "/containerInfo": case u.Path == "/containerInfo":
// NOTE: The master appears to pass a Pod.ID
// The server appears to pass a Pod.ID
container := u.Query().Get("container") container := u.Query().Get("container")
if len(container) == 0 { if len(container) == 0 {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, "Missing container selector arg.") fmt.Fprint(w, "Missing container selector arg.")
return return
} }
id, found, err := s.Kubelet.GetContainerID(container) data, err := s.Kubelet.GetContainerInfo(container)
if !found {
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, "{}")
return
}
body, err := s.Kubelet.GetContainerInfo(id)
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "Internal Error: %#v", err) fmt.Fprintf(w, "Internal Error: %#v", err)
return return
} }
w.Header().Add("Content-type", "application/json")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
fmt.Fprint(w, body) w.Header().Add("Content-type", "application/json")
fmt.Fprint(w, data)
default: default:
w.WriteHeader(http.StatusNotFound) w.WriteHeader(http.StatusNotFound)
fmt.Fprint(w, "Not found.") fmt.Fprint(w, "Not found.")

View File

@ -32,7 +32,6 @@ import (
type fakeKubelet struct { type fakeKubelet struct {
infoFunc func(name string) (string, error) infoFunc func(name string) (string, error)
idFunc func(name string) (string, bool, error)
statsFunc func(name string) (*api.ContainerStats, error) statsFunc func(name string) (*api.ContainerStats, error)
} }
@ -40,10 +39,6 @@ func (fk *fakeKubelet) GetContainerInfo(name string) (string, error) {
return fk.infoFunc(name) return fk.infoFunc(name)
} }
func (fk *fakeKubelet) GetContainerID(name string) (string, bool, error) {
return fk.idFunc(name)
}
func (fk *fakeKubelet) GetContainerStats(name string) (*api.ContainerStats, error) { func (fk *fakeKubelet) GetContainerStats(name string) (*api.ContainerStats, error) {
return fk.statsFunc(name) return fk.statsFunc(name)
} }
@ -122,12 +117,6 @@ func TestContainers(t *testing.T) {
func TestContainerInfo(t *testing.T) { func TestContainerInfo(t *testing.T) {
fw := makeServerTest() fw := makeServerTest()
expected := "good container info string" expected := "good container info string"
fw.fakeKubelet.idFunc = func(name string) (string, bool, error) {
if name == "goodcontainer" {
return name, true, nil
}
return "", false, fmt.Errorf("bad container")
}
fw.fakeKubelet.infoFunc = func(name string) (string, error) { fw.fakeKubelet.infoFunc = func(name string) (string, error) {
if name == "goodcontainer" { if name == "goodcontainer" {
return expected, nil return expected, nil

View File

@ -133,7 +133,7 @@ func TestContainerManifestNaming(t *testing.T) {
verifyPackUnpack(t, "_m___anifest", "-_-container") verifyPackUnpack(t, "_m___anifest", "-_-container")
} }
func TestContainerExists(t *testing.T) { func TestGetContainerId(t *testing.T) {
fakeDocker := FakeDockerClient{ fakeDocker := FakeDockerClient{
err: nil, err: nil,
} }
@ -149,19 +149,21 @@ func TestContainerExists(t *testing.T) {
} }
fakeDocker.containerList = []docker.APIContainers{ fakeDocker.containerList = []docker.APIContainers{
{ {
ID: "foobar",
Names: []string{"/k8s--foo--qux--1234"}, Names: []string{"/k8s--foo--qux--1234"},
}, },
{ {
Names: []string{"/k8s--bar--qux--1234"}, ID: "barbar",
Names: []string{"/k8s--bar--qux--2565"},
}, },
} }
fakeDocker.container = &docker.Container{ fakeDocker.container = &docker.Container{
ID: "foobar", ID: "foobar",
} }
exists, _, err := kubelet.ContainerExists(&manifest, &container) id, err := kubelet.getContainerId(&manifest, &container)
verifyCalls(t, fakeDocker, []string{"list", "list", "inspect"}) verifyCalls(t, fakeDocker, []string{"list"})
if !exists { if id == "" {
t.Errorf("Failed to find container %#v", container) t.Errorf("Failed to find container %#v", container)
} }
if err != nil { if err != nil {
@ -170,113 +172,24 @@ func TestContainerExists(t *testing.T) {
fakeDocker.clearCalls() fakeDocker.clearCalls()
missingManifest := api.ContainerManifest{Id: "foobar"} missingManifest := api.ContainerManifest{Id: "foobar"}
exists, _, err = kubelet.ContainerExists(&missingManifest, &container) id, err = kubelet.getContainerId(&missingManifest, &container)
verifyCalls(t, fakeDocker, []string{"list"}) verifyCalls(t, fakeDocker, []string{"list"})
if exists { if id != "" {
t.Errorf("Failed to not find container %#v, missingManifest") t.Errorf("Failed to not find container %#v", missingManifest)
} }
} }
func TestGetContainerID(t *testing.T) {
fakeDocker := FakeDockerClient{
err: nil,
}
kubelet := Kubelet{
DockerClient: &fakeDocker,
DockerPuller: &FakeDockerPuller{},
}
fakeDocker.containerList = []docker.APIContainers{
{
Names: []string{"foo"},
ID: "1234",
},
{
Names: []string{"bar"},
ID: "4567",
},
}
id, found, err := kubelet.GetContainerID("foo")
verifyBoolean(t, true, found)
verifyStringEquals(t, id, "1234")
verifyNoError(t, err)
verifyCalls(t, fakeDocker, []string{"list"})
fakeDocker.clearCalls()
id, found, err = kubelet.GetContainerID("bar")
verifyBoolean(t, true, found)
verifyStringEquals(t, id, "4567")
verifyNoError(t, err)
verifyCalls(t, fakeDocker, []string{"list"})
fakeDocker.clearCalls()
id, found, err = kubelet.GetContainerID("NotFound")
verifyBoolean(t, false, found)
verifyNoError(t, err)
verifyCalls(t, fakeDocker, []string{"list"})
}
func TestGetContainerByName(t *testing.T) {
fakeDocker := FakeDockerClient{
err: nil,
}
kubelet := Kubelet{
DockerClient: &fakeDocker,
DockerPuller: &FakeDockerPuller{},
}
fakeDocker.containerList = []docker.APIContainers{
{
Names: []string{"foo"},
},
{
Names: []string{"bar"},
},
}
fakeDocker.container = &docker.Container{
ID: "foobar",
}
container, err := kubelet.GetContainerByName("foo")
verifyCalls(t, fakeDocker, []string{"list", "inspect"})
if container == nil {
t.Errorf("Unexpected nil container")
}
verifyStringEquals(t, container.ID, "foobar")
verifyNoError(t, err)
}
func TestListContainers(t *testing.T) {
fakeDocker := FakeDockerClient{
err: nil,
}
kubelet := Kubelet{
DockerClient: &fakeDocker,
DockerPuller: &FakeDockerPuller{},
}
fakeDocker.containerList = []docker.APIContainers{
{
Names: []string{"foo"},
},
{
Names: []string{"bar"},
},
}
containers, err := kubelet.ListContainers()
verifyStringArrayEquals(t, containers, []string{"foo", "bar"})
verifyNoError(t, err)
verifyCalls(t, fakeDocker, []string{"list"})
}
func TestKillContainerWithError(t *testing.T) { func TestKillContainerWithError(t *testing.T) {
fakeDocker := FakeDockerClient{ fakeDocker := FakeDockerClient{
err: fmt.Errorf("sample error"), err: fmt.Errorf("sample error"),
containerList: []docker.APIContainers{ containerList: []docker.APIContainers{
{ {
Names: []string{"foo"}, ID: "1234",
Names: []string{"/k8s--foo--qux--1234"},
}, },
{ {
Names: []string{"bar"}, ID: "5678",
Names: []string{"/k8s--bar--qux--5678"},
}, },
}, },
} }
@ -284,9 +197,9 @@ func TestKillContainerWithError(t *testing.T) {
DockerClient: &fakeDocker, DockerClient: &fakeDocker,
DockerPuller: &FakeDockerPuller{}, DockerPuller: &FakeDockerPuller{},
} }
err := kubelet.KillContainer("foo") err := kubelet.killContainer(fakeDocker.containerList[0])
verifyError(t, err) verifyError(t, err)
verifyCalls(t, fakeDocker, []string{"list"}) verifyCalls(t, fakeDocker, []string{"stop"})
} }
func TestKillContainer(t *testing.T) { func TestKillContainer(t *testing.T) {
@ -299,19 +212,21 @@ func TestKillContainer(t *testing.T) {
} }
fakeDocker.containerList = []docker.APIContainers{ fakeDocker.containerList = []docker.APIContainers{
{ {
Names: []string{"foo"}, ID: "1234",
Names: []string{"/k8s--foo--qux--1234"},
}, },
{ {
Names: []string{"bar"}, ID: "5678",
Names: []string{"/k8s--bar--qux--5678"},
}, },
} }
fakeDocker.container = &docker.Container{ fakeDocker.container = &docker.Container{
ID: "foobar", ID: "foobar",
} }
err := kubelet.KillContainer("foo") err := kubelet.killContainer(fakeDocker.containerList[0])
verifyNoError(t, err) verifyNoError(t, err)
verifyCalls(t, fakeDocker, []string{"list", "stop"}) verifyCalls(t, fakeDocker, []string{"stop"})
} }
func TestResponseToContainersNil(t *testing.T) { func TestResponseToContainersNil(t *testing.T) {
@ -491,14 +406,7 @@ func TestSyncManifestsDoesNothing(t *testing.T) {
}, },
}) })
expectNoError(t, err) expectNoError(t, err)
if len(fakeDocker.called) != 5 || verifyCalls(t, fakeDocker, []string{"list", "list", "list"})
fakeDocker.called[0] != "list" ||
fakeDocker.called[1] != "list" ||
fakeDocker.called[2] != "list" ||
fakeDocker.called[3] != "inspect" ||
fakeDocker.called[4] != "list" {
t.Errorf("Unexpected call sequence: %#v", fakeDocker.called)
}
} }
func TestSyncManifestsDeletes(t *testing.T) { func TestSyncManifestsDeletes(t *testing.T) {
@ -527,15 +435,11 @@ func TestSyncManifestsDeletes(t *testing.T) {
} }
err := kubelet.SyncManifests([]api.ContainerManifest{}) err := kubelet.SyncManifests([]api.ContainerManifest{})
expectNoError(t, err) expectNoError(t, err)
if len(fakeDocker.called) != 5 || verifyCalls(t, fakeDocker, []string{"list", "stop", "stop"})
fakeDocker.called[0] != "list" || if len(fakeDocker.stopped) != 2 ||
fakeDocker.called[1] != "list" ||
fakeDocker.called[2] != "stop" ||
fakeDocker.called[3] != "list" ||
fakeDocker.called[4] != "stop" ||
fakeDocker.stopped[0] != "1234" || fakeDocker.stopped[0] != "1234" ||
fakeDocker.stopped[1] != "9876" { fakeDocker.stopped[1] != "9876" {
t.Errorf("Unexpected call sequence: %#v %s", fakeDocker.called, fakeDocker.stopped) t.Errorf("Unexpected sequence of stopped containers: %s", fakeDocker.stopped)
} }
} }