From f61d1814cc482128ba0299b01ab02b14df6d0a13 Mon Sep 17 00:00:00 2001 From: Umut Deniz Date: Thu, 23 Jan 2025 13:09:55 +0100 Subject: [PATCH 1/2] Add debug logging doc and example --- devel/debug_logging.md | 34 ++++++++++++++++++ examples/enable_debug_logging.py | 61 ++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 devel/debug_logging.md create mode 100644 examples/enable_debug_logging.py diff --git a/devel/debug_logging.md b/devel/debug_logging.md new file mode 100644 index 0000000000..966e3d38e4 --- /dev/null +++ b/devel/debug_logging.md @@ -0,0 +1,34 @@ +# Enabling Debug Logging in Kubernetes Python Client + +This document describes how to enable debug logging, view logged information, and provides examples for creating, patching, and deleting Kubernetes resources. + +## 1. Why Enable Debug Logging? + +Debug logging is useful for troubleshooting as it shows details like HTTP request and response headers and bodies. These details can help identify issues during interactions with the Kubernetes API server. + +--- + +## 2. Enabling Debug Logging + +To enable debug logging in the Kubernetes Python client, follow these steps: + +1. **Modify the Configuration Object:** + Enable debug mode by setting the `debug` attribute of the `client.Configuration` object to `True`. + +2. **Example Code to Enable Debug Logging:** + Below is an example showing how to enable debug logging: + ```python + from kubernetes import client, config + + # Load kube config + config.load_kube_config() + + # Enable debug logging + c = client.Configuration() + c.debug = True + + # Pass the updated configuration to the API client + api_client = client.ApiClient(configuration=c) + + # Use the API client with debug logging enabled + apps_v1 = client.AppsV1Api(api_client=api_client) diff --git a/examples/enable_debug_logging.py b/examples/enable_debug_logging.py new file mode 100644 index 0000000000..573948f3c1 --- /dev/null +++ b/examples/enable_debug_logging.py @@ -0,0 +1,61 @@ +# Copyright 2025 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# You may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# This example demonstrates how to enable debug logging in the Kubernetes +# Python client and how it can be used for troubleshooting requests/responses. + +from kubernetes import client, config + + +def main(): + # Load kubeconfig from default location + config.load_kube_config() + + # Enable debug logging + configuration = client.Configuration() + configuration.debug = True + api_client = client.ApiClient(configuration=configuration) + + # Use AppsV1Api with debug logging enabled + apps_v1 = client.AppsV1Api(api_client=api_client) + + # Example: Create a dummy deployment (adjust namespace as needed) + deployment = client.V1Deployment( + api_version="apps/v1", + kind="Deployment", + metadata=client.V1ObjectMeta(name="debug-example"), + spec=client.V1DeploymentSpec( + replicas=1, + selector={"matchLabels": {"app": "debug"}}, + template=client.V1PodTemplateSpec( + metadata=client.V1ObjectMeta(labels={"app": "debug"}), + spec=client.V1PodSpec( + containers=[ + client.V1Container( + name="busybox", + image="busybox", + command=["sh", "-c", "echo Hello, Kubernetes! && sleep 3600"] + ) + ] + ), + ), + ), + ) + + # Create the deployment + try: + print("[INFO] Creating deployment...") + apps_v1.create_namespaced_deployment( + namespace="default", body=deployment + ) + except client.exceptions.ApiException as e: + print("[ERROR] Exception occurred:", e) + + +if __name__ == "__main__": + main() From 06f66ba78dd030772d09641aed8ac877d4087efe Mon Sep 17 00:00:00 2001 From: FirstName LastName Date: Thu, 23 Jan 2025 13:09:55 +0100 Subject: [PATCH 2/2] Add debug logging doc and example --- devel/debug_logging.md | 34 ++++++++++++++++++ examples/enable_debug_logging.py | 61 ++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 devel/debug_logging.md create mode 100644 examples/enable_debug_logging.py diff --git a/devel/debug_logging.md b/devel/debug_logging.md new file mode 100644 index 0000000000..966e3d38e4 --- /dev/null +++ b/devel/debug_logging.md @@ -0,0 +1,34 @@ +# Enabling Debug Logging in Kubernetes Python Client + +This document describes how to enable debug logging, view logged information, and provides examples for creating, patching, and deleting Kubernetes resources. + +## 1. Why Enable Debug Logging? + +Debug logging is useful for troubleshooting as it shows details like HTTP request and response headers and bodies. These details can help identify issues during interactions with the Kubernetes API server. + +--- + +## 2. Enabling Debug Logging + +To enable debug logging in the Kubernetes Python client, follow these steps: + +1. **Modify the Configuration Object:** + Enable debug mode by setting the `debug` attribute of the `client.Configuration` object to `True`. + +2. **Example Code to Enable Debug Logging:** + Below is an example showing how to enable debug logging: + ```python + from kubernetes import client, config + + # Load kube config + config.load_kube_config() + + # Enable debug logging + c = client.Configuration() + c.debug = True + + # Pass the updated configuration to the API client + api_client = client.ApiClient(configuration=c) + + # Use the API client with debug logging enabled + apps_v1 = client.AppsV1Api(api_client=api_client) diff --git a/examples/enable_debug_logging.py b/examples/enable_debug_logging.py new file mode 100644 index 0000000000..573948f3c1 --- /dev/null +++ b/examples/enable_debug_logging.py @@ -0,0 +1,61 @@ +# Copyright 2025 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# You may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# This example demonstrates how to enable debug logging in the Kubernetes +# Python client and how it can be used for troubleshooting requests/responses. + +from kubernetes import client, config + + +def main(): + # Load kubeconfig from default location + config.load_kube_config() + + # Enable debug logging + configuration = client.Configuration() + configuration.debug = True + api_client = client.ApiClient(configuration=configuration) + + # Use AppsV1Api with debug logging enabled + apps_v1 = client.AppsV1Api(api_client=api_client) + + # Example: Create a dummy deployment (adjust namespace as needed) + deployment = client.V1Deployment( + api_version="apps/v1", + kind="Deployment", + metadata=client.V1ObjectMeta(name="debug-example"), + spec=client.V1DeploymentSpec( + replicas=1, + selector={"matchLabels": {"app": "debug"}}, + template=client.V1PodTemplateSpec( + metadata=client.V1ObjectMeta(labels={"app": "debug"}), + spec=client.V1PodSpec( + containers=[ + client.V1Container( + name="busybox", + image="busybox", + command=["sh", "-c", "echo Hello, Kubernetes! && sleep 3600"] + ) + ] + ), + ), + ), + ) + + # Create the deployment + try: + print("[INFO] Creating deployment...") + apps_v1.create_namespaced_deployment( + namespace="default", body=deployment + ) + except client.exceptions.ApiException as e: + print("[ERROR] Exception occurred:", e) + + +if __name__ == "__main__": + main()