#![allow(clippy::format_push_string)]
use crate::deployment::contract::DeploymentContract;
#[derive(Debug, Clone)]
pub struct ArgocdConfig {
pub argocd_namespace: String,
pub dest_namespace: String,
pub dest_server: String,
pub repo_url: String,
pub target_revision: String,
pub chart_path: String,
pub project: String,
pub sync_wave: i32,
pub extra_ignore_differences: Vec<String>,
}
impl Default for ArgocdConfig {
fn default() -> Self {
Self {
argocd_namespace: "argocd".into(),
dest_namespace: "dfe".into(),
dest_server: "https://kubernetes.default.svc".into(),
repo_url: String::new(),
target_revision: "main".into(),
chart_path: "chart".into(),
project: "default".into(),
sync_wave: crate::deployment::WAVE_APPS,
extra_ignore_differences: Vec::new(),
}
}
}
#[must_use]
pub fn generate_argocd_application(
contract: &DeploymentContract,
argo: &ArgocdConfig,
identity: Option<&crate::deployment::ContractIdentity>,
) -> String {
let identity_block = identity
.map(|id| format!("\n{ann}", ann = id.as_yaml_annotations(4)))
.unwrap_or_default();
let extras_block = if argo.extra_ignore_differences.is_empty() {
String::new()
} else {
let mut buf = String::new();
for entry in &argo.extra_ignore_differences {
for line in entry.lines() {
buf.push_str(" ");
buf.push_str(line);
buf.push('\n');
}
}
buf
};
format!(
r#"# AUTOGENERATED -- do not edit by hand.
# Generated by hyperi-rustlib::deployment::generate_argocd_application()
# Schema version: {schema_version}
# Source contract: {app_name}::deployment::contract()
# Regenerate with: `{binary} emit-argocd > application.yaml`
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: {app_name}
namespace: {argocd_namespace}
annotations:
argocd.argoproj.io/sync-wave: "{sync_wave}"{identity_block}
finalizers:
- resources-finalizer.argocd.argoproj.io
spec:
project: {project}
source:
repoURL: {repo_url}
targetRevision: {target_revision}
path: {chart_path}
helm:
releaseName: {app_name}
destination:
server: {dest_server}
namespace: {dest_namespace}
syncPolicy:
automated:
prune: true
selfHeal: true
allowEmpty: false
syncOptions:
- CreateNamespace=true
- PrunePropagationPolicy=foreground
- PruneLast=true
- ServerSideApply=true
retry:
limit: 5
backoff:
duration: 5s
factor: 2
maxDuration: 3m
ignoreDifferences:
- group: apps
kind: Deployment
jsonPointers:
- /spec/replicas
- group: ""
kind: Service
jsonPointers:
- /spec/clusterIP
- /spec/clusterIPs
- group: admissionregistration.k8s.io
kind: ValidatingWebhookConfiguration
jqPathExpressions:
- .webhooks[].clientConfig.caBundle
{extras_block}"#,
schema_version = contract.schema_version,
app_name = contract.app_name,
binary = contract.binary(),
argocd_namespace = argo.argocd_namespace,
sync_wave = argo.sync_wave,
project = argo.project,
repo_url = argo.repo_url,
target_revision = argo.target_revision,
chart_path = argo.chart_path,
dest_server = argo.dest_server,
dest_namespace = argo.dest_namespace,
extras_block = extras_block,
identity_block = identity_block,
)
}