package exporters import ( "encoding/json" "regexp" v1 "k8s.io/api/core/v1" "sigs.k8s.io/yaml" ) // Service removes basic stuff like namespace, status, managedBy and lastAppliedConfiguration func Service(svc *v1.Service, jqcommands []string, nobuiltinmods bool, chartname string, lessOpinions bool) string { if !nobuiltinmods { // All the below do not belong in a helm chart yaml delete(svc.ObjectMeta.Annotations, "kubectl.kubernetes.io/last-applied-configuration") delete(svc.ObjectMeta.Annotations, "deployment.kubernetes.io/revision") svc.ObjectMeta.ManagedFields = nil svc.ObjectMeta.Namespace = "" svc.ObjectMeta.ResourceVersion = "" svc.ObjectMeta.Generation = 0 svc.ObjectMeta.SelfLink = "" svc.ObjectMeta.UID = "" jqcommands = append(jqcommands, "del(.metadata.creationTimestamp)") jqcommands = append(jqcommands, "del(.status)") // 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 }}\"}") jqcommands = append(jqcommands, "del(.spec.clusterIP)") jqcommands = append(jqcommands, ".spec.externalTrafficPolicy = \"{{ .Values.service.externalTrafficPolicy }}\"") jqcommands = append(jqcommands, ".spec.type = \"{{ .Values.service.type }}\"") jqcommands = append(jqcommands, "del(.spec.ports[].nodePort)") } } // Jump to json for jq ;-) jsonForm, err := json.Marshal(svc) 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.service.enabled -}}\n" ret += "{{- $fullName := include \"" + chartname + ".fullname\" . -}}\n" ret += "kind: Service\n" ret += "apiVersion: v1\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) ret += "{{- end -}}" // if .Values.service.enabled return ret }