Merge pull request #78 from ahmedyarub/ay/refactor_cmake_scripts

Add examples, and pre and post CMake building scripts
This commit is contained in:
Kubernetes Prow Robot
2021-09-04 05:37:19 -07:00
committed by GitHub
9 changed files with 102 additions and 55 deletions

View File

@@ -15,15 +15,25 @@ jobs:
- name: Prepare
run: |
sudo apt-get update
sudo apt-get install -y libssl-dev libcurl4-openssl-dev uncrustify libyaml-dev
sudo apt-get install -y libssl-dev libcurl4-openssl-dev uncrustify
- name: Prepare libwebsockets
run: |
git clone https://libwebsockets.org/repo/libwebsockets --depth 1 --branch v4.2-stable
cd libwebsockets
mkdir build
cd build
cmake ..
make
cmake .. -DLWS_WITHOUT_TESTAPPS=ON -DLWS_WITHOUT_TEST_SERVER=ON-DLWS_WITHOUT_TEST_SERVER_EXTPOLL=ON \
-DLWS_WITHOUT_TEST_PING=ON -DLWS_WITHOUT_TEST_CLIENT=ON -DCMAKE_C_FLAGS="-fpic"
make -j $(cat /proc/cpuinfo | grep processor | wc -l)
sudo make install
- name: Prepare libyaml
run: |
git clone https://github.com/yaml/libyaml --depth 1 --branch release/0.2.5
cd libyaml
mkdir build
cd build
cmake .. -DBUILD_TESTING=OFF
make -j $(cat /proc/cpuinfo | grep processor | wc -l)
sudo make install
- name: Build client library
run: |
@@ -31,7 +41,7 @@ jobs:
mkdir build
cd build
cmake ..
make
make -j $(cat /proc/cpuinfo | grep processor | wc -l)
- name: Build authentication plugin - oidc
run: |
cd kubernetes/config/authn_plugin/plugins/oidc

View File

@@ -14,14 +14,24 @@ git clone https://github.com/kubernetes-client/c
CLIENT_REPO_ROOT=${PWD}/c
# Install pre-requisites
sudo apt-get install libssl-dev libcurl4-openssl-dev uncrustify libyaml-dev
sudo apt-get install libssl-dev libcurl4-openssl-dev uncrustify
# Build pre-requisite: libwebsockets
git clone https://libwebsockets.org/repo/libwebsockets --depth 1 --branch v4.2-stable
cd libwebsockets
mkdir build
cd build
cmake -DCMAKE_INSTALL_PREFIX=/usr/local ..
cmake -DLWS_WITHOUT_TESTAPPS=ON -DLWS_WITHOUT_TEST_SERVER=ON-DLWS_WITHOUT_TEST_SERVER_EXTPOLL=ON \
-DLWS_WITHOUT_TEST_PING=ON -DLWS_WITHOUT_TEST_CLIENT=ON -DCMAKE_C_FLAGS="-fpic" -DCMAKE_INSTALL_PREFIX=/usr/local ..
make
sudo make install
# Build pre-requisite: libyaml
git clone https://github.com/yaml/libyaml --depth 1 --branch release/0.2.5
cd libyaml
mkdir build
cd build
cmake -DCMAKE_INSTALL_PREFIX=/usr/local -DBUILD_TESTING=OFF ..
make
sudo make install

3
examples/CMakeLists.txt Normal file
View File

@@ -0,0 +1,3 @@
set(pkgName "kubernetes")
add_subdirectory(list_pod)

View File

@@ -0,0 +1,5 @@
add_executable(list_pod main.c)
find_package(${pkgName})
target_link_libraries(list_pod PRIVATE ${pkgName})

View File

@@ -1,9 +1,7 @@
#include <kube_config.h>
#include <apiClient.h>
#include <CoreV1API.h>
#include <malloc.h>
#include <stdio.h>
#include <errno.h>
void list_pod(apiClient_t * apiClient)
{
@@ -35,7 +33,7 @@ void list_pod(apiClient_t * apiClient)
}
}
int main(int argc, char *argv[])
int main()
{
char *basePath = NULL;
sslConfig_t *sslConfig = NULL;

View File

@@ -1,4 +1,4 @@
cmake_minimum_required (VERSION 2.6)
cmake_minimum_required (VERSION 2.6...3.10.2)
project (CGenerator)
cmake_policy(SET CMP0063 NEW)
@@ -6,48 +6,41 @@ cmake_policy(SET CMP0063 NEW)
set(CMAKE_C_VISIBILITY_PRESET default)
set(CMAKE_CXX_VISIBILITY_PRESET default)
set(CMAKE_VISIBILITY_INLINES_HIDDEN OFF)
set(CMAKE_BUILD_TYPE Debug)
option(BUILD_SHARED_LIBS "Build using shared libraries" ON)
find_package(OpenSSL)
if (OPENSSL_FOUND)
message (STATUS "OPENSSL found")
set (CMAKE_C_FLAGS "-DOPENSSL")
include_directories(${OPENSSL_INCLUDE_DIR})
include_directories(${OPENSSL_INCLUDE_DIRS})
link_directories(${OPENSSL_LIBRARIES})
message(STATUS "Using OpenSSL ${OPENSSL_VERSION}")
else()
message (STATUS "OpenSSL Not found.")
message (STATUS "OPENSSL found")
set(CMAKE_C_FLAGS "-DOPENSSL")
if(CMAKE_VERSION VERSION_LESS 3.4)
include_directories(${OPENSSL_INCLUDE_DIR})
include_directories(${OPENSSL_INCLUDE_DIRS})
link_directories(${OPENSSL_LIBRARIES})
endif()
message(STATUS "Using OpenSSL ${OPENSSL_VERSION}")
else()
message (STATUS "OpenSSL Not found.")
endif()
set(pkgName "kubernetes")
find_package(CURL 7.58.0 REQUIRED)
if(CURL_FOUND)
include_directories(${CURL_INCLUDE_DIR})
set(PLATFORM_LIBRARIES ${PLATFORM_LIBRARIES} ${CURL_LIBRARIES} )
include_directories(${CURL_INCLUDE_DIR})
set(PLATFORM_LIBRARIES ${PLATFORM_LIBRARIES} ${CURL_LIBRARIES} )
else(CURL_FOUND)
message(FATAL_ERROR "Could not find the CURL library and development files.")
message(FATAL_ERROR "Could not find the CURL library and development files.")
endif()
set(SRCS
config/kube_config_model.c
config/kube_config_yaml.c
config/kube_config_util.c
config/kube_config.c
config/incluster_config.c
config/exec_provider.c
config/authn_plugin/authn_plugin_util.c
config/authn_plugin/authn_plugin.c
watch/watch_util.c
websocket/wsclient.c
websocket/kube_exec.c
src/list.c
src/apiKey.c
src/apiClient.c
src/binary.c
src/generic.c
external/cJSON.c
model/object.c
model/admissionregistration_v1_service_reference.c
@@ -796,23 +789,10 @@ set(SRCS
)
set(HDRS
config/kube_config_common.h
config/kube_config_model.h
config/kube_config_yaml.h
config/kube_config_util.h
config/kube_config.h
config/incluster_config.h
config/exec_provider.h
config/authn_plugin/authn_plugin_util.h
config/authn_plugin/authn_plugin.h
watch/watch_util.h
websocket/wsclient.h
websocket/kube_exec.h
include/apiClient.h
include/list.h
include/binary.h
include/keyValuePair.h
include/generic.h
external/cJSON.h
model/object.h
model/admissionregistration_v1_service_reference.h
@@ -1560,10 +1540,18 @@ set(HDRS
)
# Add library with project file with projectname as library name
add_library(${pkgName} SHARED ${SRCS} ${HDRS})
include(PreTarget.cmake OPTIONAL)
# Add library with project file with project name as library name
add_library(${pkgName} ${SRCS} ${HDRS})
# Link dependent libraries
if(NOT CMAKE_VERSION VERSION_LESS 3.4)
target_link_libraries(${pkgName} OpenSSL::SSL OpenSSL::Crypto)
endif()
target_link_libraries(${pkgName} ${CURL_LIBRARIES} )
include(PostTarget.cmake OPTIONAL)
#install library to destination
install(TARGETS ${pkgName} DESTINATION ${CMAKE_INSTALL_PREFIX})
@@ -1572,7 +1560,7 @@ set(SRCS "")
set(HDRS "")
## This section shows how to use the above compiled libary to compile the source files
## This section shows how to use the above compiled library to compile the source files
## set source files
#set(SRCS
# unit-tests/manual-AdmissionregistrationAPI.c
@@ -1653,12 +1641,12 @@ set(HDRS "")
## loop over all files in SRCS variable
#foreach(SOURCE_FILE ${SRCS})
# # Get only the file name from the file as add_executable doesnot support executable with slash("/")
# # Get only the file name from the file as add_executable does not support executable with slash("/")
# get_filename_component(FILE_NAME_ONLY ${SOURCE_FILE} NAME_WE)
# # Remove .c from the file name and set it as executable name
# string( REPLACE ".c" "" EXECUTABLE_FILE ${FILE_NAME_ONLY})
# # Add executable for every source file in SRCS
# add_executable(unit-${EXECUTABLE_FILE} ${SOURCE_FILE})
# # Link above created libary to executable and dependent libary curl
# # Link above created library to executable and dependent library curl
# target_link_libraries(unit-${EXECUTABLE_FILE} ${CURL_LIBRARIES} ${pkgName} )
#endforeach(SOURCE_FILE ${SRCS})

View File

@@ -0,0 +1,2 @@
target_link_libraries(${pkgName} yaml websockets)
set_target_properties(${pkgName} PROPERTIES LINKER_LANGUAGE C)

View File

@@ -0,0 +1,31 @@
list(APPEND SRCS
config/kube_config_model.c
config/kube_config_yaml.c
config/kube_config_util.c
config/kube_config.c
config/incluster_config.c
config/exec_provider.c
config/authn_plugin/authn_plugin_util.c
config/authn_plugin/authn_plugin.c
watch/watch_util.c
websocket/wsclient.c
websocket/kube_exec.c
src/generic.c)
list(APPEND HDRS
config/kube_config_common.h
config/kube_config_model.h
config/kube_config_yaml.h
config/kube_config_util.h
config/kube_config.h
config/incluster_config.h
config/exec_provider.h
config/authn_plugin/authn_plugin_util.h
config/authn_plugin/authn_plugin.h
watch/watch_util.h
websocket/wsclient.h
websocket/kube_exec.h
include/generic.h)
find_package(libwebsockets REQUIRED)
find_package(yaml CONFIG REQUIRED)

View File

@@ -39,7 +39,7 @@ This will compile the generated code and create a library in the build folder wh
mkdir build
cd build
// To install library to specific location, use following commands
cmake -DCMAKE_INSTALL_PREFIX=/pathtolocaiton ..
cmake -DCMAKE_INSTALL_PREFIX=/pathtolocation ..
// for normal install use following command
cmake ..
make
@@ -49,14 +49,14 @@ sudo make install
Considering the test/source code which uses the API is written in main.c(respective api include is written and all objects necessary are defined and created)
To compile main.c(considering the file is present in build folder) use following command
-L - locaiton of the library(not required if cmake with normal installation is performed)
-L - location of the library(not required if cmake with normal installation is performed)
-l library name
```bash
gcc main.c -L. -lkubernetes -o main
```
Once compiled, you can run it with ``` ./main ```
Note: You don't need to specify includes for models and include folder seperately as they are path linked. You just have to import the api.h file in your code, the include linking will work.
Note: You don't need to specify includes for models and include folder separately as they are path linked. You just have to import the api.h file in your code, the include linking will work.
## Documentation for API Endpoints