Skip to content

Commit 3ca6f8e

Browse files
Merge pull request #161 from bradmwilliams/examples
More examples
2 parents ca1de74 + 430c2e6 commit 3ca6f8e

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

examples/etcd_status.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/python
2+
3+
import openshift_client as oc
4+
5+
if __name__ == '__main__':
6+
options = {
7+
'as': 'system:admin',
8+
}
9+
10+
with oc.client_host():
11+
with oc.timeout(60 * 5):
12+
with oc.options(options):
13+
with oc.project("openshift-etcd"):
14+
pods = oc.selector("pods", labels={'app': 'etcd'}).objects()
15+
print(f'Found: {len(pods)} pods')
16+
result = pods[0].execute(cmd_to_exec=['etcdctl', 'endpoint', 'status', '--cluster', '-w', 'table'])
17+
print(f'Result:\n{result.out()}')

examples/login.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/python
2+
3+
import argparse
4+
import traceback
5+
6+
import openshift_client as oc
7+
from openshift_client import OpenShiftPythonException, Context
8+
9+
if __name__ == '__main__':
10+
parser = argparse.ArgumentParser(description='OpenShift Client Login Example')
11+
parser.add_argument('-k', '--kubeconfig', help='The kubeconfig to create', required=True)
12+
parser.add_argument('-s', '--server', help='The API Server to communicate with', required=True)
13+
parser.add_argument('-t', '--token', help='The login token', required=True)
14+
args = vars(parser.parse_args())
15+
16+
my_context = Context()
17+
my_context.token = args["token"]
18+
my_context.api_server = args["server"]
19+
my_context.kubeconfig_path = args["kubeconfig"]
20+
21+
with oc.timeout(60 * 30), oc.tracking() as t, my_context:
22+
if oc.get_config_context() is None:
23+
print(f'Current context not set! Logging into API server: {my_context.api_server}\n')
24+
try:
25+
oc.invoke('login')
26+
except OpenShiftPythonException:
27+
print('error occurred logging into API Server')
28+
traceback.print_exc()
29+
print(f'Tracking:\n{t.get_result().as_json(redact_streams=False)}\n\n')
30+
exit(1)
31+
32+
print(f'Current context: {oc.get_config_context()}')
33+
34+
try:
35+
pods = oc.selector('pods').objects()
36+
print(f'Found: {len(pods)} pods')
37+
except OpenShiftPythonException:
38+
print('Error occurred getting pods')
39+
traceback.print_exc()
40+
print(f'Tracking:\n{t.get_result().as_json(redact_streams=False)}\n\n')

examples/quotas.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/python
2+
3+
import openshift_client as oc
4+
5+
if __name__ == '__main__':
6+
with oc.client_host():
7+
with oc.timeout(60 * 5):
8+
with oc.project('openshift-client-python'):
9+
resource_quotas = oc.selector('resourcequotas').objects()
10+
print(f'Found: {len(resource_quotas)} ResourceQuotas')
11+
12+
for resource_quota in resource_quotas:
13+
print(f'Processing ResourceQuota: {resource_quota.name()}')
14+
for key in resource_quota.model.spec.hard:
15+
print(f' - {key}: {resource_quota.model.spec.hard[key]}')
16+
17+
limit_ranges = oc.selector('limitranges').objects()
18+
print(f'\nFound: {len(limit_ranges)} LimitRanges')
19+
20+
for limit_range in limit_ranges:
21+
print(f'Processing LimitRange: {limit_range.name()}')
22+
for limit in limit_range.model.spec.limits:
23+
print(f' Type: {limit.type}')
24+
print(f' Default CPU Limit: {limit.default.cpu}')
25+
print(f' Default Memory Limit: {limit.default.memory}')
26+
print(f' Default CPU Request: {limit.defaultRequest.cpu}')
27+
print(f' Default Memory Request: {limit.defaultRequest.memory}')
28+
29+
pods = oc.selector('pods').objects()
30+
print(f'\nFound: {len(pods)} Pods')
31+
32+
for pod in pods:
33+
print(f'Processing Pod: {pod.name()}')
34+
for container in pod.model.spec.containers:
35+
print(f' Processing Container: {container.name}')
36+
print(f' CPU Limit: {container.resources.limits.cpu}')
37+
print(f' CPU Request: {container.resources.requests.cpu}')
38+
print(f' Memory Limit: {container.resources.limits.memory}')
39+
print(f' Memory Request: {container.resources.requests.memory}')

0 commit comments

Comments
 (0)