File: enable_debug_logging.py

package info (click to toggle)
python-kubernetes 35.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 47,168 kB
  • sloc: python: 145,021; sh: 763; makefile: 46
file content (61 lines) | stat: -rw-r--r-- 2,005 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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()