From 96cd9ba725076ac66b327eb2f02582de06e3c52e Mon Sep 17 00:00:00 2001 From: "bingshen.wbs" Date: Mon, 17 Jul 2023 11:00:51 +0800 Subject: [PATCH] support k3s runtime api & support specify cri address Signed-off-by: bingshen.wbs --- pkg/skoop/collector/manager/config.go | 2 ++ pkg/skoop/collector/manager/manager.go | 6 ++++++ pkg/skoop/collector/podcollector/collector.go | 15 ++++++++++++++- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/pkg/skoop/collector/manager/config.go b/pkg/skoop/collector/manager/config.go index bcb1d9ac..7919ff98 100644 --- a/pkg/skoop/collector/manager/config.go +++ b/pkg/skoop/collector/manager/config.go @@ -21,6 +21,7 @@ func init() { type SimplePodCollectorConfig struct { Image string CollectorNamespace string + RuntimeAPIAddress string WaitInterval time.Duration WaitTimeout time.Duration PreserveCollectorPod bool @@ -29,6 +30,7 @@ type SimplePodCollectorConfig struct { func (cc *SimplePodCollectorConfig) BindFlags(fs *pflag.FlagSet) { fs.StringVarP(&cc.Image, "collector-image", "", "kubeskoop/kubeskoop:v0.1.0", "Image used for collector.") fs.StringVarP(&cc.CollectorNamespace, "collector-namespace", "", "skoop", "Namespace where collector pods in.") + fs.StringVarP(&cc.CollectorNamespace, "collector-cri-address", "", "", "Runtime CRI API endpoint address.") fs.DurationVarP(&cc.WaitInterval, "collector-pod-wait-interval", "", 2*time.Second, "Collector pod running check interval.") fs.DurationVarP(&cc.WaitTimeout, "collector-pod-wait-timeout", "", 120*time.Second, "Collector pod running check timeout.") fs.BoolVarP(&cc.PreserveCollectorPod, "preserve-collector-pod", "", false, "Preserve collector pod after diagnosis complete.") diff --git a/pkg/skoop/collector/manager/manager.go b/pkg/skoop/collector/manager/manager.go index c0695218..533567c0 100644 --- a/pkg/skoop/collector/manager/manager.go +++ b/pkg/skoop/collector/manager/manager.go @@ -46,6 +46,7 @@ type SimplePodCollectorManagerOptions struct { type simplePodCollectorManager struct { image string namespace string + runtimeAPIAddress string client *kubernetes.Clientset restConfig *rest.Config ipCache *k8s.IPCache @@ -86,6 +87,7 @@ func NewSimplePodCollectorManager(ctx *ctx.Context) (collector.Manager, error) { waitInterval: Config.SimplePodCollectorConfig.WaitInterval, waitTimeout: Config.SimplePodCollectorConfig.WaitTimeout, preserveCollectorPod: Config.SimplePodCollectorConfig.PreserveCollectorPod, + runtimeAPIAddress: Config.SimplePodCollectorConfig.RuntimeAPIAddress, }, nil } @@ -334,6 +336,10 @@ func (m *simplePodCollectorManager) createCollectorPod(nodeName string) (*v1.Pod SecurityContext: &v1.SecurityContext{ Privileged: pointer.Bool(true), }, + Env: []v1.EnvVar{{ + Name: "RUNTIME_SOCK", + Value: m.runtimeAPIAddress, + }}, Command: []string{"/bin/pod-collector"}, VolumeMounts: []v1.VolumeMount{ { diff --git a/pkg/skoop/collector/podcollector/collector.go b/pkg/skoop/collector/podcollector/collector.go index be04e520..1cceea8d 100644 --- a/pkg/skoop/collector/podcollector/collector.go +++ b/pkg/skoop/collector/podcollector/collector.go @@ -88,7 +88,20 @@ func NewCollector(podNamespace, podName, runtimeEndpoint string) (collector.Coll _, err := os.Stat("/var/run/dockershim.sock") if err != nil { if os.IsNotExist(err) { - socket = "unix:///run/containerd/containerd.sock" + containerdSockets := []string{ + "unix:///run/containerd/containerd.sock", + "unix:///run/k3s/containerd/containerd.sock", + } + + for _, containerdAddr := range containerdSockets { + if _, err = os.Stat(strings.TrimPrefix(containerdAddr, "unix://")); err == nil { + socket = containerdAddr + break + } + } + if socket == "" { + return nil, fmt.Errorf("cannot found comportable endpoint address for cri-api, please specify cri address by --collector-cri-address") + } } else { return nil, err }