func ListImages()

in pkg/minikube/machine/cache_images.go [674:771]


func ListImages(profile *config.Profile, format string) error {
	api, err := NewAPIClient()
	if err != nil {
		return errors.Wrap(err, "error creating api client")
	}
	defer api.Close()

	pName := profile.Name

	c, err := config.Load(pName)
	if err != nil {
		klog.Errorf("Failed to load profile %q: %v", pName, err)
		return errors.Wrapf(err, "error loading config for profile :%v", pName)
	}

	images := map[string]cruntime.ListImage{}
	for _, n := range c.Nodes {
		m := config.MachineName(*c, n)

		status, err := Status(api, m)
		if err != nil {
			klog.Warningf("error getting status for %s: %v", m, err)
			continue
		}

		if status == state.Running.String() {
			h, err := api.Load(m)
			if err != nil {
				klog.Warningf("Failed to load machine %q: %v", m, err)
				continue
			}
			runner, err := CommandRunner(h)
			if err != nil {
				return err
			}
			cr, err := cruntime.New(cruntime.Config{Type: c.KubernetesConfig.ContainerRuntime, Runner: runner})
			if err != nil {
				return errors.Wrap(err, "error creating container runtime")
			}
			list, err := cr.ListImages(cruntime.ListImagesOptions{})
			if err != nil {
				klog.Warningf("Failed to list images for profile %s %v", pName, err.Error())
				continue
			}

			for _, img := range list {
				if _, ok := images[img.ID]; !ok {
					images[img.ID] = img
				}
			}
		}
	}

	uniqueImages := []cruntime.ListImage{}
	for _, img := range images {
		uniqueImages = append(uniqueImages, img)
	}

	switch format {
	case "table":
		var data [][]string
		for _, item := range uniqueImages {
			imageSize := humanImageSize(item.Size)
			id := parseImageID(item.ID)
			for _, img := range item.RepoTags {
				imageName, tag := parseRepoTag(img)
				if imageName == "" {
					continue
				}
				data = append(data, []string{imageName, tag, id, imageSize})
			}
		}
		renderImagesTable(data)
	case "json":
		json, err := json.Marshal(uniqueImages)
		if err != nil {
			klog.Warningf("Error marshalling images list: %v", err.Error())
			return nil
		}
		fmt.Printf(string(json) + "\n")
	case "yaml":
		yaml, err := yaml.Marshal(uniqueImages)
		if err != nil {
			klog.Warningf("Error marshalling images list: %v", err.Error())
			return nil
		}
		fmt.Printf(string(yaml) + "\n")
	default:
		res := []string{}
		for _, item := range uniqueImages {
			res = append(res, item.RepoTags...)
		}
		sort.Sort(sort.Reverse(sort.StringSlice(res)))
		fmt.Printf(strings.Join(res, "\n") + "\n")
	}

	return nil
}