Skip to content

Commit

Permalink
mirrord-config: specify custom path for kubeconfig (#1036)
Browse files Browse the repository at this point in the history
* Add a field for custom path kubeconfig to config

* Update schema

* changelog

* .

* update schema

---------

Co-authored-by: Aviram Hassan <[email protected]>
  • Loading branch information
infiniteregrets and aviramha authored Feb 9, 2023
1 parent 5458036 commit bf2d635
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how

## [Unreleased]

### Added

- Add a field to mirrord-config to specify custom path for kubeconfig , resolves [#1027](https://github.com/metalbear-co/mirrord/issues/1027).

### Changed

- IntelliJ-ext: change the dialog to provide a sorted list and make it searchable, resolves [#1031](https://github.com/metalbear-co/mirrord/issues/1031).
Expand Down
7 changes: 7 additions & 0 deletions mirrord-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@
}
]
},
"kubeconfig": {
"description": "Path to a kubeconfig file, if not specified, will use KUBECONFIG or ~/.kube/config or the in-cluster config.",
"type": [
"string",
"null"
]
},
"operator": {
"description": "Allow to lookup if operator is installed on cluster and use it",
"type": [
Expand Down
6 changes: 6 additions & 0 deletions mirrord/config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ pub struct LayerConfig {
/// Allow to lookup if operator is installed on cluster and use it
#[config(env = "MIRRORD_OPERATOR_ENABLE", default = true)]
pub operator: bool,

/// Path to a kubeconfig file, if not specified, will use KUBECONFIG or ~/.kube/config or the
/// in-cluster config.
#[config(env = "MIRRORD_KUBECONFIG")]
pub kubeconfig: Option<String>,
}

impl LayerConfig {
Expand Down Expand Up @@ -318,6 +323,7 @@ mod tests {

let expect = LayerFileConfig {
accept_invalid_certificates: Some(false),
kubeconfig: None,
connect_agent_name: None,
connect_agent_port: None,
target: Some(TargetFileConfig::Advanced {
Expand Down
13 changes: 11 additions & 2 deletions mirrord/kube/src/api/kubernetes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use std::time::Duration;

use async_trait::async_trait;
use k8s_openapi::api::core::v1::Pod;
use kube::{Api, Client, Config};
use kube::{
config::{KubeConfigOptions, Kubeconfig},
Api, Client, Config,
};
use mirrord_config::{agent::AgentConfig, target::TargetConfig, LayerConfig};
use mirrord_progress::Progress;
use mirrord_protocol::{ClientMessage, DaemonMessage};
Expand Down Expand Up @@ -148,6 +151,13 @@ pub async fn create_kube_api(config: Option<LayerConfig>) -> Result<Client> {
let mut kube_config = Config::infer().await?;

if let Some(config) = config {
if let Some(kube_config_file) = config.kubeconfig {
let parsed_kube_config = Kubeconfig::read_from(kube_config_file)?;
kube_config =
Config::from_custom_kubeconfig(parsed_kube_config, &KubeConfigOptions::default())
.await?;
}

#[cfg_attr(not(feature = "env_guard"), allow(unused_mut))]
if config.accept_invalid_certificates {
kube_config.accept_invalid_certs = true;
Expand All @@ -157,7 +167,6 @@ pub async fn create_kube_api(config: Option<LayerConfig>) -> Result<Client> {
}
}
}

#[cfg(feature = "env_guard")]
_guard.prepare_config(&mut kube_config);

Expand Down
3 changes: 3 additions & 0 deletions mirrord/kube/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ pub enum KubeApiError {
#[error("mirrord-layer: Failed to get `KubeConfig`!")]
KubeConfigError(#[from] kube::config::InferConfigError),

#[error("mirrord-layer: Failed to get the custom path for `KubeConfig`!")]
KubeConfigPathError(#[from] kube::config::KubeconfigError),

#[error("mirrord-layer: JSON convert error")]
JSONConvertError(#[from] serde_json::Error),

Expand Down

0 comments on commit bf2d635

Please sign in to comment.