Skip to content

Commit 52f7d1d

Browse files
authored
Merge pull request #2203 from 0902horn/configurable-containerd-factory
Make the containerd factory configurable
2 parents 50076da + 150c78b commit 52f7d1d

File tree

2 files changed

+6
-11
lines changed

2 files changed

+6
-11
lines changed

container/containerd/client.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,6 @@ import (
3131
"google.golang.org/grpc"
3232
)
3333

34-
const (
35-
// k8sNamespace is the namespace we use to connect containerd.
36-
k8sNamespace = "k8s.io"
37-
)
38-
3934
type client struct {
4035
containerService containersapi.ContainersClient
4136
taskService tasksapi.TasksClient
@@ -52,13 +47,12 @@ var once sync.Once
5247
var ctrdClient containerdClient = nil
5348

5449
const (
55-
address = "/run/containerd/containerd.sock"
5650
maxBackoffDelay = 3 * time.Second
5751
connectionTimeout = 2 * time.Second
5852
)
5953

6054
// Client creates a containerd client
61-
func Client() (containerdClient, error) {
55+
func Client(address, namespace string) (containerdClient, error) {
6256
var retErr error
6357
once.Do(func() {
6458
tryConn, err := net.DialTimeout("unix", address, connectionTimeout)
@@ -75,7 +69,7 @@ func Client() (containerdClient, error) {
7569
grpc.WithBackoffMaxDelay(maxBackoffDelay),
7670
grpc.WithTimeout(connectionTimeout),
7771
}
78-
unary, stream := newNSInterceptors(k8sNamespace)
72+
unary, stream := newNSInterceptors(namespace)
7973
gopts = append(gopts,
8074
grpc.WithUnaryInterceptor(unary),
8175
grpc.WithStreamInterceptor(stream),

container/containerd/factory.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ import (
3131
"github.com/google/cadvisor/manager/watcher"
3232
)
3333

34-
var ArgContainerdEndpoint = flag.String("containerd", "unix:///var/run/containerd.sock", "containerd endpoint")
34+
var ArgContainerdEndpoint = flag.String("containerd", "/run/containerd/containerd.sock", "containerd endpoint")
35+
var ArgContainerdNamespace = flag.String("containerd-namespace", "k8s.io", "containerd namespace")
3536

3637
// The namespace under which containerd aliases are unique.
3738
const k8sContainerdNamespace = "containerd"
@@ -56,7 +57,7 @@ func (self *containerdFactory) String() string {
5657
}
5758

5859
func (self *containerdFactory) NewContainerHandler(name string, inHostNamespace bool) (handler container.ContainerHandler, err error) {
59-
client, err := Client()
60+
client, err := Client(*ArgContainerdEndpoint, *ArgContainerdNamespace)
6061
if err != nil {
6162
return
6263
}
@@ -118,7 +119,7 @@ func (self *containerdFactory) DebugInfo() map[string][]string {
118119

119120
// Register root container before running this function!
120121
func Register(factory info.MachineInfoFactory, fsInfo fs.FsInfo, includedMetrics container.MetricSet) error {
121-
client, err := Client()
122+
client, err := Client(*ArgContainerdEndpoint, *ArgContainerdNamespace)
122123
if err != nil {
123124
return fmt.Errorf("unable to create containerd client: %v", err)
124125
}

0 commit comments

Comments
 (0)