如何使用 python 客户端库修补 configmap 字段

Sah*_*ena 2 python-3.x kubernetes

我有下面的 configmap.yml 我想从 kubernates 部署中的容器的 python 脚本修补/更新日期字段我搜索了各个方面,但无法获得任何参考。任何参考或代码示例都会有很大帮助

apiVersion: v1
kind: ConfigMap
metadata:
  name: sample-configmap
  labels:
    app: test
    parameter-type: sample
data:
  storage.ini: |
    [DateInfo]
    date=1970-01-01T00:00:00.01Z
Run Code Online (Sandbox Code Playgroud)

我浏览了这个参考代码,但无法弄清楚body我应该使用哪个参数以及我应该忽略哪个参数的内容

部分更新指定的 ConfigMap

from __future__ import print_function
import time
import kubernetes.client
from kubernetes.client.rest
import ApiException
from pprint import pprint

configuration = kubernetes.client.Configuration()
# Configure API key authorization: BearerToken configuration.api_key['authorization'] = 'YOUR_API_KEY'
# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
# configuration.api_key_prefix['authorization'] = 'Bearer'
    
# Defining host is optional and default to http://localhost configuration.host = "http://localhost"
    
# Enter a context with an instance of the API kubernetes.client
with kubernetes.client.ApiClient(configuration) as api_client:
    # Create an instance of the API class
    api_instance = kubernetes.client.CoreV1Api(api_client)
    name = 'name_example' # str | name of the ConfigMap
    namespace = 'namespace_example' # str | object name and auth scope, such as for teams and projects
    body = None # object |
    pretty = 'pretty_example'
    dry_run = 'dry_run_example'
    field_manager = 'field_manager_example'
    force = True 
    
try:
    api_response = api_instance.patch_namespaced_config_map(name, namespace, body, pretty=pretty, dry_run=dry_run, field_manager=field_manager, force=force)
    pprint(api_response)
except ApiException as e:
    print("Exception when calling CoreV1Api->patch_namespaced_config_map: %s\n" % e)
Run Code Online (Sandbox Code Playgroud)

Kri*_*sia 5

body参数patch_namespaced_config_map是实际的configmap 是要修补,并与先获取需要的数据read_namespaced_config_map

所有具有body参数的操作都需要以下步骤:

  1. 使用read_*/get_*方法获取数据
  2. 使用API​​中第一步返回的数据修改对象。

此外,在大多数情况下,它足以即传递所需的参数 namenamespacebody但这里是关于每个信息:

参数

姓名 类型 描述 笔记
姓名 字符串 ConfigMap 的名称
命名空间 字符串 对象名称和身份验证范围,例如团队和项目
身体 目的
漂亮 字符串 如果为 'true',则输出打印得很漂亮。 [可选的]
空运行 字符串 当存在时,表示不应保留修改。无效或无法识别的 dryRun 指令将导致错误响应并且不会进一步处理请求。有效值为: - All:将处理所有试运行阶段 [可选的]
field_manager 字符串 fieldManager 是与进行这些更改的参与者或实体相关联的名称。该值必须少于或 128 个字符长,并且仅包含可打印字符,如https://golang.org/pkg/unicode/#IsPrint所定义。此字段对于应用请求(应用程序/应用补丁)是必需的,但对于非应用补丁类型(JsonPatch、MergePatch、StrategicMergePatch)是可选的。 [可选的]
力量 布尔值 强制将“强制” 应用请求。这意味着用户将重新获取其他人拥有的冲突字段。对于非应用补丁请求,必须取消设置强制标志。 [可选的]

查看K8s python 客户端 README以获取所有支持的 API 及其用法的列表。