Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(client): fallback 'gefyra run' namespace from kubeconfig #140

Merged
merged 3 commits into from
Aug 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .github/workflows/python-tester.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,22 @@ jobs:
test $? -eq 0
curl localhost:8000
test $? -eq 0
- name: Run gefyra run with no given namespace and no fallback (uses default)
working-directory: ./client
run: |
poetry run coverage run -a -m gefyra run -i pyserver -N mypyserver1
test $? -eq 0
test $(docker inspect mypyserver1 --format {{.HostConfig.DnsSearch}}) == [default.svc.cluster.local]
docker kill mypyserver1
- name: Run gefyra run with default namespace from kubeconfig (uses 'fancy')
working-directory: ./client
run: |
kubectl config set-context --current --namespace=fancy
poetry run coverage run -a -m gefyra run -i pyserver -N mypyserver2
test $? -eq 0
test $(docker inspect mypyserver2 --format {{.HostConfig.DnsSearch}}) == [fancy.svc.cluster.local]
kubectl config set-context --current --namespace=default
docker kill mypyserver2
- name: Run gefyra bridge with invalid deployment
working-directory: ./client
shell: bash --noprofile --norc -o pipefail {0}
Expand Down
1 change: 0 additions & 1 deletion client/gefyra/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@
"-n",
"--namespace",
help="the namespace for this container to run in",
default="default",
)
run_parser.add_argument(
"--env",
Expand Down
14 changes: 12 additions & 2 deletions client/gefyra/api/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def run(
ports: dict = None,
detach: bool = True,
auto_remove: bool = True,
namespace: str = "default",
namespace: str = None,
env: list = None,
env_from: str = None,
config=default_configuration,
Expand All @@ -80,6 +80,16 @@ def run(
from gefyra.local.cargo import probe_wireguard_connection
from docker.errors import APIError

# #125: Fallback to namespace in kube config
if namespace is None:
from kubernetes.config import kube_config

_, active_context = kube_config.list_kube_config_contexts()
namespace = active_context["context"].get("namespace") or "default"
ns_source = "kubeconfig"
else:
ns_source = "--namespace argument"

dns_search = f"{namespace}.svc.cluster.local"
config = set_gefyra_network_from_cargo(config)
#
Expand Down Expand Up @@ -146,7 +156,7 @@ def run(

logger.info(
f"Container image '{', '.join(container.image.tags)}' started with name '{container.name}' in "
f"Kubernetes namespace '{namespace}'"
f"Kubernetes namespace '{namespace}' (from {ns_source})"
)
if detach:
return True
Expand Down