diff --git a/generator/writers/ingress.go b/generator/writers/ingress.go index b11a552..9c00f89 100644 --- a/generator/writers/ingress.go +++ b/generator/writers/ingress.go @@ -10,8 +10,18 @@ import ( "gopkg.in/yaml.v3" ) -const classAndVersionCondition = `{{- if and .Values.__name__.ingress.class (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion) }}` + "\n" -const versionCondition = `{{- if semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion }}` + "\n" +const ( + classAndVersionCondition = `{{- if and .Values.__name__.ingress.class (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion) }}` + "\n" + versionCondition118 = `{{- if semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion }}` + "\n" + versionCondition119 = `{{- if semverCompare ">=1.19-0" .Capabilities.KubeVersion.GitVersion }}` + "\n" + apiVersion = `{{- if semverCompare ">=1.19-0" .Capabilities.KubeVersion.GitVersion -}} +apiVersion: networking.k8s.io/v1 +{{- else if semverCompare ">=1.14-0" .Capabilities.KubeVersion.GitVersion -}} +apiVersion: networking.k8s.io/v1beta1 +{{- else -}} +apiVersion: extensions/v1beta1 +{{- end }}` +) func BuildIngress(ingress *helm.Ingress, name, templatesDir string) { // Set the backend for 1.18 @@ -38,6 +48,19 @@ func BuildIngress(ingress *helm.Ingress, name, templatesDir string) { backendHit := false for _, l := range lines { + // apiVersion is a pain... + if strings.Contains(l, "apiVersion:") { + l = apiVersion + } + + // pathTyype is ony for 1.19+ + if strings.Contains(l, "pathType:") { + n := CountSpaces(l) + l = strings.Repeat(" ", n) + versionCondition118 + + l + "\n" + + strings.Repeat(" ", n) + "{{- end -}}" + } + if strings.Contains(l, "ingressClassName") { // should be set only if the version of Kubernetes is 1.18-0 or higher cond := strings.ReplaceAll(classAndVersionCondition, "__name__", name) @@ -47,14 +70,14 @@ func BuildIngress(ingress *helm.Ingress, name, templatesDir string) { // manage the backend format following the Kubernetes 1.18-0 version or higher if strings.Contains(l, "service:") { n := CountSpaces(l) - l = strings.Repeat(" ", n) + versionCondition + l + l = strings.Repeat(" ", n) + versionCondition119 + l } if strings.Contains(l, "serviceName:") || strings.Contains(l, "servicePort:") { n := CountSpaces(l) if !backendHit { l = strings.Repeat(" ", n) + "{{- else }}\n" + l } else { - l = l + "\n" + strings.Repeat(" ", n) + "{{- end }}\n" + l = l + "\n" + strings.Repeat(" ", n) + "{{- end }}" } backendHit = true }