From 150c78bd124181c983f86b423c6877fab1451ff7 Mon Sep 17 00:00:00 2001 From: 0902horn <0902horn@gmail.com> Date: Fri, 22 Mar 2019 16:35:33 +0800 Subject: [PATCH] Make the containerd factory configurable --- container/containerd/client.go | 10 ++-------- container/containerd/factory.go | 7 ++++--- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/container/containerd/client.go b/container/containerd/client.go index 4eccf4f6..beaa2021 100644 --- a/container/containerd/client.go +++ b/container/containerd/client.go @@ -31,11 +31,6 @@ import ( "google.golang.org/grpc" ) -const ( - // k8sNamespace is the namespace we use to connect containerd. - k8sNamespace = "k8s.io" -) - type client struct { containerService containersapi.ContainersClient taskService tasksapi.TasksClient @@ -52,13 +47,12 @@ var once sync.Once var ctrdClient containerdClient = nil const ( - address = "/run/containerd/containerd.sock" maxBackoffDelay = 3 * time.Second connectionTimeout = 2 * time.Second ) // Client creates a containerd client -func Client() (containerdClient, error) { +func Client(address, namespace string) (containerdClient, error) { var retErr error once.Do(func() { tryConn, err := net.DialTimeout("unix", address, connectionTimeout) @@ -75,7 +69,7 @@ func Client() (containerdClient, error) { grpc.WithBackoffMaxDelay(maxBackoffDelay), grpc.WithTimeout(connectionTimeout), } - unary, stream := newNSInterceptors(k8sNamespace) + unary, stream := newNSInterceptors(namespace) gopts = append(gopts, grpc.WithUnaryInterceptor(unary), grpc.WithStreamInterceptor(stream), diff --git a/container/containerd/factory.go b/container/containerd/factory.go index 2a7f9dd5..87cf0e99 100644 --- a/container/containerd/factory.go +++ b/container/containerd/factory.go @@ -31,7 +31,8 @@ import ( "github.com/google/cadvisor/manager/watcher" ) -var ArgContainerdEndpoint = flag.String("containerd", "unix:///var/run/containerd.sock", "containerd endpoint") +var ArgContainerdEndpoint = flag.String("containerd", "/run/containerd/containerd.sock", "containerd endpoint") +var ArgContainerdNamespace = flag.String("containerd-namespace", "k8s.io", "containerd namespace") // The namespace under which containerd aliases are unique. const k8sContainerdNamespace = "containerd" @@ -56,7 +57,7 @@ func (self *containerdFactory) String() string { } func (self *containerdFactory) NewContainerHandler(name string, inHostNamespace bool) (handler container.ContainerHandler, err error) { - client, err := Client() + client, err := Client(*ArgContainerdEndpoint, *ArgContainerdNamespace) if err != nil { return } @@ -118,7 +119,7 @@ func (self *containerdFactory) DebugInfo() map[string][]string { // Register root container before running this function! func Register(factory info.MachineInfoFactory, fsInfo fs.FsInfo, includedMetrics container.MetricSet) error { - client, err := Client() + client, err := Client(*ArgContainerdEndpoint, *ArgContainerdNamespace) if err != nil { return fmt.Errorf("unable to create containerd client: %v", err) }