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

Read applications from all namespaces #78

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Overrides done by argocd-diff-preview
# These will happen after the values.yaml file is loaded
# These will happen AFTER the values.yaml file is loaded
notifications:
enabled: false
dex:
Expand Down
9 changes: 9 additions & 0 deletions argocd-config/values-pre.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Overrides done by argocd-diff-preview
# These will happen BEFORE the values.yaml file is loaded
notifications:
enabled: false
dex:
enabled: false
configs:
params:
application.namespaces: "*"
11 changes: 6 additions & 5 deletions src/argo_resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub struct ArgoResource {
pub yaml: serde_yaml::Value,
pub kind: ApplicationKind,
pub name: String,
pub namespace: String,
// Where the resource was found
pub file_name: String,
}
Expand All @@ -36,11 +37,6 @@ impl PartialEq for ArgoResource {
}

impl ArgoResource {
pub fn set_namespace(mut self, namespace: &str) -> ArgoResource {
self.yaml["metadata"]["namespace"] = serde_yaml::Value::String(namespace.to_string());
self
}

pub fn set_project_to_default(mut self) -> Result<ArgoResource, Box<dyn Error>> {
let spec = match self.kind {
ApplicationKind::Application => self.yaml["spec"].as_mapping_mut(),
Expand Down Expand Up @@ -209,11 +205,16 @@ impl ArgoResource {
_ => None,
})?;

let namespace = k8s_resource.yaml["metadata"]["namespace"]
.as_str()
.unwrap_or("default");

match k8s_resource.yaml["metadata"]["name"].as_str() {
Some(name) => Some(ArgoResource {
kind,
file_name: k8s_resource.file_name,
name: name.to_string(),
namespace: namespace.to_string(),
yaml: k8s_resource.yaml,
}),
_ => None,
Expand Down
39 changes: 24 additions & 15 deletions src/argocd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,32 +32,40 @@ impl ArgoCDInstallation {
self.version.clone().unwrap_or("latest".to_string())
);

let (values, values_override) = match std::fs::read_dir(&self.config_path) {
let (values_pre, values, values_post) = match std::fs::read_dir(&self.config_path) {
Ok(dir) => {
debug!("📂 Files in folder '{}':", self.config_path);
debug!("📂 Files in folder 'argocd-config':");
for file in dir {
debug!("- 📄 {:?}", file.unwrap().file_name());
}
let values_exist = std::fs::metadata(format!("{}/values.yaml", self.config_path))
let values_pre = std::fs::metadata(format!("{}/values-pre.yaml", self.config_path))
.is_ok()
.then_some(format!("-f {}/values-pre.yaml", self.config_path));
let values = std::fs::metadata(format!("{}/values.yaml", self.config_path))
.is_ok()
.then_some(format!("-f {}/values.yaml", self.config_path));
let values_override_exist =
std::fs::metadata(format!("{}/values-override.yaml", self.config_path))
let values_post =
std::fs::metadata(format!("{}/values-post.yaml", self.config_path))
.is_ok()
.then_some(format!("-f {}/values-override.yaml", self.config_path));
(values_exist, values_override_exist)
.then_some(format!("-f {}/values-post.yaml", self.config_path));
(values, values_pre, values_post)
}
Err(_e) => {
info!("📂 Folder '{}' doesn't exist. Installing Argo CD Helm Chart with default configuration", self.config_path);
(None, None)
info!(
"📂 Folder '{}' doesn't exist. Installing Argo CD Helm Chart with default configuration",
self.config_path
);
(None, None, None)
}
};

// add argo repo to helm
run_command("helm repo add argo https://argoproj.github.io/argo-helm").map_err(|e| {
error!("❌ Failed to add argo repo");
CommandError::new(e)
})?;
run_command("helm repo add argo https://argoproj.github.io/argo-helm").map_err(
|e| {
error!("❌ Failed to add argo repo");
CommandError::new(e)
},
)?;

// helm update
run_command("helm repo update").map_err(|e| {
Expand All @@ -66,10 +74,11 @@ impl ArgoCDInstallation {
})?;

let helm_install_command = format!(
"helm install argocd argo/argo-cd -n {} {} {} {}",
"helm install argocd argo/argo-cd -n {} {} {} {} {}",
self.namespace,
values.unwrap_or_default(),
values_override.unwrap_or_default(),
values_pre.unwrap_or_default(),
values_post.unwrap_or_default(),
self.version
.clone()
.map(|a| format!("--version {}", a))
Expand Down
14 changes: 12 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,8 @@ async fn run() -> Result<(), Box<dyn Error>> {

// remove .git from repo
let repo = repo.trim_end_matches(".git");

let (base_apps, target_apps) = parsing::get_applications_for_branches(
&argocd_namespace,
&base_branch,
&target_branch,
&file_regex,
Expand All @@ -310,14 +310,24 @@ async fn run() -> Result<(), Box<dyn Error>> {
return Ok(());
}

let unique_namespaces = base_apps
.iter()
.map(|a| a.namespace.clone())
.chain(target_apps.iter().map(|a| a.namespace.clone()))
.collect::<std::collections::HashSet<String>>();
match cluster_tool {
ClusterTool::Kind => kind::create_cluster(&cluster_name)?,
ClusterTool::Minikube => minikube::create_cluster()?,
}

// create Argo CD namespace
create_namespace(&argocd_namespace)?;

create_folder_if_not_exists(secrets_folder)?;
// Create a namespace for each application
for namespace in unique_namespaces {
create_namespace(&namespace)?;
}

match apply_folder(secrets_folder) {
Ok(count) if count > 0 => info!("🤫 Applied {} secrets", count),
Ok(_) => info!("🤷 No secrets found in {}", secrets_folder),
Expand Down
13 changes: 3 additions & 10 deletions src/parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ impl Clone for K8sResource {
}

pub fn get_applications_for_branches(
argo_cd_namespace: &str,
base_branch: &Branch,
target_branch: &Branch,
regex: &Option<Regex>,
Expand All @@ -34,7 +33,6 @@ pub fn get_applications_for_branches(
ignore_invalid_watch_pattern: bool,
) -> Result<(Vec<ArgoResource>, Vec<ArgoResource>), Box<dyn Error>> {
let base_apps = get_applications(
argo_cd_namespace,
base_branch,
regex,
selector,
Expand All @@ -43,7 +41,6 @@ pub fn get_applications_for_branches(
ignore_invalid_watch_pattern,
)?;
let target_apps = get_applications(
argo_cd_namespace,
target_branch,
regex,
selector,
Expand Down Expand Up @@ -115,7 +112,6 @@ pub fn get_applications_for_branches(
}

fn get_applications(
argo_cd_namespace: &str,
branch: &Branch,
regex: &Option<Regex>,
selector: &Option<Vec<Selector>>,
Expand All @@ -133,7 +129,7 @@ fn get_applications(
);
if !applications.is_empty() {
info!("🤖 Patching Application[Sets] for branch: {}", branch.name);
let apps = patch_applications(argo_cd_namespace, applications, branch, repo)?;
let apps = patch_applications(applications, branch, repo)?;
info!(
"🤖 Patching {} Argo CD Application[Sets] for branch: {}",
apps.len(),
Expand Down Expand Up @@ -227,14 +223,12 @@ fn parse_yaml(directory: &str, files: Vec<String>) -> Vec<K8sResource> {
}

fn patch_application(
argo_cd_namespace: &str,
application: ArgoResource,
branch: &Branch,
repo: &str,
) -> Result<ArgoResource, Box<dyn Error>> {
let app_name = application.name.clone();
let app = application
.set_namespace(argo_cd_namespace)
.remove_sync_policy()
.set_project_to_default()
.and_then(|a| a.point_destination_to_in_cluster())
Expand All @@ -249,14 +243,13 @@ fn patch_application(
}

fn patch_applications(
argo_cd_namespace: &str,
applications: Vec<ArgoResource>,
branch: &Branch,
repo: &str,
) -> Result<Vec<ArgoResource>, Box<dyn Error>> {
applications
.into_iter()
.map(|a| patch_application(argo_cd_namespace, a, branch, repo))
.map(|a| patch_application(a, branch, repo))
.collect()
}

Expand Down Expand Up @@ -379,7 +372,7 @@ pub fn generate_apps_from_app_set(
})
})
.collect::<Vec<ArgoResource>>();
patch_applications(&argocd.namespace, apps, branch, repo)
patch_applications( apps, branch, repo)
}
};

Expand Down
1 change: 1 addition & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ pub fn run_command_in_dir(
command: &str,
current_dir: &str,
) -> Result<CommandOutput, CommandOutput> {
debug!("Running command: {}", command);
let args = command.split_whitespace().collect::<Vec<&str>>();
run_command_from_list(args, Some(current_dir), None)
}
Expand Down
Loading