Skip to content

Commit f97b071

Browse files
committed
examples: add README and improve error handling in apply_from_dict
1 parent e752675 commit f97b071

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

examples/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,15 @@ installed following the directions
1515

1616
If you find a problem please file an
1717
[issue](https://github.com/kubernetes-client/python/issues).
18+
19+
20+
---
21+
22+
## Running Examples Locally
23+
24+
### Prerequisites
25+
26+
- Python 3.8 or newer
27+
- Kubernetes Python client installed:
28+
```bash
29+
pip install kubernetes

examples/apply_from_dict.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import sys
2+
from kubernetes.client.rest import ApiException
3+
4+
15
from kubernetes import client, config, utils
26
def main():
37
config.load_kube_config()
@@ -6,5 +10,12 @@ def main():
610
example_dict = {'apiVersion': 'apps/v1', 'kind': 'Deployment', 'metadata': {'name': 'k8s-py-client-nginx'}, 'spec': {'selector': {'matchLabels': {'app': 'nginx'}}, 'replicas': 1, 'template': {'metadata': {'labels': {'app': 'nginx'}}, 'spec': {'containers': [{'name': 'nginx', 'image': 'nginx:1.14.2', 'ports': [{'containerPort': 80}]}]}}}}
711
utils.create_from_dict(k8s_client, example_dict)
812

9-
if __name__ == '__main__':
10-
main()
13+
if __name__ == "__main__":
14+
try:
15+
main()
16+
except ApiException as e:
17+
print(f"Kubernetes API error: {e}", file=sys.stderr)
18+
sys.exit(1)
19+
except Exception as e:
20+
print(f"Unexpected error: {e}", file=sys.stderr)
21+
sys.exit(2)

0 commit comments

Comments
 (0)