Skip to content

Commit

Permalink
fix: fix containerd mergeddir (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
xzchaoo authored Mar 7, 2024
1 parent 8ebf721 commit 7ad2670
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
11 changes: 11 additions & 0 deletions pkg/cri/containerdutils/containerd_k3s.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* Copyright 2022 Holoinsight Project Authors. Licensed under Apache-2.0.
*/

package containerdutils

// K3s is a commonly used distribution of K8s and we have built-in support for it.
const (
K3sDefaultStateDir = "/run/k3s/containerd"
K3sDefaultAddress = "/run/k3s/containerd/containerd.sock"
)
3 changes: 2 additions & 1 deletion pkg/cri/containerdutils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import (
"context"
"errors"
"github.com/containerd/containerd"
"github.com/containerd/containerd/defaults"
"github.com/traas-stack/holoinsight-agent/pkg/core"
"time"
)

// NewClientFromEnv create a containerd client based on the agreed environment configuration information.
func NewClientFromEnv(addrs ...string) (*containerd.Client, containerd.Version, error) {
// Default to two well known containerd sock
defaultAddrs := append(addrs, "/run/containerd/containerd.sock", "/run/k3s/containerd/containerd.sock")
defaultAddrs := append(addrs, defaults.DefaultAddress, K3sDefaultAddress)

addr := core.FindFirstSockInHostfs("CONTAINERD_SOCK", defaultAddrs...)
if addr == "" {
Expand Down
19 changes: 18 additions & 1 deletion pkg/cri/impl/engine/containerd_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ import (
"github.com/containerd/containerd"
"github.com/containerd/containerd/api/services/tasks/v1"
"github.com/containerd/containerd/cio"
"github.com/containerd/containerd/defaults"
runc_options "github.com/containerd/containerd/runtime/v2/runc/options"
"github.com/traas-stack/holoinsight-agent/pkg/core"
"github.com/traas-stack/holoinsight-agent/pkg/cri/containerdutils"
"github.com/traas-stack/holoinsight-agent/pkg/logger"
"github.com/traas-stack/holoinsight-agent/pkg/util"
"go.uber.org/zap"
Expand Down Expand Up @@ -215,10 +218,24 @@ func (e *ContainerdContainerEngine) GetContainerDetail(ctx context.Context, cid
detail.NetworkMode = "netns:" + sandboxMeta.NetNSPath
}

// I don't know how to get containerd's state dir.
// But I know its default value.
// Tested OK on containerd v1.6.8.
maybeContainerdStateDirs := []string{defaults.DefaultStateDir, containerdutils.K3sDefaultStateDir}
for _, stateDir := range maybeContainerdStateDirs {
// TODO It seems that for the old version of containerd, the rootfs of the container is not this.
containerRootfs := filepath.Join(stateDir, "io.containerd.runtime.v2.task", "k8s.io", cid, "rootfs")
if st, err := os.Stat(filepath.Join(core.GetHostfs(), containerRootfs)); err == nil && st.IsDir() {
detail.MergedDir = containerRootfs
break
}
}

logger.Debugz("[containerd] details",
zap.String("cid", cid),
zap.Any("container", &container),
zap.Any("runtime", runtime),
zap.Any("runtime", &runtime),
zap.Any("spec", &spec),
zap.Any("containerMeta", &containerMeta),
zap.Any("sandboxMeta", &sandboxMeta),
)
Expand Down

0 comments on commit 7ad2670

Please sign in to comment.