mirror of https://github.com/k3s-io/k3s
Merge pull request #71 from brendandburns/container_info
Fix some problems in container info handling if the container's no present.pull/6/head
commit
f053a49988
|
@ -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 {
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
@ -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{
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
@ -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" {
|
||||
|
|
|
@ -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__")
|
||||
|
@ -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"})
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue