Skip to content

Commit 4ce78a7

Browse files
committed
container/(docker|podman): align code
Align the code between the implementations for easier comparison; mostly renaming some vars, changing order, and syncing comments. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 2be6e3f commit 4ce78a7

File tree

2 files changed

+54
-44
lines changed

2 files changed

+54
-44
lines changed

container/docker/handler.go

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ type containerHandler struct {
8181
// The IP address of the container
8282
ipAddress string
8383

84-
includedMetrics container.MetricSet
84+
metrics container.MetricSet
8585

8686
// the devicemapper poolname
87-
poolName string
87+
thinPoolName string
8888

8989
// zfsParent is the parent for docker zfs
9090
zfsParent string
@@ -127,7 +127,7 @@ func newContainerHandler(
127127
inHostNamespace bool,
128128
metadataEnvAllowList []string,
129129
dockerVersion []int,
130-
includedMetrics container.MetricSet,
130+
metrics container.MetricSet,
131131
thinPoolName string,
132132
thinPoolWatcher *devicemapper.ThinPoolWatcher,
133133
zfsWatcher *zfs.ZfsWatcher,
@@ -187,19 +187,18 @@ func newContainerHandler(
187187
}
188188

189189
// Do not report network metrics for containers that share netns with another container.
190-
metrics := common.RemoveNetMetrics(includedMetrics, ctnr.HostConfig.NetworkMode.IsContainer())
190+
includedMetrics := common.RemoveNetMetrics(metrics, ctnr.HostConfig.NetworkMode.IsContainer())
191191

192-
// TODO: extract object mother method
193192
handler := &containerHandler{
194193
machineInfoFactory: machineInfoFactory,
195194
cgroupPaths: cgroupPaths,
196-
fsInfo: fsInfo,
197195
storageDriver: storageDriver,
198-
poolName: thinPoolName,
196+
fsInfo: fsInfo,
199197
rootfsStorageDir: rootfsStorageDir,
200198
envs: make(map[string]string),
201199
labels: ctnr.Config.Labels,
202-
includedMetrics: metrics,
200+
metrics: includedMetrics,
201+
thinPoolName: thinPoolName,
203202
zfsParent: zfsParent,
204203
}
205204
// Health status may be nil if no health check is configured
@@ -209,7 +208,6 @@ func newContainerHandler(
209208
// Timestamp returned by Docker is in time.RFC3339Nano format.
210209
handler.creationTime, err = time.Parse(time.RFC3339Nano, ctnr.Created)
211210
if err != nil {
212-
// This should not happen, report the error just in case
213211
return nil, fmt.Errorf("failed to parse the create timestamp %q for container %q: %v", ctnr.Created, id, err)
214212
}
215213
handler.libcontainerHandler = containerlibcontainer.NewHandler(cgroupManager, rootFs, ctnr.State.Pid, metrics)
@@ -222,7 +220,7 @@ func newContainerHandler(
222220
Namespace: DockerNamespace,
223221
}
224222
handler.image = ctnr.Config.Image
225-
// Only adds restartcount label if it's greater than 0
223+
226224
if ctnr.RestartCount > 0 {
227225
handler.labels["restartcount"] = strconv.Itoa(ctnr.RestartCount)
228226
}
@@ -236,11 +234,10 @@ func newContainerHandler(
236234
containerID := strings.TrimPrefix(networkMode, "container:")
237235
c, err := client.ContainerInspect(context.Background(), containerID)
238236
if err != nil {
239-
return nil, fmt.Errorf("failed to inspect container %q: %v", id, err)
237+
return nil, fmt.Errorf("failed to inspect container %q: %v", containerID, err)
240238
}
241239
ipAddress = c.NetworkSettings.IPAddress
242240
}
243-
244241
handler.ipAddress = ipAddress
245242

246243
if includedMetrics.Has(container.DiskUsageMetrics) {
@@ -253,7 +250,7 @@ func newContainerHandler(
253250
}
254251
}
255252

256-
// split env vars to get metadata map.
253+
// Split env vars to get metadata map.
257254
for _, exposedEnv := range metadataEnvAllowList {
258255
if exposedEnv == "" {
259256
// if no dockerEnvWhitelist provided, len(metadataEnvAllowList) == 1, metadataEnvAllowList[0] == ""
@@ -296,25 +293,13 @@ func DetermineDeviceStorage(storageDriver StorageDriver, storageDir string, rwLa
296293
return
297294
}
298295

299-
func (h *containerHandler) Start() {
300-
if h.fsHandler != nil {
301-
h.fsHandler.Start()
302-
}
303-
}
304-
305-
func (h *containerHandler) Cleanup() {
306-
if h.fsHandler != nil {
307-
h.fsHandler.Stop()
308-
}
309-
}
310-
311296
func (h *containerHandler) ContainerReference() (info.ContainerReference, error) {
312297
return h.reference, nil
313298
}
314299

315300
func (h *containerHandler) GetSpec() (info.ContainerSpec, error) {
316-
hasFilesystem := h.includedMetrics.Has(container.DiskUsageMetrics)
317-
hasNetwork := h.includedMetrics.Has(container.NetworkUsageMetrics)
301+
hasFilesystem := h.metrics.Has(container.DiskUsageMetrics)
302+
hasNetwork := h.metrics.Has(container.NetworkUsageMetrics)
318303
spec, err := common.GetSpec(h.cgroupPaths, h.machineInfoFactory, hasNetwork, hasFilesystem)
319304
if err != nil {
320305
return info.ContainerSpec{}, err
@@ -337,20 +322,23 @@ func (h *containerHandler) GetStats() (*info.ContainerStats, error) {
337322
stats.Health.Status = h.healthStatus
338323

339324
// Get filesystem stats.
340-
err = FsStats(stats, h.machineInfoFactory, h.includedMetrics, h.storageDriver,
341-
h.fsHandler, h.fsInfo, h.poolName, h.rootfsStorageDir, h.zfsParent)
325+
err = FsStats(stats, h.machineInfoFactory, h.metrics, h.storageDriver,
326+
h.fsHandler, h.fsInfo, h.thinPoolName, h.rootfsStorageDir, h.zfsParent)
342327
if err != nil {
343328
return stats, err
344329
}
345330

346331
return stats, nil
347332
}
348333

349-
func (h *containerHandler) ListContainers(listType container.ListType) ([]info.ContainerReference, error) {
350-
// No-op for Docker driver.
334+
func (h *containerHandler) ListContainers(container.ListType) ([]info.ContainerReference, error) {
351335
return []info.ContainerReference{}, nil
352336
}
353337

338+
func (h *containerHandler) ListProcesses(container.ListType) ([]int, error) {
339+
return h.libcontainerHandler.GetProcesses()
340+
}
341+
354342
func (h *containerHandler) GetCgroupPath(resource string) (string, error) {
355343
var res string
356344
if !cgroups.IsCgroup2UnifiedMode() {
@@ -371,14 +359,22 @@ func (h *containerHandler) GetContainerIPAddress() string {
371359
return h.ipAddress
372360
}
373361

374-
func (h *containerHandler) ListProcesses(listType container.ListType) ([]int, error) {
375-
return h.libcontainerHandler.GetProcesses()
376-
}
377-
378362
func (h *containerHandler) Exists() bool {
379363
return common.CgroupExists(h.cgroupPaths)
380364
}
381365

366+
func (h *containerHandler) Cleanup() {
367+
if h.fsHandler != nil {
368+
h.fsHandler.Stop()
369+
}
370+
}
371+
372+
func (h *containerHandler) Start() {
373+
if h.fsHandler != nil {
374+
h.fsHandler.Start()
375+
}
376+
}
377+
382378
func (h *containerHandler) Type() container.ContainerType {
383379
return container.ContainerTypeDocker
384380
}

container/podman/handler.go

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"fmt"
1919
"path"
2020
"path/filepath"
21+
"strconv"
2122
"strings"
2223
"time"
2324

@@ -43,30 +44,38 @@ type containerHandler struct {
4344
// (e.g.: "cpu" -> "/sys/fs/cgroup/cpu/test")
4445
cgroupPaths map[string]string
4546

47+
// the docker storage driver
4648
storageDriver docker.StorageDriver
4749
fsInfo fs.FsInfo
4850
rootfsStorageDir string
4951

52+
// Time at which this container was created.
5053
creationTime time.Time
5154

5255
// Metadata associated with the container.
5356
envs map[string]string
5457
labels map[string]string
5558

59+
// Image name used for this container.
5660
image string
5761

5862
networkMode dockercontainer.NetworkMode
5963

64+
// Filesystem handler.
6065
fsHandler common.FsHandler
6166

67+
// The IP address of the container
6268
ipAddress string
6369

6470
metrics container.MetricSet
6571

72+
// the devicemapper poolname
6673
thinPoolName string
6774

75+
// zfsParent is the parent for docker zfs
6876
zfsParent string
6977

78+
// Reference to the container
7079
reference info.ContainerReference
7180

7281
libcontainerHandler *containerlibcontainer.Handler
@@ -89,6 +98,7 @@ func newContainerHandler(
8998
// Create the cgroup paths.
9099
cgroupPaths := common.MakeCgroupPaths(cgroupSubsystems, name)
91100

101+
// Generate the equivalent cgroup manager for this container.
92102
cgroupManager, err := containerlibcontainer.NewCgroupManager(name, cgroupPaths)
93103
if err != nil {
94104
return nil, err
@@ -118,6 +128,8 @@ func newContainerHandler(
118128
return nil, err
119129
}
120130

131+
// Determine the rootfs storage dir OR the pool name to determine the device.
132+
// For devicemapper, we only need the thin pool name, and that is passed in to this call
121133
rootfsStorageDir, zfsParent, zfsFilesystem, err := determineDeviceStorage(storageDriver, storageDir, rwLayerID)
122134
if err != nil {
123135
return nil, err
@@ -131,7 +143,6 @@ func newContainerHandler(
131143
storageDriver: storageDriver,
132144
fsInfo: fsInfo,
133145
rootfsStorageDir: rootfsStorageDir,
134-
ipAddress: ctnr.NetworkSettings.IPAddress,
135146
envs: make(map[string]string),
136147
labels: ctnr.Config.Labels,
137148
image: ctnr.Config.Image,
@@ -155,21 +166,23 @@ func newContainerHandler(
155166
}
156167

157168
if ctnr.RestartCount > 0 {
158-
handler.labels["restartcount"] = fmt.Sprint(ctnr.RestartCount)
169+
handler.labels["restartcount"] = strconv.Itoa(ctnr.RestartCount)
159170
}
160171

161172
// Obtain the IP address for the container.
162173
// If the NetworkMode starts with 'container:' then we need to use the IP address of the container specified.
163174
// This happens in cases such as kubernetes where the containers doesn't have an IP address itself and we need to use the pod's address
164-
networkMode := string(handler.networkMode)
165-
if handler.ipAddress == "" && strings.HasPrefix(networkMode, "container:") {
175+
ipAddress := ctnr.NetworkSettings.IPAddress
176+
networkMode := string(ctnr.HostConfig.NetworkMode)
177+
if ipAddress == "" && strings.HasPrefix(networkMode, "container:") {
166178
containerID := strings.TrimPrefix(networkMode, "container:")
167179
c, err := InspectContainer(containerID)
168180
if err != nil {
169-
return nil, err
181+
return nil, fmt.Errorf("failed to inspect container %q: %v", containerID, err)
170182
}
171-
handler.ipAddress = c.NetworkSettings.IPAddress
183+
ipAddress = c.NetworkSettings.IPAddress
172184
}
185+
handler.ipAddress = ipAddress
173186

174187
if metrics.Has(container.DiskUsageMetrics) {
175188
handler.fsHandler = &docker.FsHandler{
@@ -250,6 +263,7 @@ func (h *containerHandler) GetStats() (*info.ContainerStats, error) {
250263
stats.Network = info.NetworkStats{}
251264
}
252265

266+
// Get filesystem stats.
253267
err = docker.FsStats(stats, h.machineInfoFactory, h.metrics, h.storageDriver,
254268
h.fsHandler, h.fsInfo, h.thinPoolName, h.rootfsStorageDir, h.zfsParent)
255269
if err != nil {
@@ -259,11 +273,11 @@ func (h *containerHandler) GetStats() (*info.ContainerStats, error) {
259273
return stats, nil
260274
}
261275

262-
func (h *containerHandler) ListContainers(listType container.ListType) ([]info.ContainerReference, error) {
276+
func (h *containerHandler) ListContainers(container.ListType) ([]info.ContainerReference, error) {
263277
return []info.ContainerReference{}, nil
264278
}
265279

266-
func (h *containerHandler) ListProcesses(listType container.ListType) ([]int, error) {
280+
func (h *containerHandler) ListProcesses(container.ListType) ([]int, error) {
267281
return h.libcontainerHandler.GetProcesses()
268282
}
269283

@@ -274,7 +288,7 @@ func (h *containerHandler) GetCgroupPath(resource string) (string, error) {
274288
}
275289
cgroupPath, ok := h.cgroupPaths[res]
276290
if !ok {
277-
return "", fmt.Errorf("couldn't find path for resource %q for container %q", resource, h.reference.Name)
291+
return "", fmt.Errorf("could not find path for resource %q for container %q", resource, h.reference.Name)
278292
}
279293

280294
return cgroupPath, nil

0 commit comments

Comments
 (0)