// ListContainers lists all containers by filters.
ListContainers lists all containers by filters.
// ListContainers lists all containers by filters .
func (h *Runtime) ListContainers(filter *kubeapi.ContainerFilter) ([]*kubeapi.Container, error) {
	containerList, err := h.client.GetContainerList()
	if err != nil {
		glog.Errorf("Get container list failed: %v", err)
		return nil, err
	}
	containers := make([]*kubeapi.Container, 0, len(containerList))
	for _, c := range containerList {
		state := toKubeContainerState(c.Status)
		_, _, _, containerName, attempt, err := parseContainerName(strings.Replace(c.ContainerName, "/", "", -1))
		if err != nil {
			glog.V(3).Infof("ParseContainerName for %q failed (%v), assuming it is not managed by frakti", c.ContainerName, err)
			continue
		}
		if filter != nil {
			if filter.Id != "" && c.ContainerID != filter.Id {
				continue
			}
			if filter.PodSandboxId != "" && c.PodID != filter.PodSandboxId {
				continue
			}
			if filter.State != nil && state != filter.GetState().State {
				continue
			}
		}
		info, err := h.client.GetContainerInfo(c.ContainerID)
		if err != nil {
			glog.Errorf("Get container info for %s failed: %v", c.ContainerID, err)
			return nil, err
		}
		annotations := getAnnotationsFromLabels(info.Container.Labels)
		kubeletLabels := getKubeletLabels(info.Container.Labels)
		if filter != nil {
			if filter.LabelSelector != nil && !inMap(filter.LabelSelector, kubeletLabels) {
				continue
			}
		}
		containerMetadata := &kubeapi.ContainerMetadata{
			Name:    containerName,
			Attempt: attempt,
		}
		createdAtNano := info.CreatedAt * secondToNano
		containers = append(containers, &kubeapi.Container{
			Id:           c.ContainerID,
			PodSandboxId: c.PodID,
			CreatedAt:    createdAtNano,
			Metadata:     containerMetadata,
			Image:        &kubeapi.ImageSpec{Image: info.Container.Image},
			ImageRef:     info.Container.ImageID,
			State:        state,
			Labels:       kubeletLabels,
			Annotations:  annotations,
		})
	}
	return containers, nil
}
func ( h * Runtime ) ListContainers ( filter * kubeapi . ContainerFilter ) ( [ ] * kubeapi . Container , error ) { containerList , err := h . client . GetContainerList ( ) if err != nil { glog . Errorf ( " " , err ) return nil , err } containers := make ( [ ] * kubeapi . Container , 0 , len ( containerList ) ) for _ , c := range containerList { state := toKubeContainerState ( c . Status ) _ , _ , _ , containerName , attempt , err := parseContainerName ( strings . Replace ( c . ContainerName , " " , " " , - 1 ) ) if err != nil { glog . V ( 3 ) . Infof ( " " , c . ContainerName , err ) continue } if filter != nil { if filter . Id != " " && c . ContainerID != filter . Id { continue } if filter . PodSandboxId != " " && c . PodID != filter . PodSandboxId { continue } if filter . State != nil && state != filter . GetState ( ) . State { continue } } info , err := h . client . GetContainerInfo ( c . ContainerID ) if err != nil { glog . Errorf ( " " , c . ContainerID , err ) return nil , err } annotations := getAnnotationsFromLabels ( info . Container . Labels ) kubeletLabels := getKubeletLabels ( info . Container . Labels ) if filter != nil { if filter . LabelSelector != nil && ! inMap ( filter . LabelSelector , kubeletLabels ) { continue } } containerMetadata := & kubeapi . ContainerMetadata { Name : containerName , Attempt : attempt , } createdAtNano := info . CreatedAt * secondToNano containers = append ( containers , & kubeapi . Container { Id : c . ContainerID , PodSandboxId : c . PodID , CreatedAt : createdAtNano , Metadata : containerMetadata , Image : & kubeapi . ImageSpec { Image : info . Container . Image } , ImageRef : info . Container . ImageID , State : state , Labels : kubeletLabels , Annotations : annotations , } ) } return containers , nil }
ListContainers lists all containers by filters.
ListContainers lists all containers by filters .