From 8d97ebd6d3a6e8c9f18306bd1b3b24fb464bfe8d Mon Sep 17 00:00:00 2001 From: kjuulh Date: Fri, 29 Nov 2024 09:04:48 +0100 Subject: [PATCH] feat: add postgres and more templates Signed-off-by: kjuulh --- .../src/catalog/postgres_database.rs | 13 ++++++----- crates/cuddle-clusters/src/process.rs | 6 +++++ .../can_run_for_env/expected/dev/test.yaml | 2 +- .../tests/environment/expected/dev/some.yaml | 2 +- .../tests/environment/expected/prod/some.yaml | 2 +- .../tests/jinja/expected/dev/some.yaml | 2 +- .../nested/expected/dev/service-template | 2 +- .../tests/nested/expected/dev/some-other.yaml | 2 +- .../tests/nested/expected/dev/some.yaml | 2 +- .../with_crdb/expected/dev/cuddle-crdb.yaml | 2 +- .../tests/with_crdb/expected/dev/some.yaml | 2 +- .../with_ingress/expected/dev/ingress.yaml | 2 +- .../tests/with_ingress/expected/dev/some.yaml | 2 +- .../with_ingress/expected/prod/ingress.yaml | 2 +- .../with_ingress/expected/prod/some.yaml | 2 +- .../expected/dev/cuddle-postgres.yaml | 12 ++++++++++ .../expected/dev/cuddle-postgresql.yaml | 9 -------- .../expected/dev/some.yaml | 23 +++++++++++++++---- .../templates/clusters/some.yaml.jinja2 | 2 +- .../with_vault_secrets/expected/dev/some.yaml | 2 +- .../expected/dev/vault_secret.yaml | 2 +- 21 files changed, 60 insertions(+), 35 deletions(-) create mode 100644 crates/cuddle-clusters/tests/with_postgres_database/expected/dev/cuddle-postgres.yaml delete mode 100644 crates/cuddle-clusters/tests/with_postgres_database/expected/dev/cuddle-postgresql.yaml diff --git a/crates/cuddle-clusters/src/catalog/postgres_database.rs b/crates/cuddle-clusters/src/catalog/postgres_database.rs index 8e8dc41..2cfc6c6 100644 --- a/crates/cuddle-clusters/src/catalog/postgres_database.rs +++ b/crates/cuddle-clusters/src/catalog/postgres_database.rs @@ -89,14 +89,14 @@ impl Component for PostgresDatabase { format!("{}.yaml", self.name().replace("/", "-")), r#" {%- if environment == "dev" %} -{% set port = 5433 %} -{% else %} -{% set port = 5432 %} -{%- endif%} +{%- set port = 5433 %} +{%- else %} +{%- set port = 5432 %} +{%- endif %} apiVersion: v1 kind: ConfigMap metadata: - name: {{ vars.cuddle_crdb.file_name(vars.cuddle_vars.service) }} + name: {{ vars.cuddle_postgres.file_name(vars.cuddle_vars.service) }} namespace: {{ vars.cluster_vars.namespace }} data: DATABASE_TYPE: postgresql @@ -104,7 +104,8 @@ data: DATABASE_PORT: {{ port }} DATABASE_USER: {{ vars.cuddle_vars.service | replace("_", "-") }} DATABASE_DB: {{ vars.cuddle_vars.service | replace("_", "-") }} - "# + +"# .into(), ))); } diff --git a/crates/cuddle-clusters/src/process.rs b/crates/cuddle-clusters/src/process.rs index 7348e1a..976d300 100644 --- a/crates/cuddle-clusters/src/process.rs +++ b/crates/cuddle-clusters/src/process.rs @@ -354,6 +354,12 @@ async fn process_render_template( vars => variables })?; + let rendered = if rendered.is_empty() || rendered.ends_with("\n") { + rendered + } else { + format!("{rendered}\n") + }; + dest_file.write_all(rendered.as_bytes()).await?; Ok(()) diff --git a/crates/cuddle-clusters/tests/can_run_for_env/expected/dev/test.yaml b/crates/cuddle-clusters/tests/can_run_for_env/expected/dev/test.yaml index 92dbfb6..5418072 100644 --- a/crates/cuddle-clusters/tests/can_run_for_env/expected/dev/test.yaml +++ b/crates/cuddle-clusters/tests/can_run_for_env/expected/dev/test.yaml @@ -3,4 +3,4 @@ some = { thing = "some" } -} \ No newline at end of file +} diff --git a/crates/cuddle-clusters/tests/environment/expected/dev/some.yaml b/crates/cuddle-clusters/tests/environment/expected/dev/some.yaml index 4a067a0..b8500d2 100644 --- a/crates/cuddle-clusters/tests/environment/expected/dev/some.yaml +++ b/crates/cuddle-clusters/tests/environment/expected/dev/some.yaml @@ -1 +1 @@ -env: dev \ No newline at end of file +env: dev diff --git a/crates/cuddle-clusters/tests/environment/expected/prod/some.yaml b/crates/cuddle-clusters/tests/environment/expected/prod/some.yaml index 46b998b..2fdef9a 100644 --- a/crates/cuddle-clusters/tests/environment/expected/prod/some.yaml +++ b/crates/cuddle-clusters/tests/environment/expected/prod/some.yaml @@ -1 +1 @@ -env: prod \ No newline at end of file +env: prod diff --git a/crates/cuddle-clusters/tests/jinja/expected/dev/some.yaml b/crates/cuddle-clusters/tests/jinja/expected/dev/some.yaml index f8c3979..b9c3a29 100644 --- a/crates/cuddle-clusters/tests/jinja/expected/dev/some.yaml +++ b/crates/cuddle-clusters/tests/jinja/expected/dev/some.yaml @@ -1 +1 @@ -some_file: 4 \ No newline at end of file +some_file: 4 diff --git a/crates/cuddle-clusters/tests/nested/expected/dev/service-template b/crates/cuddle-clusters/tests/nested/expected/dev/service-template index ce93fac..24e1098 100644 --- a/crates/cuddle-clusters/tests/nested/expected/dev/service-template +++ b/crates/cuddle-clusters/tests/nested/expected/dev/service-template @@ -1 +1 @@ -service \ No newline at end of file +service diff --git a/crates/cuddle-clusters/tests/nested/expected/dev/some-other.yaml b/crates/cuddle-clusters/tests/nested/expected/dev/some-other.yaml index ce93fac..24e1098 100644 --- a/crates/cuddle-clusters/tests/nested/expected/dev/some-other.yaml +++ b/crates/cuddle-clusters/tests/nested/expected/dev/some-other.yaml @@ -1 +1 @@ -service \ No newline at end of file +service diff --git a/crates/cuddle-clusters/tests/nested/expected/dev/some.yaml b/crates/cuddle-clusters/tests/nested/expected/dev/some.yaml index ce93fac..24e1098 100644 --- a/crates/cuddle-clusters/tests/nested/expected/dev/some.yaml +++ b/crates/cuddle-clusters/tests/nested/expected/dev/some.yaml @@ -1 +1 @@ -service \ No newline at end of file +service diff --git a/crates/cuddle-clusters/tests/with_crdb/expected/dev/cuddle-crdb.yaml b/crates/cuddle-clusters/tests/with_crdb/expected/dev/cuddle-crdb.yaml index 8a380ff..35a1cac 100644 --- a/crates/cuddle-clusters/tests/with_crdb/expected/dev/cuddle-crdb.yaml +++ b/crates/cuddle-clusters/tests/with_crdb/expected/dev/cuddle-crdb.yaml @@ -6,4 +6,4 @@ metadata: namespace: dev data: DATABASE_URL: postgresql://root@dev-cluster:26257/service - \ No newline at end of file + diff --git a/crates/cuddle-clusters/tests/with_crdb/expected/dev/some.yaml b/crates/cuddle-clusters/tests/with_crdb/expected/dev/some.yaml index 6286958..0db2e5a 100644 --- a/crates/cuddle-clusters/tests/with_crdb/expected/dev/some.yaml +++ b/crates/cuddle-clusters/tests/with_crdb/expected/dev/some.yaml @@ -36,4 +36,4 @@ spec: - containerPort: 3001 name: internal-http - containerPort: 3002 - name: internal-grpc \ No newline at end of file + name: internal-grpc diff --git a/crates/cuddle-clusters/tests/with_ingress/expected/dev/ingress.yaml b/crates/cuddle-clusters/tests/with_ingress/expected/dev/ingress.yaml index b7d7d60..5fe644c 100644 --- a/crates/cuddle-clusters/tests/with_ingress/expected/dev/ingress.yaml +++ b/crates/cuddle-clusters/tests/with_ingress/expected/dev/ingress.yaml @@ -118,4 +118,4 @@ spec: tls: - hosts: - grpc.service.dev.internal.kjuulh.app - secretName: tls-service-kjuulh-app-internal-grpc-ingress-dns \ No newline at end of file + secretName: tls-service-kjuulh-app-internal-grpc-ingress-dns diff --git a/crates/cuddle-clusters/tests/with_ingress/expected/dev/some.yaml b/crates/cuddle-clusters/tests/with_ingress/expected/dev/some.yaml index 43c5173..87146b9 100644 --- a/crates/cuddle-clusters/tests/with_ingress/expected/dev/some.yaml +++ b/crates/cuddle-clusters/tests/with_ingress/expected/dev/some.yaml @@ -27,4 +27,4 @@ spec: - containerPort: 3001 name: internal-http - containerPort: 3002 - name: internal-grpc \ No newline at end of file + name: internal-grpc diff --git a/crates/cuddle-clusters/tests/with_ingress/expected/prod/ingress.yaml b/crates/cuddle-clusters/tests/with_ingress/expected/prod/ingress.yaml index 0847f5f..b738d43 100644 --- a/crates/cuddle-clusters/tests/with_ingress/expected/prod/ingress.yaml +++ b/crates/cuddle-clusters/tests/with_ingress/expected/prod/ingress.yaml @@ -118,4 +118,4 @@ spec: tls: - hosts: - grpc.service.prod.internal.kjuulh.app - secretName: tls-service-kjuulh-app-internal-grpc-ingress-dns \ No newline at end of file + secretName: tls-service-kjuulh-app-internal-grpc-ingress-dns diff --git a/crates/cuddle-clusters/tests/with_ingress/expected/prod/some.yaml b/crates/cuddle-clusters/tests/with_ingress/expected/prod/some.yaml index 43c5173..87146b9 100644 --- a/crates/cuddle-clusters/tests/with_ingress/expected/prod/some.yaml +++ b/crates/cuddle-clusters/tests/with_ingress/expected/prod/some.yaml @@ -27,4 +27,4 @@ spec: - containerPort: 3001 name: internal-http - containerPort: 3002 - name: internal-grpc \ No newline at end of file + name: internal-grpc diff --git a/crates/cuddle-clusters/tests/with_postgres_database/expected/dev/cuddle-postgres.yaml b/crates/cuddle-clusters/tests/with_postgres_database/expected/dev/cuddle-postgres.yaml new file mode 100644 index 0000000..dbef0ec --- /dev/null +++ b/crates/cuddle-clusters/tests/with_postgres_database/expected/dev/cuddle-postgres.yaml @@ -0,0 +1,12 @@ + +apiVersion: v1 +kind: ConfigMap +metadata: + name: service-cuddle-postgres + namespace: dev +data: + DATABASE_TYPE: postgresql + DATABASE_HOST: dev.postgresql.kjuulh.app + DATABASE_PORT: 5433 + DATABASE_USER: service + DATABASE_DB: service diff --git a/crates/cuddle-clusters/tests/with_postgres_database/expected/dev/cuddle-postgresql.yaml b/crates/cuddle-clusters/tests/with_postgres_database/expected/dev/cuddle-postgresql.yaml deleted file mode 100644 index 8a380ff..0000000 --- a/crates/cuddle-clusters/tests/with_postgres_database/expected/dev/cuddle-postgresql.yaml +++ /dev/null @@ -1,9 +0,0 @@ - -apiVersion: v1 -kind: ConfigMap -metadata: - name: service-cuddle-crdb - namespace: dev -data: - DATABASE_URL: postgresql://root@dev-cluster:26257/service - \ No newline at end of file diff --git a/crates/cuddle-clusters/tests/with_postgres_database/expected/dev/some.yaml b/crates/cuddle-clusters/tests/with_postgres_database/expected/dev/some.yaml index 6286958..ba5d876 100644 --- a/crates/cuddle-clusters/tests/with_postgres_database/expected/dev/some.yaml +++ b/crates/cuddle-clusters/tests/with_postgres_database/expected/dev/some.yaml @@ -25,15 +25,30 @@ spec: - configMapRef: name: service-config env: - - name: DATABASE_URL + - name: DATABASE_HOST valueFrom: secretKeyRef: - name: service-cuddle-crdb - key: DATABASE_URL + name: service-cuddle-postgres + key: DATABASE_HOST + - name: DATABASE_PORT + valueFrom: + secretKeyRef: + name: service-cuddle-postgres + key: DATABASE_PORT + - name: DATABASE_USER + valueFrom: + secretKeyRef: + name: service-cuddle-postgres + key: DATABASE_USER + - name: DATABASE_DB + valueFrom: + secretKeyRef: + name: service-cuddle-postgres + key: DATABASE_DB ports: - containerPort: 3000 name: external-http - containerPort: 3001 name: internal-http - containerPort: 3002 - name: internal-grpc \ No newline at end of file + name: internal-grpc diff --git a/crates/cuddle-clusters/tests/with_postgres_database/templates/clusters/some.yaml.jinja2 b/crates/cuddle-clusters/tests/with_postgres_database/templates/clusters/some.yaml.jinja2 index 4ce643a..c9dff3c 100644 --- a/crates/cuddle-clusters/tests/with_postgres_database/templates/clusters/some.yaml.jinja2 +++ b/crates/cuddle-clusters/tests/with_postgres_database/templates/clusters/some.yaml.jinja2 @@ -26,7 +26,7 @@ spec: envFrom: - configMapRef: name: {{service_name}}-config - {%- if vars.vault_secret.has_values or vars.cuddle_crdb.has_values %} + {%- if vars.vault_secret.has_values or vars.cuddle_postgres.has_values %} env: {%- if vars.vault_secret.has_values %} {%- for secret in vars.vault_secret.secrets %} diff --git a/crates/cuddle-clusters/tests/with_vault_secrets/expected/dev/some.yaml b/crates/cuddle-clusters/tests/with_vault_secrets/expected/dev/some.yaml index e36f68e..3aabbe6 100644 --- a/crates/cuddle-clusters/tests/with_vault_secrets/expected/dev/some.yaml +++ b/crates/cuddle-clusters/tests/with_vault_secrets/expected/dev/some.yaml @@ -36,4 +36,4 @@ spec: - containerPort: 3001 name: internal-http - containerPort: 3002 - name: internal-grpc \ No newline at end of file + name: internal-grpc diff --git a/crates/cuddle-clusters/tests/with_vault_secrets/expected/dev/vault_secret.yaml b/crates/cuddle-clusters/tests/with_vault_secrets/expected/dev/vault_secret.yaml index 2e28f58..d8c93eb 100644 --- a/crates/cuddle-clusters/tests/with_vault_secrets/expected/dev/vault_secret.yaml +++ b/crates/cuddle-clusters/tests/with_vault_secrets/expected/dev/vault_secret.yaml @@ -10,4 +10,4 @@ spec: mount: kvv2 path: service/dev refreshAfter: 30s - type: kv-v2 \ No newline at end of file + type: kv-v2