MCPcopy
hub / github.com/google/cadvisor / newContainerdContainerHandler

Function newContainerdContainerHandler

lib/container/containerd/handler.go:62–175  ·  view source on GitHub ↗

newContainerdContainerHandler returns a new container.ContainerHandler

(
	client ContainerdClient,
	name string,
	machineInfoFactory info.MachineInfoFactory,
	fsInfo fs.FsInfo,
	cgroupSubsystems map[string]string,
	inHostNamespace bool,
	metadataEnvAllowList []string,
	includedMetrics container.MetricSet,
)

Source from the content-addressed store, hash-verified

60
61// newContainerdContainerHandler returns a new container.ContainerHandler
62func newContainerdContainerHandler(
63 client ContainerdClient,
64 name string,
65 machineInfoFactory info.MachineInfoFactory,
66 fsInfo fs.FsInfo,
67 cgroupSubsystems map[string]string,
68 inHostNamespace bool,
69 metadataEnvAllowList []string,
70 includedMetrics container.MetricSet,
71) (container.ContainerHandler, error) {
72 // Create the cgroup paths.
73 cgroupPaths := common.MakeCgroupPaths(cgroupSubsystems, name)
74
75 // Generate the equivalent cgroup manager for this container.
76 cgroupManager, err := containerlibcontainer.NewCgroupManager(name, cgroupPaths)
77 if err != nil {
78 return nil, err
79 }
80
81 id := ContainerNameToContainerdID(name)
82 // We assume that if load fails then the container is not known to containerd.
83 ctx := context.Background()
84 cntr, err := client.LoadContainer(ctx, id)
85 if err != nil {
86 return nil, err
87 }
88
89 var spec specs.Spec
90 if err := json.Unmarshal(cntr.Spec.Value, &spec); err != nil {
91 return nil, err
92 }
93
94 // Cgroup is created during task creation. When cadvisor sees the cgroup,
95 // task may not be fully created yet. Use a retry+backoff to tolerant the
96 // race condition.
97 // TODO(random-liu): Use cri-containerd client to talk with cri-containerd
98 // instead. cri-containerd has some internal synchronization to make sure
99 // `ContainerStatus` only returns result after `StartContainer` finishes.
100 var taskPid uint32
101 backoff := 100 * time.Millisecond
102 retry := 5
103 for {
104 taskPid, err = client.TaskPid(ctx, id)
105 if err == nil {
106 break
107 }
108
109 // Retry when task is not created yet or task is in unknown state (likely in process of initializing)
110 isRetriableError := errors.Is(err, ErrNotFound) || errors.Is(err, ErrTaskIsInUnknownState)
111 if !isRetriableError || retry == 0 {
112 return nil, err
113 }
114
115 retry--
116 time.Sleep(backoff)
117 backoff *= 2
118 }
119

Callers 2

NewContainerHandlerMethod · 0.85
TestHandlerFunction · 0.85

Calls 5

MakeCgroupPathsFunction · 0.92
RemoveNetMetricsFunction · 0.92
LoadContainerMethod · 0.65
TaskPidMethod · 0.65

Tested by 1

TestHandlerFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…