Skip to content

Commit

Permalink
Merge pull request kubernetes#71 from brendandburns/container_info
Browse files Browse the repository at this point in the history
Fix some problems in container info handling if the container's no present.
  • Loading branch information
brendandburns committed Jun 13, 2014
2 parents 4a8c53e + c36a789 commit f053a49
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 18 deletions.
2 changes: 1 addition & 1 deletion cmd/cloudcfg/cloudcfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func main() {
}
body, err := cloudcfg.DoRequest(request, auth)
if err != nil {
log.Fatalf("Error: %#v", err)
log.Fatalf("Error: %#v %s", err, body)
}
err = printer.Print(body, os.Stdout)
if err != nil {
Expand Down
23 changes: 17 additions & 6 deletions pkg/kubelet/kubelet.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,26 +155,32 @@ func (kl *Kubelet) ContainerExists(manifest *api.ContainerManifest, container *a
return false, "", nil
}

func (kl *Kubelet) GetContainerID(name string) (string, error) {
// 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 "", err
return "", false, err
}
for _, value := range containerList {
if strings.Contains(value.Names[0], name) {
return value.ID, nil
return value.ID, true, nil
}
}
return "", fmt.Errorf("couldn't find name: %s", name)
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, err := kl.GetContainerID(name)
id, found, err := kl.GetContainerID(name)
if err != nil {
return nil, err
}
if !found {
return nil, nil
}
return kl.DockerClient.InspectContainer(id)
}

Expand Down Expand Up @@ -317,10 +323,15 @@ func (kl *Kubelet) RunContainer(manifest *api.ContainerManifest, container *api.
}

func (kl *Kubelet) KillContainer(name string) error {
id, err := kl.GetContainerID(name)
id, found, err := kl.GetContainerID(name)
if err != nil {
return err
}
if !found {
// This is weird, but not an error, so yell and then return nil
log.Printf("Couldn't find container: %s", name)
return nil
}
err = kl.DockerClient.StopContainer(id, 10)
manifestId, containerName := dockerNameToManifestAndContainer(name)
kl.LogEvent(&api.Event{
Expand Down
9 changes: 7 additions & 2 deletions pkg/kubelet/kubelet_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type KubeletServer struct {
// kubeletInterface contains all the kubelet methods required by the server.
// For testablitiy.
type kubeletInterface interface {
GetContainerID(name string) (string, error)
GetContainerID(name string) (string, bool, error)
GetContainerInfo(name string) (string, error)
}

Expand Down Expand Up @@ -71,7 +71,12 @@ func (s *KubeletServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
fmt.Fprint(w, "Missing container query arg.")
return
}
id, err := s.Kubelet.GetContainerID(container)
id, found, err := s.Kubelet.GetContainerID(container)
if (!found) {
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, "{}")
return
}
body, err := s.Kubelet.GetContainerInfo(id)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
Expand Down
10 changes: 5 additions & 5 deletions pkg/kubelet/kubelet_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ import (

type fakeKubelet struct {
infoFunc func(name string) (string, error)
idFunc func(name string) (string, error)
idFunc func(name string) (string, bool, error)
}

func (fk *fakeKubelet) GetContainerInfo(name string) (string, error) {
return fk.infoFunc(name)
}

func (fk *fakeKubelet) GetContainerID(name string) (string, error) {
func (fk *fakeKubelet) GetContainerID(name string) (string, bool, error) {
return fk.idFunc(name)
}

Expand Down Expand Up @@ -105,11 +105,11 @@ func TestContainer(t *testing.T) {
func TestContainerInfo(t *testing.T) {
fw := makeServerTest()
expected := "good container info string"
fw.fakeKubelet.idFunc = func(name string) (string, error) {
fw.fakeKubelet.idFunc = func(name string) (string, bool, error) {
if name == "goodcontainer" {
return name, nil
return name, true, nil
}
return "", fmt.Errorf("bad container")
return "", false, fmt.Errorf("bad container")
}
fw.fakeKubelet.infoFunc = func(name string) (string, error) {
if name == "goodcontainer" {
Expand Down
17 changes: 13 additions & 4 deletions pkg/kubelet/kubelet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,12 @@ func verifyPackUnpack(t *testing.T, manifestId, containerName string) {
}
}

func verifyBoolean(t *testing.T, expected, value bool) {
if expected != value {
t.Errorf("Unexpected boolean. Expected %s. Found %s", expected, value)
}
}

func TestContainerManifestNaming(t *testing.T) {
verifyPackUnpack(t, "manifest1234", "container5678")
verifyPackUnpack(t, "manifest--", "container__")
Expand Down Expand Up @@ -222,20 +228,23 @@ func TestGetContainerID(t *testing.T) {
},
}

id, err := kubelet.GetContainerID("foo")
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, err = kubelet.GetContainerID("bar")
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, err = kubelet.GetContainerID("NotFound")
verifyError(t, err)
id, found, err = kubelet.GetContainerID("NotFound")
verifyBoolean(t, false, found)
verifyNoError(t, err)
verifyCalls(t, fakeDocker, []string{"list"})
}

Expand Down

0 comments on commit f053a49

Please sign in to comment.