From fc0b024fcdd55cf6cb893fc88de01e0e647905a9 Mon Sep 17 00:00:00 2001 From: Bryce Kahle Date: Wed, 17 Jan 2024 14:45:17 -0800 Subject: [PATCH] fix empty platform information If github.com/shirou/gopsutil/v3 is used and a [/host]/etc/redhat-release file/dir exists but is empty, then it returns empty platform information. This affects helm-chart users who have kernel header downloading turned on, because it always mounts this file and k8s will create it even if it doesn't exist. --- pkg/util/kernel/platform.go | 8 +++--- pkg/util/kernel/platform_test.go | 46 ++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 pkg/util/kernel/platform_test.go diff --git a/pkg/util/kernel/platform.go b/pkg/util/kernel/platform.go index c79dcf47a6f60..96fa41ca3d785 100644 --- a/pkg/util/kernel/platform.go +++ b/pkg/util/kernel/platform.go @@ -8,7 +8,7 @@ package kernel import ( - gopsutilhost "github.com/shirou/gopsutil/v3/host" + gopsutilhost "github.com/DataDog/gopsutil/host" "github.com/DataDog/datadog-agent/pkg/util/funcs" ) @@ -31,7 +31,9 @@ var PlatformVersion = funcs.Memoize(func() (string, error) { return pi.version, err }) -var platformInformation = funcs.Memoize(func() (platformInfo, error) { +var platformInformation = funcs.Memoize(getPlatformInformation) + +func getPlatformInformation() (platformInfo, error) { platform, family, version, err := gopsutilhost.PlatformInformation() return platformInfo{platform, family, version}, err -}) +} diff --git a/pkg/util/kernel/platform_test.go b/pkg/util/kernel/platform_test.go new file mode 100644 index 0000000000000..814dad4e00bd2 --- /dev/null +++ b/pkg/util/kernel/platform_test.go @@ -0,0 +1,46 @@ +// Unless explicitly stated otherwise all files in this repository are licensed +// under the Apache License Version 2.0. +// This product includes software developed at Datadog (https://www.datadoghq.com/). +// Copyright 2016-present Datadog, Inc. + +//go:build linux + +package kernel + +import ( + "errors" + "io" + "io/fs" + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestPlatform(t *testing.T) { + osr, err := os.Open("/etc/os-release") + if err != nil && errors.Is(err, fs.ErrNotExist) { + t.Skip("/etc/os-release does not exist") + } + require.NoError(t, err) + t.Cleanup(func() { _ = osr.Close() }) + + tmp := t.TempDir() + t.Setenv("HOST_ETC", tmp) + require.NoError(t, os.Mkdir(filepath.Join(tmp, "redhat-release"), 0755)) + + // copy /etc/os-release to /os-release + dosr, err := os.Create(filepath.Join(tmp, "os-release")) + require.NoError(t, err) + t.Cleanup(func() { _ = dosr.Close() }) + _, err = io.Copy(dosr, osr) + require.NoError(t, err) + _ = dosr.Close() + + pi, err := getPlatformInformation() + require.NoError(t, err) + require.NotEmpty(t, pi.platform, "platform") + require.NotEmpty(t, pi.family, "family") + require.NotEmpty(t, pi.version, "version") +}