|
| 1 | +# Copyright 2025 The Kubernetes Authors. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# You may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# This example demonstrates how to enable debug logging in the Kubernetes |
| 10 | +# Python client and how it can be used for troubleshooting requests/responses. |
| 11 | + |
| 12 | +from kubernetes import client, config |
| 13 | + |
| 14 | + |
| 15 | +def main(): |
| 16 | + # Load kubeconfig from default location |
| 17 | + config.load_kube_config() |
| 18 | + |
| 19 | + # Enable debug logging |
| 20 | + configuration = client.Configuration() |
| 21 | + configuration.debug = True |
| 22 | + api_client = client.ApiClient(configuration=configuration) |
| 23 | + |
| 24 | + # Use AppsV1Api with debug logging enabled |
| 25 | + apps_v1 = client.AppsV1Api(api_client=api_client) |
| 26 | + |
| 27 | + # Example: Create a dummy deployment (adjust namespace as needed) |
| 28 | + deployment = client.V1Deployment( |
| 29 | + api_version="apps/v1", |
| 30 | + kind="Deployment", |
| 31 | + metadata=client.V1ObjectMeta(name="debug-example"), |
| 32 | + spec=client.V1DeploymentSpec( |
| 33 | + replicas=1, |
| 34 | + selector={"matchLabels": {"app": "debug"}}, |
| 35 | + template=client.V1PodTemplateSpec( |
| 36 | + metadata=client.V1ObjectMeta(labels={"app": "debug"}), |
| 37 | + spec=client.V1PodSpec( |
| 38 | + containers=[ |
| 39 | + client.V1Container( |
| 40 | + name="busybox", |
| 41 | + image="busybox", |
| 42 | + command=["sh", "-c", "echo Hello, Kubernetes! && sleep 3600"] |
| 43 | + ) |
| 44 | + ] |
| 45 | + ), |
| 46 | + ), |
| 47 | + ), |
| 48 | + ) |
| 49 | + |
| 50 | + # Create the deployment |
| 51 | + try: |
| 52 | + print("[INFO] Creating deployment...") |
| 53 | + apps_v1.create_namespaced_deployment( |
| 54 | + namespace="default", body=deployment |
| 55 | + ) |
| 56 | + except client.exceptions.ApiException as e: |
| 57 | + print("[ERROR] Exception occurred:", e) |
| 58 | + |
| 59 | + |
| 60 | +if __name__ == "__main__": |
| 61 | + main() |
0 commit comments