Skip to content

Commit 3314af1

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 a0cc8da commit 3314af1

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 (
@@ -80,10 +80,10 @@ type containerHandler struct {
8080
// The IP address of the container
8181
ipAddress string
8282

83-
includedMetrics container.MetricSet
83+
metrics container.MetricSet
8484

8585
// the devicemapper poolname
86-
poolName string
86+
thinPoolName string
8787

8888
// zfsParent is the parent for docker zfs
8989
zfsParent string
@@ -129,7 +129,7 @@ func newContainerHandler(
129129
inHostNamespace bool,
130130
metadataEnvAllowList []string,
131131
dockerVersion []int,
132-
includedMetrics container.MetricSet,
132+
metrics container.MetricSet,
133133
thinPoolName string,
134134
thinPoolWatcher *devicemapper.ThinPoolWatcher,
135135
zfsWatcher *zfs.ZfsWatcher,
@@ -189,26 +189,24 @@ func newContainerHandler(
189189
}
190190

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

194-
// TODO: extract object mother method
195194
handler := &containerHandler{
196195
machineInfoFactory: machineInfoFactory,
197196
cgroupPaths: cgroupPaths,
198-
fsInfo: fsInfo,
199197
storageDriver: storageDriver,
200-
poolName: thinPoolName,
198+
fsInfo: fsInfo,
201199
rootfsStorageDir: rootfsStorageDir,
202200
envs: make(map[string]string),
203201
labels: ctnr.Config.Labels,
204-
includedMetrics: metrics,
202+
metrics: includedMetrics,
203+
thinPoolName: thinPoolName,
205204
zfsParent: zfsParent,
206205
client: client,
207206
}
208207
// Timestamp returned by Docker is in time.RFC3339Nano format.
209208
handler.creationTime, err = time.Parse(time.RFC3339Nano, ctnr.Created)
210209
if err != nil {
211-
// This should not happen, report the error just in case
212210
return nil, fmt.Errorf("failed to parse the create timestamp %q for container %q: %v", ctnr.Created, id, err)
213211
}
214212
handler.libcontainerHandler = containerlibcontainer.NewHandler(cgroupManager, rootFs, ctnr.State.Pid, metrics)
@@ -221,7 +219,7 @@ func newContainerHandler(
221219
Namespace: DockerNamespace,
222220
}
223221
handler.image = ctnr.Config.Image
224-
// Only adds restartcount label if it's greater than 0
222+
225223
if ctnr.RestartCount > 0 {
226224
handler.labels["restartcount"] = strconv.Itoa(ctnr.RestartCount)
227225
}
@@ -235,11 +233,10 @@ func newContainerHandler(
235233
containerID := strings.TrimPrefix(networkMode, "container:")
236234
c, err := client.ContainerInspect(context.Background(), containerID)
237235
if err != nil {
238-
return nil, fmt.Errorf("failed to inspect container %q: %v", id, err)
236+
return nil, fmt.Errorf("failed to inspect container %q: %v", containerID, err)
239237
}
240238
ipAddress = c.NetworkSettings.IPAddress
241239
}
242-
243240
handler.ipAddress = ipAddress
244241

245242
if includedMetrics.Has(container.DiskUsageMetrics) {
@@ -252,7 +249,7 @@ func newContainerHandler(
252249
}
253250
}
254251

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

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

314299
func (h *containerHandler) GetSpec() (info.ContainerSpec, error) {
315-
hasFilesystem := h.includedMetrics.Has(container.DiskUsageMetrics)
316-
hasNetwork := h.includedMetrics.Has(container.NetworkUsageMetrics)
300+
hasFilesystem := h.metrics.Has(container.DiskUsageMetrics)
301+
hasNetwork := h.metrics.Has(container.NetworkUsageMetrics)
317302
spec, err := common.GetSpec(h.cgroupPaths, h.machineInfoFactory, hasNetwork, hasFilesystem)
318303
if err != nil {
319304
return info.ContainerSpec{}, err
@@ -345,20 +330,23 @@ func (h *containerHandler) GetStats() (*info.ContainerStats, error) {
345330
}
346331

347332
// Get filesystem stats.
348-
err = FsStats(stats, h.machineInfoFactory, h.includedMetrics, h.storageDriver,
349-
h.fsHandler, h.fsInfo, h.poolName, h.rootfsStorageDir, h.zfsParent)
333+
err = FsStats(stats, h.machineInfoFactory, h.metrics, h.storageDriver,
334+
h.fsHandler, h.fsInfo, h.thinPoolName, h.rootfsStorageDir, h.zfsParent)
350335
if err != nil {
351336
return stats, err
352337
}
353338

354339
return stats, nil
355340
}
356341

357-
func (h *containerHandler) ListContainers(listType container.ListType) ([]info.ContainerReference, error) {
358-
// No-op for Docker driver.
342+
func (h *containerHandler) ListContainers(container.ListType) ([]info.ContainerReference, error) {
359343
return []info.ContainerReference{}, nil
360344
}
361345

346+
func (h *containerHandler) ListProcesses(container.ListType) ([]int, error) {
347+
return h.libcontainerHandler.GetProcesses()
348+
}
349+
362350
func (h *containerHandler) GetCgroupPath(resource string) (string, error) {
363351
var res string
364352
if !cgroups.IsCgroup2UnifiedMode() {
@@ -379,14 +367,22 @@ func (h *containerHandler) GetContainerIPAddress() string {
379367
return h.ipAddress
380368
}
381369

382-
func (h *containerHandler) ListProcesses(listType container.ListType) ([]int, error) {
383-
return h.libcontainerHandler.GetProcesses()
384-
}
385-
386370
func (h *containerHandler) Exists() bool {
387371
return common.CgroupExists(h.cgroupPaths)
388372
}
389373

374+
func (h *containerHandler) Cleanup() {
375+
if h.fsHandler != nil {
376+
h.fsHandler.Stop()
377+
}
378+
}
379+
380+
func (h *containerHandler) Start() {
381+
if h.fsHandler != nil {
382+
h.fsHandler.Start()
383+
}
384+
}
385+
390386
func (h *containerHandler) Type() container.ContainerType {
391387
return container.ContainerTypeDocker
392388
}

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)