File: system.go

package info (click to toggle)
podman 5.6.2%2Bds1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 23,304 kB
  • sloc: sh: 4,493; python: 2,676; perl: 1,885; ansic: 1,484; makefile: 988; ruby: 42; csh: 8
file content (105 lines) | stat: -rw-r--r-- 2,909 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//go:build !remote

package compat

import (
	"net/http"
	"strings"

	"github.com/containers/podman/v5/libpod"
	"github.com/containers/podman/v5/pkg/api/handlers"
	"github.com/containers/podman/v5/pkg/api/handlers/utils"
	"github.com/containers/podman/v5/pkg/api/handlers/utils/apiutil"
	api "github.com/containers/podman/v5/pkg/api/types"
	"github.com/containers/podman/v5/pkg/domain/entities"
	"github.com/containers/podman/v5/pkg/domain/infra/abi"
	docker "github.com/docker/docker/api/types"
	dockerImage "github.com/docker/docker/api/types/image"
	"github.com/docker/docker/api/types/volume"
)

func GetDiskUsage(w http.ResponseWriter, r *http.Request) {
	options := entities.SystemDfOptions{}
	runtime := r.Context().Value(api.RuntimeKey).(*libpod.Runtime)
	ic := abi.ContainerEngine{Libpod: runtime}
	df, err := ic.SystemDf(r.Context(), options)
	if err != nil {
		utils.InternalServerError(w, err)
		return
	}

	imgs := make([]*dockerImage.Summary, len(df.Images))
	for i, o := range df.Images {
		t := dockerImage.Summary{
			Containers:  int64(o.Containers),
			Created:     o.Created.Unix(),
			ID:          o.ImageID,
			Labels:      map[string]string{},
			ParentID:    "",
			RepoDigests: nil,
			RepoTags:    []string{o.Tag},
			SharedSize:  o.SharedSize,
			Size:        o.Size,
		}

		if _, err := apiutil.SupportedVersion(r, "<1.44.0"); err == nil {
			t.VirtualSize = o.Size - o.UniqueSize //nolint:staticcheck // Deprecated field
		}

		imgs[i] = &t
	}

	ctnrs := make([]*docker.Container, len(df.Containers))
	for i, o := range df.Containers {
		t := docker.Container{
			ID:         o.ContainerID,
			Names:      []string{o.Names},
			Image:      o.Image,
			ImageID:    o.Image,
			Command:    strings.Join(o.Command, " "),
			Created:    o.Created.Unix(),
			Ports:      nil,
			SizeRw:     o.RWSize,
			SizeRootFs: o.Size,
			Labels:     map[string]string{},
			State:      o.Status,
			Status:     o.Status,
			HostConfig: struct {
				NetworkMode string `json:",omitempty"`
			}{},
			NetworkSettings: nil,
			Mounts:          nil,
		}
		ctnrs[i] = &t
	}

	vols := make([]*volume.Volume, len(df.Volumes))
	for i, o := range df.Volumes {
		t := volume.Volume{
			CreatedAt:  "",
			Driver:     "",
			Labels:     map[string]string{},
			Mountpoint: "",
			Name:       o.VolumeName,
			Options:    nil,
			Scope:      "local",
			Status:     nil,
			UsageData: &volume.UsageData{
				RefCount: int64(o.Links),
				Size:     o.Size,
			},
		}
		vols[i] = &t
	}

	utils.WriteResponse(w, http.StatusOK, handlers.DiskUsage{DiskUsage: docker.DiskUsage{
		// BuilderSize was explicitly omitted since Docker deprecated its in ver 1.42
		// and suggests to use BuildCache.
		// https://docs.docker.com/reference/api/engine/version-history/#v142-api-changes
		LayersSize: df.ImagesSize,
		Images:     imgs,
		Containers: ctnrs,
		Volumes:    vols,
		BuildCache: []*docker.BuildCache{},
	}})
}