首页 > 解决方案 > 在 Kubernetes API 客户端操作中为 Python 试运行

问题描述

我一直在编写一些需要在Kubernetes中使用Python Client官方库执行的操作。

一个例子是更新标签值的服务补丁:

# Code to instantiate the api object. 
config.load_kube_config()
self.v1 = client.CoreV1Api()
# Code to call the patch function
def patch_namespaced_service(self, name, namespace, body, dry_run=True):
    logging.debug(body)
    response = self.v1.patch_namespaced_service(name, namespace, body, dry_run=dry_run)
    logging.info("Service patched. status='%s'" % str(response.status))

它就像一个魅力,但该dry_run选项永远不会应用,无论我发送什么值,它总是执行补丁。

这是记录dry_run参数的源代码:

def patch_namespaced_service(self, name, namespace, body, **kwargs):  # noqa: E501
        """patch_namespaced_service  # noqa: E501

        partially update the specified Service  # noqa: E501
        This method makes a synchronous HTTP request by default. To make an
        asynchronous HTTP request, please pass async_req=True
        >>> thread = api.patch_namespaced_service(name, namespace, body, async_req=True)
        >>> result = thread.get()

        :param async_req bool
        :param str name: name of the Service (required)
        :param str namespace: object name and auth scope, such as for teams and projects (required)
        :param UNKNOWN_BASE_TYPE body: (required)
        :param str pretty: If 'true', then the output is pretty printed.
        :param str dry_run: When present, indicates that modifications should not be persisted. An invalid or unrecognized dryRun directive will result in an error response and no further processing of the request. Valid values are: - All: all dry run stages will be processed
        :param str field_manager: fieldManager is a name associated with the actor or entity that is making these changes. The value must be less than or 128 characters long, and only contain printable characters, as defined by https://golang.org/pkg/unicode/#IsPrint. This field is required for apply requests (application/apply-patch) but optional for non-apply patch types (JsonPatch, MergePatch, StrategicMergePatch).
        :param bool force: Force is going to \"force\" Apply requests. It means user will re-acquire conflicting fields owned by other people. Force flag must be unset for non-apply patch requests.
        :return: V1Service
                 If the method is called asynchronously,
                 returns the request thread.
        """

我想评论这部分:

Valid values are: - All: all dry run stages will be processed

dry_run在他们用作列表 的文档的其他部分: https ://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1DeleteOptions.md

但是我试过了,dry_run=True没有成功。dry_run="All"dry_run=["All"]

有谁知道如何使用 a 执行此操作dry_run

标签: pythonkubernetes

解决方案


推荐阅读