// 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 }
// 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 }
func (r *FakeRuntimeService) ListContainers(filter *runtimeApi.ContainerFilter) ([]*runtimeApi.Container, error) { r.Lock() defer r.Unlock() r.Called = append(r.Called, "ListContainers") result := make([]*runtimeApi.Container, 0) for _, s := range r.Containers { if filter != nil { if filter.Id != nil && filter.GetId() != s.GetId() { continue } if filter.Name != nil && filter.GetName() != s.Metadata.GetName() { continue } if filter.PodSandboxId != nil && filter.GetPodSandboxId() != s.SandboxID { continue } if filter.State != nil && filter.GetState() != s.GetState() { continue } if filter.LabelSelector != nil && !filterInLabels(filter.LabelSelector, s.GetLabels()) { continue } } result = append(result, &runtimeApi.Container{ Id: s.Id, Metadata: s.Metadata, State: s.State, Image: s.Image, ImageRef: s.ImageRef, Labels: s.Labels, }) } return result, nil }