// 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 *KubernetesExecutor) killKubeletContainers() { if containers, err := dockertools.GetKubeletDockerContainers(k.dockerClient, true); err == nil { opts := docker.RemoveContainerOptions{ RemoveVolumes: true, Force: true, } for _, container := range containers { opts.ID = container.ID log.V(2).Infof("Removing container: %v", opts.ID) if err := k.dockerClient.RemoveContainer(opts); err != nil { log.Warning(err) } } } else { log.Warningf("Failed to list kubelet docker containers: %v", err) } }