Use compose-go + improvements (#9)
Use compose-go https://github.com/compose-spec/compose-go to make Katenary parsing compose file the official way. Add labels: - `volume-from` (with `same-pod`) to avoid volume repetition - `ignore` to ignore a service - `mapenv` (replaces the `env-to-service`) to map environment to helm variable (as a template string) - `secret-vars` declares variables as secret values More: - Now, environment (as secret vars) are set in values.yaml - Ingress has got annotations in values.yaml - Probes (liveness probe) are improved - fixed code to optimize - many others fixes about path, bad volume check, refactorisation, tests...
This commit is contained in:
@@ -10,9 +10,20 @@ 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 }}`
|
||||
)
|
||||
|
||||
// BuildIngress generates the ingress yaml file with conditions.
|
||||
func BuildIngress(ingress *helm.Ingress, name, templatesDir string) {
|
||||
// Set the backend for 1.18
|
||||
for _, b := range ingress.Spec.Rules {
|
||||
@@ -32,22 +43,49 @@ func BuildIngress(ingress *helm.Ingress, name, templatesDir string) {
|
||||
enc.Encode(ingress)
|
||||
buffer.WriteString("{{- end -}}")
|
||||
|
||||
fp, _ := os.Create(fname)
|
||||
fp, err := os.Create(fname)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer fp.Close()
|
||||
|
||||
content := string(buffer.Bytes())
|
||||
lines := strings.Split(content, "\n")
|
||||
|
||||
backendHit := false
|
||||
for _, l := range lines {
|
||||
// apiVersion is a pain...
|
||||
if strings.Contains(l, "apiVersion:") {
|
||||
l = apiVersion
|
||||
}
|
||||
|
||||
// add annotations linked to the Values
|
||||
if strings.Contains(l, "annotations:") {
|
||||
n := CountSpaces(l) + IndentSize
|
||||
l += "\n" + strings.Repeat(" ", n) + "{{- range $k, $v := .Values.__name__.ingress.annotations }}\n"
|
||||
l += strings.Repeat(" ", n) + "{{ $k }}: {{ $v }}\n"
|
||||
l += strings.Repeat(" ", n) + "{{- end }}"
|
||||
l = strings.ReplaceAll(l, "__name__", name)
|
||||
}
|
||||
|
||||
// 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)
|
||||
l = ` ` + cond + l + "\n" + ` {{- end }}`
|
||||
}
|
||||
|
||||
// manage the backend format following the Kubernetes 1.18-0 version or higher
|
||||
// manage the backend format following the Kubernetes 1.19-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)
|
||||
@@ -58,9 +96,6 @@ func BuildIngress(ingress *helm.Ingress, name, templatesDir string) {
|
||||
}
|
||||
backendHit = true
|
||||
}
|
||||
|
||||
fp.WriteString(l + "\n")
|
||||
}
|
||||
|
||||
fp.Close()
|
||||
}
|
||||
|
Reference in New Issue
Block a user