Fix a bug if the container is non-existent.
This commit is contained in:
parent
fd36e8b9f2
commit
62f6291377
@ -152,26 +152,29 @@ func (kl *Kubelet) ContainerExists(manifest *api.ContainerManifest, container *a
|
||||
return false, "", nil
|
||||
}
|
||||
|
||||
func (kl *Kubelet) GetContainerID(name string) (string, error) {
|
||||
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)
|
||||
}
|
||||
|
||||
@ -300,10 +303,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)
|
||||
|
Loading…
Reference in New Issue
Block a user