Skip to content

Commit

Permalink
Merge pull request #74 from dylanhart/feature/load-default-ns
Browse files Browse the repository at this point in the history
Load namespace when run in a cluster
  • Loading branch information
clux authored Oct 6, 2019
2 parents c3aaba5 + bf7976a commit 5fa24aa
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
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

0 comments on commit 5fa24aa

Please sign in to comment.