From 2c5a11a55bf76e934b35839569fb97aa604f72b7 Mon Sep 17 00:00:00 2001 From: David Leadbeater Date: Fri, 16 Jun 2023 17:53:04 +1000 Subject: [PATCH] Match on cri-o socket suffix only This deals with the case that a user can configure cri-o to use /run/crio/crio.sock and get very confusing behavior. See https://github.com/cri-o/cri-o/issues/7010#issuecomment-1594149469 --- pkg/kubelet/cadvisor/cadvisor_linux_test.go | 3 ++- pkg/kubelet/cadvisor/helpers_linux.go | 3 ++- pkg/kubelet/cadvisor/util.go | 10 +++++++--- pkg/kubelet/cadvisor/util_test.go | 3 ++- 4 files changed, 13 insertions(+), 6 deletions(-) diff --git a/pkg/kubelet/cadvisor/cadvisor_linux_test.go b/pkg/kubelet/cadvisor/cadvisor_linux_test.go index c508dccdaae..144516939ce 100644 --- a/pkg/kubelet/cadvisor/cadvisor_linux_test.go +++ b/pkg/kubelet/cadvisor/cadvisor_linux_test.go @@ -25,6 +25,7 @@ import ( "github.com/stretchr/testify/assert" + "github.com/google/cadvisor/container/crio" cadvisorfs "github.com/google/cadvisor/fs" ) @@ -37,7 +38,7 @@ func TestImageFsInfoLabel(t *testing.T) { expectedError error }{{ description: "LabelCrioImages should be returned", - runtimeEndpoint: CrioSocket, + runtimeEndpoint: crio.CrioSocket, expectedLabel: cadvisorfs.LabelCrioImages, expectedError: nil, }, { diff --git a/pkg/kubelet/cadvisor/helpers_linux.go b/pkg/kubelet/cadvisor/helpers_linux.go index c512d3d0510..7851cf5376b 100644 --- a/pkg/kubelet/cadvisor/helpers_linux.go +++ b/pkg/kubelet/cadvisor/helpers_linux.go @@ -21,6 +21,7 @@ package cadvisor import ( "fmt" + "strings" cadvisorfs "github.com/google/cadvisor/fs" ) @@ -37,7 +38,7 @@ func (i *imageFsInfoProvider) ImageFsInfoLabel() (string, error) { // This is a temporary workaround to get stats for cri-o from cadvisor // and should be removed. // Related to https://github.com/kubernetes/kubernetes/issues/51798 - if i.runtimeEndpoint == CrioSocket || i.runtimeEndpoint == "unix://"+CrioSocket { + if strings.HasSuffix(i.runtimeEndpoint, CrioSocketSuffix) { return cadvisorfs.LabelCrioImages, nil } return "", fmt.Errorf("no imagefs label for configured runtime") diff --git a/pkg/kubelet/cadvisor/util.go b/pkg/kubelet/cadvisor/util.go index d5ad3a6b580..24f9e5eb50a 100644 --- a/pkg/kubelet/cadvisor/util.go +++ b/pkg/kubelet/cadvisor/util.go @@ -17,6 +17,8 @@ limitations under the License. package cadvisor import ( + "strings" + cadvisorapi "github.com/google/cadvisor/info/v1" cadvisorapi2 "github.com/google/cadvisor/info/v2" "k8s.io/api/core/v1" @@ -25,10 +27,12 @@ import ( ) const ( - // CrioSocket is the path to the CRI-O socket. + // CrioSocketSuffix is the path to the CRI-O socket. // Please keep this in sync with the one in: // github.com/google/cadvisor/tree/master/container/crio/client.go - CrioSocket = "/var/run/crio/crio.sock" + // Note that however we only match on the suffix, as /var/run is often a + // symlink to /run, so the user can specify either path. + CrioSocketSuffix = "run/crio/crio.sock" ) // CapacityFromMachineInfo returns the capacity of the resources from the machine info. @@ -69,5 +73,5 @@ func EphemeralStorageCapacityFromFsInfo(info cadvisorapi2.FsInfo) v1.ResourceLis // be removed. Related issue: // https://github.com/kubernetes/kubernetes/issues/51798 func UsingLegacyCadvisorStats(runtimeEndpoint string) bool { - return runtimeEndpoint == CrioSocket || runtimeEndpoint == "unix://"+CrioSocket + return strings.HasSuffix(runtimeEndpoint, CrioSocketSuffix) } diff --git a/pkg/kubelet/cadvisor/util_test.go b/pkg/kubelet/cadvisor/util_test.go index 2fa09e54f2f..375b1cf4fdb 100644 --- a/pkg/kubelet/cadvisor/util_test.go +++ b/pkg/kubelet/cadvisor/util_test.go @@ -21,6 +21,7 @@ package cadvisor import ( "reflect" + "strings" "testing" "github.com/google/cadvisor/container/crio" @@ -54,5 +55,5 @@ func TestCapacityFromMachineInfoWithHugePagesEnable(t *testing.T) { } func TestCrioSocket(t *testing.T) { - assert.EqualValues(t, CrioSocket, crio.CrioSocket, "CrioSocket in this package must equal the one in github.com/google/cadvisor/container/crio/client.go") + assert.True(t, strings.HasSuffix(crio.CrioSocket, CrioSocketSuffix), "CrioSocketSuffix in this package must be a suffix of the one in github.com/google/cadvisor/container/crio/client.go") }