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

feat: use installation type in cli commands #3675

Merged
merged 1 commit into from
Nov 10, 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: 2 additions & 2 deletions .github/workflows/cd_dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ jobs:
- name: Run diagnostics
if: ${{ !success() }}
timeout-minutes: 5
run: ~/.fluvio/bin/fluvio cluster diagnostics --k8
run: ~/.fluvio/bin/fluvio cluster diagnostics
- name: Upload diagnostics
uses: actions/upload-artifact@v3
timeout-minutes: 5
Expand Down Expand Up @@ -236,7 +236,7 @@ jobs:
- name: Run diagnostics
if: ${{ !success() }}
timeout-minutes: 5
run: fluvio cluster diagnostics --k8
run: fluvio cluster diagnostics
- name: Upload diagnostics
uses: actions/upload-artifact@v3
timeout-minutes: 5
Expand Down
12 changes: 6 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ jobs:
- name: Run diagnostics
if: ${{ !success() }}
timeout-minutes: 5
run: fluvio cluster diagnostics --local
run: fluvio cluster diagnostics
- name: Upload diagnostics
uses: actions/upload-artifact@v3
timeout-minutes: 5
Expand Down Expand Up @@ -840,7 +840,7 @@ jobs:
- name: Run diagnostics
if: ${{ !success() }}
timeout-minutes: 5
run: fluvio cluster diagnostics --k8
run: fluvio cluster diagnostics
- name: Upload diagnostics
timeout-minutes: 5
if: ${{ !success() }}
Expand Down Expand Up @@ -903,7 +903,7 @@ jobs:
- name: Run diagnostics
if: ${{ !success() }}
timeout-minutes: 5
run: ~/bin/fluvio cluster diagnostics --k8
run: ~/bin/fluvio cluster diagnostics
- name: Upload logs
timeout-minutes: 5
if: ${{ !success() }}
Expand Down Expand Up @@ -1075,7 +1075,7 @@ jobs:
- name: Run diagnostics
if: ${{ !success() }}
timeout-minutes: 5
run: fluvio cluster diagnostics --k8
run: fluvio cluster diagnostics
- name: Upload diagnostics
uses: actions/upload-artifact@v3
timeout-minutes: 5
Expand Down Expand Up @@ -1216,12 +1216,12 @@ jobs:

- name: Shutdown Fluvio cluster
timeout-minutes: 10
run: fluvio cluster shutdown --local
run: fluvio cluster shutdown

- name: Run diagnostics
if: ${{ !success() }}
timeout-minutes: 5
run: fluvio cluster diagnostics --local
run: fluvio cluster diagnostics

- name: Upload diagnostics
uses: actions/upload-artifact@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/hourly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ jobs:
- name: Run diagnostics
if: failure()
timeout-minutes: 5
run: fluvio cluster diagnostics --k8
run: fluvio cluster diagnostics
- name: Upload diagnostics
uses: actions/upload-artifact@v3
timeout-minutes: 5
Expand Down
18 changes: 9 additions & 9 deletions crates/fluvio-cli/src/profile/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ use clap::Parser;
use anyhow::Result;

use fluvio::config::{ConfigFile, TlsPolicy};
use fluvio_cluster::InstallationType;

#[derive(Debug, Parser)]
pub struct ManualAddOpt {
/// Name of profile to add
profile_name: String,

/// address of cluster, e.g. 127.0.0.1:9003
/// Address of cluster, e.g. 127.0.0.1:9003
cluster_address: String,

/// Installation type of cluster, e.g. local, local-k8, k8
installation_type: Option<InstallationType>,
}
// todo: p2 add tls config, p1 is default disabled for manual add

Expand All @@ -23,14 +27,10 @@ impl ManualAddOpt {
&self.cluster_address,
&def_tls,
)?;
if config_file
.mut_config()
.set_current_profile(&self.profile_name)
{
println!("Switched to profile {}", &self.profile_name);
} else {
return Err(anyhow::anyhow!("error creating profile"));
}
let config = config_file.mut_config().current_cluster_mut()?;
self.installation_type.unwrap_or_default().save_to(config)?;
config_file.save()?;
println!("Switched to profile {}", &self.profile_name);
}
Err(_) => println!("no profile can be found"),
}
Expand Down
18 changes: 13 additions & 5 deletions crates/fluvio-cli/src/profile/list.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::sync::Arc;

use fluvio_cluster::InstallationType;
use serde::Serialize;
use comfy_table::Row;
use clap::Parser;
Expand Down Expand Up @@ -50,7 +51,7 @@ fn format_tls(tls: &TlsPolicy) -> &'static str {

impl TableOutputHandler for ListConfig<'_> {
fn header(&self) -> Row {
Row::from(["", "PROFILE", "CLUSTER", "ADDRESS", "TLS"])
Row::from(["", "PROFILE", "CLUSTER", "ADDRESS", "TLS", "INSTALLATION"])
}

fn content(&self) -> Vec<Row> {
Expand All @@ -65,13 +66,20 @@ impl TableOutputHandler for ListConfig<'_> {
.map(|active| if active { "*" } else { "" })
.unwrap_or("");

let (cluster, addr, tls) = self
let (cluster, addr, tls, installation_type) = self
.0
.cluster(&profile.cluster)
.map(|it| (&*profile.cluster, &*it.endpoint, format_tls(&it.tls)))
.unwrap_or(("", "", ""));
.map(|it| {
(
&*profile.cluster,
&*it.endpoint,
format_tls(&it.tls),
InstallationType::load_or_default(it).to_string(),
)
})
.unwrap_or(("", "", "", "".to_string()));

Row::from([active, profile_name, cluster, addr, tls])
Row::from([active, profile_name, cluster, addr, tls, &installation_type])
})
.collect()
}
Expand Down
2 changes: 1 addition & 1 deletion crates/fluvio-cluster/src/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ pub enum ClusterCheckError {
VersionError(#[from] semver::Error),

/// local fluvio exists
#[error("Loocal Fluvio running")]
#[error("Local Fluvio running")]
LocalClusterExists,

/// Other misc
Expand Down
49 changes: 31 additions & 18 deletions crates/fluvio-cluster/src/cli/delete.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use clap::Parser;
use fluvio::config::ConfigFile;
use tracing::debug;

use crate::InstallationType;
use crate::delete::ClusterUninstallConfig;
use crate::cli::ClusterCliError;

Expand All @@ -8,40 +11,50 @@ pub struct DeleteOpt {
#[arg(long, value_name = "Kubernetes namespace")]
namespace: Option<String>,

/// Remove only local spu/sc(custom) fluvio installation
#[arg(long, conflicts_with = "k8", conflicts_with = "sys")]
local: bool,

/// Remove only k8 fluvio installation
#[arg(long, conflicts_with = "local", conflicts_with = "sys")]
k8: bool,
#[arg(long = "k8", conflicts_with = "sys_only")]
k8_only: bool,

#[arg(long, conflicts_with = "k8", conflicts_with = "local")]
/// delete system chart
sys: bool,
#[arg(long = "sys", conflicts_with = "k8_only")]
/// Remove system chart only
sys_only: bool,
}

impl DeleteOpt {
pub async fn process(self) -> Result<(), ClusterCliError> {
let mut builder = ClusterUninstallConfig::builder();
builder.hide_spinner(false);

if self.sys {
if self.sys_only {
builder.uninstall_local(false);
builder.uninstall_k8(false);
builder.uninstall_sys(true);
} else if self.local {
builder.uninstall_local(true);
builder.uninstall_k8(false);
builder.uninstall_sys(false);
} else if self.k8 {
} else if self.k8_only {
builder.uninstall_local(false);
builder.uninstall_k8(true);
builder.uninstall_sys(false);
} else {
builder.uninstall_local(true);
builder.uninstall_k8(true);
builder.uninstall_sys(true);
let config_file = ConfigFile::load_default_or_new()?;
let installation_type =
InstallationType::load_or_default(config_file.config().current_cluster()?);
debug!(?installation_type);
match installation_type {
InstallationType::K8 => {
builder.uninstall_local(false);
builder.uninstall_k8(true);
builder.uninstall_sys(true);
}
InstallationType::LocalK8 => {
builder.uninstall_local(true);
builder.uninstall_k8(true);
builder.uninstall_sys(true);
}
InstallationType::Local | InstallationType::ReadOnly => {
builder.uninstall_local(true);
builder.uninstall_k8(false);
builder.uninstall_sys(false);
}
}
}

if let Some(namespace) = self.namespace {
Expand Down
78 changes: 34 additions & 44 deletions crates/fluvio-cluster/src/cli/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,21 @@ use fluvio::config::ConfigFile;
use fluvio::metadata::{topic::TopicSpec, partition::PartitionSpec, spg::SpuGroupSpec, spu::SpuSpec};
use fluvio_sc_schema::objects::Metadata;

use crate::InstallationType;
use crate::cli::ClusterCliError;
use crate::cli::start::get_log_directory;
use crate::start::local::DEFAULT_DATA_DIR as DEFAULT_LOCAL_DIR;

#[derive(Debug)]
enum ProfileType {
K8,
Local,
Cloud,
}
use crate::start::local::{DEFAULT_DATA_DIR as DEFAULT_LOCAL_DIR, DEFAULT_METADATA_SUB_DIR};

#[derive(Parser, Debug)]
pub struct DiagnosticsOpt {
#[arg(long)]
quiet: bool,

#[arg(long)]
k8: bool,

#[arg(long)]
local: bool,

#[arg(long)]
cloud: bool,
}

impl DiagnosticsOpt {
pub async fn process(self) -> Result<()> {
let profile_ty = self.get_profile_ty()?;
println!("Using profile type: {profile_ty:#?}");
let installation_ty = self.get_installation_ty()?;
println!("Using installation: {installation_ty:#?}");
let temp_dir = tempfile::Builder::new()
.prefix("fluvio-diagnostics")
.tempdir()?;
Expand All @@ -57,21 +42,25 @@ impl DiagnosticsOpt {
};

// write internal fluvio cluster internal state
match profile_ty {
match installation_ty {
// Local cluster
ProfileType::Local => {
self.write_helm(temp_path)?;
InstallationType::Local | InstallationType::ReadOnly => {
self.copy_local_logs(temp_path)?;
self.copy_local_metadata(temp_path)?;
for spu in spu_specs {
self.spu_disk_usage(None, temp_path, &spu.spec)?;
}
}
// Cloud cluster
ProfileType::Cloud => {
println!("Cannot collect logs from Cloud, skipping");
// Local cluster with k8 metadata
InstallationType::LocalK8 => {
self.write_helm(temp_path)?;
self.copy_local_logs(temp_path)?;
for spu in spu_specs {
self.spu_disk_usage(None, temp_path, &spu.spec)?;
}
}
// Guess Kubernetes cluster
ProfileType::K8 => {
// Kubernetes cluster
InstallationType::K8 => {
let kubectl = match which("kubectl") {
Ok(kubectl) => kubectl,
Err(_) => {
Expand Down Expand Up @@ -110,23 +99,11 @@ impl DiagnosticsOpt {
Ok(())
}

// get type of profile
fn get_profile_ty(&self) -> Result<ProfileType> {
if self.k8 {
Ok(ProfileType::K8)
} else if self.local {
Ok(ProfileType::Local)
} else if self.cloud {
Ok(ProfileType::Cloud)
} else {
let config = ConfigFile::load_default_or_new()?;
match config.config().current_profile_name() {
Some("local") => Ok(ProfileType::Local),
// Cloud cluster
Some(other) if other.contains("cloud") => Ok(ProfileType::Cloud),
_ => Ok(ProfileType::K8),
}
}
fn get_installation_ty(&self) -> Result<InstallationType> {
let config = ConfigFile::load_default_or_new()?;
Ok(InstallationType::load_or_default(
config.config().current_cluster()?,
))
}

fn zip_files(&self, source: &Path, output: &mut std::fs::File) -> Result<(), std::io::Error> {
Expand Down Expand Up @@ -158,6 +135,19 @@ impl DiagnosticsOpt {
Ok(())
}

fn copy_local_metadata(&self, dest_dir: &Path) -> Result<()> {
let metadata_path = DEFAULT_LOCAL_DIR
.to_owned()
.unwrap_or_default()
.join(DEFAULT_METADATA_SUB_DIR);
if metadata_path.exists() {
println!("reading local metadata from {metadata_path:?}");
let mut metadata_file = std::fs::File::create(dest_dir.join("metadata.tar.gz"))?;
self.zip_files(&metadata_path, &mut metadata_file)?;
}
Ok(())
}

/// get logs from k8 pod
fn copy_kubernetes_logs(&self, kubectl: &Path, dest_dir: &Path) -> Result<()> {
let pods = cmd!(
Expand Down
Loading
Loading