Skip to content

Commit 43e93b7

Browse files
Allow kubernete context to be supplied
Closes #554 Co-authored-by: Philippe Pepiot <[email protected]>
1 parent 49fe891 commit 43e93b7

File tree

4 files changed

+23
-8
lines changed

4 files changed

+23
-8
lines changed

doc/source/backends.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ namespace::
127127
$ py.test --hosts='kubectl://mypod-a1b2c3'
128128
# specify container name and namespace
129129
$ py.test --hosts='kubectl://somepod-2536ab?container=nginx&namespace=web'
130+
# specify the kubeconfig context to use
131+
$ py.test --hosts='kubectl://somepod-2536ab?context=k8s-cluster-a&container=nginx'
130132
# you can specify kubeconfig either from KUBECONFIG environment variable
131133
# or when working with multiple configuration with the "kubeconfig" option
132134
$ py.test --hosts='kubectl://somepod-123?kubeconfig=/path/kubeconfig,kubectl://otherpod-123?kubeconfig=/other/kubeconfig'

test/test_backends.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -441,18 +441,27 @@ def test_parse_hostspec(hostspec, expected):
441441
assert BaseBackend.parse_hostspec(hostspec) == expected
442442

443443

444-
@pytest.mark.parametrize('hostspec,pod,container,namespace,kubeconfig', [
445-
('kubectl://pod', 'pod', None, None, None),
446-
('kubectl://pod?namespace=n', 'pod', None, 'n', None),
447-
('kubectl://pod?container=c&namespace=n', 'pod', 'c', 'n', None),
448-
('kubectl://pod?namespace=n&kubeconfig=k', 'pod', None, 'n', 'k')
449-
])
450-
def test_kubectl_hostspec(hostspec, pod, container, namespace, kubeconfig):
444+
@pytest.mark.parametrize(
445+
'hostspec,pod,container,namespace,kubeconfig,context', [
446+
('kubectl://pod', 'pod', None, None, None, None),
447+
('kubectl://pod?namespace=n', 'pod', None, 'n', None, None),
448+
('kubectl://pod?container=c&namespace=n',
449+
'pod', 'c', 'n', None, None),
450+
('kubectl://pod?namespace=n&kubeconfig=k',
451+
'pod', None, 'n', 'k', None),
452+
('kubectl://pod?context=ctx&container=c',
453+
'pod', 'c', None, None, 'ctx')
454+
]
455+
)
456+
def test_kubectl_hostspec(
457+
hostspec, pod, container, namespace, kubeconfig, context,
458+
):
451459
backend = testinfra.get_host(hostspec).backend
452460
assert backend.name == pod
453461
assert backend.container == container
454462
assert backend.namespace == namespace
455463
assert backend.kubeconfig == kubeconfig
464+
assert backend.context == context
456465

457466

458467
@pytest.mark.parametrize('hostspec,pod,container,namespace,kubeconfig', [

testinfra/backend/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def parse_hostspec(hostspec):
5252
kw[key] = True
5353
for key in ("sudo_user", 'namespace', 'container', 'read_timeout_sec',
5454
'operation_timeout_sec', 'timeout', 'controlpersist',
55-
'kubeconfig'):
55+
'kubeconfig', 'context'):
5656
if key in query:
5757
kw[key] = query.get(key)[0]
5858
for key in (

testinfra/backend/kubectl.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ def __init__(self, name, *args, **kwargs):
2121
self.container = kwargs.get('container')
2222
self.namespace = kwargs.get('namespace')
2323
self.kubeconfig = kwargs.get('kubeconfig')
24+
self.context = kwargs.get('context')
2425
super().__init__(self.name, *args, **kwargs)
2526

2627
def run(self, command, *args, **kwargs):
@@ -32,6 +33,9 @@ def run(self, command, *args, **kwargs):
3233
if self.kubeconfig is not None:
3334
kcmd += '--kubeconfig="%s" '
3435
kcmd_args.append(self.kubeconfig)
36+
if self.context is not None:
37+
kcmd += '--context="%s" '
38+
kcmd_args.append(self.context)
3539
if self.namespace is not None:
3640
kcmd += '-n %s '
3741
kcmd_args.append(self.namespace)

0 commit comments

Comments
 (0)