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

Add namespace info in case of service account connection #291

Merged
merged 1 commit into from
Nov 11, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

<!-- ### Added | Changed | Deprecated | Removed | Fixed | Security -->

### Added

- `K8s.Conn` - Add namespace info in case of service account connection.

<!--------------------- Don't add new entries after this line --------------------->

## [2.4.2] - 2023-10-18
Expand Down
19 changes: 15 additions & 4 deletions lib/k8s/conn.ex
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,13 @@ defmodule K8s.Conn do
discovery_driver: K8s.default_discovery_driver(),
discovery_opts: K8s.default_discovery_opts(),
http_provider: K8s.default_http_provider(),
cacertfile: K8s.default_cacertfile()
cacertfile: K8s.default_cacertfile(),
namespace: nil

@typedoc ~S"""
* `cluster_name` - The cluster name if read from a kubeconfig file
* `user_name` - The user name if read from a kubeconfig file
* `namespace` - The namespace if read from a service account token
* `url` - The Kubernetes API URL
"""
@type t :: %__MODULE__{
Expand All @@ -79,7 +81,8 @@ defmodule K8s.Conn do
discovery_driver: module(),
discovery_opts: Keyword.t(),
http_provider: module(),
cacertfile: String.t()
cacertfile: String.t(),
namespace: String.t() | nil
}

@doc ~S"""
Expand Down Expand Up @@ -202,15 +205,23 @@ defmodule K8s.Conn do
def from_service_account(service_account_path, opts) do
cert_path = Path.join(service_account_path, "ca.crt")
token_path = Path.join(service_account_path, "token")
namespace_path = Path.join(service_account_path, "namespace")
insecure_skip_tls_verify = Keyword.get(opts, :insecure_skip_tls_verify, false)

with {:ok, token} <- File.read(token_path),
{:ok, ca_cert} <- PKI.cert_from_pem(cert_path) do
namespace =
case File.read(namespace_path) do
{:ok, namespace} -> namespace
_ -> nil
end

conn = %Conn{
url: kubernetes_service_url(),
ca_cert: ca_cert,
auth: %K8s.Conn.Auth.Token{token: token},
insecure_skip_tls_verify: insecure_skip_tls_verify
insecure_skip_tls_verify: insecure_skip_tls_verify,
namespace: namespace
}

{:ok, conn}
Expand Down Expand Up @@ -307,7 +318,7 @@ defmodule K8s.Conn do
end
end

@spec maybe_update_defaults(Conn.t(), keyword()) :: Conn.t()
@spec maybe_update_defaults(t(), keyword()) :: t()
defp maybe_update_defaults(conn, opts) do
struct!(conn, Keyword.take(opts, [:discovery_driver, :discovery_opts, :http_provider]))
end
Expand Down
3 changes: 2 additions & 1 deletion test/k8s/conn_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ defmodule K8s.ConnTest do
assert conn.cluster_name == nil
assert conn.url == "https://kewlhost:1337"
assert conn.ca_cert
assert conn.auth.token
assert conn.auth.token == "imatoken"
assert conn.namespace == "imanamespace"
end
end

Expand Down
1 change: 1 addition & 0 deletions test/support/tls/namespace
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
imanamespace
2 changes: 1 addition & 1 deletion test/support/tls/token
Original file line number Diff line number Diff line change
@@ -1 +1 @@
imatoken
imatoken