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
|
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{})
|
containerList, err := kl.DockerClient.ListContainers(docker.ListContainersOptions{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", false, err
|
||||||
}
|
}
|
||||||
for _, value := range containerList {
|
for _, value := range containerList {
|
||||||
if strings.Contains(value.Names[0], name) {
|
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.
|
// Get a container by name.
|
||||||
// returns the container data from Docker, or an error if one exists.
|
// returns the container data from Docker, or an error if one exists.
|
||||||
func (kl *Kubelet) GetContainerByName(name string) (*docker.Container, error) {
|
func (kl *Kubelet) GetContainerByName(name string) (*docker.Container, error) {
|
||||||
id, err := kl.GetContainerID(name)
|
id, found, err := kl.GetContainerID(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
if !found {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
return kl.DockerClient.InspectContainer(id)
|
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 {
|
func (kl *Kubelet) KillContainer(name string) error {
|
||||||
id, err := kl.GetContainerID(name)
|
id, found, err := kl.GetContainerID(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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)
|
err = kl.DockerClient.StopContainer(id, 10)
|
||||||
manifestId, containerName := dockerNameToManifestAndContainer(name)
|
manifestId, containerName := dockerNameToManifestAndContainer(name)
|
||||||
kl.LogEvent(&api.Event{
|
kl.LogEvent(&api.Event{
|
||||||
|
@ -34,7 +34,7 @@ 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, error)
|
GetContainerID(name string) (string, bool, error)
|
||||||
GetContainerInfo(name string) (string, 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.")
|
fmt.Fprint(w, "Missing container query arg.")
|
||||||
return
|
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)
|
body, err := s.Kubelet.GetContainerInfo(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
Loading…
Reference in New Issue
Block a user