Skip to content

Commit 9b73fc0

Browse files
committed
chore(k8smeta): simplify cmake scripts and use VCPKG for dependencies
Simplify the cmake scripts by using `find_package` and VCPKG. Also generate the gRPC files using `protobuf_generate` instead of using a custom command. Signed-off-by: Iacopo Rozzo <[email protected]>
1 parent 0aa2f52 commit 9b73fc0

File tree

11 files changed

+154
-81
lines changed

11 files changed

+154
-81
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: 'install-vcpkg'
2+
description: 'Install vcpkg and make it available in PATH.'
3+
4+
outputs:
5+
vcpkg_root:
6+
description: "VCPKG_ROOT"
7+
value: ${{ steps.vcpkg.outputs.vcpkg_root }}
8+
9+
runs:
10+
using: "composite"
11+
steps:
12+
- name: Store vcpkg version as local output
13+
shell: bash
14+
id: store
15+
env:
16+
VCPKG_VERSION: '2025.09.17'
17+
run: |
18+
echo "vcpkg_version=${VCPKG_VERSION}" >> "$GITHUB_OUTPUT"
19+
20+
- name: Download vcpkg
21+
shell: bash
22+
run: |
23+
git clone https://github.com/microsoft/vcpkg.git --branch ${{ steps.store.outputs.vcpkg_version }} --single-branch
24+
25+
- name: Setup vcpkg
26+
shell: bash
27+
id: vcpkg
28+
run: |
29+
# Note, this is a workaround to avoid building debug versions that are not used in the build process
30+
# TODO: Find a cleaner way to do this
31+
find "$(pwd)/vcpkg/triplets/" -name "*.cmake" -type f -exec sh -c "echo \"set(VCPKG_BUILD_TYPE release)\" >> {}" \;
32+
VCPKG_MAX_CONCURRENCY=6 ./vcpkg/bootstrap-vcpkg.sh
33+
echo "$(pwd)/vcpkg" >> $GITHUB_PATH
34+
echo "VCPKG_ROOT=$(pwd)/vcpkg" >> $GITHUB_ENV
35+
# Set the maximum concurrency to 6 to avoid overwhelming the CI system
36+
echo "VCPKG_MAX_CONCURRENCY=6" >> $GITHUB_ENV
37+
38+
- name: Set Outputs
39+
id: store-outputs
40+
shell: bash
41+
run: |
42+
echo "vcpkg_root=${VCPKG_ROOT}" >> $GITHUB_OUTPUT

.github/workflows/k8smeta-ci.yaml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,13 @@ jobs:
3434
- name: Install deps ⛓️
3535
run: |
3636
sudo apt update -y
37-
sudo apt install -y --no-install-recommends cmake build-essential autoconf libtool pkg-config
37+
sudo apt install -y --no-install-recommends cmake build-essential autoconf libtool pkg-config zip unzip tar git wget
38+
39+
- name: Install vcpkg 📦
40+
uses: ./.github/actions/install-vcpkg
41+
with:
42+
# Using a specific commit to avoid unexpected issues
43+
vcpkg_version: 2025.09.17
3844

3945
- name: Initialize CodeQL
4046
uses: github/codeql-action/init@64d10c13136e1c5bce3e5fbde8d4906eeaafc885 # v3.30.6
@@ -44,9 +50,8 @@ jobs:
4450
- name: Build k8s meta plugin 🏗️
4551
run: |
4652
cd plugins/k8smeta
47-
mkdir build
48-
cd build && cmake -DCMAKE_BUILD_TYPE=Release ../
49-
make k8smeta -j6
53+
cmake -B build --preset vcpkg-release
54+
cmake --build build --target k8smeta -j$(nproc)
5055
5156
- name: Perform CodeQL Analysis
5257
uses: github/codeql-action/analyze@64d10c13136e1c5bce3e5fbde8d4906eeaafc885 # v3.30.6

.github/workflows/reusable_build_packages.yaml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
run: |
2727
apt update
2828
apt install -y --no-install-recommends awscli build-essential autoconf libelf-dev libtool autotools-dev \
29-
automake zip unzip ninja-build wget lsb-release software-properties-common gnupg
29+
automake zip unzip tar git ninja-build wget lsb-release software-properties-common gnupg
3030
3131
- name: Install updated clang version ⛓️
3232
run: |
@@ -61,6 +61,12 @@ jobs:
6161
fetch-depth: 0
6262
submodules: "recursive"
6363

64+
- name: Install vcpkg 📦
65+
uses: ./.github/actions/install-vcpkg
66+
with:
67+
# Using a specific commit to avoid unexpected issues
68+
vcpkg_version: 2025.09.17
69+
6470
- name: Safe directory
6571
run: git config --global --add safe.directory $GITHUB_WORKSPACE
6672

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
.vscode
33
.DS_Store
44
.idea
5+
.cache
56
output/
67
plugins/*/*.so
78
plugins/*/lib*.h

plugins/k8smeta/CMakeLists.txt

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,39 +13,49 @@ project(
1313

1414
# dependencies
1515
include(FetchContent)
16-
include(grpc)
17-
include(spdlog)
1816
include(plugin-sdk-cpp)
1917
include(k8s-metacollector)
2018

21-
set(PROTO_PATH "${K8S_METACOLLECTOR_DIR}/metadata/metadata.proto")
19+
find_package(protobuf CONFIG REQUIRED)
20+
find_package(gRPC REQUIRED)
21+
find_package(spdlog REQUIRED)
2222

23+
set(PROTO_PATH "${K8S_METACOLLECTOR_DIR}/metadata/metadata.proto")
2324
get_filename_component(meta_proto "${PROTO_PATH}" ABSOLUTE)
2425
get_filename_component(meta_proto_path "${meta_proto}" PATH)
2526

26-
# Generated sources
27-
set(PROTO_GENERATED_INCLUDE "${CMAKE_BINARY_DIR}/generated")
28-
if(NOT EXISTS "${PROTO_GENERATED_INCLUDE}")
29-
file(MAKE_DIRECTORY "${PROTO_GENERATED_INCLUDE}")
30-
endif()
27+
set(PROTO_OUTPUT_DIR "${CMAKE_BINARY_DIR}/generated")
3128

32-
set(meta_proto_srcs "${PROTO_GENERATED_INCLUDE}/metadata.pb.cc")
33-
set(meta_proto_hdrs "${PROTO_GENERATED_INCLUDE}/metadata.pb.h")
34-
set(meta_grpc_srcs "${PROTO_GENERATED_INCLUDE}/metadata.grpc.pb.cc")
35-
set(meta_grpc_hdrs "${PROTO_GENERATED_INCLUDE}/metadata.grpc.pb.h")
36-
add_custom_command(
37-
OUTPUT "${meta_proto_srcs}" "${meta_proto_hdrs}" "${meta_grpc_srcs}"
38-
"${meta_grpc_hdrs}"
39-
COMMAND
40-
${_PROTOBUF_PROTOC} ARGS --grpc_out "${PROTO_GENERATED_INCLUDE}" --cpp_out
41-
"${PROTO_GENERATED_INCLUDE}" -I "${meta_proto_path}"
42-
--plugin=protoc-gen-grpc="${_GRPC_CPP_PLUGIN_EXECUTABLE}" "${meta_proto}"
43-
DEPENDS "${meta_proto}")
29+
# generate the protobuf output directory
30+
file(MAKE_DIRECTORY "${PROTO_OUTPUT_DIR}")
31+
message(STATUS "Protobuf files will be generated in: ${PROTO_OUTPUT_DIR}")
4432

4533
# project target
46-
file(GLOB_RECURSE K8S_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp")
47-
add_library(k8smeta SHARED ${K8S_SOURCES} ${meta_grpc_srcs} ${meta_grpc_hdrs}
48-
${meta_proto_srcs} ${meta_proto_hdrs})
34+
add_library(k8smeta SHARED
35+
src/plugin.cpp
36+
src/grpc_client.cpp
37+
)
38+
39+
# Set the output directory for protobuf generation
40+
set_target_properties(k8smeta PROPERTIES
41+
PROTOC_OUT_DIR "${PROTO_OUTPUT_DIR}")
42+
43+
# Generate protobuf files
44+
protobuf_generate(
45+
TARGET k8smeta
46+
PROTOS "${meta_proto}"
47+
IMPORT_DIRS "${meta_proto_path}"
48+
PROTOC_OUT_DIR "${PROTO_OUTPUT_DIR}")
49+
50+
# Generate gRPC files
51+
protobuf_generate(
52+
TARGET k8smeta
53+
LANGUAGE grpc
54+
GENERATE_EXTENSIONS .grpc.pb.h .grpc.pb.cc
55+
PLUGIN "protoc-gen-grpc=$<TARGET_FILE:gRPC::grpc_cpp_plugin>"
56+
PROTOS "${meta_proto}"
57+
IMPORT_DIRS "${meta_proto_path}"
58+
PROTOC_OUT_DIR "${PROTO_OUTPUT_DIR}")
4959
set_target_properties(k8smeta PROPERTIES CXX_EXTENSIONS OFF)
5060

5161
# project compilation options
@@ -59,12 +69,12 @@ target_compile_features(k8smeta PUBLIC cxx_std_17)
5969

6070
# project includes
6171
target_include_directories(
62-
k8smeta PRIVATE "${PLUGIN_SDK_INLCUDE}" "${PROTO_GENERATED_INCLUDE}"
63-
"${SPDLOG_INLCUDE}")
72+
k8smeta PRIVATE "${PLUGIN_SDK_INLCUDE}" "${PROTO_OUTPUT_DIR}")
6473

6574
# project linked libraries
66-
target_link_libraries(k8smeta ${_REFLECTION} ${_GRPC_GRPCPP}
67-
${_PROTOBUF_LIBPROTOBUF} re2::re2)
75+
target_link_libraries(k8smeta
76+
spdlog::spdlog
77+
gRPC::grpc++)
6878

6979
# Testing
7080
if(BUILD_TESTS)

plugins/k8smeta/CMakePresets.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"version": 3,
3+
"configurePresets": [
4+
{
5+
"name": "vcpkg-release",
6+
"displayName": "VCPKG Release Configuration",
7+
"description": "VCPKG release build configuration",
8+
"generator": "Unix Makefiles",
9+
"cacheVariables": {
10+
"CMAKE_BUILD_TYPE": "Release",
11+
"CMAKE_EXPORT_COMPILE_COMMANDS": "ON",
12+
"CMAKE_TOOLCHAIN_FILE": "$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake"
13+
}
14+
}
15+
],
16+
"buildPresets": [
17+
{
18+
"name": "vcpkg-release",
19+
"configurePreset": "vcpkg-release",
20+
"displayName": "VCPKG Release Build",
21+
"description": "Build with VCPKG release configuration"
22+
}
23+
]
24+
}

plugins/k8smeta/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ clean:
2222
rm -rf build $(OUTPUT)
2323

2424
# This Makefile requies CMake installed on the system
25+
.PHONY: $(OUTPUT)
2526
$(OUTPUT):
26-
mkdir build && cd build && cmake -DCMAKE_BUILD_TYPE=Release ../ && make k8smeta -j6 && cp ./$(OUTPUT) ../$(OUTPUT)
27+
cmake -B build --preset vcpkg-release && cmake --build build --target k8smeta -j$(nproc) && cp ./build/$(OUTPUT) ./$(OUTPUT)
2728

2829
readme:
2930
@$(READMETOOL) -p ./$(OUTPUT) -f README.md

plugins/k8smeta/README.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,16 +116,30 @@ falco -c falco.yaml -r falco_rules.yaml
116116

117117
## Local development
118118

119+
### Prerequisites
120+
121+
#### Dependencies management
122+
123+
The easiest way to install the required dependencies is to use [vcpkg](https://learn.microsoft.com/en-us/vcpkg/get_started/get-started?pivots=shell-bash).
124+
125+
```bash
126+
git clone https://github.com/microsoft/vcpkg.git
127+
cd vcpkg
128+
./bootstrap-vcpkg.sh
129+
export VCPKG_ROOT="$(pwd)"
130+
export PATH=$VCPKG_ROOT:$PATH
131+
```
132+
119133
### Build and test
120134

121-
Build the plugin on a fresh `Ubuntu 22.04` machine:
135+
Build the plugin on a `Ubuntu 22.04` machine:
122136

123137
```bash
124138
sudo apt update -y
125139
sudo apt install -y cmake build-essential autoconf libtool pkg-config
126140
git clone https://github.com/falcosecurity/plugins.git
127141
cd plugins/k8smeta
128-
cmake -S . -B build
142+
cmake -S . -B build --preset vcpkg-release
129143
cmake --build build --target k8smeta -j $(nproc)
130144
```
131145

plugins/k8smeta/cmake/modules/grpc.cmake

Lines changed: 0 additions & 35 deletions
This file was deleted.

plugins/k8smeta/cmake/modules/spdlog.cmake

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)