-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
info.go
287 lines (269 loc) · 10.4 KB
/
info.go
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
Copyright 2019 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package oci
import (
"encoding/json"
"os/exec"
"strings"
"time"
"github.com/pkg/errors"
"k8s.io/klog/v2"
)
// SysInfo Info represents common system Information between docker and podman that minikube cares
type SysInfo struct {
CPUs int // CPUs is Number of CPUs
TotalMemory int64 // TotalMemory Total available ram
OSType string // container's OsType (windows or linux)
Swarm bool // Weather or not the docker swarm is active
Rootless bool // Weather or not the docker is running on rootless mode
StorageDriver string // the storage driver for the daemon (for example overlay2)
Errors []string // any server issues
}
var (
cachedSysInfo *SysInfo
cachedSysInfoErr *error
)
// CachedDaemonInfo will run and return a docker/podman info only once per minikube run time. to avoid performance
func CachedDaemonInfo(ociBin string) (SysInfo, error) {
if cachedSysInfo == nil {
si, err := DaemonInfo(ociBin)
cachedSysInfo = &si
cachedSysInfoErr = &err
}
if cachedSysInfoErr == nil {
return *cachedSysInfo, nil
}
return *cachedSysInfo, *cachedSysInfoErr
}
// DaemonInfo returns common docker/podman daemon system info that minikube cares about
func DaemonInfo(ociBin string) (SysInfo, error) {
if ociBin == Podman {
p, err := podmanSystemInfo()
cachedSysInfo = &SysInfo{CPUs: p.Host.Cpus, TotalMemory: p.Host.MemTotal, OSType: p.Host.Os, Swarm: false, StorageDriver: p.Store.GraphDriverName}
return *cachedSysInfo, err
}
d, err := dockerSystemInfo()
rootless := false
for _, se := range d.SecurityOptions {
if strings.HasPrefix(se, "name=rootless") {
rootless = true
break
}
}
cachedSysInfo = &SysInfo{CPUs: d.NCPU, TotalMemory: d.MemTotal, OSType: d.OSType, Swarm: d.Swarm.LocalNodeState == "active", Rootless: rootless, StorageDriver: d.Driver, Errors: d.ServerErrors}
return *cachedSysInfo, err
}
// dockerSysInfo represents the output of docker system info --format '{{json .}}'
type dockerSysInfo struct {
ID string `json:"ID"`
Containers int `json:"Containers"`
ContainersRunning int `json:"ContainersRunning"`
ContainersPaused int `json:"ContainersPaused"`
ContainersStopped int `json:"ContainersStopped"`
Images int `json:"Images"`
Driver string `json:"Driver"`
DriverStatus [][]string `json:"DriverStatus"`
SystemStatus interface{} `json:"SystemStatus"`
Plugins struct {
Volume []string `json:"Volume"`
Network []string `json:"Network"`
Authorization interface{} `json:"Authorization"`
Log []string `json:"Log"`
} `json:"Plugins"`
MemoryLimit bool `json:"MemoryLimit"`
SwapLimit bool `json:"SwapLimit"`
KernelMemory bool `json:"KernelMemory"`
KernelMemoryTCP bool `json:"KernelMemoryTCP"`
CPUCfsPeriod bool `json:"CpuCfsPeriod"`
CPUCfsQuota bool `json:"CpuCfsQuota"`
CPUShares bool `json:"CPUShares"`
CPUSet bool `json:"CPUSet"`
PidsLimit bool `json:"PidsLimit"`
IPv4Forwarding bool `json:"IPv4Forwarding"`
BridgeNfIptables bool `json:"BridgeNfIptables"`
BridgeNfIP6Tables bool `json:"BridgeNfIp6tables"`
Debug bool `json:"Debug"`
NFd int `json:"NFd"`
OomKillDisable bool `json:"OomKillDisable"`
NGoroutines int `json:"NGoroutines"`
SystemTime time.Time `json:"SystemTime"`
LoggingDriver string `json:"LoggingDriver"`
CgroupDriver string `json:"CgroupDriver"`
NEventsListener int `json:"NEventsListener"`
KernelVersion string `json:"KernelVersion"`
OperatingSystem string `json:"OperatingSystem"`
OSType string `json:"OSType"`
Architecture string `json:"Architecture"`
IndexServerAddress string `json:"IndexServerAddress"`
RegistryConfig struct {
AllowNondistributableArtifactsCIDRs []interface{} `json:"AllowNondistributableArtifactsCIDRs"`
AllowNondistributableArtifactsHostnames []interface{} `json:"AllowNondistributableArtifactsHostnames"`
InsecureRegistryCIDRs []string `json:"InsecureRegistryCIDRs"`
IndexConfigs struct {
DockerIo struct {
Name string `json:"Name"`
Mirrors []interface{} `json:"Mirrors"`
Secure bool `json:"Secure"`
Official bool `json:"Official"`
} `json:"docker.io"`
} `json:"IndexConfigs"`
Mirrors []interface{} `json:"Mirrors"`
} `json:"RegistryConfig"`
NCPU int `json:"NCPU"`
MemTotal int64 `json:"MemTotal"`
GenericResources interface{} `json:"GenericResources"`
DockerRootDir string `json:"DockerRootDir"`
HTTPProxy string `json:"HttpProxy"`
HTTPSProxy string `json:"HttpsProxy"`
NoProxy string `json:"NoProxy"`
Name string `json:"Name"`
Labels []interface{} `json:"Labels"`
ExperimentalBuild bool `json:"ExperimentalBuild"`
ServerVersion string `json:"ServerVersion"`
ClusterStore string `json:"ClusterStore"`
ClusterAdvertise string `json:"ClusterAdvertise"`
Runtimes struct {
Runc struct {
Path string `json:"path"`
} `json:"runc"`
} `json:"Runtimes"`
DefaultRuntime string `json:"DefaultRuntime"`
Swarm struct {
NodeID string `json:"NodeID"`
NodeAddr string `json:"NodeAddr"`
LocalNodeState string `json:"LocalNodeState"`
ControlAvailable bool `json:"ControlAvailable"`
Error string `json:"Error"`
RemoteManagers interface{} `json:"RemoteManagers"`
} `json:"Swarm"`
LiveRestoreEnabled bool `json:"LiveRestoreEnabled"`
Isolation string `json:"Isolation"`
InitBinary string `json:"InitBinary"`
ContainerdCommit struct {
ID string `json:"ID"`
Expected string `json:"Expected"`
} `json:"ContainerdCommit"`
RuncCommit struct {
ID string `json:"ID"`
Expected string `json:"Expected"`
} `json:"RuncCommit"`
InitCommit struct {
ID string `json:"ID"`
Expected string `json:"Expected"`
} `json:"InitCommit"`
SecurityOptions []string `json:"SecurityOptions"`
ProductLicense string `json:"ProductLicense"`
Warnings interface{} `json:"Warnings"`
ServerErrors []string
ClientInfo struct {
Debug bool `json:"Debug"`
Plugins []interface{} `json:"Plugins"`
Warnings interface{} `json:"Warnings"`
} `json:"ClientInfo"`
}
// podmanSysInfo represents the output of podman system info --format '{{json .}}'
type podmanSysInfo struct {
Host struct {
BuildahVersion string `json:"BuildahVersion"`
CgroupVersion string `json:"CgroupVersion"`
Conmon struct {
Package string `json:"package"`
Path string `json:"path"`
Version string `json:"version"`
} `json:"Conmon"`
Distribution struct {
Distribution string `json:"distribution"`
Version string `json:"version"`
} `json:"Distribution"`
MemFree int `json:"MemFree"`
MemTotal int64 `json:"MemTotal"`
OCIRuntime struct {
Name string `json:"name"`
Package string `json:"package"`
Path string `json:"path"`
Version string `json:"version"`
} `json:"OCIRuntime"`
SwapFree int `json:"SwapFree"`
SwapTotal int `json:"SwapTotal"`
Arch string `json:"arch"`
Cpus int `json:"cpus"`
Eventlogger string `json:"eventlogger"`
Hostname string `json:"hostname"`
Kernel string `json:"kernel"`
Os string `json:"os"`
Rootless bool `json:"rootless"`
Uptime string `json:"uptime"`
} `json:"host"`
Registries struct {
Search []string `json:"search"`
} `json:"registries"`
Store struct {
ConfigFile string `json:"ConfigFile"`
ContainerStore struct {
Number int `json:"number"`
} `json:"ContainerStore"`
GraphDriverName string `json:"GraphDriverName"`
GraphOptions struct {
} `json:"GraphOptions"`
GraphRoot string `json:"GraphRoot"`
GraphStatus struct {
BackingFilesystem string `json:"Backing Filesystem"`
NativeOverlayDiff string `json:"Native Overlay Diff"`
SupportsDType string `json:"Supports d_type"`
UsingMetacopy string `json:"Using metacopy"`
} `json:"GraphStatus"`
ImageStore struct {
Number int `json:"number"`
} `json:"ImageStore"`
RunRoot string `json:"RunRoot"`
VolumePath string `json:"VolumePath"`
} `json:"store"`
}
var dockerInfoGetter = func() (string, error) {
rr, err := runCmd(exec.Command(Docker, "system", "info", "--format", "{{json .}}"))
return rr.Stdout.String(), err
}
// dockerSystemInfo returns docker system info --format '{{json .}}'
func dockerSystemInfo() (dockerSysInfo, error) {
var ds dockerSysInfo
rawJSON, err := dockerInfoGetter()
if err != nil {
klog.Warningf("docker info: %v", err)
return ds, errors.Wrap(err, "docker system info")
}
if err := json.Unmarshal([]byte(strings.TrimSpace(rawJSON)), &ds); err != nil {
klog.Warningf("unmarshal docker info: %v", err)
return ds, errors.Wrapf(err, "unmarshal docker system info")
}
klog.Infof("docker info: %+v", ds)
return ds, nil
}
var podmanInfoGetter = func() (string, error) {
rr, err := runCmd(exec.Command(Podman, "system", "info", "--format", "json"))
return rr.Stdout.String(), err
}
// podmanSysInfo returns podman system info --format '{{json .}}'
func podmanSystemInfo() (podmanSysInfo, error) {
var ps podmanSysInfo
rawJSON, err := podmanInfoGetter()
if err != nil {
klog.Warningf("podman info: %v", err)
return ps, errors.Wrap(err, "podman system info")
}
if err := json.Unmarshal([]byte(strings.TrimSpace(rawJSON)), &ps); err != nil {
klog.Warningf("unmarshal podman info: %v", err)
return ps, errors.Wrapf(err, "unmarshal podman system info")
}
klog.Infof("podman info: %+v", ps)
return ps, nil
}