Skip to content

Commit cbd29b2

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 11008db commit cbd29b2

File tree

2 files changed

+56
-45
lines changed

2 files changed

+56
-45
lines changed

container/docker/handler.go

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
// Handler for Docker containers.
15+
// Package docker implements a handler for Docker containers.
1616
package docker
1717

1818
import (
@@ -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: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
// Package podman implements a handler for Podman containers.
1516
package podman
1617

1718
import (
1819
"fmt"
1920
"path"
2021
"path/filepath"
22+
"strconv"
2123
"strings"
2224
"time"
2325

@@ -43,30 +45,38 @@ type containerHandler struct {
4345
// (e.g.: "cpu" -> "/sys/fs/cgroup/cpu/test")
4446
cgroupPaths map[string]string
4547

48+
// the docker storage driver
4649
storageDriver docker.StorageDriver
4750
fsInfo fs.FsInfo
4851
rootfsStorageDir string
4952

53+
// Time at which this container was created.
5054
creationTime time.Time
5155

5256
// Metadata associated with the container.
5357
envs map[string]string
5458
labels map[string]string
5559

60+
// Image name used for this container.
5661
image string
5762

5863
networkMode dockercontainer.NetworkMode
5964

65+
// Filesystem handler.
6066
fsHandler common.FsHandler
6167

68+
// The IP address of the container
6269
ipAddress string
6370

6471
metrics container.MetricSet
6572

73+
// the devicemapper poolname
6674
thinPoolName string
6775

76+
// zfsParent is the parent for docker zfs
6877
zfsParent string
6978

79+
// Reference to the container
7080
reference info.ContainerReference
7181

7282
libcontainerHandler *containerlibcontainer.Handler
@@ -89,6 +99,7 @@ func newContainerHandler(
8999
// Create the cgroup paths.
90100
cgroupPaths := common.MakeCgroupPaths(cgroupSubsystems, name)
91101

102+
// Generate the equivalent cgroup manager for this container.
92103
cgroupManager, err := containerlibcontainer.NewCgroupManager(name, cgroupPaths)
93104
if err != nil {
94105
return nil, err
@@ -118,6 +129,8 @@ func newContainerHandler(
118129
return nil, err
119130
}
120131

132+
// Determine the rootfs storage dir OR the pool name to determine the device.
133+
// For devicemapper, we only need the thin pool name, and that is passed in to this call
121134
rootfsStorageDir, zfsFilesystem, zfsParent, err := determineDeviceStorage(storageDriver, storageDir, layerID)
122135
if err != nil {
123136
return nil, err
@@ -131,7 +144,6 @@ func newContainerHandler(
131144
storageDriver: storageDriver,
132145
fsInfo: fsInfo,
133146
rootfsStorageDir: rootfsStorageDir,
134-
ipAddress: ctnr.NetworkSettings.IPAddress,
135147
envs: make(map[string]string),
136148
labels: ctnr.Config.Labels,
137149
image: ctnr.Config.Image,
@@ -155,21 +167,23 @@ func newContainerHandler(
155167
}
156168

157169
if ctnr.RestartCount > 0 {
158-
handler.labels["restartcount"] = fmt.Sprint(ctnr.RestartCount)
170+
handler.labels["restartcount"] = strconv.Itoa(ctnr.RestartCount)
159171
}
160172

161173
// Obtain the IP address for the container.
162174
// If the NetworkMode starts with 'container:' then we need to use the IP address of the container specified.
163175
// 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:") {
176+
ipAddress := ctnr.NetworkSettings.IPAddress
177+
networkMode := string(ctnr.HostConfig.NetworkMode)
178+
if ipAddress == "" && strings.HasPrefix(networkMode, "container:") {
166179
containerID := strings.TrimPrefix(networkMode, "container:")
167180
c, err := InspectContainer(containerID)
168181
if err != nil {
169-
return nil, err
182+
return nil, fmt.Errorf("failed to inspect container %q: %v", containerID, err)
170183
}
171-
handler.ipAddress = c.NetworkSettings.IPAddress
184+
ipAddress = c.NetworkSettings.IPAddress
172185
}
186+
handler.ipAddress = ipAddress
173187

174188
if metrics.Has(container.DiskUsageMetrics) {
175189
handler.fsHandler = &docker.FsHandler{
@@ -250,6 +264,7 @@ func (h *containerHandler) GetStats() (*info.ContainerStats, error) {
250264
stats.Network = info.NetworkStats{}
251265
}
252266

267+
// Get filesystem stats.
253268
err = docker.FsStats(stats, h.machineInfoFactory, h.metrics, h.storageDriver,
254269
h.fsHandler, h.fsInfo, h.thinPoolName, h.rootfsStorageDir, h.zfsParent)
255270
if err != nil {
@@ -259,11 +274,11 @@ func (h *containerHandler) GetStats() (*info.ContainerStats, error) {
259274
return stats, nil
260275
}
261276

262-
func (h *containerHandler) ListContainers(listType container.ListType) ([]info.ContainerReference, error) {
277+
func (h *containerHandler) ListContainers(container.ListType) ([]info.ContainerReference, error) {
263278
return []info.ContainerReference{}, nil
264279
}
265280

266-
func (h *containerHandler) ListProcesses(listType container.ListType) ([]int, error) {
281+
func (h *containerHandler) ListProcesses(container.ListType) ([]int, error) {
267282
return h.libcontainerHandler.GetProcesses()
268283
}
269284

@@ -274,7 +289,7 @@ func (h *containerHandler) GetCgroupPath(resource string) (string, error) {
274289
}
275290
cgroupPath, ok := h.cgroupPaths[res]
276291
if !ok {
277-
return "", fmt.Errorf("couldn't find path for resource %q for container %q", resource, h.reference.Name)
292+
return "", fmt.Errorf("could not find path for resource %q for container %q", resource, h.reference.Name)
278293
}
279294

280295
return cgroupPath, nil

0 commit comments

Comments
 (0)