// ListContainers lists all containers matching the filter. func (ds *dockerService) ListContainers(filter *runtimeApi.ContainerFilter) ([]*runtimeApi.Container, error) { opts := dockertypes.ContainerListOptions{All: true} opts.Filter = dockerfilters.NewArgs() if filter != nil { if filter.Name != nil { opts.Filter.Add("name", filter.GetName()) } if filter.Id != nil { opts.Filter.Add("id", filter.GetId()) } if filter.State != nil { opts.Filter.Add("status", toDockerContainerStatus(filter.GetState())) } if filter.PodSandboxId != nil { // TODO: implement this after sandbox functions are implemented. } if filter.LabelSelector != nil { for k, v := range filter.LabelSelector { opts.Filter.Add("label", fmt.Sprintf("%s=%s", k, v)) } } } containers, err := ds.client.ListContainers(opts) if err != nil { return nil, err } // Convert docker to runtime api containers. result := []*runtimeApi.Container{} for _, c := range containers { result = append(result, toRuntimeAPIContainer(&c)) } return result, nil }
// ListPodSandbox returns a list of Sandbox. func (ds *dockerService) ListPodSandbox(filter *runtimeApi.PodSandboxFilter) ([]*runtimeApi.PodSandbox, error) { // By default, list all containers whether they are running or not. opts := dockertypes.ContainerListOptions{All: true} filterOutReadySandboxes := false opts.Filter = dockerfilters.NewArgs() f := newDockerFilter(&opts.Filter) // Add filter to select only sandbox containers. f.AddLabel(containerTypeLabelKey, containerTypeLabelSandbox) if filter != nil { if filter.Id != nil { f.Add("id", filter.GetId()) } if filter.State != nil { if filter.GetState() == runtimeApi.PodSandBoxState_READY { // Only list running containers. opts.All = false } else { // runtimeApi.PodSandBoxState_NOTREADY can mean the // container is in any of the non-running state (e.g., created, // exited). We can't tell docker to filter out running // containers directly, so we'll need to filter them out // ourselves after getting the results. filterOutReadySandboxes = true } } if filter.LabelSelector != nil { for k, v := range filter.LabelSelector { f.AddLabel(k, v) } } } containers, err := ds.client.ListContainers(opts) if err != nil { return nil, err } // Convert docker containers to runtime api sandboxes. result := []*runtimeApi.PodSandbox{} for i := range containers { c := containers[i] converted, err := toRuntimeAPISandbox(&c) if err != nil { glog.V(5).Infof("Unable to convert docker to runtime API sandbox: %v", err) continue } if filterOutReadySandboxes && converted.GetState() == runtimeApi.PodSandBoxState_READY { continue } result = append(result, converted) } return result, nil }
// ListContainers lists all containers matching the filter. func (ds *dockerService) ListContainers(filter *runtimeApi.ContainerFilter) ([]*runtimeApi.Container, error) { opts := dockertypes.ContainerListOptions{All: true} opts.Filter = dockerfilters.NewArgs() f := newDockerFilter(&opts.Filter) // Add filter to get *only* (non-sandbox) containers. f.AddLabel(containerTypeLabelKey, containerTypeLabelContainer) if filter != nil { if filter.Id != nil { f.Add("id", filter.GetId()) } if filter.State != nil { f.Add("status", toDockerContainerStatus(filter.GetState())) } if filter.PodSandboxId != nil { f.AddLabel(sandboxIDLabelKey, *filter.PodSandboxId) } if filter.LabelSelector != nil { for k, v := range filter.LabelSelector { f.AddLabel(k, v) } } } containers, err := ds.client.ListContainers(opts) if err != nil { return nil, err } // Convert docker to runtime api containers. result := []*runtimeApi.Container{} for i := range containers { c := containers[i] converted, err := toRuntimeAPIContainer(&c) if err != nil { glog.V(5).Infof("Unable to convert docker to runtime API container: %v", err) continue } result = append(result, converted) } return result, nil }
// ListContainers lists all containers matching the filter. func (ds *dockerService) ListContainers(filter *runtimeApi.ContainerFilter) ([]*runtimeApi.Container, error) { opts := dockertypes.ContainerListOptions{All: true} opts.Filter = dockerfilters.NewArgs() f := newDockerFilter(&opts.Filter) if filter != nil { if filter.Id != nil { f.Add("id", filter.GetId()) } if filter.State != nil { f.Add("status", toDockerContainerStatus(filter.GetState())) } if filter.PodSandboxId != nil { // TODO: implement this after sandbox functions are implemented. } if filter.LabelSelector != nil { for k, v := range filter.LabelSelector { f.AddLabel(k, v) } } // Filter out sandbox containers. f.AddLabel(containerTypeLabelKey, containerTypeLabelContainer) } containers, err := ds.client.ListContainers(opts) if err != nil { return nil, err } // Convert docker to runtime api containers. result := []*runtimeApi.Container{} for _, c := range containers { if len(filter.GetName()) > 0 { _, _, _, containerName, _, err := parseContainerName(c.Names[0]) if err != nil || containerName != filter.GetName() { continue } } result = append(result, toRuntimeAPIContainer(&c)) } return result, nil }