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

FLA-542 Support Traefik v2 rules #73

Merged
merged 2 commits into from
Nov 6, 2024
Merged
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
15 changes: 7 additions & 8 deletions examples/full-example.yaml
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
apiVersion: fintlabs.no/v1alpha1
kind: Application
metadata:
name: flais-test-application
namespace: rogfk-no
name: test-application
namespace: test-namespace
labels:
app.kubernetes.io/name: flais-test-application
app.kubernetes.io/instance: flais-test-application_rogfk_no
app.kubernetes.io/instance: flais-test-application_test-com
app.kubernetes.io/version: latest
app.kubernetes.io/component: test-application
app.kubernetes.io/part-of: flais
fintlabs.no/team: flais
spec:
org-id: rogfk.no
org-id: test.com

replicas: 1
port: 80
image: docker/getting-started
resources:
limits:
memory: "1024Mi"
cpu: "1"
memory: "256Mi"
cpu: "500m"
requests:
memory: "128Mi"
cpu: "100m"
Expand Down Expand Up @@ -67,9 +67,8 @@ spec:
x-forwarded-path: re:^test(.*)$
queries:
showAll: false
isRegex: re:.*
- host: flais-test-application.example.com
path: re:/test2(.*)$
path: /team/{name:[a-z]+}
middlewares:
- some-middleware
middlewares:
Expand Down
24 changes: 24 additions & 0 deletions examples/simple-deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
apiVersion: fintlabs.no/v1alpha1
kind: Application
metadata:
name: test-application
namespace: test-namespace
labels:
app.kubernetes.io/name: flais-test-application
app.kubernetes.io/instance: flais-test-application_test-com
app.kubernetes.io/version: latest
app.kubernetes.io/component: test-application
app.kubernetes.io/part-of: flais
fintlabs.no/team: flais
spec:
org-id: test.com
replicas: 1
port: 80
image: docker/getting-started
resources:
limits:
memory: "256Mi"
cpu: "200m"
requests:
memory: "128Mi"
cpu: "100m"
10 changes: 6 additions & 4 deletions src/main/kotlin/no/fintlabs/operator/IngressDR.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,17 @@ class IngressDR : CRUDKubernetesDependentResource<IngressRoute, FlaisApplication
append("Host(`${route.host}`)")
route.path?.takeIf { it.isNotEmpty() }?.let {
append(" && ")
append(if (it.isRegex()) "PathRegexp(`${it.stripRegexPrefix().toRegex()}`)" else "PathPrefix(`$it`)")
append("PathPrefix(`$it`)")
}
route.queries?.filter { it.key.isNotEmpty() && it.value.isNotEmpty() }?.forEach { (key, value) ->
route.queries?.filter { it.key.isNotEmpty() && it.value.isNotEmpty() }?.map { (key, value) ->
"`$key=$value`"
}?.let {
append(" && ")
append(if (value.isRegex()) "QueryRegexp(`$key`, `${value.stripRegexPrefix().toRegex()}`)" else "Query(`$key`, `$value`)")
append("Query(${it.joinToString(", ")})")
}
route.headers?.filter { it.key.isNotEmpty() && it.value.isNotEmpty() }?.forEach { (key, value) ->
append(" && ")
append(if (value.isRegex()) "HeaderRegexp(`$key`, `${value.stripRegexPrefix().toRegex()}`)" else "Header(`$key`, `$value`)")
append(if (value.isRegex()) "HeadersRegexp(`$key`, `${value.stripRegexPrefix().toRegex()}`)" else "Headers(`$key`, `$value`)")
}
}

Expand Down
33 changes: 10 additions & 23 deletions src/test/integration/kotlin/no/fintlabs/operator/IngressDRTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class IngressDRTest{
assertNotNull(ingressRoute)
assertEquals("test", ingressRoute.metadata.name)
assertEquals("web", ingressRoute.spec.entryPoints[0])
assertEquals("Host(`test.example.com`) && PathPrefix(`/test`) && Query(`key`, `value`) && Header(`header`, `value`)", ingressRoute.spec.routes[0].match)
assertEquals("Host(`test.example.com`) && PathPrefix(`/test`) && Query(`key=value`) && Headers(`header`, `value`)", ingressRoute.spec.routes[0].match)
assertEquals(8080, ingressRoute.spec.routes[0].services[0].port.intVal)
assertEquals("test", ingressRoute.spec.routes[0].services[0].name)
assertEquals(context.namespace, ingressRoute.spec.routes[0].services[0].namespace)
Expand Down Expand Up @@ -124,7 +124,7 @@ class IngressDRTest{

val ingressRoute = context.createAndGetIngressRoute(flaisApplication)
assertNotNull(ingressRoute)
assertEquals("Host(`test.example.com`) && PathPrefix(`/test`) && Query(`key`, `value`) && Query(`key2`, `value2`)", ingressRoute.spec.routes[0].match)
assertEquals("Host(`test.example.com`) && PathPrefix(`/test`) && Query(`key=value`, `key2=value2`)", ingressRoute.spec.routes[0].match)
}

@Test
Expand All @@ -137,33 +137,20 @@ class IngressDRTest{

val ingressRoute = context.createAndGetIngressRoute(flaisApplication)
assertNotNull(ingressRoute)
assertEquals("Host(`test.example.com`) && PathPrefix(`/test`) && Header(`header`, `value`) && Header(`header2`, `value2`)", ingressRoute.spec.routes[0].match)
assertEquals("Host(`test.example.com`) && PathPrefix(`/test`) && Headers(`header`, `value`) && Headers(`header2`, `value2`)", ingressRoute.spec.routes[0].match)
}

@Test
fun `should create IngressRoute with regex path`(context: KubernetesOperatorContext) {
val flaisApplication = createTestFlaisApplication().apply {
spec = spec.copy(ingress = Ingress(routes = listOf(
Ingress.Route("test.example.com", "re:/test.*")
Ingress.Route("test.example.com", "/{path:test.*}")
)))
}

val ingressRoute = context.createAndGetIngressRoute(flaisApplication)
assertNotNull(ingressRoute)
assertEquals("Host(`test.example.com`) && PathRegexp(`/test.*`)", ingressRoute.spec.routes[0].match)
}

@Test
fun `should create IngressRoute with regex query`(context: KubernetesOperatorContext) {
val flaisApplication = createTestFlaisApplication().apply {
spec = spec.copy(ingress = Ingress(routes = listOf(
Ingress.Route("test.example.com", "/test", queries = mapOf("key" to "re:value.*"))
)))
}

val ingressRoute = context.createAndGetIngressRoute(flaisApplication)
assertNotNull(ingressRoute)
assertEquals("Host(`test.example.com`) && PathPrefix(`/test`) && QueryRegexp(`key`, `value.*`)", ingressRoute.spec.routes[0].match)
assertEquals("Host(`test.example.com`) && PathPrefix(`/{path:test.*}`)", ingressRoute.spec.routes[0].match)
}

@Test
Expand All @@ -176,33 +163,33 @@ class IngressDRTest{

val ingressRoute = context.createAndGetIngressRoute(flaisApplication)
assertNotNull(ingressRoute)
assertEquals("Host(`test.example.com`) && PathPrefix(`/test`) && HeaderRegexp(`header`, `value.*`)", ingressRoute.spec.routes[0].match)
assertEquals("Host(`test.example.com`) && PathPrefix(`/test`) && HeadersRegexp(`header`, `value.*`)", ingressRoute.spec.routes[0].match)
}

@Test
fun `should create IngressRoute with multiple regexes`(context: KubernetesOperatorContext) {
val flaisApplication = createTestFlaisApplication().apply {
spec = spec.copy(ingress = Ingress(routes = listOf(
Ingress.Route("test.example.com", "re:/test.*", queries = mapOf("key" to "re:value.*"), headers = mapOf("header" to "re:value.*"))
Ingress.Route("test.example.com", "/{name:test.*}", queries = mapOf("key" to "value"), headers = mapOf("header" to "re:value.*"))
)))
}

val ingressRoute = context.createAndGetIngressRoute(flaisApplication)
assertNotNull(ingressRoute)
assertEquals("Host(`test.example.com`) && PathRegexp(`/test.*`) && QueryRegexp(`key`, `value.*`) && HeaderRegexp(`header`, `value.*`)", ingressRoute.spec.routes[0].match)
assertEquals("Host(`test.example.com`) && PathPrefix(`/{name:test.*}`) && Query(`key=value`) && HeadersRegexp(`header`, `value.*`)", ingressRoute.spec.routes[0].match)
}

@Test
fun `should create IngressRoute with multiple regexes and non-regexes`(context: KubernetesOperatorContext) {
val flaisApplication = createTestFlaisApplication().apply {
spec = spec.copy(ingress = Ingress(routes = listOf(
Ingress.Route("test.example.com", "/test", queries = mapOf("key" to "re:value.*", "key2" to "value2"), headers = mapOf("header" to "re:value.*", "header2" to "value2"))
Ingress.Route("test.example.com", "/test", queries = mapOf("key" to "value", "key2" to "value2"), headers = mapOf("header" to "re:value.*", "header2" to "value2"))
)))
}

val ingressRoute = context.createAndGetIngressRoute(flaisApplication)
assertNotNull(ingressRoute)
assertEquals("Host(`test.example.com`) && PathPrefix(`/test`) && QueryRegexp(`key`, `value.*`) && Query(`key2`, `value2`) && HeaderRegexp(`header`, `value.*`) && Header(`header2`, `value2`)", ingressRoute.spec.routes[0].match)
assertEquals("Host(`test.example.com`) && PathPrefix(`/test`) && Query(`key=value`, `key2=value2`) && HeadersRegexp(`header`, `value.*`) && Headers(`header2`, `value2`)", ingressRoute.spec.routes[0].match)
}
//endregion

Expand Down