使用Kubernetes C Client修补部署

oknwwptz  于 2024-01-06  发布在  Kubernetes
关注(0)|答案(1)|浏览(614)

我正在尝试为本地minikube集群中现有的Kubernetes部署创建补丁。我已经成功实现了创建新部署的功能。
对于部署补丁,我想我必须在api\AppsV1API.c中使用以下函数:

v1_deployment_t *
AppsV1API_patchNamespacedDeployment(apiClient_t *apiClient, char *name, char *_namespace, object_t *body, char *pretty, char *dryRun, char *fieldManager, char *fieldValidation, int force)

字符串
我不明白object_t *body的函数的正确参数。
部署配置文件:

{
    "apiVersion": "apps/v1",
    "kind": "Deployment",
    "metadata": {
      "name": "busybox-deployment"
    },
    "spec": {
      "replicas": 1,
      "selector": {
        "matchLabels": {
          "app": "busybox"
        }
      },
      "template": {
        "metadata": {
          "labels": {
            "app": "busybox"
          }
        },
        "spec": {
          "containers": [
            {
              "name": "busybox-container",
              "image": "busybox",
              "command": ["sleep", "3600"]
            }
          ]
        }
      }
    }
  }


我想把replicas从1改为3。
我用来插入[{'op': 'replace', 'path': '/spec/replicas', 'value': 2}]作为主体的代码:

cJSON *jsonArray = cJSON_CreateArray();

cJSON *jsonObject = cJSON_CreateObject();
cJSON_AddStringToObject(jsonObject, "op", "replace");
cJSON_AddStringToObject(jsonObject, "path", "/spec/replicas");
cJSON_AddNumberToObject(jsonObject, "value", 2);

cJSON_AddItemToArray(jsonArray, jsonObject);
object_t *body = object_parseFromJSON(jsonArray);

v1_deployment_t *updatedDeployment = AppsV1API_patchNamespacedDeployment(apiClient, deploymentInfo->metadata->name, namespc, body, NULL, NULL, NULL, NULL, 0);


我用来插入{'spec': {'replicas': 2}}作为主体的代码:

cJSON *root = cJSON_CreateObject();

cJSON *specObject = cJSON_CreateObject();

cJSON_AddNumberToObject(specObject, "replicas", 2);

cJSON_AddItemToObject(root, "spec", specObject);
object_t *body = object_parseFromJSON(root);

v1_deployment_t *updatedDeployment = AppsV1API_patchNamespacedDeployment(apiClient, deploymentInfo->metadata->name, namespc, body, NULL, NULL, NULL, NULL, 0);


两者都失败,apiClient->responseCode为422(有效负载错误)。

cwdobuhd

cwdobuhd1#

你的代码看起来和我写的一样好用。下面是我完整的测试用例(对AppsV1API_patchNamespacedDeployment的调用和你的一样,除了namenamespace是用字符串文字指定的):

#include <config/kube_config.h>
#include <api/AppsV1API.h>
#include <model/object.h>
#include <stdio.h>

void patch_deployment(apiClient_t *apiClient)
{
    v1_deployment_t *deploy;
    object_t * body;

    cJSON *jsonArray = cJSON_CreateArray();
    cJSON *jsonObject = cJSON_CreateObject();
    cJSON_AddStringToObject(jsonObject, "op", "replace");
    cJSON_AddStringToObject(jsonObject, "path", "/spec/replicas");
    cJSON_AddNumberToObject(jsonObject, "value", 2);

    cJSON_AddItemToArray(jsonArray, jsonObject);

    body = object_parseFromJSON(jsonArray);
    if (! body) {
        fprintf(stderr, "failed to convert patch to object\n");
        return;
    }

    v1_deployment_t *updatedDeployment = AppsV1API_patchNamespacedDeployment(
            apiClient,
            "web",
            "default",
            body,
            NULL,
            NULL,
            NULL,
            NULL,
            0);

    if (apiClient->response_code != 200) {
        fprintf(stderr, "failed to patch resource: %ld\n", apiClient->response_code);
    }
}

int main()
{
    char *basePath = NULL;
    sslConfig_t *sslConfig = NULL;
    list_t *apiKeys = NULL;
    int rc = load_kube_config(&basePath, &sslConfig, &apiKeys, NULL); /* NULL means loading configuration from $HOME/.kube/config */
    if (rc != 0)
    {
        printf("Cannot load kubernetes configuration.\n");
        return -1;
    }
    apiClient_t *apiClient = apiClient_create_with_base_path(basePath, sslConfig, apiKeys);
    if (!apiClient)
    {
        printf("Cannot create a kubernetes client.\n");
        return -1;
    }

    patch_deployment(apiClient);

    apiClient_free(apiClient);
    apiClient = NULL;
    free_client_config(basePath, sslConfig, apiKeys);
    basePath = NULL;
    sslConfig = NULL;
    apiKeys = NULL;
    apiClient_unsetupGlobalEnv();

    return 0;
}

字符串
考虑以下部署:

$ kubectl get deploy
NAME   READY   UP-TO-DATE   AVAILABLE   AGE
web    1/1     1            1           32d


上面的代码成功运行,结果是:

NAME   READY   UP-TO-DATE   AVAILABLE   AGE
web    2/2     2            2           32d

相关问题