From 05d33f1fab81c9c80cce33380400bf55829587cd Mon Sep 17 00:00:00 2001 From: kjuulh Date: Sun, 17 Nov 2024 16:02:39 +0100 Subject: [PATCH] feat: add replicas Signed-off-by: kjuulh --- .../src/catalog/cluster_vars.rs | 13 ++++++- crates/cuddle-clusters/tests/tests.rs | 13 +------ .../tests/with_actual_deployment/cuddle.yaml | 15 -------- .../expected/dev/configmap.yaml | 8 ---- .../expected/dev/deployment.yaml | 37 ------------------- .../tests/with_cluster_vars/cuddle.yaml | 12 ++++++ .../with_cluster_vars/expected/dev/some.yaml | 5 +++ .../with_cluster_vars/expected/prod/some.yaml | 4 ++ .../templates/clusters/some.yaml.jinja2 | 8 ++++ 9 files changed, 43 insertions(+), 72 deletions(-) delete mode 100644 crates/cuddle-clusters/tests/with_actual_deployment/cuddle.yaml delete mode 100644 crates/cuddle-clusters/tests/with_actual_deployment/expected/dev/configmap.yaml delete mode 100644 crates/cuddle-clusters/tests/with_actual_deployment/expected/dev/deployment.yaml create mode 100644 crates/cuddle-clusters/tests/with_cluster_vars/cuddle.yaml create mode 100644 crates/cuddle-clusters/tests/with_cluster_vars/expected/dev/some.yaml create mode 100644 crates/cuddle-clusters/tests/with_cluster_vars/expected/prod/some.yaml create mode 100644 crates/cuddle-clusters/tests/with_cluster_vars/templates/clusters/some.yaml.jinja2 diff --git a/crates/cuddle-clusters/src/catalog/cluster_vars.rs b/crates/cuddle-clusters/src/catalog/cluster_vars.rs index b19d1ab..fadbeeb 100644 --- a/crates/cuddle-clusters/src/catalog/cluster_vars.rs +++ b/crates/cuddle-clusters/src/catalog/cluster_vars.rs @@ -17,6 +17,7 @@ pub struct ClusterVariables { env: ClusterEnv, name: String, namespace: String, + replicas: u64, } #[derive(Default)] @@ -36,7 +37,10 @@ impl Component for ClusterVars { environment: &str, value: &serde_yaml::Value, ) -> Option> { - let mut vars = ClusterVariables::default(); + let mut vars = ClusterVariables { + replicas: 3, + ..Default::default() + }; // TODO: actually extract values @@ -52,6 +56,12 @@ impl Component for ClusterVars { } } } + + if let Some(replicas) = mapping.get("replicas") { + if let Some(replicas) = replicas.as_u64() { + vars.replicas = replicas; + } + } } vars.name = environment.into(); @@ -67,6 +77,7 @@ impl minijinja::value::Object for ClusterVariables { "env" => minijinja::Value::from_object(self.env.clone()), "name" => minijinja::Value::from_safe_string(self.name.clone()), "namespace" => minijinja::Value::from_safe_string(self.namespace.clone()), + "replicas" => minijinja::Value::from_safe_string(self.replicas.to_string()), _ => return None, }; diff --git a/crates/cuddle-clusters/tests/tests.rs b/crates/cuddle-clusters/tests/tests.rs index 8fa96bf..5f4208f 100644 --- a/crates/cuddle-clusters/tests/tests.rs +++ b/crates/cuddle-clusters/tests/tests.rs @@ -68,17 +68,8 @@ async fn with_cuddle_vars() -> anyhow::Result<()> { } #[tokio::test] -async fn with_actual_deployment() -> anyhow::Result<()> { - let current_dir = std::env::current_dir()?.join("tests/with_cuddle_vars"); - - run_test_with_components( - "with_actual_deployment", - vec![ - CuddleVars::new(¤t_dir).await?.into_component(), - ClusterVars::default().into_component(), - ], - ) - .await?; +async fn with_cluster_vars() -> anyhow::Result<()> { + run_test_with_components("with_cluster_vars", vec![ClusterVars::default()]).await?; Ok(()) } diff --git a/crates/cuddle-clusters/tests/with_actual_deployment/cuddle.yaml b/crates/cuddle-clusters/tests/with_actual_deployment/cuddle.yaml deleted file mode 100644 index cc48aac..0000000 --- a/crates/cuddle-clusters/tests/with_actual_deployment/cuddle.yaml +++ /dev/null @@ -1,15 +0,0 @@ -vars: - service: service - some: - nested: - item: something - array: - - item: item - - -cuddle/clusters: - dev: - env: - something: thing - nested.item: item - diff --git a/crates/cuddle-clusters/tests/with_actual_deployment/expected/dev/configmap.yaml b/crates/cuddle-clusters/tests/with_actual_deployment/expected/dev/configmap.yaml deleted file mode 100644 index 60f6ab4..0000000 --- a/crates/cuddle-clusters/tests/with_actual_deployment/expected/dev/configmap.yaml +++ /dev/null @@ -1,8 +0,0 @@ -apiVersion: v1 -kind: ConfigMap -metadata: - name: service-config -data: - environment: - NESTED_ITEM: item - SOMETHING: thing \ No newline at end of file diff --git a/crates/cuddle-clusters/tests/with_actual_deployment/expected/dev/deployment.yaml b/crates/cuddle-clusters/tests/with_actual_deployment/expected/dev/deployment.yaml deleted file mode 100644 index 3a2643e..0000000 --- a/crates/cuddle-clusters/tests/with_actual_deployment/expected/dev/deployment.yaml +++ /dev/null @@ -1,37 +0,0 @@ -apiVersion: apps/v1 -kind: Deployment -metadata: - labels: - app: service - cluster: dev - name: service - namespace: dev -spec: - replicas: 3 - selector: - matchLabels: - app: service - cluster: dev - template: - metadata: - labels: - app: service - cluster: dev - spec: - containers: - - args: - - serve - command: - - service - image: kasperhermansen/service:main-1715336504 - name: service - envFrom: - - configMapRef: - name: service-config - ports: - - containerPort: 3000 - name: external-http - - containerPort: 3001 - name: internal-http - - containerPort: 3002 - name: internal-grpc \ No newline at end of file diff --git a/crates/cuddle-clusters/tests/with_cluster_vars/cuddle.yaml b/crates/cuddle-clusters/tests/with_cluster_vars/cuddle.yaml new file mode 100644 index 0000000..015809a --- /dev/null +++ b/crates/cuddle-clusters/tests/with_cluster_vars/cuddle.yaml @@ -0,0 +1,12 @@ +vars: + +cuddle/clusters: + dev: + replicas: 1 + env: + something.something: something + something: + # ignored + vault: true + prod: + env: diff --git a/crates/cuddle-clusters/tests/with_cluster_vars/expected/dev/some.yaml b/crates/cuddle-clusters/tests/with_cluster_vars/expected/dev/some.yaml new file mode 100644 index 0000000..3b221a4 --- /dev/null +++ b/crates/cuddle-clusters/tests/with_cluster_vars/expected/dev/some.yaml @@ -0,0 +1,5 @@ +name: dev +namespace: dev +replicas: 1 +items: + - something.something diff --git a/crates/cuddle-clusters/tests/with_cluster_vars/expected/prod/some.yaml b/crates/cuddle-clusters/tests/with_cluster_vars/expected/prod/some.yaml new file mode 100644 index 0000000..caf56ca --- /dev/null +++ b/crates/cuddle-clusters/tests/with_cluster_vars/expected/prod/some.yaml @@ -0,0 +1,4 @@ +name: prod +namespace: prod +replicas: 3 +items: diff --git a/crates/cuddle-clusters/tests/with_cluster_vars/templates/clusters/some.yaml.jinja2 b/crates/cuddle-clusters/tests/with_cluster_vars/templates/clusters/some.yaml.jinja2 new file mode 100644 index 0000000..f3c364f --- /dev/null +++ b/crates/cuddle-clusters/tests/with_cluster_vars/templates/clusters/some.yaml.jinja2 @@ -0,0 +1,8 @@ +name: {{ vars.cluster_vars.name }} +namespace: {{ vars.cluster_vars.namespace }} +replicas: {{ vars.cluster_vars.replicas }} +items: + {%- for val in vars.cluster_vars.env %} + - {{val}} + {%- endfor %} +