From e0978dc8cbd947554e781689d47e719a4217edad Mon Sep 17 00:00:00 2001 From: Claudio Netto Date: Fri, 10 Sep 2021 19:32:11 -0300 Subject: [PATCH] feat(controller): support to render custom values on ingress annotations --- controllers/controller.go | 30 ++++++++++++++++++++++++++++++ controllers/controller_test.go | 10 ++++++++++ 2 files changed, 40 insertions(+) diff --git a/controllers/controller.go b/controllers/controller.go index bb712910b..6e598276f 100644 --- a/controllers/controller.go +++ b/controllers/controller.go @@ -1309,6 +1309,10 @@ func renderCustomValues(instance *v1alpha1.RpaasInstance) error { return err } + if err := renderIngressCustomAnnotations(instance); err != nil { + return err + } + return nil } @@ -1338,6 +1342,32 @@ func renderServiceCustomAnnotations(instance *v1alpha1.RpaasInstance) error { return nil } +func renderIngressCustomAnnotations(instance *v1alpha1.RpaasInstance) error { + if instance == nil { + return nil + } + + if instance.Spec.Ingress == nil { + return nil + } + + for k, v := range instance.Spec.Ingress.Annotations { + tmpl, err := template.New("rpaasv2.ingress.annotations").Parse(v) + if err != nil { + return err + } + + var buffer bytes.Buffer + if err = tmpl.Execute(&buffer, instance); err != nil { + return err + } + + instance.Spec.Ingress.Annotations[k] = buffer.String() + } + + return nil +} + func mergeInstance(base v1alpha1.RpaasInstanceSpec, override v1alpha1.RpaasInstanceSpec) (merged v1alpha1.RpaasInstanceSpec, err error) { err = genericMerge(&merged, base, override) return diff --git a/controllers/controller_test.go b/controllers/controller_test.go index 3721b4ca4..ca0fdab53 100644 --- a/controllers/controller_test.go +++ b/controllers/controller_test.go @@ -237,6 +237,11 @@ func TestReconcileRpaasInstance_getRpaasInstance(t *testing.T) { "some-instance-annotation-key": "my custom value: {{ .Labels.rpaas_service }}/{{ .Labels.rpaas_instance }}/{{ .Name }}", }, } + instance4.Spec.Ingress = &nginxv1alpha1.NginxIngress{ + Annotations: map[string]string{ + "some-instance-annotation-key": "my custom value: {{ .Labels.rpaas_service }}/{{ .Name }}", + }, + } instance5 := newEmptyRpaasInstance() instance5.Name = "instance5" @@ -608,6 +613,11 @@ func TestReconcileRpaasInstance_getRpaasInstance(t *testing.T) { Zone: "test-zone", TTL: func() *int32 { ttl := int32(30); return &ttl }(), }, + Ingress: &nginxv1alpha1.NginxIngress{ + Annotations: map[string]string{ + "some-instance-annotation-key": "my custom value: my-service-name/instance4", + }, + }, }, }, },