【问题标题】:Delete Kubernetes resources using python client使用 python 客户端删除 Kubernetes 资源
【发布时间】:2019-09-16 23:35:02
【问题描述】:

想用K8s的python客户端,删除statefulset等一些资源。我可以删除 statefulset 本身,但 Ks8 不会删除正在运行的 pod。我发现了一些示例,他们在删除选项中设置了 propagation_policy="Foreground" 并完成了工作,但问题出在 kubernetes 客户端 > 9.0 中,他们更改了 API,当我通过 delete_options 时它返回此错误:

TypeError: delete_namespaced_stateful_set() takes 3 positional arguments but 4 were given

我试图找到设置删除传播策略的正确方法,但它不起作用,并且正在运行的 pod 没有被杀死,我如何在一个 api 调用中删除所有正在运行的 pod 和 statefuleset?

我当前的代码只是删除了有状态集:

api_response = k8s_api.delete_namespaced_stateful_set(statefulset_name,namespace)

产生上述错误的代码:

delete_options = client.V1DeleteOptions(propagation_policy="Foreground", grace_period_seconds=5)
api_response = k8s_api.delete_namespaced_stateful_set(statefulset_name,namespace,delete_options)

【问题讨论】:

    标签: kubernetes kubernetes-python-client


    【解决方案1】:

    看完source code

    def delete_namespaced_stateful_set(self, name, namespace, **kwargs):
        """
        delete a StatefulSet
        This method makes a synchronous HTTP request by default. To make an
        asynchronous HTTP request, please pass async_req=True
        >>> thread = api.delete_namespaced_stateful_set(name, namespace, async_req=True)
        >>> result = thread.get()
        :param async_req bool
        :param str name: name of the StatefulSet (required)
        :param str namespace: object name and auth scope, such as for teams and projects (required)
        :param str pretty: If 'true', then the output is pretty printed.
        :param V1DeleteOptions body:
        :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 int grace_period_seconds: The duration in seconds before the object should be deleted. Value must be non-negative integer. The value zero indicates delete immediately. If this value is nil, the default grace period for the specified type will be used. Defaults to a per object value if not specified. zero means delete immediately.
        :param bool orphan_dependents: Deprecated: please use the PropagationPolicy, this field will be deprecated in 1.7. Should the dependent objects be orphaned. If true/false, the \"orphan\" finalizer will be added to/removed from the object's finalizers list. Either this field or PropagationPolicy may be set, but not both.
        :param str propagation_policy: Whether and how garbage collection will be performed. Either this field or OrphanDependents may be set, but not both. The default policy is decided by the existing finalizer set in the metadata.finalizers and the resource-specific default policy. Acceptable values are: 'Orphan' - orphan the dependents; 'Background' - allow the garbage collector to delete the dependents in the background; 'Foreground' - a cascading policy that deletes all dependents in the foreground.
        :return: V1Status
                 If the method is called asynchronously,
                 returns the request thread.
        """
    

    我相信设置额外参数 (**kwargs) 的正确方法是使用 key=value 格式,而且 V1DeleteOptions 不是有效参数,因此不要将 delete_options 作为参数传递您应该传递V1DeleteOptions的每个选项,例如propagation_policy="Foreground"grace_period_seconds=5

    api_response = k8s_api.delete_namespaced_stateful_set(statefulset_name,namespace,propagation_policy="Foreground", grace_period_seconds=5)
    

    但是,有一种方法可以传递整个 V1DeleteOptions 对象。您可以先将其转换为 dict,然后将其解包:

    delete_options = client.V1DeleteOptions(propagation_policy="Foreground", grace_period_seconds=5)
    api_response = k8s_api.delete_namespaced_stateful_set(statefulset_name,namespace,**delete_options.do_dict())
    

    【讨论】:

    • 感谢您的回答。其实我需要通过propagation_policy:api_response = k8s_api.delete_namespaced_stateful_set(statefulset_name,namespace,propagation_policy='Foreground')
    • 你是对的。实际上不可能传递 V1DeleteOptions 对象。我已经编辑了我的答案,将其转换为参数
    猜你喜欢
    • 1970-01-01
    • 2021-11-06
    • 2018-07-24
    • 2020-11-27
    • 2020-08-30
    • 2021-09-04
    • 2018-12-02
    • 2020-06-14
    • 1970-01-01
    相关资源
    最近更新 更多