Skip to content

Commit dd3d58c

Browse files
ddelnanok8sstormcenter-buildbot
authored andcommitted
Reduce boiler plate for Go container image C++ headers (pixie-io#2307)
Summary: Reduce boiler plate for Go container image C++ headers Previously, adding a new Go version required: 1. Creating new header files for each container type (grpc_server, grpc_client, tls_server, tls_client) 2. Adding BUILD.bazel entries for each new library 3. Updating test files with new #include statements These header files account for ~100-200 lines of boilerplate code per Go version (~50 lines for each grpc and tls client/server pair) and add overhead when upgrading our Go version. This PR reduces this boilerplate by generating these files with a new Bazel macro `go_container_libraries`. This macro generates: - Individual C++ headers for each version (e.g., `go_1_24_grpc_server_container.h`) - Aggregate headers that include all versions for a given container type (e.g., `go_grpc_server_containers.h`, `go_tls_client_containers.h`) Relevant Issues: N/A Type of change: /kind cleanup Test Plan: Build should succeed Signed-off-by: Dom Del Nano <ddelnano@gmail.com> GitOrigin-RevId: ce714e6
1 parent d549b47 commit dd3d58c

2 files changed

Lines changed: 4 additions & 83 deletions

File tree

src/shared/metadata/cgroup_path_resolver.cc

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -70,24 +70,13 @@ StatusOr<std::vector<std::string>> CGroupBasePaths(std::string_view sysfs_path)
7070

7171
StatusOr<std::string> FindSelfCGroupProcs(std::string_view base_path) {
7272
int pid = getpid();
73-
std::error_code ec;
7473

75-
auto it = std::filesystem::recursive_directory_iterator(
76-
base_path, std::filesystem::directory_options::skip_permission_denied, ec);
77-
if (ec) {
78-
return error::Internal("Failed to iterate cgroup path: $0", ec.message());
79-
}
80-
81-
for (auto end = std::filesystem::recursive_directory_iterator(); it != end; it.increment(ec)) {
82-
if (ec) {
83-
ec.clear();
84-
continue;
85-
}
86-
if (it->path().filename() == "cgroup.procs") {
87-
std::string contents = ReadFileToString(it->path().string()).ValueOr("");
74+
for (auto& p : std::filesystem::recursive_directory_iterator(base_path)) {
75+
if (p.path().filename() == "cgroup.procs") {
76+
std::string contents = ReadFileToString(p.path().string()).ValueOr("");
8877
int contents_pid;
8978
if (absl::SimpleAtoi(contents, &contents_pid) && pid == contents_pid) {
90-
return it->path().string();
79+
return p.path().string();
9180
}
9281
}
9382
}

src/shared/metadata/cgroup_path_resolver_test.cc

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,11 @@
1616
* SPDX-License-Identifier: Apache-2.0
1717
*/
1818

19-
#include <sys/stat.h>
20-
#include <unistd.h>
21-
2219
#include <gtest/gtest.h>
2320

24-
#include <fstream>
2521
#include <string>
2622
#include <vector>
2723

28-
#include "src/common/testing/temp_dir.h"
2924
#include "src/common/testing/testing.h"
3025
#include "src/shared/metadata/cgroup_path_resolver.h"
3126

@@ -395,68 +390,5 @@ TEST(CGroupPathResolver, Cgroup2Format) {
395390
* 4. cgroup1+cgroup2 w/ cgroup1 succeeding
396391
*/
397392

398-
// Test that FindSelfCGroupProcs gracefully handles permission-denied directories
399-
// (e.g. CrowdStrike Falcon's sandbox.falcon) instead of crashing with an uncaught exception.
400-
TEST(FindSelfCGroupProcs, SkipsPermissionDeniedDirectories) {
401-
// This test requires running as non-root, since root bypasses permission checks.
402-
if (getuid() == 0) {
403-
GTEST_SKIP() << "Test requires non-root user";
404-
}
405-
406-
px::testing::TempDir tmp_dir;
407-
auto base_path = tmp_dir.path();
408-
409-
// Create a directory structure with an accessible cgroup.procs containing our PID,
410-
// and a restricted directory that simulates CrowdStrike Falcon's sandbox.
411-
auto accessible_dir = base_path / "kubepods" / "pod1234";
412-
std::filesystem::create_directories(accessible_dir);
413-
414-
// Write our PID to cgroup.procs so FindSelfCGroupProcs can find it.
415-
{
416-
std::ofstream ofs((accessible_dir / "cgroup.procs").string());
417-
ofs << getpid();
418-
}
419-
420-
// Create a restricted directory that the iterator cannot enter.
421-
auto restricted_dir = base_path / "system.slice" / "falcon-sensor.service" / "sandbox.falcon";
422-
std::filesystem::create_directories(restricted_dir);
423-
// Remove all permissions on the sandbox directory.
424-
chmod(restricted_dir.c_str(), 0000);
425-
426-
// FindSelfCGroupProcs should succeed and find our cgroup.procs,
427-
// skipping the restricted directory instead of throwing.
428-
ASSERT_OK_AND_ASSIGN(auto result, FindSelfCGroupProcs(base_path.string()));
429-
EXPECT_EQ(result, (accessible_dir / "cgroup.procs").string());
430-
431-
// Restore permissions so TempDir cleanup can remove it.
432-
chmod(restricted_dir.c_str(), 0755);
433-
}
434-
435-
// Test that FindSelfCGroupProcs returns NotFound (not a crash) when the only
436-
// cgroup.procs is behind a restricted directory.
437-
TEST(FindSelfCGroupProcs, ReturnsNotFoundWhenAllPathsRestricted) {
438-
if (getuid() == 0) {
439-
GTEST_SKIP() << "Test requires non-root user";
440-
}
441-
442-
px::testing::TempDir tmp_dir;
443-
auto base_path = tmp_dir.path();
444-
445-
// Put cgroup.procs inside a restricted directory so it's unreachable.
446-
auto restricted_dir = base_path / "restricted";
447-
std::filesystem::create_directories(restricted_dir);
448-
{
449-
std::ofstream ofs((restricted_dir / "cgroup.procs").string());
450-
ofs << getpid();
451-
}
452-
chmod(restricted_dir.c_str(), 0000);
453-
454-
// Should return NotFound, not crash.
455-
auto result = FindSelfCGroupProcs(base_path.string());
456-
EXPECT_NOT_OK(result);
457-
458-
chmod(restricted_dir.c_str(), 0755);
459-
}
460-
461393
} // namespace md
462394
} // namespace px

0 commit comments

Comments
 (0)