feat: add grpc ports as well
Some checks failed
continuous-integration/drone/push Build is failing

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
Kasper Juul Hermansen 2024-05-29 21:21:34 +02:00
parent c08dcb049d
commit 36aea1c05c
Signed by: kjuulh
GPG Key ID: 9AA7BC13CE474394
6 changed files with 249 additions and 102 deletions

41
Cargo.lock generated
View File

@ -4,9 +4,9 @@ version = 3
[[package]] [[package]]
name = "addr2line" name = "addr2line"
version = "0.21.0" version = "0.22.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a30b2e23b9e17a9f90641c7ab1549cd9b44f296d3ccbf309d2863cfe398a0cb" checksum = "6e4503c46a5c0c7844e948c9a4d6acd9f50cccb4de1c48eb9e291ea17470c678"
dependencies = [ dependencies = [
"gimli", "gimli",
] ]
@ -431,9 +431,9 @@ dependencies = [
[[package]] [[package]]
name = "aws-smithy-checksums" name = "aws-smithy-checksums"
version = "0.60.8" version = "0.60.9"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "509e33efbd853e1e670c47e49af2f4df3d2ae0de8b845b068ddbf04636a6700d" checksum = "6242d6a54d3b4b83458f4abd7057ba93c4419dc71e8217e9acd3a748d656d99e"
dependencies = [ dependencies = [
"aws-smithy-http", "aws-smithy-http",
"aws-smithy-types", "aws-smithy-types",
@ -696,9 +696,9 @@ dependencies = [
[[package]] [[package]]
name = "backtrace" name = "backtrace"
version = "0.3.71" version = "0.3.72"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26b05800d2e817c8b3b4b54abd461726265fa9789ae34330622f2db9ee696f9d" checksum = "17c6a35df3749d2e8bb1b7b21a976d82b15548788d2735b9d82f329268f71a11"
dependencies = [ dependencies = [
"addr2line", "addr2line",
"cc", "cc",
@ -899,9 +899,9 @@ dependencies = [
[[package]] [[package]]
name = "clang-sys" name = "clang-sys"
version = "1.8.0" version = "1.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a483f3cbf7cec2e153d424d0e92329d816becc6421389bd494375c6065921b9b" checksum = "0b023947811758c97c59bf9d1c188fd619ad4718dcaa767947df1cadb14f39f4"
dependencies = [ dependencies = [
"glob", "glob",
"libc", "libc",
@ -1032,9 +1032,9 @@ checksum = "19d374276b40fb8bbdee95aef7c7fa6b5316ec764510eb64b8dd0e2ed0d7e7f5"
[[package]] [[package]]
name = "crc32c" name = "crc32c"
version = "0.6.6" version = "0.6.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "716b38bb6e49e9071060ab2c5e26195b70274f83fdf6cbc44542d63bb2f45c7d" checksum = "89254598aa9b9fa608de44b3ae54c810f0f06d755e24c50177f1f8f31ff50ce2"
dependencies = [ dependencies = [
"rustc_version", "rustc_version",
] ]
@ -1324,9 +1324,9 @@ checksum = "0206175f82b8d6bf6652ff7d71a1e27fd2e4efde587fd368662814d6ec1d9ce0"
[[package]] [[package]]
name = "event-listener" name = "event-listener"
version = "5.3.0" version = "5.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d9944b8ca13534cdfb2800775f8dd4902ff3fc75a50101466decadfdf322a24" checksum = "6032be9bd27023a771701cc49f9f053c751055f71efb2e0ae5c15809093675ba"
dependencies = [ dependencies = [
"concurrent-queue", "concurrent-queue",
"parking", "parking",
@ -1339,7 +1339,7 @@ version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0f214dc438f977e6d4e3500aaa277f5ad94ca83fbbd9b1a15713ce2344ccc5a1" checksum = "0f214dc438f977e6d4e3500aaa277f5ad94ca83fbbd9b1a15713ce2344ccc5a1"
dependencies = [ dependencies = [
"event-listener 5.3.0", "event-listener 5.3.1",
"pin-project-lite", "pin-project-lite",
] ]
@ -1406,7 +1406,7 @@ dependencies = [
[[package]] [[package]]
name = "flux-releaser" name = "flux-releaser"
version = "0.1.0" version = "0.1.0"
source = "git+https://git.front.kjuulh.io/kjuulh/flux-releaser?branch=main#3ca0a836ca9382dfc53ecc159a7ddc10e08e6755" source = "git+https://git.front.kjuulh.io/kjuulh/flux-releaser?branch=main#44ae9c2d2b1fbf418d2894e9a608671ecac6f187"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"async-trait", "async-trait",
@ -1589,9 +1589,9 @@ dependencies = [
[[package]] [[package]]
name = "gimli" name = "gimli"
version = "0.28.1" version = "0.29.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253" checksum = "40ecd4077b5ae9fd2e9e169b102c6c330d0605168eb0e8bf79952b256dbefffd"
[[package]] [[package]]
name = "git2" name = "git2"
@ -1864,9 +1864,9 @@ dependencies = [
[[package]] [[package]]
name = "hyper-util" name = "hyper-util"
version = "0.1.4" version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d8d52be92d09acc2e01dddb7fde3ad983fc6489c7db4837e605bc3fca4cb63e" checksum = "7b875924a60b96e5d7b9ae7b066540b1dd1cbd90d1828f54c92e02a283351c56"
dependencies = [ dependencies = [
"bytes", "bytes",
"futures-util", "futures-util",
@ -2146,6 +2146,7 @@ version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7165d0e94806d52ad5295e4b54a95176d831814840bc067298ca647e1c956338" checksum = "7165d0e94806d52ad5295e4b54a95176d831814840bc067298ca647e1c956338"
dependencies = [ dependencies = [
"aho-corasick",
"serde", "serde",
] ]
@ -2334,9 +2335,9 @@ dependencies = [
[[package]] [[package]]
name = "object" name = "object"
version = "0.32.2" version = "0.35.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a6a622008b6e321afc04970976f62ee297fdbaa6f95318ca343e3eebb9648441" checksum = "b8ec7ab813848ba4522158d5517a6093db1ded27575b070f4177b8d12b41db5e"
dependencies = [ dependencies = [
"memchr", "memchr",
] ]

View File

@ -17,7 +17,7 @@ uuid = { version = "1.7.0", features = ["v4"] }
serde_yaml = "0.9.34" serde_yaml = "0.9.34"
tokio-stream = { version = "0.1.15", features = ["full"] } tokio-stream = { version = "0.1.15", features = ["full"] }
walkdir = "2.5.0" walkdir = "2.5.0"
minijinja = "2.0.1" minijinja = { version = "2.0.1", features = ["custom_syntax"] }
futures = "0.3.30" futures = "0.3.30"
flux-releaser.workspace = true flux-releaser.workspace = true

View File

@ -1,5 +1,7 @@
use std::path::Path; use std::path::Path;
use minijinja::{context, syntax::SyntaxConfig};
use crate::Component; use crate::Component;
use super::cuddle_vars::{load_cuddle_file, CuddleVariable, CuddleVariables}; use super::cuddle_vars::{load_cuddle_file, CuddleVariable, CuddleVariables};
@ -7,6 +9,8 @@ use super::cuddle_vars::{load_cuddle_file, CuddleVariable, CuddleVariables};
pub enum IngressType { pub enum IngressType {
External, External,
Internal, Internal,
ExternalGrpc,
InternalGrpc,
} }
pub struct Ingress { pub struct Ingress {
@ -19,6 +23,94 @@ impl Ingress {
Ok(Self { variables }) Ok(Self { variables })
} }
fn render_ingress_types(
&self,
ingress_types: Vec<IngressType>,
) -> anyhow::Result<(String, String)> {
let mut templates = Vec::new();
let internal_template = r#"
{%- set service_name = vars.cuddle_vars.service %}
{%- set host_name = vars.cuddle_vars.service | replace("_", "-") | replace(".", "-") %}
<%- macro host() -%>
<% if connection_type is defined %><<connection_type>>.<% endif %>{{ host_name }}.{{ environment }}.<< base_host >>
<%- endmacro %>
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
cert-manager.io/issuer: << issuer >>
traefik.ingress.kubernetes.io/router.entrypoints: web
traefik.ingress.kubernetes.io/router.tls: "true"
labels:
app: {{ service_name }}
cluster: {{ vars.cluster_vars.name }}
name: {{ service_name }}-<< name >>
namespace: {{ vars.cluster_vars.namespace }}
spec:
rules:
- host: << host() >>
http:
paths:
- backend:
service:
name: {{ service_name }}
port:
name: << name >>
path: /
pathType: Prefix
tls:
- hosts:
- << host() >>
secretName: tls-{{ service_name }}-<< issuer >>-<< name >>-ingress-dns
"#;
let get_template = |name, base_host, connection_type| {
let mut env = minijinja::Environment::new();
env.set_syntax(
SyntaxConfig::builder()
.block_delimiters("<%", "%>")
.variable_delimiters("<<", ">>")
.comment_delimiters("<#", "#>")
.build()
.expect("to be able to build minijinja syntax"),
);
env.add_global("name", name);
env.add_global("base_host", base_host);
if let Some(connection_type) = connection_type {
env.add_global("connection_type", connection_type);
}
env.add_global("issuer", "kjuulh-app");
env.render_named_str("ingress.yaml", internal_template, context! {})
};
for ingress_type in ingress_types {
match ingress_type {
IngressType::External => {
templates.push(get_template("external-http", "kjuulh.app", None)?)
}
IngressType::Internal => {
templates.push(get_template("internal-http", "internal.kjuulh.app", None)?)
}
IngressType::ExternalGrpc => {
templates.push(get_template("external-grpc", "kjuulh.app", Some("grpc"))?)
}
IngressType::InternalGrpc => templates.push(get_template(
"internal-grpc",
"internal.kjuulh.app",
Some("grpc"),
)?),
}
}
Ok(("ingress.yaml".into(), templates.join("\n")))
}
} }
impl Component for Ingress { impl Component for Ingress {
@ -49,6 +141,10 @@ impl Component for Ingress {
types.push(IngressType::External) types.push(IngressType::External)
} else if o.0.contains_key("internal") { } else if o.0.contains_key("internal") {
types.push(IngressType::Internal) types.push(IngressType::Internal)
} else if o.0.contains_key("external_grpc") {
types.push(IngressType::ExternalGrpc)
} else if o.0.contains_key("internal_grpc") {
types.push(IngressType::InternalGrpc)
} else { } else {
continue; continue;
} }
@ -60,83 +156,7 @@ impl Component for Ingress {
types types
}) })
{ {
let mut templates = Vec::new(); return Some(self.render_ingress_types(ingress_types));
for ingress_type in ingress_types {
match ingress_type {
IngressType::External => templates.push(
r#"
{%- set service_name = vars.cuddle_vars.service %}
{%- set host_name = vars.cuddle_vars.service | replace("_", "-") | replace(".", "-") %}
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
cert-manager.io/issuer: kjuulh-app
traefik.ingress.kubernetes.io/router.entrypoints: web
traefik.ingress.kubernetes.io/router.tls: "true"
labels:
app: {{ service_name }}
cluster: {{ vars.cluster_vars.namespace }}
name: {{ service_name }}-external-http
namespace: {{ vars.cluster_vars.namespace }}
spec:
rules:
- host: {{ host_name }}.{{ environment }}.kjuulh.app
http:
paths:
- backend:
service:
name: {{ service_name }}
port:
name: external-http
path: /
pathType: Prefix
tls:
- hosts:
- {{ host_name }}.{{ environment }}.kjuulh.app
secretName: tls-{{ service_name }}-kjuulh-app-external-ingress-dns
"#,
),
IngressType::Internal => templates.push(
r#"
{%- set service_name = vars.cuddle_vars.service %}
{%- set host_name = vars.cuddle_vars.service | replace("_", "-") | replace(".", "-") %}
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
cert-manager.io/issuer: kjuulh-app
traefik.ingress.kubernetes.io/router.entrypoints: web
traefik.ingress.kubernetes.io/router.tls: "true"
labels:
app: {{ service_name }}
cluster: {{ vars.cluster_vars.namespace }}
name: {{ service_name }}-internal-http
namespace: {{ vars.cluster_vars.namespace }}
spec:
rules:
- host: {{ host_name }}.{{ environment }}.internal.kjuulh.app
http:
paths:
- backend:
service:
name: {{ service_name }}
port:
name: internal-http
path: /
pathType: Prefix
tls:
- hosts:
- {{ host_name }}.{{ environment }}.internal.kjuulh.app
secretName: tls-{{ service_name }}-kjuulh-app-internal-ingress-dns
"#,
),
}
}
return Some(Ok(("ingress.yaml".into(), templates.join("\n"))));
} }
None None

View File

@ -3,6 +3,8 @@ vars:
ingress: ingress:
- external: "true" - external: "true"
- internal: "true" - internal: "true"
- external_grpc: "true"
- internal_grpc: "true"
cuddle/clusters: cuddle/clusters:
dev: dev:

View File

@ -1,4 +1,5 @@
--- ---
apiVersion: networking.k8s.io/v1 apiVersion: networking.k8s.io/v1
kind: Ingress kind: Ingress
@ -27,7 +28,8 @@ spec:
tls: tls:
- hosts: - hosts:
- service.dev.kjuulh.app - service.dev.kjuulh.app
secretName: tls-service-kjuulh-app-external-ingress-dns secretName: tls-service-kjuulh-app-external-http-ingress-dns
--- ---
apiVersion: networking.k8s.io/v1 apiVersion: networking.k8s.io/v1
kind: Ingress kind: Ingress
@ -56,4 +58,64 @@ spec:
tls: tls:
- hosts: - hosts:
- service.dev.internal.kjuulh.app - service.dev.internal.kjuulh.app
secretName: tls-service-kjuulh-app-internal-ingress-dns secretName: tls-service-kjuulh-app-internal-http-ingress-dns
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
cert-manager.io/issuer: kjuulh-app
traefik.ingress.kubernetes.io/router.entrypoints: web
traefik.ingress.kubernetes.io/router.tls: "true"
labels:
app: service
cluster: dev
name: service-external-grpc
namespace: dev
spec:
rules:
- host: grpc.service.dev.kjuulh.app
http:
paths:
- backend:
service:
name: service
port:
name: external-grpc
path: /
pathType: Prefix
tls:
- hosts:
- grpc.service.dev.kjuulh.app
secretName: tls-service-kjuulh-app-external-grpc-ingress-dns
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
cert-manager.io/issuer: kjuulh-app
traefik.ingress.kubernetes.io/router.entrypoints: web
traefik.ingress.kubernetes.io/router.tls: "true"
labels:
app: service
cluster: dev
name: service-internal-grpc
namespace: dev
spec:
rules:
- host: grpc.service.dev.internal.kjuulh.app
http:
paths:
- backend:
service:
name: service
port:
name: internal-grpc
path: /
pathType: Prefix
tls:
- hosts:
- grpc.service.dev.internal.kjuulh.app
secretName: tls-service-kjuulh-app-internal-grpc-ingress-dns

View File

@ -1,4 +1,5 @@
--- ---
apiVersion: networking.k8s.io/v1 apiVersion: networking.k8s.io/v1
kind: Ingress kind: Ingress
@ -27,7 +28,8 @@ spec:
tls: tls:
- hosts: - hosts:
- service.prod.kjuulh.app - service.prod.kjuulh.app
secretName: tls-service-kjuulh-app-external-ingress-dns secretName: tls-service-kjuulh-app-external-http-ingress-dns
--- ---
apiVersion: networking.k8s.io/v1 apiVersion: networking.k8s.io/v1
kind: Ingress kind: Ingress
@ -56,4 +58,64 @@ spec:
tls: tls:
- hosts: - hosts:
- service.prod.internal.kjuulh.app - service.prod.internal.kjuulh.app
secretName: tls-service-kjuulh-app-internal-ingress-dns secretName: tls-service-kjuulh-app-internal-http-ingress-dns
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
cert-manager.io/issuer: kjuulh-app
traefik.ingress.kubernetes.io/router.entrypoints: web
traefik.ingress.kubernetes.io/router.tls: "true"
labels:
app: service
cluster: prod
name: service-external-grpc
namespace: prod
spec:
rules:
- host: grpc.service.prod.kjuulh.app
http:
paths:
- backend:
service:
name: service
port:
name: external-grpc
path: /
pathType: Prefix
tls:
- hosts:
- grpc.service.prod.kjuulh.app
secretName: tls-service-kjuulh-app-external-grpc-ingress-dns
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
cert-manager.io/issuer: kjuulh-app
traefik.ingress.kubernetes.io/router.entrypoints: web
traefik.ingress.kubernetes.io/router.tls: "true"
labels:
app: service
cluster: prod
name: service-internal-grpc
namespace: prod
spec:
rules:
- host: grpc.service.prod.internal.kjuulh.app
http:
paths:
- backend:
service:
name: service
port:
name: internal-grpc
path: /
pathType: Prefix
tls:
- hosts:
- grpc.service.prod.internal.kjuulh.app
secretName: tls-service-kjuulh-app-internal-grpc-ingress-dns