Skip to content

Commit 06f66ba

Browse files
usernamenileshumutdz
authored andcommitted
Add debug logging doc and example
1 parent e93f240 commit 06f66ba

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

devel/debug_logging.md

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Enabling Debug Logging in Kubernetes Python Client
2+
3+
This document describes how to enable debug logging, view logged information, and provides examples for creating, patching, and deleting Kubernetes resources.
4+
5+
## 1. Why Enable Debug Logging?
6+
7+
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.
8+
9+
---
10+
11+
## 2. Enabling Debug Logging
12+
13+
To enable debug logging in the Kubernetes Python client, follow these steps:
14+
15+
1. **Modify the Configuration Object:**
16+
Enable debug mode by setting the `debug` attribute of the `client.Configuration` object to `True`.
17+
18+
2. **Example Code to Enable Debug Logging:**
19+
Below is an example showing how to enable debug logging:
20+
```python
21+
from kubernetes import client, config
22+
23+
# Load kube config
24+
config.load_kube_config()
25+
26+
# Enable debug logging
27+
c = client.Configuration()
28+
c.debug = True
29+
30+
# Pass the updated configuration to the API client
31+
api_client = client.ApiClient(configuration=c)
32+
33+
# Use the API client with debug logging enabled
34+
apps_v1 = client.AppsV1Api(api_client=api_client)

examples/enable_debug_logging.py

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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

Comments
 (0)