package exporters import ( "encoding/json" "regexp" v1 "k8s.io/api/apps/v1" "sigs.k8s.io/yaml" ) // Deployment removes basic stuff like namespace, status, managedBy and lastAppliedConfiguration func Deployment(deploy *v1.Deployment, jqcommands []string, nobuiltinmods bool, chartname string, lessOpinions bool) string { if !nobuiltinmods { // All the below do not belong in a helm chart yaml delete(deploy.ObjectMeta.Annotations, "kubectl.kubernetes.io/last-applied-configuration") delete(deploy.ObjectMeta.Annotations, "deployment.kubernetes.io/revision") deploy.ObjectMeta.ManagedFields = nil deploy.ObjectMeta.Namespace = "" deploy.ObjectMeta.ResourceVersion = "" deploy.ObjectMeta.Generation = 0 deploy.ObjectMeta.SelfLink = "" deploy.ObjectMeta.UID = "" jqcommands = append(jqcommands, "del(.metadata.creationTimestamp)") jqcommands = append(jqcommands, "del(.status)") if !lessOpinions { jqcommands = append(jqcommands, ".metadata.name = \"{{ $fullName }}\"") jqcommands = append(jqcommands, ".spec.replicas = \"{{ .values.replicaCount }}\"") jqcommands = append(jqcommands, ".metadata.labels += {HELMTEMPLATEDELETEKEY: \"{{- include \\\""+chartname+".labels\\\" . | nindent 4 }}\"}") jqcommands = append(jqcommands, ".metadata.annotations += {HELMTEMPLATEDELETEKEY: \"{{- include \\\""+chartname+".annotations\\\" . | nindent 4 }}\"}") // damn, "checksum/config" works as a key in yaml, not in json! jqcommands = append(jqcommands, ".metadata.annotations += {checksumConfig: \"{{- include (print $.Template.BasePath \\\"/configmap.yaml\\\") . | sha256sum }}\"}") jqcommands = append(jqcommands, ".metadata.annotations += {checksumSecret: \"{{- include (print $.Template.BasePath \\\"/secret.yaml\\\") . | sha256sum }}\"}") jqcommands = append(jqcommands, ".spec.selector.matchLabels += {HELMTEMPLATEDELETEKEY: \"{{- include \\\""+chartname+".selectorLabels\\\" . | nindent 6 }}\"}") jqcommands = append(jqcommands, ".spec.template.metadata.labels += {HELMTEMPLATEDELETEKEY: \"{{- include \\\""+chartname+".labels\\\" . | nindent 8 }}\"}") jqcommands = append(jqcommands, ".spec.template.metadata.annotations += {HELMTEMPLATEDELETEKEY: \"{{- include \\\""+chartname+".annotations\\\" . | nindent 8 }}\"}") jqcommands = append(jqcommands, ".spec.template.spec.serviceAccountName = \"{{ include \\\""+chartname+".serviceAccountName\\\" . }}\"") jqcommands = append(jqcommands, ".spec.template.spec.securityContext += {HELMTEMPLATEDELETEKEY: \"{{- toYaml .podSecurityContext | nindent 8 }}\"}") jqcommands = append(jqcommands, ".spec.template.spec.containers[0].env += [{HELMTEMPLATEDELETEKEYANDARRAYMARKER: \"{{- range $key, $value := .Values."+chartname+".env }}\"}]") jqcommands = append(jqcommands, ".spec.template.spec.containers[0].env += [{ name: \"{{ $key }}\", valueFrom: {configMapKeyRef: {name: \"{{ $fullName }}\", key: \"{{ $key | quote }}\" }} }]") jqcommands = append(jqcommands, ".spec.template.spec.containers[0].env += [{HELMTEMPLATEDELETEKEYANDARRAYMARKER: \"{{- end }}\"}]") jqcommands = append(jqcommands, ".spec.template.spec.containers[0].env += [{HELMTEMPLATEDELETEKEYANDARRAYMARKER: \"{{- range $key, $value := .Values."+chartname+".secretenv }}\"}]") jqcommands = append(jqcommands, ".spec.template.spec.containers[0].env += [{ name: \"{{ $key }}\", valueFrom: {secretKeyRef: {name: \"{{ $fullName }}\", key: \"{{ $key | quote }}\" }} }]") jqcommands = append(jqcommands, ".spec.template.spec.containers[0].env += [{HELMTEMPLATEDELETEKEYANDARRAYMARKER: \"{{- end }}\"}]") } } // Jump to json for jq ;-) jsonForm, err := json.Marshal(deploy) 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 := "{{- $fullName := include \"" + chartname + ".fullname\" . -}}\n" ret += "kind: Deployment\n" ret += "apiVersion: Apps/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) return ret }