Skip to content

Commit

Permalink
fix: sc should use namespace in k8 mode
Browse files Browse the repository at this point in the history
  • Loading branch information
nacardin committed Nov 1, 2023
1 parent f0f4968 commit af648b5
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 39 deletions.
42 changes: 8 additions & 34 deletions crates/fluvio-sc/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@ use tracing::info;
use tracing::debug;
use clap::Parser;

use k8_client::K8Config;
use fluvio_types::print_cli_err;
use fluvio_types::defaults::TLS_SERVER_SECRET_NAME;
use fluvio_future::openssl::TlsAcceptor;
use fluvio_future::openssl::SslVerifyMode;

use crate::services::auth::basic::BasicRbacPolicy;
use crate::error::ScError;
use crate::config::ScConfig;

type Config = (ScConfig, Option<BasicRbacPolicy>);
Expand Down Expand Up @@ -91,31 +89,13 @@ pub enum RunMode<'a> {
impl ScOpt {
pub fn mode(&self) -> RunMode<'_> {
match (&self.local, &self.read_only, self.k8) {
(Some(metadata), _, _) => RunMode::Local(metadata),
(_, Some(path), _) => RunMode::ReadOnly(path),
_ => RunMode::K8s,
(Some(metadata), None, false) => RunMode::Local(metadata),
(None, Some(path), false) => RunMode::ReadOnly(path),
(None, None, true) => RunMode::K8s,
_ => panic!("Params do not satisfy defined run modes"),
}
}

#[allow(clippy::type_complexity)]
fn get_sc_and_k8_config(
mut self,
) -> Result<(Config, K8Config, Option<(String, TlsConfig)>), ScError> {
let k8_config = K8Config::load().expect("no k8 config founded");
info!(?k8_config, "k8 config");

// if name space is specified, use one from k8 config
if self.namespace.is_none() {
let k8_namespace = k8_config.namespace().to_owned();
info!("using {} as namespace from kubernetes config", k8_namespace);
self.namespace = Some(k8_namespace);
}

let (sc_config, tls_option) = self.as_sc_config()?;

Ok((sc_config, k8_config, tls_option))
}

/// as sc configuration, 2nd part of tls configuration(proxy addr, tls config)
/// 3rd part is path to read only metadata config
#[allow(clippy::wrong_self_convention)]
Expand All @@ -131,6 +111,10 @@ impl ScOpt {
config.private_endpoint = private_addr;
}

if let Some(namespace) = self.namespace {
config.namespace = namespace
}

config.x509_auth_scopes = self.x509_auth_scopes;
config.white_list = self.white_list.into_iter().collect();
config.read_only_metadata = self.read_only.is_some();
Expand Down Expand Up @@ -169,16 +153,6 @@ impl ScOpt {
}
}

pub fn parse_k8s_cli_or_exit(self) -> (Config, K8Config, Option<(String, TlsConfig)>) {
match self.get_sc_and_k8_config() {
Err(err) => {
print_cli_err!(err);
process::exit(-1);
}
Ok(config) => config,
}
}

pub fn parse_cli_or_exit(self) -> (Config, Option<(String, TlsConfig)>) {
match self.as_sc_config() {
Err(err) => {
Expand Down
2 changes: 1 addition & 1 deletion crates/fluvio-sc/src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
mod sc_config;

pub use self::sc_config::ScConfig;

pub use self::sc_config::ScConfigBuilder;
pub use self::sc_config::DEFAULT_NAMESPACE;

macro_rules! whitelist {
($config:expr,$name:expr,$start:expr) => {
Expand Down
6 changes: 3 additions & 3 deletions crates/fluvio-sc/src/config/sc_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use std::{io::Error as IoError, path::PathBuf};
use fluvio_types::defaults::SC_PUBLIC_PORT;
use fluvio_types::defaults::SC_PRIVATE_PORT;

pub const DEFAULT_NAMESPACE: &str = "default";

// -----------------------------------
// Traits
// -----------------------------------
Expand All @@ -24,7 +26,6 @@ pub struct ScConfig {
pub read_only_metadata: bool,
pub public_endpoint: String,
pub private_endpoint: String,
pub run_k8_dispatchers: bool,
pub namespace: String,
pub x509_auth_scopes: Option<PathBuf>,
pub white_list: HashSet<String>,
Expand All @@ -36,8 +37,7 @@ impl ::std::default::Default for ScConfig {
read_only_metadata: false,
public_endpoint: format!("0.0.0.0:{SC_PUBLIC_PORT}"),
private_endpoint: format!("0.0.0.0:{SC_PRIVATE_PORT}"),
run_k8_dispatchers: true,
namespace: "default".to_owned(),
namespace: DEFAULT_NAMESPACE.to_owned(),
x509_auth_scopes: None,
white_list: HashSet::new(),
}
Expand Down
14 changes: 13 additions & 1 deletion crates/fluvio-sc/src/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::{
cli::{ScOpt, TlsConfig, RunMode},
services::auth::basic::BasicRbacPolicy,
config::ScConfig,
config::DEFAULT_NAMESPACE,
};

pub fn main_loop(opt: ScOpt) {
Expand Down Expand Up @@ -47,7 +48,18 @@ pub fn main_loop(opt: ScOpt) {
RunMode::K8s => {
info!("Running with K8");

let ((sc_config, auth_policy), k8_config, tls_option) = opt.parse_k8s_cli_or_exit();
let ((mut sc_config, auth_policy), tls_option) = opt.parse_cli_or_exit();

let k8_config = K8Config::load().expect("no k8 config founded");
info!(?k8_config, "k8 config");

// if name space is specified, use one from k8 config
if sc_config.namespace == DEFAULT_NAMESPACE {
let k8_namespace = k8_config.namespace().to_owned();
info!("using {} as namespace from kubernetes config", k8_namespace);
sc_config.namespace = k8_namespace;
}

let client = create_k8_client(k8_config).expect("failed to create k8 client");
k8_main_loop(sc_config, client, auth_policy, tls_option)
}
Expand Down

0 comments on commit af648b5

Please sign in to comment.