MCPcopy Index your code
hub / github.com/kubernetes-client/python / exec_commands

Function exec_commands

examples/pod_exec.py:28–117  ·  view source on GitHub ↗
(api_instance)

Source from the content-addressed store, hash-verified

26
27
28def exec_commands(api_instance):
29 name = 'busybox-test'
30 resp = None
31 try:
32 resp = api_instance.read_namespaced_pod(name=name,
33 namespace='default')
34 except ApiException as e:
35 if e.status != 404:
36 print(f"Unknown error: {e}")
37 exit(1)
38
39 if not resp:
40 print(f"Pod {name} does not exist. Creating it...")
41 pod_manifest = {
42 'apiVersion': 'v1',
43 'kind': 'Pod',
44 'metadata': {
45 'name': name
46 },
47 'spec': {
48 'containers': [{
49 'image': 'busybox',
50 'name': 'sleep',
51 "args": [
52 "/bin/sh",
53 "-c",
54 "while true;do date;sleep 5; done"
55 ]
56 }]
57 }
58 }
59 resp = api_instance.create_namespaced_pod(body=pod_manifest,
60 namespace='default')
61 while True:
62 resp = api_instance.read_namespaced_pod(name=name,
63 namespace='default')
64 if resp.status.phase != 'Pending':
65 break
66 time.sleep(1)
67 print("Done.")
68
69 # Calling exec and waiting for response
70 exec_command = [
71 '/bin/sh',
72 '-c',
73 'echo This message goes to stderr; echo This message goes to stdout']
74 # When calling a pod with multiple containers running the target container
75 # has to be specified with a keyword argument container=<name>.
76 resp = stream(api_instance.connect_get_namespaced_pod_exec,
77 name,
78 'default',
79 command=exec_command,
80 stderr=True, stdin=False,
81 stdout=True, tty=False)
82 print("Response: " + resp)
83
84 # Calling exec interactively
85 exec_command = ['/bin/sh']

Callers 1

mainFunction · 0.85

Calls 12

is_openMethod · 0.80
peek_stdoutMethod · 0.80
read_stdoutMethod · 0.80
peek_stderrMethod · 0.80
read_stderrMethod · 0.80
write_stdinMethod · 0.80
readline_stdoutMethod · 0.80
read_namespaced_podMethod · 0.45
create_namespaced_podMethod · 0.45
sleepMethod · 0.45
updateMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected