Skip to content

Commit

Permalink
handle multiple possible cri socket locations
Browse files Browse the repository at this point in the history
  • Loading branch information
orishavit committed Sep 17, 2024
1 parent f968856 commit 3b19f6c
Showing 1 changed file with 41 additions and 7 deletions.
48 changes: 41 additions & 7 deletions src/node-agent/pkg/service/cri.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,59 @@
package service

import (
"fmt"
cri "github.com/otterize/network-mapper/src/shared/criclient"
"github.com/samber/lo"
"github.com/sirupsen/logrus"
internalapi "k8s.io/cri-api/pkg/apis"
"k8s.io/klog/v2"
"os"
"time"
)

const CRISocketsPath = "/run/cri"

// CreateCRIClientOrDie tries to connect to CRI sockets in CRISocketsPath
// and returns a RuntimeService for the first valid socket found. If
// none found, the function panics.
// This is done to handle multiple possible locations of CRI sockets, as
// we cannot know during deployment which container runtime will be used.
func CreateCRIClientOrDie() internalapi.RuntimeService {
logger := klog.Background()

criClient, err := cri.NewRemoteRuntimeService(
"unix:///var/run/containerd/containerd.sock",
time.Second*5,
&logger,
)
endpoints, err := getPossibleCRISockets(CRISocketsPath)

if err != nil {
logrus.WithError(err).Panic("failed to list CRI sockets")
}

for _, endpoint := range endpoints {
logrus.Debugf("Trying CRI socket: %s", endpoint)

criClient, err := cri.NewRemoteRuntimeService(
fmt.Sprintf("unix://%s", endpoint),
time.Second*5,
&logger,
)

if err == nil {
logrus.Infof("Connected to CRI socket: %s", endpoint)
return criClient
}
}

logrus.Fatal("Failed to connect to any CRI socket")
return nil
}

func getPossibleCRISockets(basePath string) ([]string, error) {
sockets, err := os.ReadDir(basePath)

if err != nil {
logrus.WithError(err).Panic("failed to create CRI client")
return nil, err
}

return criClient
return lo.Map(sockets, func(info os.DirEntry, _ int) string {
return basePath + "/" + info.Name()
}), nil
}

0 comments on commit 3b19f6c

Please sign in to comment.