From 682c13303ad0247391619b4486e569d964f15938 Mon Sep 17 00:00:00 2001 From: Ugo Mignon Date: Wed, 5 Jul 2023 08:43:54 +0000 Subject: [PATCH] feat: add support of custom resources --- assets/auto-deploy-app/README.md | 1 + .../templates/custom-resources.yaml | 3 + .../test/templates/custom-resources.go | 67 +++++++++++++++++++ assets/auto-deploy-app/values.yaml | 24 +++++++ 4 files changed, 95 insertions(+) create mode 100644 assets/auto-deploy-app/templates/custom-resources.yaml create mode 100644 assets/auto-deploy-app/test/templates/custom-resources.go diff --git a/assets/auto-deploy-app/README.md b/assets/auto-deploy-app/README.md index 20a38e5..22358d7 100644 --- a/assets/auto-deploy-app/README.md +++ b/assets/auto-deploy-app/README.md @@ -136,4 +136,5 @@ | cronjob.job.livenessProbe | If defined, enables livenessProbe in the cronjob. If not defined, it uses top-level `livenessProbe` setting to the job. (To see details about the default probes check values.yaml) | | | cronjob.job.readinessProbe | If defined, enables readinessProbe in the cronjob. If not defined, it uses top-level `readinessProbe` setting to the job. (To see details about the default probes check values.yaml) | | | cronjob.activeDeadlineSeconds | Alternative to terminate a Job: Once a Job reaches `activeDeadlineSeconds` value, all of its running Pods are terminated and the Job status will become `type: Failed` with `reason: DeadlineExceeded` | `nil` | +| customResources | This field allows to add custom resources to your Deployment. | `[]` | diff --git a/assets/auto-deploy-app/templates/custom-resources.yaml b/assets/auto-deploy-app/templates/custom-resources.yaml new file mode 100644 index 0000000..352b63c --- /dev/null +++ b/assets/auto-deploy-app/templates/custom-resources.yaml @@ -0,0 +1,3 @@ +{{- range .Values.customResources }} +{{- toYaml . | nindent 0 }} +{{- end }} \ No newline at end of file diff --git a/assets/auto-deploy-app/test/templates/custom-resources.go b/assets/auto-deploy-app/test/templates/custom-resources.go new file mode 100644 index 0000000..d364ce6 --- /dev/null +++ b/assets/auto-deploy-app/test/templates/custom-resources.go @@ -0,0 +1,67 @@ +package main + +import ( + "strings" + "testing" + + "github.com/gruntwork-io/terratest/modules/helm" + "github.com/gruntwork-io/terratest/modules/k8s" + "github.com/gruntwork-io/terratest/modules/random" + "github.com/stretchr/testify/require" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" +) + +func TestCustomResource(t *testing.T) { + releaseName := "custom-resource-test" + Template := "templates/custom-resource.yaml" // Your template file path + + tcs := []struct { + CaseName string + Values map[string]string + }{ + { + CaseName: "test-single-custom-resource", + Values: map[string]string{ + "customResources[0].apiVersion": "traefik.containo.us/v1alpha1", + "customResources[0].kind": "IngressRoute", + "customResources[0].metadata.name": "ingress-route", + }, + }, + { + CaseName: "test-multiple-custom-resources", + Values: map[string]string{ + "customResources[0].apiVersion": "traefik.containo.us/v1alpha1", + "customResources[0].kind": "IngressRoute", + "customResources[0].metadata.name": "ingress-route", + "customResources[1].apiVersion": "v1", + "customResources[1].kind": "Pod", + "customResources[1].metadata.name": "my-pod", + }, + }, + } + + for _, tc := range tcs { + t.Run(tc.CaseName, func(t *testing.T) { + + namespaceName := "test-namespace-" + strings.ToLower(random.UniqueId()) + + options := &helm.Options{ + SetValues: tc.Values, + KubectlOptions: k8s.NewKubectlOptions("", "", namespaceName), + } + + output, err := helm.RenderTemplateE(t, options, helmChartPath, releaseName, []string{Template}) + + if err != nil { + t.Error(err) + return + } + + var renderedObjects []*unstructured.Unstructured + helm.UnmarshalK8SYaml(t, output, &renderedObjects) + + // Check if at least one custom resource is present + require.GreaterOrEqual(t, len(renderedObjects), 1) + }) + } +} diff --git a/assets/auto-deploy-app/values.yaml b/assets/auto-deploy-app/values.yaml index 73fa77b..72f3f15 100644 --- a/assets/auto-deploy-app/values.yaml +++ b/assets/auto-deploy-app/values.yaml @@ -299,3 +299,27 @@ cronjobs: {} # extraVolumes: [] # extraVolumeMounts: [] # extraEnvFrom: [] + +# customResources: +# - apiVersion: "traefik.containo.us/v1alpha1" +# kind: "IngressRoute" +# metadata: +# name: "ingress-route" +# namespace: "default" +# spec: +# entryPoints: +# - "web" +# routes: +# - match: "Host(`{{ .Values.envName }}.{{ .Values.envURL }}`)" +# kind: "Rule" +# services: +# - name: "web" +# port: {{ .Values.service.internalPort }} +# weight: 100 +# tls: +# secretName: "tls-secret" +# options: +# name: "tls-options" +# namespace: "default" +# kind: "TLSOptions" +# minVersion: "VersionTLS12" -- GitLab