From 9358076a361c96bd6c7d0d5310e409b5e5eb23bb Mon Sep 17 00:00:00 2001 From: Patrice Ferlet Date: Fri, 8 Nov 2024 15:51:36 +0100 Subject: [PATCH 1/2] chore(ingress): Allow tls activation --- generator/ingress.go | 29 +++++++++++++++++++++++++++++ generator/labelStructs/ingress.go | 6 ++++++ generator/values.go | 3 +++ 3 files changed, 38 insertions(+) diff --git a/generator/ingress.go b/generator/ingress.go index f2f1ef0..c407872 100644 --- a/generator/ingress.go +++ b/generator/ingress.go @@ -55,6 +55,7 @@ func NewIngress(service types.ServiceConfig, Chart *HelmChart) *Ingress { Host: mapping.Hostname, Class: mapping.Class, Annotations: mapping.Annotations, + TLS: mapping.TLS, } // ingressClassName := `{{ .Values.` + service.Name + `.ingress.class }}` @@ -131,6 +132,34 @@ func (ingress *Ingress) Yaml() ([]byte, error) { ret = UnWrapTPL(ret) lines := strings.Split(string(ret), "\n") + + // first pass, wrap the tls part with `{{- if .Values.serviceName.ingress.tlsEnabled -}}` + // and `{{- end -}}` + + from := -1 + to := -1 + spaces := -1 + for i, line := range lines { + if strings.Contains(line, "tls:") { + from = i + spaces = utils.CountStartingSpaces(line) + continue + } + if from > -1 { + if utils.CountStartingSpaces(line) >= spaces { + to = i + continue + } + } + } + if from > -1 && to > -1 { + lines[from] = strings.Repeat(" ", spaces) + + `{{- if .Values.` + serviceName + `.ingress.tls.enabled }}` + + "\n" + + lines[from] + lines[to] = strings.Repeat(" ", spaces) + `{{ end -}}` + } + out := []string{ `{{- if .Values.` + serviceName + `.ingress.enabled -}}`, } diff --git a/generator/labelStructs/ingress.go b/generator/labelStructs/ingress.go index 22c5b01..86faea4 100644 --- a/generator/labelStructs/ingress.go +++ b/generator/labelStructs/ingress.go @@ -2,6 +2,10 @@ package labelStructs import "gopkg.in/yaml.v3" +type TLS struct { + Enabled bool `yaml:"enabled"` +} + type Ingress struct { Port *int32 `yaml:"port,omitempty"` Annotations map[string]string `yaml:"annotations,omitempty"` @@ -9,6 +13,7 @@ type Ingress struct { Path string `yaml:"path"` Class string `yaml:"class"` Enabled bool `yaml:"enabled"` + TLS TLS `yaml:"tls"` } // IngressFrom creates a new Ingress from a compose service. @@ -19,6 +24,7 @@ func IngressFrom(data string) (*Ingress, error) { Enabled: false, Class: "-", Port: nil, + TLS: TLS{Enabled: true}, } if err := yaml.Unmarshal([]byte(data), &mapping); err != nil { return nil, err diff --git a/generator/values.go b/generator/values.go index cec4fef..9d57eba 100644 --- a/generator/values.go +++ b/generator/values.go @@ -27,6 +27,9 @@ type IngressValue struct { Path string `yaml:"path"` Class string `yaml:"class"` Enabled bool `yaml:"enabled"` + TLS struct { + Enabled bool `yaml:"enabled"` + } `yaml:"tls"` } // Value will be saved in values.yaml. It contains configuraiton for all deployment and services. From 9b392a1f645d1b21874b040904789ae3ec416979 Mon Sep 17 00:00:00 2001 From: Patrice Ferlet Date: Fri, 8 Nov 2024 16:55:18 +0100 Subject: [PATCH 2/2] Fix problem on getting tls mapping The types were not compatible to get TLS activation --- generator/ingress.go | 2 +- generator/values.go | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/generator/ingress.go b/generator/ingress.go index c407872..7d012d4 100644 --- a/generator/ingress.go +++ b/generator/ingress.go @@ -55,7 +55,7 @@ func NewIngress(service types.ServiceConfig, Chart *HelmChart) *Ingress { Host: mapping.Hostname, Class: mapping.Class, Annotations: mapping.Annotations, - TLS: mapping.TLS, + TLS: TLS{Enabled: mapping.TLS.Enabled}, } // ingressClassName := `{{ .Values.` + service.Name + `.ingress.class }}` diff --git a/generator/values.go b/generator/values.go index 9d57eba..2b6f83a 100644 --- a/generator/values.go +++ b/generator/values.go @@ -20,6 +20,10 @@ type PersistenceValue struct { Enabled bool `yaml:"enabled"` } +type TLS struct { + Enabled bool `yaml:"enabled"` +} + // IngressValue is a ingress configuration that will be saved in values.yaml. type IngressValue struct { Annotations map[string]string `yaml:"annotations"` @@ -27,9 +31,7 @@ type IngressValue struct { Path string `yaml:"path"` Class string `yaml:"class"` Enabled bool `yaml:"enabled"` - TLS struct { - Enabled bool `yaml:"enabled"` - } `yaml:"tls"` + TLS TLS `yaml:"tls"` } // Value will be saved in values.yaml. It contains configuraiton for all deployment and services.