Skip to content

Commit ce51bc5

Browse files
committed
Move CLI arg into KubernetesClusterInfoCliOpts struct
1 parent d6db64f commit ce51bc5

File tree

3 files changed

+64
-24
lines changed

3 files changed

+64
-24
lines changed

crates/stackable-operator/src/cli.rs

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,10 @@ use clap::Args;
115115
use product_config::ProductConfigManager;
116116
use snafu::{ResultExt, Snafu};
117117

118-
use crate::{commons::networking::DomainName, logging::TracingTarget, namespace::WatchNamespace};
118+
use crate::{
119+
logging::TracingTarget, namespace::WatchNamespace,
120+
utils::cluster_info::KubernetesClusterInfoCliOpts,
121+
};
119122

120123
pub const AUTHOR: &str = "Stackable GmbH - info@stackable.tech";
121124

@@ -171,16 +174,22 @@ pub enum Command<Run: Args = ProductOperatorRun> {
171174
/// common: ProductOperatorRun,
172175
/// }
173176
/// use clap::Parser;
174-
/// use stackable_operator::logging::TracingTarget;
175-
/// use stackable_operator::namespace::WatchNamespace;
177+
/// use stackable_operator::{
178+
/// logging::TracingTarget,
179+
/// namespace::WatchNamespace,
180+
/// utils::cluster_info::KubernetesClusterInfoCliOpts
181+
/// };
182+
///
176183
/// let opts = Command::<Run>::parse_from(["foobar-operator", "run", "--name", "foo", "--product-config", "bar", "--watch-namespace", "foobar"]);
177184
/// assert_eq!(opts, Command::Run(Run {
178185
/// name: "foo".to_string(),
179186
/// common: ProductOperatorRun {
180187
/// product_config: ProductConfigPath::from("bar".as_ref()),
181188
/// watch_namespace: WatchNamespace::One("foobar".to_string()),
182189
/// tracing_target: TracingTarget::None,
183-
/// kubernetes_cluster_domain: None,
190+
/// cluster_info_opts: KubernetesClusterInfoCliOpts {
191+
/// kubernetes_cluster_domain: None
192+
/// }
184193
/// },
185194
/// }));
186195
/// ```
@@ -215,11 +224,8 @@ pub struct ProductOperatorRun {
215224
#[arg(long, env, default_value_t, value_enum)]
216225
pub tracing_target: TracingTarget,
217226

218-
/// Kubernetes cluster domain, usually this is `cluster.local`.
219-
// We are not using a default value here, as operators will probably do an more advanced
220-
// auto-detection of the cluster domain in case it is not specified in the future.
221-
#[arg(long, env)]
222-
pub kubernetes_cluster_domain: Option<DomainName>,
227+
#[command(flatten)]
228+
pub cluster_info_opts: KubernetesClusterInfoCliOpts,
223229
}
224230

225231
/// A path to a [`ProductConfigManager`] spec file
@@ -393,7 +399,9 @@ mod tests {
393399
product_config: ProductConfigPath::from("bar".as_ref()),
394400
watch_namespace: WatchNamespace::One("foo".to_string()),
395401
tracing_target: TracingTarget::None,
396-
kubernetes_cluster_domain: None,
402+
cluster_info_opts: KubernetesClusterInfoCliOpts {
403+
kubernetes_cluster_domain: None
404+
}
397405
}
398406
);
399407

@@ -405,7 +413,9 @@ mod tests {
405413
product_config: ProductConfigPath::from("bar".as_ref()),
406414
watch_namespace: WatchNamespace::All,
407415
tracing_target: TracingTarget::None,
408-
kubernetes_cluster_domain: None,
416+
cluster_info_opts: KubernetesClusterInfoCliOpts {
417+
kubernetes_cluster_domain: None
418+
}
409419
}
410420
);
411421

@@ -418,7 +428,9 @@ mod tests {
418428
product_config: ProductConfigPath::from("bar".as_ref()),
419429
watch_namespace: WatchNamespace::One("foo".to_string()),
420430
tracing_target: TracingTarget::None,
421-
kubernetes_cluster_domain: None,
431+
cluster_info_opts: KubernetesClusterInfoCliOpts {
432+
kubernetes_cluster_domain: None
433+
}
422434
}
423435
);
424436
}

crates/stackable-operator/src/client.rs

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ use snafu::{OptionExt, ResultExt, Snafu};
2020
use tracing::trace;
2121

2222
use crate::{
23-
commons::networking::DomainName, kvp::LabelSelectorExt,
24-
utils::cluster_info::KubernetesClusterInfo,
23+
kvp::LabelSelectorExt,
24+
utils::cluster_info::{KubernetesClusterInfo, KubernetesClusterInfoCliOpts},
2525
};
2626

2727
pub type Result<T, E = Error> = std::result::Result<T, E>;
@@ -521,12 +521,20 @@ impl Client {
521521
/// use tokio::time::error::Elapsed;
522522
/// use kube::runtime::watcher;
523523
/// use k8s_openapi::api::core::v1::Pod;
524-
/// use stackable_operator::client::{Client, initialize_operator};
524+
/// use stackable_operator::{
525+
/// client::{Client, initialize_operator},
526+
/// utils::cluster_info::KubernetesClusterInfoCliOpts
527+
/// };
525528
///
526529
/// #[tokio::main]
527-
/// async fn main(){
530+
/// async fn main() {
528531
///
529-
/// let client: Client = initialize_operator(&None, None).await.expect("Unable to construct client.");
532+
/// let cluster_info_cli_opts = KubernetesClusterInfoCliOpts {
533+
/// kubernetes_cluster_domain: None,
534+
/// };
535+
/// let client = initialize_operator(None, &cluster_info_cli_opts)
536+
/// .await
537+
/// .expect("Unable to construct client.");
530538
/// let watcher_config: watcher::Config =
531539
/// watcher::Config::default().fields(&format!("metadata.name=nonexistent-pod"));
532540
///
@@ -634,16 +642,16 @@ where
634642
}
635643

636644
pub async fn initialize_operator(
637-
cli_kubernetes_cluster_domain: &Option<DomainName>,
638645
field_manager: Option<String>,
646+
cluster_info_cli_opts: &KubernetesClusterInfoCliOpts,
639647
) -> Result<Client> {
640648
let kubeconfig: Config = kube::Config::infer()
641649
.await
642650
.map_err(kube::Error::InferConfig)
643651
.context(InferKubeConfigSnafu)?;
644652
let default_namespace = kubeconfig.default_namespace.clone();
645653
let client = kube::Client::try_from(kubeconfig).context(CreateKubeClientSnafu)?;
646-
let cluster_info = KubernetesClusterInfo::new(cli_kubernetes_cluster_domain);
654+
let cluster_info = KubernetesClusterInfo::new(cluster_info_cli_opts);
647655

648656
Ok(Client::new(
649657
client,
@@ -668,10 +676,15 @@ mod tests {
668676
};
669677
use tokio::time::error::Elapsed;
670678

679+
use crate::utils::cluster_info::KubernetesClusterInfoCliOpts;
680+
671681
#[tokio::test]
672682
#[ignore = "Tests depending on Kubernetes are not ran by default"]
673683
async fn k8s_test_wait_created() {
674-
let client = super::initialize_operator(&None, None)
684+
let cluster_info_cli_opts = KubernetesClusterInfoCliOpts {
685+
kubernetes_cluster_domain: None,
686+
};
687+
let client = super::initialize_operator(None, &cluster_info_cli_opts)
675688
.await
676689
.expect("KUBECONFIG variable must be configured.");
677690

@@ -749,7 +762,10 @@ mod tests {
749762
#[tokio::test]
750763
#[ignore = "Tests depending on Kubernetes are not ran by default"]
751764
async fn k8s_test_wait_created_timeout() {
752-
let client = super::initialize_operator(&None, None)
765+
let cluster_info_cli_opts = KubernetesClusterInfoCliOpts {
766+
kubernetes_cluster_domain: None,
767+
};
768+
let client = super::initialize_operator(None, &cluster_info_cli_opts)
753769
.await
754770
.expect("KUBECONFIG variable must be configured.");
755771

@@ -769,7 +785,10 @@ mod tests {
769785
#[tokio::test]
770786
#[ignore = "Tests depending on Kubernetes are not ran by default"]
771787
async fn k8s_test_list_with_label_selector() {
772-
let client = super::initialize_operator(&None, None)
788+
let cluster_info_cli_opts = KubernetesClusterInfoCliOpts {
789+
kubernetes_cluster_domain: None,
790+
};
791+
let client = super::initialize_operator(None, &cluster_info_cli_opts)
773792
.await
774793
.expect("KUBECONFIG variable must be configured.");
775794

crates/stackable-operator/src/utils/cluster_info.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,18 @@ pub struct KubernetesClusterInfo {
99
pub cluster_domain: DomainName,
1010
}
1111

12+
#[derive(clap::Parser, Debug, PartialEq, Eq)]
13+
pub struct KubernetesClusterInfoCliOpts {
14+
/// Kubernetes cluster domain, usually this is `cluster.local`.
15+
// We are not using a default value here, as operators will probably do an more advanced
16+
// auto-detection of the cluster domain in case it is not specified in the future.
17+
#[arg(long, env)]
18+
pub kubernetes_cluster_domain: Option<DomainName>,
19+
}
20+
1221
impl KubernetesClusterInfo {
13-
pub fn new(cli_kubernetes_cluster_domain: &Option<DomainName>) -> Self {
14-
let cluster_domain = match cli_kubernetes_cluster_domain {
22+
pub fn new(cluster_info_cli_opts: &KubernetesClusterInfoCliOpts) -> Self {
23+
let cluster_domain = match &cluster_info_cli_opts.kubernetes_cluster_domain {
1524
Some(cluster_domain) => {
1625
tracing::info!(%cluster_domain, "Using configured Kubernetes cluster domain");
1726

0 commit comments

Comments
 (0)