Skip to content

Commit 61893cb

Browse files
Add kubernetes inventory.
1 parent 4e56eb0 commit 61893cb

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

pyinfra/api/inventories/__init__.py

Whitespace-only changes.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from pyinfra.api.exceptions import InventoryError
2+
from pyinfra.api.connectors.util import run_local_process, split_combined_output
3+
4+
def get_pods(selector, namespace='default', all_namespaces=False, container=None):
5+
if all_namespaces:
6+
ns = ['-A']
7+
else:
8+
ns = ['-n', namespace]
9+
10+
return_code, combined_output = run_local_process(
11+
['"$@"', '-',
12+
'kubectl', 'get', 'pods', *ns, '-l', selector, '--template',
13+
r'{{range .items}}@kubernetes/' +
14+
r'{{.metadata.namespace}}/{{.metadata.name}}{{"\n"}}{{end}}'
15+
]
16+
)
17+
stdout, stderr = split_combined_output(combined_output)
18+
19+
if return_code == 0:
20+
data = {}
21+
if container:
22+
data['container'] = container
23+
return list(map(lambda s: (s, data), stdout))
24+
else:
25+
raise InventoryError('kubectl failed (status {0}): {1}'.
26+
format(return_code, '\n'.join(stderr)))

0 commit comments

Comments
 (0)