helm-import/internal/exporters/ingress.go

73 lines
2.5 KiB
Go
Executable File

package exporters
import (
"encoding/json"
"regexp"
v1beta1 "k8s.io/api/extensions/v1beta1"
"sigs.k8s.io/yaml"
)
// Configmap removes basic stuff like namespace, status, managedBy and lastAppliedConfiguration
func Ingress(ing *v1beta1.Ingress, jqcommands []string, nobuiltinmods bool, chartname string, lessOpinions bool) string {
if !nobuiltinmods {
// All the below do not belong in a helm chart yaml
delete(ing.ObjectMeta.Annotations, "kubectl.kubernetes.io/last-applied-configuration")
delete(ing.ObjectMeta.Annotations, "deployment.kubernetes.io/revision")
ing.ObjectMeta.ManagedFields = nil
ing.ObjectMeta.Namespace = ""
ing.ObjectMeta.ResourceVersion = ""
ing.ObjectMeta.Generation = 0
ing.ObjectMeta.SelfLink = ""
ing.ObjectMeta.UID = ""
jqcommands = append(jqcommands, "del(.metadata.creationTimestamp)")
// Here's where we get more opinionated though
if !lessOpinions {
jqcommands = append(jqcommands, ".metadata.name = \"{{ $fullName }}\"")
jqcommands = append(jqcommands, ".metadata.labels += {HELMTEMPLATEDELETEKEY: \"{{- include \\\""+chartname+".labels\\\" . | nindent 4 }}\"}")
jqcommands = append(jqcommands, ".metadata.annotations += {HELMTEMPLATEDELETEKEY: \"{{- include \\\""+chartname+".annotations\\\" . | nindent 4 }}\"}")
}
}
// Jump to json for jq ;-)
jsonForm, err := json.Marshal(ing)
if err != nil {
panic(err.Error())
}
var intermediateJSONForm []byte
var finalJSONForm []byte
if len(jqcommands) > 0 {
for _, command := range jqcommands {
intermediateJSONForm = jQOnJSONByteArray(command, jsonForm)
jsonForm = intermediateJSONForm
}
finalJSONForm = jsonForm
} else {
finalJSONForm = jsonForm
}
y, err := yaml.JSONToYAML(finalJSONForm)
if err != nil {
panic(err.Error())
}
ret := "{{- if .Values.ingress.enabled -}}\n"
ret += "{{- $fullName := include \"" + chartname + ".fullname\" . -}}\n"
ret += "{{- $releaseName := .Release.Name}}\n"
ret += "{{- $svcPort := .Values.service.port -}}\n"
ret += "{{- if semverCompare \">=1.14-0\" .Capabilities.KubeVersion.GitVersion -}}\n"
ret += "apiVersion: networking.k8s.io/v1beta1\n"
ret += "{{- else -}}\n"
ret += "apiVersion: extensions/v1beta1\n"
ret += "{{- end }}\n"
ret += "kind: Ingress\n"
out := string(y)
var re = regexp.MustCompile(`HELMTEMPLATEDELETEKEY: '([^']*)'`)
out = re.ReplaceAllString(out, "$1")
re = regexp.MustCompile(`(?s)- HELMTEMPLATEDELETEKEYANDARRAYMARKER: '([^']*)'`)
out = re.ReplaceAllString(out, "$1")
ret += string(out)
return ret
}