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.
1616package docker
1717
1818import (
@@ -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-
310295func (h * containerHandler ) ContainerReference () (info.ContainerReference , error ) {
311296 return h .reference , nil
312297}
313298
314299func (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+
362350func (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-
386370func (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+
390386func (h * containerHandler ) Type () container.ContainerType {
391387 return container .ContainerTypeDocker
392388}
0 commit comments