// Runs containerGCTest using the docker runtime. func dockerContainerGCTest(f *framework.Framework, test testRun) { var runtime docker.DockerInterface BeforeEach(func() { runtime = docker.ConnectToDockerOrDie(defaultDockerEndpoint, defaultRuntimeRequestTimeoutDuration, defaultImagePullProgressDeadline) }) for _, pod := range test.testPods { // Initialize the getContainerNames function to use the dockertools api thisPrefix := pod.containerPrefix pod.getContainerNames = func() ([]string, error) { relevantContainers := []string{} dockerContainers, err := docker.GetKubeletDockerContainers(runtime, true) if err != nil { return relevantContainers, err } for _, container := range dockerContainers { // only look for containers from this testspec if strings.Contains(container.Names[0], thisPrefix) { relevantContainers = append(relevantContainers, container.Names[0]) } } return relevantContainers, nil } } containerGCTest(f, test) }
// Get all containers that are evictable. Evictable containers are: not running // and created more than MinAge ago. func (cgc *realContainerGC) evictableContainers() (containersByEvictUnit, []containerGCInfo, error) { containers, err := dockertools.GetKubeletDockerContainers(cgc.dockerClient, true) if err != nil { return containersByEvictUnit{}, []containerGCInfo{}, err } unidentifiedContainers := make([]containerGCInfo, 0) evictUnits := make(containersByEvictUnit) newestGCTime := time.Now().Add(-cgc.policy.MinAge) for _, container := range containers { // Prune out running containers. data, err := cgc.dockerClient.InspectContainer(container.ID) if err != nil { // Container may have been removed already, skip. continue } else if data.State.Running { continue } else if newestGCTime.Before(data.Created) { continue } containerInfo := containerGCInfo{ id: container.ID, name: container.Names[0], createTime: data.Created, } containerName, _, err := dockertools.ParseDockerName(container.Names[0]) if err != nil { unidentifiedContainers = append(unidentifiedContainers, containerInfo) } else { key := evictUnit{ uid: containerName.PodUID, name: containerName.ContainerName, } containerInfo.podNameWithNamespace = containerName.PodFullName containerInfo.containerName = containerName.ContainerName evictUnits[key] = append(evictUnits[key], containerInfo) } } // Sort the containers by age. for uid := range evictUnits { sort.Sort(byCreated(evictUnits[uid])) } return evictUnits, unidentifiedContainers, nil }
// Destroy existing k8s containers func (k *Executor) killKubeletContainers() { if containers, err := dockertools.GetKubeletDockerContainers(k.dockerClient, true); err == nil { opts := dockertypes.ContainerRemoveOptions{ RemoveVolumes: true, Force: true, } for _, container := range containers { log.V(2).Infof("Removing container: %v", container.ID) if err := k.dockerClient.RemoveContainer(container.ID, opts); err != nil { log.Warning(err) } } } else { log.Warningf("Failed to list kubelet docker containers: %v", err) } }