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

mirrord-config: specify custom path for kubeconfig #1036

Merged
merged 6 commits into from
Feb 9, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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