mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 03:41:45 +00:00
Refactor StartContainer, StopContainer and RemoveContainer.
This commit is contained in:
parent
ba4a5ed39e
commit
d3d98b372b
@ -28,7 +28,7 @@ import (
|
|||||||
|
|
||||||
clientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
|
clientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
|
||||||
|
|
||||||
"github.com/fsouza/go-dockerclient"
|
dockertypes "github.com/docker/engine-api/types"
|
||||||
"github.com/gogo/protobuf/proto"
|
"github.com/gogo/protobuf/proto"
|
||||||
log "github.com/golang/glog"
|
log "github.com/golang/glog"
|
||||||
bindings "github.com/mesos/mesos-go/executor"
|
bindings "github.com/mesos/mesos-go/executor"
|
||||||
@ -655,14 +655,13 @@ func (k *Executor) doShutdown(driver bindings.ExecutorDriver) {
|
|||||||
// Destroy existing k8s containers
|
// Destroy existing k8s containers
|
||||||
func (k *Executor) killKubeletContainers() {
|
func (k *Executor) killKubeletContainers() {
|
||||||
if containers, err := dockertools.GetKubeletDockerContainers(k.dockerClient, true); err == nil {
|
if containers, err := dockertools.GetKubeletDockerContainers(k.dockerClient, true); err == nil {
|
||||||
opts := docker.RemoveContainerOptions{
|
opts := dockertypes.ContainerRemoveOptions{
|
||||||
RemoveVolumes: true,
|
RemoveVolumes: true,
|
||||||
Force: true,
|
Force: true,
|
||||||
}
|
}
|
||||||
for _, container := range containers {
|
for _, container := range containers {
|
||||||
opts.ID = container.ID
|
log.V(2).Infof("Removing container: %v", container.ID)
|
||||||
log.V(2).Infof("Removing container: %v", opts.ID)
|
if err := k.dockerClient.RemoveContainer(container.ID, opts); err != nil {
|
||||||
if err := k.dockerClient.RemoveContainer(opts); err != nil {
|
|
||||||
log.Warning(err)
|
log.Warning(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,7 @@ import (
|
|||||||
"sort"
|
"sort"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
docker "github.com/fsouza/go-dockerclient"
|
dockertypes "github.com/docker/engine-api/types"
|
||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
|
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
|
||||||
"k8s.io/kubernetes/pkg/types"
|
"k8s.io/kubernetes/pkg/types"
|
||||||
@ -111,7 +111,7 @@ func (cgc *containerGC) removeOldestN(containers []containerGCInfo, toRemove int
|
|||||||
// Remove from oldest to newest (last to first).
|
// Remove from oldest to newest (last to first).
|
||||||
numToKeep := len(containers) - toRemove
|
numToKeep := len(containers) - toRemove
|
||||||
for i := numToKeep; i < len(containers); i++ {
|
for i := numToKeep; i < len(containers); i++ {
|
||||||
err := cgc.client.RemoveContainer(docker.RemoveContainerOptions{ID: containers[i].id, RemoveVolumes: true})
|
err := cgc.client.RemoveContainer(containers[i].id, dockertypes.ContainerRemoveOptions{RemoveVolumes: true})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
glog.Warningf("Failed to remove dead container %q: %v", containers[i].name, err)
|
glog.Warningf("Failed to remove dead container %q: %v", containers[i].name, err)
|
||||||
}
|
}
|
||||||
@ -195,7 +195,7 @@ func (cgc *containerGC) GarbageCollect(gcPolicy kubecontainer.ContainerGCPolicy)
|
|||||||
// Remove unidentified containers.
|
// Remove unidentified containers.
|
||||||
for _, container := range unidentifiedContainers {
|
for _, container := range unidentifiedContainers {
|
||||||
glog.Infof("Removing unidentified dead container %q with ID %q", container.name, container.id)
|
glog.Infof("Removing unidentified dead container %q with ID %q", container.name, container.id)
|
||||||
err = cgc.client.RemoveContainer(docker.RemoveContainerOptions{ID: container.id, RemoveVolumes: true})
|
err = cgc.client.RemoveContainer(container.id, dockertypes.ContainerRemoveOptions{RemoveVolumes: true})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
glog.Warningf("Failed to remove unidentified dead container %q: %v", container.name, err)
|
glog.Warningf("Failed to remove unidentified dead container %q: %v", container.name, err)
|
||||||
}
|
}
|
||||||
|
@ -61,9 +61,9 @@ type DockerInterface interface {
|
|||||||
ListContainers(options dockertypes.ContainerListOptions) ([]dockertypes.Container, error)
|
ListContainers(options dockertypes.ContainerListOptions) ([]dockertypes.Container, error)
|
||||||
InspectContainer(id string) (*dockertypes.ContainerJSON, error)
|
InspectContainer(id string) (*dockertypes.ContainerJSON, error)
|
||||||
CreateContainer(dockertypes.ContainerCreateConfig) (*dockertypes.ContainerCreateResponse, error)
|
CreateContainer(dockertypes.ContainerCreateConfig) (*dockertypes.ContainerCreateResponse, error)
|
||||||
StartContainer(id string, hostConfig *docker.HostConfig) error
|
StartContainer(id string) error
|
||||||
StopContainer(id string, timeout uint) error
|
StopContainer(id string, timeout int) error
|
||||||
RemoveContainer(opts docker.RemoveContainerOptions) error
|
RemoveContainer(id string, opts dockertypes.ContainerRemoveOptions) error
|
||||||
InspectImage(image string) (*docker.Image, error)
|
InspectImage(image string) (*docker.Image, error)
|
||||||
ListImages(opts docker.ListImagesOptions) ([]docker.APIImages, error)
|
ListImages(opts docker.ListImagesOptions) ([]docker.APIImages, error)
|
||||||
PullImage(opts docker.PullImageOptions, auth docker.AuthConfiguration) error
|
PullImage(opts docker.PullImageOptions, auth docker.AuthConfiguration) error
|
||||||
|
@ -328,11 +328,7 @@ func (f *FakeDockerClient) CreateContainer(c dockertypes.ContainerCreateConfig)
|
|||||||
|
|
||||||
// StartContainer is a test-spy implementation of DockerInterface.StartContainer.
|
// StartContainer is a test-spy implementation of DockerInterface.StartContainer.
|
||||||
// It adds an entry "start" to the internal method call record.
|
// It adds an entry "start" to the internal method call record.
|
||||||
// The HostConfig at StartContainer will be deprecated from docker 1.10. Now in
|
func (f *FakeDockerClient) StartContainer(id string) error {
|
||||||
// docker manager the HostConfig is set when ContainerCreate().
|
|
||||||
// TODO(random-liu): Remove the HostConfig here when it is completely removed in
|
|
||||||
// docker 1.12.
|
|
||||||
func (f *FakeDockerClient) StartContainer(id string, _ *docker.HostConfig) error {
|
|
||||||
f.Lock()
|
f.Lock()
|
||||||
defer f.Unlock()
|
defer f.Unlock()
|
||||||
f.called = append(f.called, "start")
|
f.called = append(f.called, "start")
|
||||||
@ -355,7 +351,7 @@ func (f *FakeDockerClient) StartContainer(id string, _ *docker.HostConfig) error
|
|||||||
|
|
||||||
// StopContainer is a test-spy implementation of DockerInterface.StopContainer.
|
// StopContainer is a test-spy implementation of DockerInterface.StopContainer.
|
||||||
// It adds an entry "stop" to the internal method call record.
|
// It adds an entry "stop" to the internal method call record.
|
||||||
func (f *FakeDockerClient) StopContainer(id string, timeout uint) error {
|
func (f *FakeDockerClient) StopContainer(id string, timeout int) error {
|
||||||
f.Lock()
|
f.Lock()
|
||||||
defer f.Unlock()
|
defer f.Unlock()
|
||||||
f.called = append(f.called, "stop")
|
f.called = append(f.called, "stop")
|
||||||
@ -393,7 +389,7 @@ func (f *FakeDockerClient) StopContainer(id string, timeout uint) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *FakeDockerClient) RemoveContainer(opts docker.RemoveContainerOptions) error {
|
func (f *FakeDockerClient) RemoveContainer(id string, opts dockertypes.ContainerRemoveOptions) error {
|
||||||
f.Lock()
|
f.Lock()
|
||||||
defer f.Unlock()
|
defer f.Unlock()
|
||||||
f.called = append(f.called, "remove")
|
f.called = append(f.called, "remove")
|
||||||
@ -402,10 +398,10 @@ func (f *FakeDockerClient) RemoveContainer(opts docker.RemoveContainerOptions) e
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
for i := range f.ExitedContainerList {
|
for i := range f.ExitedContainerList {
|
||||||
if f.ExitedContainerList[i].ID == opts.ID {
|
if f.ExitedContainerList[i].ID == id {
|
||||||
delete(f.ContainerMap, opts.ID)
|
delete(f.ContainerMap, id)
|
||||||
f.ExitedContainerList = append(f.ExitedContainerList[:i], f.ExitedContainerList[i+1:]...)
|
f.ExitedContainerList = append(f.ExitedContainerList[:i], f.ExitedContainerList[i+1:]...)
|
||||||
f.Removed = append(f.Removed, opts.ID)
|
f.Removed = append(f.Removed, id)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -423,7 +419,7 @@ func (f *FakeDockerClient) Logs(opts docker.LogsOptions) error {
|
|||||||
return f.popError("logs")
|
return f.popError("logs")
|
||||||
}
|
}
|
||||||
|
|
||||||
// PullImage is a test-spy implementation of DockerInterface.StopContainer.
|
// PullImage is a test-spy implementation of DockerInterface.PullImage.
|
||||||
// It adds an entry "pull" to the internal method call record.
|
// It adds an entry "pull" to the internal method call record.
|
||||||
func (f *FakeDockerClient) PullImage(opts docker.PullImageOptions, auth docker.AuthConfiguration) error {
|
func (f *FakeDockerClient) PullImage(opts docker.PullImageOptions, auth docker.AuthConfiguration) error {
|
||||||
f.Lock()
|
f.Lock()
|
||||||
|
@ -76,16 +76,16 @@ func (in instrumentedDockerInterface) CreateContainer(opts dockertypes.Container
|
|||||||
return out, err
|
return out, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (in instrumentedDockerInterface) StartContainer(id string, hostConfig *docker.HostConfig) error {
|
func (in instrumentedDockerInterface) StartContainer(id string) error {
|
||||||
const operation = "start_container"
|
const operation = "start_container"
|
||||||
defer recordOperation(operation, time.Now())
|
defer recordOperation(operation, time.Now())
|
||||||
|
|
||||||
err := in.client.StartContainer(id, hostConfig)
|
err := in.client.StartContainer(id)
|
||||||
recordError(operation, err)
|
recordError(operation, err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (in instrumentedDockerInterface) StopContainer(id string, timeout uint) error {
|
func (in instrumentedDockerInterface) StopContainer(id string, timeout int) error {
|
||||||
const operation = "stop_container"
|
const operation = "stop_container"
|
||||||
defer recordOperation(operation, time.Now())
|
defer recordOperation(operation, time.Now())
|
||||||
|
|
||||||
@ -94,11 +94,11 @@ func (in instrumentedDockerInterface) StopContainer(id string, timeout uint) err
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (in instrumentedDockerInterface) RemoveContainer(opts docker.RemoveContainerOptions) error {
|
func (in instrumentedDockerInterface) RemoveContainer(id string, opts dockertypes.ContainerRemoveOptions) error {
|
||||||
const operation = "remove_container"
|
const operation = "remove_container"
|
||||||
defer recordOperation(operation, time.Now())
|
defer recordOperation(operation, time.Now())
|
||||||
|
|
||||||
err := in.client.RemoveContainer(opts)
|
err := in.client.RemoveContainer(id, opts)
|
||||||
recordError(operation, err)
|
recordError(operation, err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -132,22 +132,18 @@ func (d *kubeDockerClient) CreateContainer(opts dockertypes.ContainerCreateConfi
|
|||||||
return &createResp, nil
|
return &createResp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO(random-liu): The HostConfig at container start is deprecated, will remove this in the following refactoring.
|
func (d *kubeDockerClient) StartContainer(id string) error {
|
||||||
func (d *kubeDockerClient) StartContainer(id string, _ *docker.HostConfig) error {
|
|
||||||
return d.client.ContainerStart(getDefaultContext(), id)
|
return d.client.ContainerStart(getDefaultContext(), id)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stopping an already stopped container will not cause an error in engine-api.
|
// Stopping an already stopped container will not cause an error in engine-api.
|
||||||
func (d *kubeDockerClient) StopContainer(id string, timeout uint) error {
|
func (d *kubeDockerClient) StopContainer(id string, timeout int) error {
|
||||||
return d.client.ContainerStop(getDefaultContext(), id, int(timeout))
|
return d.client.ContainerStop(getDefaultContext(), id, timeout)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *kubeDockerClient) RemoveContainer(opts docker.RemoveContainerOptions) error {
|
func (d *kubeDockerClient) RemoveContainer(id string, opts dockertypes.ContainerRemoveOptions) error {
|
||||||
return d.client.ContainerRemove(getDefaultContext(), dockertypes.ContainerRemoveOptions{
|
opts.ContainerID = id
|
||||||
ContainerID: opts.ID,
|
return d.client.ContainerRemove(getDefaultContext(), opts)
|
||||||
RemoveVolumes: opts.RemoveVolumes,
|
|
||||||
Force: opts.Force,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *kubeDockerClient) InspectImage(image string) (*docker.Image, error) {
|
func (d *kubeDockerClient) InspectImage(image string) (*docker.Image, error) {
|
||||||
|
@ -674,7 +674,7 @@ func (dm *DockerManager) runContainer(
|
|||||||
}
|
}
|
||||||
dm.recorder.Eventf(ref, api.EventTypeNormal, kubecontainer.CreatedContainer, "Created container with docker id %v", utilstrings.ShortenString(createResp.ID, 12))
|
dm.recorder.Eventf(ref, api.EventTypeNormal, kubecontainer.CreatedContainer, "Created container with docker id %v", utilstrings.ShortenString(createResp.ID, 12))
|
||||||
|
|
||||||
if err = dm.client.StartContainer(createResp.ID, nil); err != nil {
|
if err = dm.client.StartContainer(createResp.ID); err != nil {
|
||||||
dm.recorder.Eventf(ref, api.EventTypeWarning, kubecontainer.FailedToStartContainer,
|
dm.recorder.Eventf(ref, api.EventTypeWarning, kubecontainer.FailedToStartContainer,
|
||||||
"Failed to start container with docker id %v with error: %v", utilstrings.ShortenString(createResp.ID, 12), err)
|
"Failed to start container with docker id %v with error: %v", utilstrings.ShortenString(createResp.ID, 12), err)
|
||||||
return kubecontainer.ContainerID{}, err
|
return kubecontainer.ContainerID{}, err
|
||||||
@ -1397,7 +1397,7 @@ func (dm *DockerManager) killContainer(containerID kubecontainer.ContainerID, co
|
|||||||
if gracePeriod < minimumGracePeriodInSeconds {
|
if gracePeriod < minimumGracePeriodInSeconds {
|
||||||
gracePeriod = minimumGracePeriodInSeconds
|
gracePeriod = minimumGracePeriodInSeconds
|
||||||
}
|
}
|
||||||
err := dm.client.StopContainer(ID, uint(gracePeriod))
|
err := dm.client.StopContainer(ID, int(gracePeriod))
|
||||||
if err == nil {
|
if err == nil {
|
||||||
glog.V(2).Infof("Container %q exited after %s", name, unversioned.Now().Sub(start.Time))
|
glog.V(2).Infof("Container %q exited after %s", name, unversioned.Now().Sub(start.Time))
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
Reference in New Issue
Block a user