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

Load namespace when run in a cluster #74

Merged
merged 3 commits into from
Oct 6, 2019
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
10 changes: 8 additions & 2 deletions src/config/incluster_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub const SERVICE_HOSTENV: &str = "KUBERNETES_SERVICE_HOST";
pub const SERVICE_PORTENV: &str = "KUBERNETES_SERVICE_PORT";
const SERVICE_TOKENFILE: &str = "/var/run/secrets/kubernetes.io/serviceaccount/token";
const SERVICE_CERTFILE: &str = "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt";
const SERVICE_DEFAULT_NS: &str = "/var/run/secrets/kubernetes.io/serviceaccount/namespace";

/// Returns kubernetes address from specified environment variables.
pub fn kube_server() -> Option<String> {
Expand All @@ -26,15 +27,20 @@ fn kube_port() -> Option<String> {

/// Returns token from specified path in cluster.
pub fn load_token() -> Result<String, Error> {
utils::data_or_file(&None, &Some(SERVICE_TOKENFILE.to_string()))
utils::data_or_file(&None, &Some(SERVICE_TOKENFILE))
}

/// Returns certification from specified path in cluster.
pub fn load_cert() -> Result<X509, Error> {
let ca = utils::data_or_file_with_base64(&None, &Some(SERVICE_CERTFILE.to_string()))?;
let ca = utils::data_or_file_with_base64(&None, &Some(SERVICE_CERTFILE))?;
X509::from_pem(&ca).map_err(Error::from)
}

/// Returns the default namespace from specified path in cluster.
pub fn load_default_ns() -> Result<String, Error> {
utils::data_or_file(&None, &Some(SERVICE_DEFAULT_NS))
}

#[test]
fn test_kube_host() {
let expected = "fake.io";
Expand Down
18 changes: 16 additions & 2 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,22 @@ use self::kube_config::KubeConfigLoader;
pub struct Configuration {
pub base_path: String,
pub client: Client,

/// The current default namespace. This will be "default" while running outside of a cluster,
/// and will be the namespace of the pod while running inside a cluster.
pub default_ns: String,
}

impl Configuration {
pub fn new(base_path: String, client: Client) -> Self {
Self::with_default_ns(base_path, client, "default".to_string())
}

pub fn with_default_ns(base_path: String, client: Client, default_ns: String) -> Self {
Configuration {
base_path: base_path.to_owned(),
client,
default_ns,
}
}
}
Expand Down Expand Up @@ -166,6 +175,10 @@ pub fn incluster_config() -> Result<Configuration> {
let token = incluster_config::load_token()
.context(ErrorKind::KubeConfig("Unable to load in cluster token".to_string()))?;

let default_ns = incluster_config::load_default_ns().context(ErrorKind::KubeConfig(
"Unable to load incluster default namespace".to_string(),
))?;

let mut headers = header::HeaderMap::new();
headers.insert(
header::AUTHORIZATION,
Expand All @@ -177,10 +190,11 @@ pub fn incluster_config() -> Result<Configuration> {
.add_root_certificate(req_ca)
.default_headers(headers);

Ok(Configuration::new(
Ok(Configuration::with_default_ns(
server,
client_builder.build()
.context(ErrorKind::KubeConfig("Unable to build client".to_string()))?
.context(ErrorKind::KubeConfig("Unable to build client".to_string()))?,
default_ns,
))
}

Expand Down