尧图网站建设 尧图网络
  • 首页
  • 关于我们
  • 服务项目
  • 案例展示
  • 建站流程
  • 资讯中心
  • 联系我们
首页/资讯中心/详情

KubernetesClient-C

KubernetesClient-C
📅 发布时间:2026/6/20 16:03:10

KubernetesC-SDK

告诉编译器:

“这些函数是 C 写的,别给它们加花哨的 C++ 名字修饰,用纯 C 风格导出。”

extern "C" {
#include <kubernetes/config/kube_config.h>
#include <kubernetes/api/CoreV1API.h>
}

结构体之间的关系

image-20251014213300727

这套结构的目的是:
让不同类型的 Kubernetes 对象(Pod、Service、Namespace等)都能用统一的链表容器 list_t 来保存,从而方便解析与遍历。

  • v1_pod_list_t 只是“某一类对象”的容器(这里是 Pod)

  • 而 list_t 是通用链表容器

  • listEntry_t 是链表节点结构


​ 1️⃣ v1_pod_list_t

typedef struct v1_pod_list_t {char *api_version;          // 版本,如 "v1"list_t *items;              // 这里是一个链表,存放多个 v1_pod_t 对象char *kind;                 // 类型,如 "PodList"struct v1_list_meta_t *metadata; // 元数据,如继续分页的 tokenint _library_owned;         // 标记内存是否由库管理
} v1_pod_list_t;

​ 2️⃣ list_t(链表头)

typedef struct list_t {listEntry_t *firstEntry;  // 第一个节点listEntry_t *lastEntry;   // 最后一个节点long count;               // 节点数量
} list_t;

​ 3️⃣ listEntry_t(链表节点)

typedef struct listEntry_t {listEntry_t *nextListEntry;  // 指向下一个节点listEntry_t *prevListEntry;  // 指向上一个节点void *data;                  // 指向实际数据(这里是 v1_pod_t*)
} listEntry_t;

遍历宏的设计

#define list_ForEach(element, list) \for (element = (list != NULL) ? (list)->firstEntry : NULL; \element != NULL; \element = element->nextListEntry)

等价写法:

listEntry_t *element = list->firstEntry;
while (element != NULL) {...element = element->nextListEntry;
}

Creating API Clients

常见的连接方式有两种

  1. 从kubeconfig 文件和使用集群内置配置,这是在 Kubernetes 集群之外运行应用程序时最常见的方法:

image-20251015085656866

// Initialize variables
char *basePath = NULL;
sslConfig_t *sslConfig = NULL;
list_t *apiKeys = NULL;
apiClient_t *apiClient = NULL;// Load configuration from kubeconfig file (typically ~/.kube/config)
int rc = load_kube_config(&basePath, &sslConfig, &apiKeys, NULL);
if (rc != 0) {// Handle errorreturn -1;
}// Create API client with loaded configuration
apiClient = apiClient_create_with_base_path(basePath, sslConfig, apiKeys);
if (!apiClient) {// Handle errorreturn -1;
}// Use the API client...// Clean up resources
apiClient_free(apiClient);
free_client_config(basePath, sslConfig, apiKeys);
apiClient_unsetupGlobalEnv();
  1. 使用集群内配置,在 Kubernetes Pod 中运行时,可以使用集群内配置,该配置会自动检测和使用服务帐户令牌

image-20251015085825038

// Initialize variables
char *basePath = NULL;
sslConfig_t *sslConfig = NULL;
list_t *apiKeys = NULL;
apiClient_t *apiClient = NULL;// Load in-cluster configuration
int rc = load_incluster_config(&basePath, &sslConfig, &apiKeys);
if (rc != 0) {// Handle errorreturn -1;
}// Create API client with loaded configuration
apiClient = apiClient_create_with_base_path(basePath, sslConfig, apiKeys);
if (!apiClient) {// Handle errorreturn -1;
}// Use the API client...// Clean up resources
apiClient_free(apiClient);
free_client_config(basePath, sslConfig, apiKeys);
apiClient_unsetupGlobalEnv();
  • basePath: The base URL of the Kubernetes API server
    basePath:Kubernetes API 服务器的基本 URL
  • sslConfig: SSL/TLS configuration for secure connections
    sslConfig:用于安全连接的 SSL/TLS 配置
  • dataReceived: Buffer containing the response data
    dataReceived:包含响应数据的缓冲区
  • dataReceivedLen: Length of the received data
    dataReceivedLen:接收数据的长度
  • response_code: HTTP response code from the last request
    response_code:上次请求的 HTTP 响应代码
  • apiKeys_BearerToken: Authentication tokens for the API server
    apiKeys_BearerToken:API 服务器的身份验证令牌

面向对象设计

#include <iostream>
#include <string>extern "C"
{
#include <kubernetes/config/kube_config.h>
#include <kubernetes/api/CoreV1API.h>
}using namespace std;struct kube_params_t
{char *_basePath;sslConfig_t *_sslConfig;list_t *_apiKeys;kube_params_t() : _basePath(nullptr), _sslConfig(nullptr), _apiKeys(nullptr) {}~kube_params_t(){if (_basePath || _sslConfig || _apiKeys){free_client_config(_basePath, _sslConfig, _apiKeys);_basePath = nullptr;_sslConfig = nullptr;_apiKeys = nullptr;}}
};class K8sCluster
{
private:kube_params_t *_params;apiClient_t *_apiClient;bool _connected;void initClient(){int rc = load_kube_config(&_params->_basePath, &_params->_sslConfig, &_params->_apiKeys, nullptr);if (rc != 0){cerr << "Cannot to connected k8s" << endl;return;}_apiClient = apiClient_create_with_base_path(_params->_basePath, _params->_sslConfig, _params->_apiKeys);if (!_apiClient){cerr << "Create apiclient Failed" << endl;return;}_connected = true;}public:K8sCluster() : _params(new kube_params_t), _apiClient(nullptr), _connected(false) { initClient(); }K8sCluster(kube_params_t *params) : _params(params), _apiClient(nullptr), _connected(false){if (_params){initClient();}}~K8sCluster(){apiClient_free(_apiClient);delete _params;}void list_pod(const char *);bool isConnected() const { return _connected; }
};void K8sCluster::list_pod(const char *ns)
{if (!_apiClient){printf("[-] apiClient is null, cannot list pods.\n");return;}v1_pod_list_t *pod_list = CoreV1API_listNamespacedPod(_apiClient, (char *)ns, nullptr, nullptr, nullptr,nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,nullptr, nullptr);printf("HTTP response code = %ld\n", _apiClient->response_code);if (!pod_list){printf("Cannot get any pod.\n");return;}listEntry_t *listEntry = nullptr;v1_pod_t *pod = nullptr;list_ForEach(listEntry, pod_list->items){pod = static_cast<v1_pod_t *>(listEntry->data);if (pod && pod->metadata && pod->metadata->name)printf("  Pod: %s Namespace: %s\n", pod->metadata->name, pod->metadata->_namespace);}v1_pod_list_free(pod_list);
}int main()
{kube_params_t *params = new kube_params_t;K8sCluster info(params);const char *ns = "kube-system";info.list_pod(ns);apiClient_unsetupGlobalEnv();return 0;
}

CMake 编译文件配置

cmake_minimum_required(VERSION 3.16.3)
project(k8scon)
set(CXX_STANDARD 11)
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
# 如果 include 不在默认路径,还要加上
include_directories(/usr/local/include)
link_directories(/usr/local/lib)
add_executable(case case1.cc)
# 如果库在 /usr/local/lib 下
target_link_libraries(casekubernetes           # <-- 核心 SDKcurl                 # SDK 使用 libcurlssl                  # OpenSSLcrypto               # OpenSSLpthread              # 网络请求线程安全
)

相关新闻

  • 2025年微型减速机工厂权威推荐榜单:蜗轮蜗杆减速机/小齿减速机/谐波减速机源头厂家精选
  • 2025 年同步时钟厂家最新推荐榜,聚焦技术实力与市场口碑深度解析,涵盖卫星北斗 GPS 授时安全领域授时安全/授时防护/信号安全/时空安全同步时钟公司推荐
  • Chef:开源 AI 全栈应用构建工具实践

最新新闻

  • 实战分布式锁
  • 2026无锡钻石回收榜首TOP|行业翘楚,高溢价透明变现标杆 - 讯息早知道
  • 2026年上海梅雨季旧房翻新全攻略:防潮防霉与靠谱机构推荐 - 优家闲谈
  • 构建实时语音转写系统:TMSpeech技术架构与应用实践
  • 2026在无锡回收首饰不玩虚高引流,线上预估价≈线下成交价,所有收费提前说明 - 讯息早知道
  • 如何快速掌握Nintendo Switch游戏备份:NxDumpTool终极指南

日新闻

  • 信任的进化:技术实现详解——如何用JavaScript构建博弈论模拟器
  • Terrakube自定义工作流:如何集成OPA、Infracost等工具扩展IaC能力
  • grunt-concurrent快速入门:5分钟学会并行运行Grunt任务

周新闻

  • 3步解锁iOS设备:applera1n激活锁绕过完全指南
  • 39 2026 人工智能证书终极盘点,普通人选 AI 证书可以从这些方向入手
  • Redis 暴露公网有多危险?从端口检查到补救步骤

月新闻

  • 【总结】入门篇:50句话让你记住架构核心概念
  • WeChatMsg技术方案解析:实现Mac微信数据自主管理的完整解决方案
  • WeChatMsg:革新性微信数据备份方案,打造你的专属数字记忆库

关于尧图

  • 公司简介
  • 团队介绍
  • 企业文化
  • 荣誉资质

服务项目

  • 定制开发
  • 电商建站
  • UI 设计
  • 运维服务

快速链接

  • 案例展示
  • 建站流程
  • 常见问题
  • 资讯中心

联系方式

  • 📍北京市朝阳区互联网产业园 A 座 10 层
  • 📞400-888-8888
  • ✉️contact@rkmt.cn
  • 🕐周一至周日 9:00-21:00

© 2024 北京尧图网络科技有限公司 版权所有 | 京 ICP 备 XXXXXXXX 号