Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dry run for Python in Kubernetes API Client Operations

I have been coding some operations that I need to perform in Kubernetes with the Python Client official library.

One example is the patch of a service to update the value of a label:

# 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))

It works like a charm but the dry_run option is never applied and no matter what value I sent, it always performs the patch.

This is the source code where is documented the dry_run parameter:

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.
        """

I would like to remark this part:

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

In other parts of the documentation they use dry_run as a list: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1DeleteOptions.md

But I have tried dry_run=True, dry_run="All" and dry_run=["All"] without success.

Does anyone have an idea about how to perform this operation with a dry_run?

like image 883
Mirdrack Avatar asked May 04 '26 16:05

Mirdrack


1 Answers

I use it like this and works fine with kubernetes package v25.3.0

kwargs = {"name": doc['metadata']['name'], "body":doc, "namespace":doc['metadata']['namespace'], "dry_run":"All"}
ret = getattr(v1, "patch_namespaced_config_map")(**kwargs)
like image 144
Shankar Ramasamy Avatar answered May 06 '26 05:05

Shankar Ramasamy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!