From 96bf2cb2cc5f2285eec9151531731ba9f0210434 Mon Sep 17 00:00:00 2001 From: Dom Del Nano Date: Thu, 8 Aug 2024 14:03:07 +0000 Subject: [PATCH] Attempt to use cgroup v2 prior to cgroup v1 in path resolver Signed-off-by: Dom Del Nano --- src/shared/metadata/cgroup_path_resolver.cc | 18 ++++++++++++------ src/shared/metadata/cgroup_path_resolver.h | 1 + .../metadata/cgroup_path_resolver_test.cc | 10 ++++++++++ 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/shared/metadata/cgroup_path_resolver.cc b/src/shared/metadata/cgroup_path_resolver.cc index 6086ef69417..51f5d8bfdf0 100644 --- a/src/shared/metadata/cgroup_path_resolver.cc +++ b/src/shared/metadata/cgroup_path_resolver.cc @@ -28,11 +28,21 @@ #include "src/shared/metadata/cgroup_path_resolver.h" DEFINE_bool(force_cgroup2_mode, true, "Flag to force assume cgroup2 fs for testing purposes"); +DEFINE_bool(force_cgroup1_mode, false, "Flag to force use of cgroup v1 fs"); namespace px { namespace md { StatusOr CGroupBasePath(std::string_view sysfs_path) { + std::string cgv2_base_path = absl::StrCat(sysfs_path, "/cgroup"); + struct statfs info; + auto fs_status = statfs(cgv2_base_path.c_str(), &info); + bool cgroupv2 = (fs_status == 0) && (info.f_type == CGROUP2_SUPER_MAGIC); + + if (cgroupv2 && !FLAGS_force_cgroup1_mode) { + return cgv2_base_path; + } + // Different hosts may mount different cgroup dirs. Try a couple for robustness. const std::vector cgroup_dirs = {"cpu,cpuacct", "cpu", "pids"}; @@ -47,15 +57,11 @@ StatusOr CGroupBasePath(std::string_view sysfs_path) { } } - std::string cgv2_base_path = absl::StrCat(sysfs_path, "/cgroup"); - struct statfs info; - auto fs_status = statfs(cgv2_base_path.c_str(), &info); - bool cgroupv2 = (fs_status == 0) && (info.f_type == CGROUP2_SUPER_MAGIC); - + // TODO(ddelnano): this aids in testing the LegacyCGroupPathResolver. Determine what + // to do with this. if (cgroupv2 || FLAGS_force_cgroup2_mode) { return cgv2_base_path; } - // (TODO): This check for cgroup2FS is eventually to be moved above the cgroupv1 check. return error::NotFound("Could not find CGroup base path"); } diff --git a/src/shared/metadata/cgroup_path_resolver.h b/src/shared/metadata/cgroup_path_resolver.h index b3aca9a5ea9..053f891337d 100644 --- a/src/shared/metadata/cgroup_path_resolver.h +++ b/src/shared/metadata/cgroup_path_resolver.h @@ -28,6 +28,7 @@ #include "src/shared/metadata/k8s_objects.h" DECLARE_bool(force_cgroup2_mode); +DECLARE_bool(force_cgroup1_mode); namespace px { namespace md { diff --git a/src/shared/metadata/cgroup_path_resolver_test.cc b/src/shared/metadata/cgroup_path_resolver_test.cc index 8046908c80b..a6282f81df8 100644 --- a/src/shared/metadata/cgroup_path_resolver_test.cc +++ b/src/shared/metadata/cgroup_path_resolver_test.cc @@ -32,6 +32,7 @@ constexpr std::string_view kContainerID = "a7638fe3934b37419cc56bca73465a02b354ba6e98e10272542d84eb2014dd62"; TEST(CGroupPathResolver, GKEFormat) { + FLAGS_force_cgroup1_mode = true; std::string cgroup_kubepod_path = "/sys/fs/cgroup/cpu,cpuacct/kubepods/pod8dbc5577-d0e2-4706-8787-57d52c03ddf2/" "14011c7d92a9e513dfd69211da0413dbf319a5e45a02b354ba6e98e10272542d/cgroup.procs"; @@ -56,6 +57,7 @@ TEST(CGroupPathResolver, GKEFormat) { } TEST(CGroupPathResolver, GKEFormat2) { + FLAGS_force_cgroup1_mode = true; std::string cgroup_kubepod_path = "/sys/fs/cgroup/cpu,cpuacct/kubepods/burstable/podc458de04-9784-4f7a-990e-cefe26b511f0/" "01aa0bfe91e8a58da5f1f4db469fa999fe9263c702111e611445cde2b9cb0c1a/cgroup.procs"; @@ -80,6 +82,7 @@ TEST(CGroupPathResolver, GKEFormat2) { } TEST(CGroupPathResolver, StandardFormatDocker) { + FLAGS_force_cgroup1_mode = true; std::string cgroup_kubepod_path = "/sys/fs/cgroup/cpu,cpuacct/kubepods.slice/" "kubepods-pod8dbc5577_d0e2_4706_8787_57d52c03ddf2.slice/" @@ -111,6 +114,7 @@ TEST(CGroupPathResolver, StandardFormatDocker) { } TEST(CGroupPathResolver, StandardFormatCRIO) { + FLAGS_force_cgroup1_mode = true; std::string cgroup_kubepod_path = "/sys/fs/cgroup/cpu,cpuacct/kubepods.slice/" "kubepods-pod8dbc5577_d0e2_4706_8787_57d52c03ddf2.slice/" @@ -142,6 +146,7 @@ TEST(CGroupPathResolver, StandardFormatCRIO) { } TEST(CGroupPathResolver, OpenShiftFormat) { + FLAGS_force_cgroup1_mode = true; std::string cgroup_kubepod_path = "/sys/fs/cgroup/cpu,cpuacct/kubepods.slice/kubepods-burstable.slice/" "kubepods-burstable-pod9b7969b2_aad0_47d4_b11c_4acfd1ce018e.slice/" @@ -181,6 +186,7 @@ TEST(CGroupPathResolver, OpenShiftFormat) { } TEST(CGroupPathResolver, BareMetalK8s_1_21) { + FLAGS_force_cgroup1_mode = true; std::string cgroup_kubepod_path = "/sys/fs/cgroup/cpu,cpuacct/system.slice/containerd.service/" "kubepods-besteffort-pod1544eb37_e4f7_49eb_8cc4_3d01c41be77b.slice:cri-containerd:" @@ -224,6 +230,7 @@ std::string GetSysFsPathFromTestDataFile(const std::string& fname, } // namespace TEST(LegacyCGroupPathResolverTest, GKEFormat) { + FLAGS_force_cgroup1_mode = true; ASSERT_OK_AND_ASSIGN( auto path_resolver, LegacyCGroupPathResolver::Create(GetSysFsPathFromTestDataFile( @@ -246,6 +253,7 @@ TEST(LegacyCGroupPathResolverTest, GKEFormat) { } TEST(LegacyCGroupPathResolverTest, StandardFormat) { + FLAGS_force_cgroup1_mode = true; ASSERT_OK_AND_ASSIGN( auto path_resolver, LegacyCGroupPathResolver::Create(GetSysFsPathFromTestDataFile( @@ -317,6 +325,7 @@ TEST(LegacyCGroupPathResolverTest, StandardFormat) { } TEST(LeagcyCGroupPathResolverTest, Cgroup2Format) { + FLAGS_force_cgroup1_mode = false; ASSERT_OK_AND_ASSIGN( auto path_resolver, LegacyCGroupPathResolver::Create(GetSysFsPathFromTestDataFile( @@ -349,6 +358,7 @@ TEST(LeagcyCGroupPathResolverTest, Cgroup2Format) { } TEST(CGroupPathResolver, Cgroup2Format) { + FLAGS_force_cgroup1_mode = false; std::string cgroup_kubepod_path = "/sys/fs/cgroup/kubepods.slice/" "kubepods-pod8dbc5577_d0e2_4706_8787_57d52c03ddf2.slice/"