Now manage kubernetes version in ingress

The backend and ingressClassName are now under condition
This commit is contained in:
2022-02-16 10:37:46 +01:00
parent 722c7424d0
commit a4834a0661
4 changed files with 55 additions and 13 deletions

View File

@@ -197,7 +197,7 @@ func createIngress(name string, port int, s *compose.Service) *helm.Ingress {
Paths: []helm.IngressPath{{ Paths: []helm.IngressPath{{
Path: "/", Path: "/",
PathType: "Prefix", PathType: "Prefix",
Backend: helm.IngressBackend{ Backend: &helm.IngressBackend{
Service: helm.IngressService{ Service: helm.IngressService{
Name: RELEASE_NAME + "-" + name, Name: RELEASE_NAME + "-" + name,
Port: map[string]interface{}{ Port: map[string]interface{}{

View File

@@ -10,7 +10,19 @@ import (
"gopkg.in/yaml.v3" "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"
func BuildIngress(ingress *helm.Ingress, name, templatesDir string) { func BuildIngress(ingress *helm.Ingress, name, templatesDir string) {
// Set the backend for 1.18
for _, b := range ingress.Spec.Rules {
for _, p := range b.Http.Paths {
p.Backend.ServiceName = p.Backend.Service.Name
if n, ok := p.Backend.Service.Port["number"]; ok {
p.Backend.ServicePort = n
}
}
}
kind := "ingress" kind := "ingress"
buffer := bytes.NewBuffer(nil) buffer := bytes.NewBuffer(nil)
fname := filepath.Join(templatesDir, name+"."+kind+".yaml") fname := filepath.Join(templatesDir, name+"."+kind+".yaml")
@@ -23,18 +35,32 @@ func BuildIngress(ingress *helm.Ingress, name, templatesDir string) {
fp, _ := os.Create(fname) fp, _ := os.Create(fname)
content := string(buffer.Bytes()) content := string(buffer.Bytes())
lines := strings.Split(content, "\n") lines := strings.Split(content, "\n")
backendHit := false
for _, l := range lines { for _, l := range lines {
if strings.Contains(l, "ingressClassName") { if strings.Contains(l, "ingressClassName") {
p := strings.Split(l, ":") // should be set only if the version of Kubernetes is 1.18-0 or higher
condition := p[1] cond := strings.ReplaceAll(classAndVersionCondition, "__name__", name)
condition = strings.ReplaceAll(condition, "'", "") l = ` ` + cond + l + "\n" + ` {{- end }}`
condition = strings.ReplaceAll(condition, "{{", "")
condition = strings.ReplaceAll(condition, "}}", "")
condition = strings.TrimSpace(condition)
condition = "{{- if " + condition + " }}"
l = " " + condition + "\n" + l + "\n {{- end }}"
} }
// 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
}
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"
}
backendHit = true
}
fp.WriteString(l + "\n") fp.WriteString(l + "\n")
} }
fp.Close() fp.Close()
} }

View File

@@ -0,0 +1,14 @@
package writers
// CountSpaces returns the number of spaces from the begining of the line
func CountSpaces(line string) int {
var spaces int
for _, char := range line {
if char == ' ' {
spaces++
} else {
break
}
}
return spaces
}

View File

@@ -38,14 +38,16 @@ type IngressHttp struct {
type IngressPath struct { type IngressPath struct {
Path string Path string
PathType string `yaml:"pathType"` PathType string `yaml:"pathType"`
Backend IngressBackend Backend *IngressBackend
} }
type IngressBackend struct { type IngressBackend struct {
Service IngressService Service IngressService
ServiceName string `yaml:"serviceName"` // for kubernetes version < 1.18
ServicePort interface{} `yaml:"servicePort"` // for kubernetes version < 1.18
} }
type IngressService struct { type IngressService struct {
Name string Name string `yaml:"name"`
Port map[string]interface{} Port map[string]interface{} `yaml:"port"`
} }