From 128daca40108f7d0651414b7d3212f645ebf3a4a Mon Sep 17 00:00:00 2001 From: Chris Stephenson Date: Wed, 8 Jan 2025 20:15:42 +0100 Subject: [PATCH 1/3] add template implementation of ingress matching humanitec/ingress --- .../template-driver/ingress/README.md | 1 + .../ingress/ingress-default.yaml | 89 +++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 resource-definitions/template-driver/ingress/ingress-default.yaml diff --git a/resource-definitions/template-driver/ingress/README.md b/resource-definitions/template-driver/ingress/README.md index 297f0b3..9ed7fa7 100644 --- a/resource-definitions/template-driver/ingress/README.md +++ b/resource-definitions/template-driver/ingress/README.md @@ -1,6 +1,7 @@ This section contains example Resource Definitions for handling Kubernetes ingress traffic. Instead of the Driver type [Ingress](https://developer.humanitec.com/integration-and-extensions/drivers/k8-drivers/ingress/), we are using the [Template](https://developer.humanitec.com/integration-and-extensions/drivers/generic-drivers/template/) Driver type, which allows us to render any Kubernetes YAML object. +* [ingress-default.yaml](ingress-default.yaml): defines an `Ingress` object in the same way as the [humanitec/ingress](https://developer.humanitec.com/integration-and-extensions/drivers/k8-drivers/ingress/) driver. * [ingress-traefik.yaml](ingress-traefik.yaml): defines an `IngressRoute` object for the [Traefik Ingress Controller](https://doc.traefik.io/traefik/) using the [IngressRoute custom resource definition](https://doc.traefik.io/traefik/providers/kubernetes-crd/). This format is for use with the [Humanitec CLI](https://developer.humanitec.com/platform-orchestrator/cli/) * [ingress-traefik-multiple-routes.yaml](./ingress-traefik-multiple-routes.yaml): defines an `IngressRoute` object for the [Traefik Ingress Controller](https://doc.traefik.io/traefik/) using the [IngressRoute custom resource definition](https://doc.traefik.io/traefik/providers/kubernetes-crd/). It dynamically extracts the routes from the `route` resource in the Resource Graph to provide multiple routes. This format is for use with the [Humanitec CLI](https://developer.humanitec.com/platform-orchestrator/cli/) * [ingress-ambassador.yaml](ingress-ambassador.yaml): defines a `Mapping` object for the [Ambassador Ingress Controller](https://www.getambassador.io/docs/emissary/latest/topics/running/ingress-controller) using the [Mapping custom resource definition](https://www.getambassador.io/docs/edge-stack/latest/topics/using/intro-mappings). This format is for use with the [Humanitec CLI](https://developer.humanitec.com/platform-orchestrator/cli/) \ No newline at end of file diff --git a/resource-definitions/template-driver/ingress/ingress-default.yaml b/resource-definitions/template-driver/ingress/ingress-default.yaml new file mode 100644 index 0000000..2f86d30 --- /dev/null +++ b/resource-definitions/template-driver/ingress/ingress-default.yaml @@ -0,0 +1,89 @@ +# This Resource Definition provisions the equivalent of the humanitec/ingress driver +apiVersion: entity.humanitec.io/v1b1 +kind: Definition +metadata: + id: default-ingress +entity: + name: default-ingress + type: ingress + driver_type: humanitec/template + driver_inputs: + values: + templates: + manifests: | + {{- /* + Only generate an ingress manifest if there are any routes defined. + */ -}} + {{- if gt (len .driver.values.routePaths ) 0 -}} + ingress.yaml: + location: namespace + data: + apiVersion: networking.k8s.io/v1 + kind: Ingress + metadata: + {{- if hasKey .driver.values "annotations" }} + annotations: {{ .driver.values.annotations | toRawJson }} + {{- end}} + {{- if hasKey .driver.values "labels" }} + labels: {{ .driver.values.labels | toRawJson }} + {{- end}} + name: {{ .id }}-ingress + spec: + {{- if .driver.values.class }} + ingressClassName: {{ .driver.values.class | toRawJson }} + {{- end }} + rules: + - host: {{ .driver.values.host | toRawJson }} + http: + paths: + {{- /* + We are guaranteed that .driver.values.routePaths is non-zero in + length from the top level if statement, so we don't need + to deal with the empty condition. + */ -}} + {{- range $index, $path := .driver.values.routePaths }} + - path: {{ $path | toRawJson }} + pathType: {{ $.driver.values.path_type | default "Prefix" | toRawJson }} + backend: + service: + name: {{ index $.driver.values.routeServices $index | toRawJson }} + port: + number: {{ index $.driver.values.routePorts $index }} + {{- end }} + {{- if not (or .driver.values.no_tls (eq .driver.values.tls_secret_name "")) }} + tls: + - hosts: + - {{ .driver.values.host | toRawJson }} + secretName: {{ .driver.values.tls_secret_name | toRawJson }} + {{- end }} + {{- end -}} + outputs: | + no_tls: {{ .driver.values.no_tls | default false }} + id: {{ .id }}-ingress + + {{/* + The host will be used from the dns resource with the same + ResID and Class as this ingress. + */}} + host: ${resources.dns.outputs.host} + + {{/* + These 3 selectors are guaranteed to return JSON arrays. + They will all be empty if there are no routes referencing this. + */}} + routePaths: ${resources.dns Date: Thu, 9 Jan 2025 10:01:46 +0100 Subject: [PATCH 2/3] resolve commments --- .../ingress/ingress-default.yaml | 25 ++++++++----------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/resource-definitions/template-driver/ingress/ingress-default.yaml b/resource-definitions/template-driver/ingress/ingress-default.yaml index 2f86d30..8baab78 100644 --- a/resource-definitions/template-driver/ingress/ingress-default.yaml +++ b/resource-definitions/template-driver/ingress/ingress-default.yaml @@ -50,7 +50,7 @@ entity: port: number: {{ index $.driver.values.routePorts $index }} {{- end }} - {{- if not (or .driver.values.no_tls (eq .driver.values.tls_secret_name "")) }} + {{- if not (or .driver.values.no_tls (eq (.driver.values.tls_secret_name | default "") "")) }} tls: - hosts: - {{ .driver.values.host | toRawJson }} @@ -61,25 +61,22 @@ entity: no_tls: {{ .driver.values.no_tls | default false }} id: {{ .id }}-ingress - {{/* - The host will be used from the dns resource with the same - ResID and Class as this ingress. - */}} + + # The host will be used from the dns resource with the same + # ResID and Class as this ingress. host: ${resources.dns.outputs.host} - {{/* - These 3 selectors are guaranteed to return JSON arrays. - They will all be empty if there are no routes referencing this. - */}} + # These 3 selectors are guaranteed to return JSON arrays. + # They will all be empty if there are no routes referencing this. routePaths: ${resources.dns Date: Thu, 9 Jan 2025 14:02:11 +0100 Subject: [PATCH 3/3] fix CI to not test ingress-default --- .github/workflows/ci.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 9381846..fb446fd 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -68,7 +68,7 @@ jobs: for resourceDefinition in ${resourceDefinitions} do echo ${resourceDefinition} - if [ "${resourceDefinition}" != "resource-definitions/template-driver/ingress/ingress-traefik-multiple-routes.yaml" ]; then + if [ "${resourceDefinition}" != "resource-definitions/template-driver/ingress/ingress-traefik-multiple-routes.yaml" ] && [ "${resourceDefinition}" != "resource-definitions/template-driver/ingress/ingress-default.yaml" ]; then inputs=$(echo ${resourceDefinition} | sed "s,/,-,g") humctl resources test-definition ${resourceDefinition} --generate > ${inputs} sed -i 's/context.res.id: ""/context.res.id: "modules.test.externals.test"/g' ${inputs}