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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
|
package compat
import (
"fmt"
"net/http"
"os"
goRuntime "runtime"
"strings"
"time"
"github.com/containers/common/pkg/config"
"github.com/containers/common/pkg/sysinfo"
"github.com/containers/image/v5/pkg/sysregistriesv2"
"github.com/containers/podman/v4/libpod"
"github.com/containers/podman/v4/libpod/define"
"github.com/containers/podman/v4/pkg/api/handlers"
"github.com/containers/podman/v4/pkg/api/handlers/utils"
api "github.com/containers/podman/v4/pkg/api/types"
"github.com/containers/podman/v4/pkg/rootless"
docker "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/registry"
"github.com/docker/docker/api/types/swarm"
"github.com/google/uuid"
log "github.com/sirupsen/logrus"
)
func GetInfo(w http.ResponseWriter, r *http.Request) {
// 200 ok
// 500 internal
runtime := r.Context().Value(api.RuntimeKey).(*libpod.Runtime)
infoData, err := runtime.Info()
if err != nil {
utils.Error(w, http.StatusInternalServerError, fmt.Errorf("failed to obtain system memory info: %w", err))
return
}
configInfo, err := runtime.GetConfig()
if err != nil {
utils.Error(w, http.StatusInternalServerError, fmt.Errorf("failed to obtain runtime config: %w", err))
return
}
versionInfo, err := define.GetVersion()
if err != nil {
utils.Error(w, http.StatusInternalServerError, fmt.Errorf("failed to obtain podman versions: %w", err))
return
}
stateInfo := getContainersState(runtime)
sysInfo := sysinfo.New(true)
// FIXME: Need to expose if runtime supports Checkpointing
// liveRestoreEnabled := criu.CheckForCriu() && configInfo.RuntimeSupportsCheckpoint()
info := &handlers.Info{
Info: docker.Info{
Architecture: goRuntime.GOARCH,
BridgeNfIP6tables: !sysInfo.BridgeNFCallIP6TablesDisabled,
BridgeNfIptables: !sysInfo.BridgeNFCallIPTablesDisabled,
CPUCfsPeriod: sysInfo.CPUCfsPeriod,
CPUCfsQuota: sysInfo.CPUCfsQuota,
CPUSet: sysInfo.Cpuset,
CPUShares: sysInfo.CPUShares,
CgroupDriver: configInfo.Engine.CgroupManager,
ClusterAdvertise: "",
ClusterStore: "",
ContainerdCommit: docker.Commit{},
Containers: infoData.Store.ContainerStore.Number,
ContainersPaused: stateInfo[define.ContainerStatePaused],
ContainersRunning: stateInfo[define.ContainerStateRunning],
ContainersStopped: stateInfo[define.ContainerStateStopped] + stateInfo[define.ContainerStateExited],
Debug: log.IsLevelEnabled(log.DebugLevel),
DefaultRuntime: configInfo.Engine.OCIRuntime,
DockerRootDir: infoData.Store.GraphRoot,
Driver: infoData.Store.GraphDriverName,
DriverStatus: getGraphStatus(infoData.Store.GraphStatus),
ExperimentalBuild: true,
GenericResources: nil,
HTTPProxy: getEnv("http_proxy"),
HTTPSProxy: getEnv("https_proxy"),
ID: uuid.New().String(),
IPv4Forwarding: !sysInfo.IPv4ForwardingDisabled,
Images: infoData.Store.ImageStore.Number,
IndexServerAddress: "",
InitBinary: "",
InitCommit: docker.Commit{},
Isolation: "",
KernelMemoryTCP: false,
KernelVersion: infoData.Host.Kernel,
Labels: nil,
LiveRestoreEnabled: false,
LoggingDriver: "",
MemTotal: infoData.Host.MemTotal,
MemoryLimit: sysInfo.MemoryLimit,
NCPU: goRuntime.NumCPU(),
NEventsListener: 0,
NFd: getFdCount(),
NGoroutines: goRuntime.NumGoroutine(),
Name: infoData.Host.Hostname,
NoProxy: getEnv("no_proxy"),
OSType: goRuntime.GOOS,
OSVersion: infoData.Host.Distribution.Version,
OomKillDisable: sysInfo.OomKillDisable,
OperatingSystem: infoData.Host.Distribution.Distribution,
PidsLimit: sysInfo.PidsLimit,
Plugins: docker.PluginsInfo{
Volume: infoData.Plugins.Volume,
Network: infoData.Plugins.Network,
Log: infoData.Plugins.Log,
},
ProductLicense: "Apache-2.0",
RegistryConfig: getServiceConfig(runtime),
RuncCommit: docker.Commit{},
Runtimes: getRuntimes(configInfo),
SecurityOptions: getSecOpts(sysInfo),
ServerVersion: versionInfo.Version,
SwapLimit: sysInfo.SwapLimit,
Swarm: swarm.Info{
LocalNodeState: swarm.LocalNodeStateInactive,
},
SystemStatus: nil,
SystemTime: time.Now().Format(time.RFC3339Nano),
Warnings: []string{},
},
BuildahVersion: infoData.Host.BuildahVersion,
CPURealtimePeriod: sysInfo.CPURealtimePeriod,
CPURealtimeRuntime: sysInfo.CPURealtimeRuntime,
CgroupVersion: strings.TrimPrefix(infoData.Host.CgroupsVersion, "v"),
Rootless: rootless.IsRootless(),
SwapFree: infoData.Host.SwapFree,
SwapTotal: infoData.Host.SwapTotal,
Uptime: infoData.Host.Uptime,
}
utils.WriteResponse(w, http.StatusOK, info)
}
func getServiceConfig(runtime *libpod.Runtime) *registry.ServiceConfig {
var indexConfs map[string]*registry.IndexInfo
regs, err := sysregistriesv2.GetRegistries(runtime.SystemContext())
if err == nil {
indexConfs = make(map[string]*registry.IndexInfo, len(regs))
for _, reg := range regs {
mirrors := make([]string, len(reg.Mirrors))
for i, mirror := range reg.Mirrors {
mirrors[i] = mirror.Location
}
indexConfs[reg.Prefix] = ®istry.IndexInfo{
Name: reg.Prefix,
Mirrors: mirrors,
Secure: !reg.Insecure,
}
}
} else {
log.Warnf("failed to get registries configuration: %v", err)
indexConfs = make(map[string]*registry.IndexInfo)
}
return ®istry.ServiceConfig{
AllowNondistributableArtifactsCIDRs: make([]*registry.NetIPNet, 0),
AllowNondistributableArtifactsHostnames: make([]string, 0),
InsecureRegistryCIDRs: make([]*registry.NetIPNet, 0),
IndexConfigs: indexConfs,
Mirrors: make([]string, 0),
}
}
func getGraphStatus(storeInfo map[string]string) [][2]string {
graphStatus := make([][2]string, 0, len(storeInfo))
for k, v := range storeInfo {
graphStatus = append(graphStatus, [2]string{k, v})
}
return graphStatus
}
func getSecOpts(sysInfo *sysinfo.SysInfo) []string {
var secOpts []string
if sysInfo.AppArmor {
secOpts = append(secOpts, "name=apparmor")
}
if sysInfo.Seccomp {
// FIXME: get profile name...
secOpts = append(secOpts, fmt.Sprintf("name=seccomp,profile=%s", "default"))
}
return secOpts
}
func getRuntimes(configInfo *config.Config) map[string]docker.Runtime {
runtimes := map[string]docker.Runtime{}
for name, paths := range configInfo.Engine.OCIRuntimes {
runtimes[name] = docker.Runtime{
Path: paths[0],
Args: nil,
}
}
return runtimes
}
func getFdCount() (count int) {
count = -1
if entries, err := os.ReadDir("/proc/self/fd"); err == nil {
count = len(entries)
}
return
}
// Just ignoring Container errors here...
func getContainersState(r *libpod.Runtime) map[define.ContainerStatus]int {
states := map[define.ContainerStatus]int{}
ctnrs, err := r.GetAllContainers()
if err == nil {
for _, ctnr := range ctnrs {
state, err := ctnr.State()
if err != nil {
continue
}
states[state]++
}
}
return states
}
func getEnv(value string) string {
if v, exists := os.LookupEnv(strings.ToUpper(value)); exists {
return v
}
if v, exists := os.LookupEnv(strings.ToLower(value)); exists {
return v
}
return ""
}
|