Skip to content
This repository was archived by the owner on Oct 3, 2020. It is now read-only.

Read default namespace from service account #70

Merged
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
12 changes: 11 additions & 1 deletion pykube/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@ def from_service_account(
"""
Construct KubeConfig from in-cluster service account.
"""
with open(os.path.join(path, "namespace")) as fp:
namespace = fp.read()

with open(os.path.join(path, "token")) as fp:
token = fp.read()

host = os.environ.get("PYKUBE_KUBERNETES_SERVICE_HOST")
if host is None:
host = os.environ["KUBERNETES_SERVICE_HOST"]
Expand All @@ -53,7 +56,14 @@ def from_service_account(
],
"users": [{"name": "self", "user": {"token": token}}],
"contexts": [
{"name": "self", "context": {"cluster": "self", "user": "self"}}
{
"name": "self",
"context": {
"cluster": "self",
"user": "self",
"namespace": namespace,
},
}
],
"current-context": "self",
}
Expand Down
7 changes: 6 additions & 1 deletion tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ def test_from_service_account_no_file(tmpdir):
config.KubeConfig.from_service_account(path=str(tmpdir))


def test_from_service_account_(tmpdir):
def test_from_service_account(tmpdir):
namespace_file = Path(tmpdir) / "namespace"
token_file = Path(tmpdir) / "token"
ca_file = Path(tmpdir) / "ca.crt"

with namespace_file.open("w") as fd:
fd.write("mynamespace")

with token_file.open("w") as fd:
fd.write("mytok")

Expand All @@ -43,6 +47,7 @@ def test_from_service_account_(tmpdir):
"certificate-authority": str(ca_file),
}
assert cfg.doc["users"][0]["user"]["token"] == "mytok"
assert cfg.namespace == "mynamespace"


def test_from_url():
Expand Down