Skip to content

Commit 4debc66

Browse files
committed
Added minikube support
1 parent 10535c7 commit 4debc66

File tree

4 files changed

+112
-30
lines changed

4 files changed

+112
-30
lines changed

src/argocd.rs

+14-13
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,20 @@ use log::{debug, error, info};
99

1010
use crate::run_command;
1111

12+
static ARGOCD_CMD_PARAMS_CM: &str = r#"
13+
apiVersion: v1
14+
kind: ConfigMap
15+
metadata:
16+
labels:
17+
app.kubernetes.io/name: argocd-cmd-params-cm
18+
app.kubernetes.io/part-of: argocd
19+
name: argocd-cmd-params-cm
20+
namespace: argocd
21+
data:
22+
reposerver.git.request.timeout: "150s"
23+
reposerver.parallelism.limit: "300"
24+
"#;
25+
1226
pub async fn install_argo_cd() -> Result<(), Box<dyn Error>> {
1327
info!("🦑 Installing Argo CD...");
1428

@@ -30,19 +44,6 @@ pub async fn install_argo_cd() -> Result<(), Box<dyn Error>> {
3044
}
3145
info!("🦑 Waiting for Argo CD to start...");
3246

33-
static ARGOCD_CMD_PARAMS_CM: &str = r#"
34-
apiVersion: v1
35-
kind: ConfigMap
36-
metadata:
37-
labels:
38-
app.kubernetes.io/name: argocd-cmd-params-cm
39-
app.kubernetes.io/part-of: argocd
40-
name: argocd-cmd-params-cm
41-
namespace: argocd
42-
data:
43-
reposerver.git.request.timeout: "150s"
44-
reposerver.parallelism.limit: "300"
45-
"#;
4647
// apply argocd-cmd-params-cm
4748
let mut child = Command::new("kubectl")
4849
.arg("apply")

src/kind.rs

+14-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
use log::{error, info};
2-
use std::{error::Error, process::Command};
2+
use std::error::Error;
33

44
use crate::{run_command, utils::spawn_command};
55

6+
pub async fn is_installed() -> bool {
7+
match run_command("kind version", None).await {
8+
Ok(_) => true,
9+
Err(_) => false,
10+
}
11+
}
12+
613
pub async fn create_cluster(cluster_name: &str) -> Result<(), Box<dyn Error>> {
714
// check if docker is running
815
match run_command("docker ps", None).await {
@@ -14,16 +21,15 @@ pub async fn create_cluster(cluster_name: &str) -> Result<(), Box<dyn Error>> {
1421
}
1522

1623
info!("🚀 Creating cluster...");
17-
match Command::new("kind")
18-
.arg("delete")
19-
.arg("cluster")
20-
.arg("--name")
21-
.arg(cluster_name)
22-
.output()
24+
match run_command(
25+
&format!("kind delete cluster --name {}", cluster_name),
26+
None,
27+
)
28+
.await
2329
{
2430
Ok(o) => o,
2531
Err(e) => {
26-
panic!("error: {}", e)
32+
panic!("error: {}", String::from_utf8_lossy(&e.stderr))
2733
}
2834
};
2935

src/main.rs

+38-9
Original file line numberDiff line numberDiff line change
@@ -69,20 +69,23 @@ struct Opt {
6969
/// Secrets folder where the secrets are read from
7070
#[structopt(short, long, default_value = "./secrets", env)]
7171
secrets_folder: String,
72+
73+
/// Local cluster tool. Options: kind, minikube, auto. Default: Auto
74+
#[structopt(long, env)]
75+
local_cluster_tool: Option<String>,
76+
}
77+
78+
#[derive(Debug)]
79+
enum ClusterTool {
80+
Kind,
81+
Minikube,
7282
}
7383

7484
enum Branch {
7585
Base,
7686
Target,
7787
}
7888

79-
fn apps_file(branch: &Branch) -> &'static str {
80-
match branch {
81-
Branch::Base => "apps_base_branch.yaml",
82-
Branch::Target => "apps_target_branch.yaml",
83-
}
84-
}
85-
8689
impl std::fmt::Display for Branch {
8790
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
8891
match self {
@@ -92,6 +95,13 @@ impl std::fmt::Display for Branch {
9295
}
9396
}
9497

98+
fn apps_file(branch: &Branch) -> &'static str {
99+
match branch {
100+
Branch::Base => "apps_base_branch.yaml",
101+
Branch::Target => "apps_target_branch.yaml",
102+
}
103+
}
104+
95105
static ERROR_MESSAGES: [&str; 6] = [
96106
"helm template .",
97107
"authentication required",
@@ -135,7 +145,20 @@ async fn main() -> Result<(), Box<dyn Error>> {
135145
let output_folder = opt.output_folder.as_str();
136146
let secrets_folder = opt.secrets_folder.as_str();
137147

148+
// select local cluster tool
149+
let tool = match opt.local_cluster_tool {
150+
Some(t) if t == "kind" => ClusterTool::Kind,
151+
Some(t) if t == "minikube" => ClusterTool::Minikube,
152+
_ if kind::is_installed().await => ClusterTool::Kind,
153+
_ if minikube::is_installed().await => ClusterTool::Minikube,
154+
_ => {
155+
error!("❌ No local cluster tool found. Please install kind or minikube");
156+
panic!("No local cluster tool found")
157+
}
158+
};
159+
138160
info!("✨ Running with:");
161+
info!("✨ - local-cluster-tool: {:?}", tool);
139162
info!("✨ - base-branch: {}", base_branch_name);
140163
info!("✨ - target-branch: {}", target_branch_name);
141164
info!("✨ - base-branch-folder: {}", base_branch_folder);
@@ -163,7 +186,10 @@ async fn main() -> Result<(), Box<dyn Error>> {
163186

164187
let cluster_name = "argocd-diff-preview";
165188

166-
kind::create_cluster(&cluster_name).await?;
189+
match tool {
190+
ClusterTool::Kind => kind::create_cluster(&cluster_name).await?,
191+
ClusterTool::Minikube => minikube::create_cluster().await?,
192+
}
167193
argocd::install_argo_cd().await?;
168194

169195
create_folder_if_not_exists(secrets_folder);
@@ -190,7 +216,10 @@ async fn main() -> Result<(), Box<dyn Error>> {
190216
tokio::time::sleep(tokio::time::Duration::from_secs(10)).await;
191217
get_resources(&Branch::Target, timeout, output_folder).await?;
192218

193-
kind::delete_cluster(&cluster_name);
219+
match tool {
220+
ClusterTool::Kind => kind::delete_cluster(&cluster_name),
221+
ClusterTool::Minikube => minikube::delete_cluster(),
222+
}
194223

195224
diff::generate_diff(
196225
output_folder,

src/minikube.rs

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
use log::{error, info};
2+
use std::error::Error;
3+
4+
use crate::{run_command, utils::spawn_command};
5+
6+
pub async fn is_installed() -> bool {
7+
match run_command("minikube status", None).await {
8+
Ok(_) => true,
9+
Err(_) => false,
10+
}
11+
}
12+
13+
pub async fn create_cluster() -> Result<(), Box<dyn Error>> {
14+
// check if docker is running
15+
match run_command("docker ps", None).await {
16+
Ok(_) => (),
17+
Err(e) => {
18+
error!("❌ Docker is not running");
19+
panic!("error: {}", String::from_utf8_lossy(&e.stderr))
20+
}
21+
}
22+
23+
info!("🚀 Creating cluster...");
24+
match run_command(&format!("minikube delete"), None).await {
25+
Ok(o) => o,
26+
Err(e) => {
27+
panic!("error: {}", String::from_utf8_lossy(&e.stderr))
28+
}
29+
};
30+
31+
match run_command(&format!("minikube start"), None).await {
32+
Ok(_) => {
33+
info!("🚀 Cluster created successfully");
34+
Ok(())
35+
}
36+
Err(e) => {
37+
error!("❌ Failed to Create cluster");
38+
panic!("error: {}", String::from_utf8_lossy(&e.stderr))
39+
}
40+
}
41+
}
42+
43+
pub fn delete_cluster() {
44+
info!("💥 Deleting cluster...");
45+
spawn_command(&format!("minikube delete"), None);
46+
}

0 commit comments

Comments
 (0)