From dbe9fc25ea9f52199147972de35daed41742df24 Mon Sep 17 00:00:00 2001 From: Patrice Ferlet Date: Thu, 5 May 2022 13:55:33 +0200 Subject: [PATCH] Add mapenv label that is more agnostic - that means that katenary.io/env-to-service is now DEPRECATED - the yaml style in label is OK, that allows more possibilities and adaptation for users --- generator/main.go | 30 ++++++++++++++++++++++++++++++ helm/container.go | 12 ++++++++++++ helm/labels.go | 9 +++++++-- 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/generator/main.go b/generator/main.go index 41d2ae6..f0a8aea 100644 --- a/generator/main.go +++ b/generator/main.go @@ -15,6 +15,7 @@ import ( "sync" "github.com/compose-spec/compose-go/types" + "gopkg.in/yaml.v3" ) const ( @@ -52,6 +53,7 @@ echo "Done" // Create a Deployment for a given compose.Service. It returns a list of objects: a Deployment and a possible Service (kubernetes represnetation as maps). func CreateReplicaObject(name string, s types.ServiceConfig, linked map[string]types.ServiceConfig) chan interface{} { ret := make(chan interface{}, len(s.Ports)+len(s.Expose)+2) + rebuildEnvMap(&s) go parseService(name, s, linked, ret) return ret } @@ -659,3 +661,31 @@ func readEnvFile(envfilename string) map[string]string { } return env } + +// rebuildEnvMap will get all LABEL_MAP_ENV to rebuild the env map with tpl. +func rebuildEnvMap(s *types.ServiceConfig) { + mapenv, ok := s.Labels[helm.LABEL_MAP_ENV] + if !ok { + return + } + + // the mapenv is a YAML string + var envmap map[string]string + err := yaml.Unmarshal([]byte(mapenv), &envmap) + if err != nil { + logger.ActivateColors = true + logger.Red(err.Error()) + logger.ActivateColors = false + return + } + + // rebuild the env map + for k, v := range envmap { + _, ok := s.Environment[k] + if !ok { + continue + } + s.Environment[k] = &v + } + +} diff --git a/helm/container.go b/helm/container.go index 9c3cb52..fbc480a 100644 --- a/helm/container.go +++ b/helm/container.go @@ -1,6 +1,7 @@ package helm import ( + "katenary/logger" "strings" "github.com/compose-spec/compose-go/types" @@ -44,6 +45,17 @@ func NewContainer(name, image string, environment types.MappingWithEquals, label if bound, ok := labels[LABEL_ENV_SERVICE]; ok { toServices = strings.Split(bound, ",") } + if len(toServices) > 0 { + // warn, it's deprecated now + logger.ActivateColors = true + logger.Yellowf( + "[deprecated] in \"%s\" service: label %s is deprecated, please use %s instead\n", + name, + LABEL_ENV_SERVICE, + LABEL_MAP_ENV, + ) + logger.ActivateColors = false + } idx := 0 for n, v := range environment { diff --git a/helm/labels.go b/helm/labels.go index bbab538..cb7ee99 100644 --- a/helm/labels.go +++ b/helm/labels.go @@ -7,15 +7,18 @@ import ( const ReleaseNameTpl = "{{ .Release.Name }}" const ( + LABEL_MAP_ENV = K + "/mapenv" LABEL_ENV_SECRET = K + "/secret-envfiles" LABEL_PORT = K + "/ports" LABEL_INGRESS = K + "/ingress" - LABEL_ENV_SERVICE = K + "/env-to-service" LABEL_VOL_CM = K + "/configmap-volumes" LABEL_HEALTHCHECK = K + "/healthcheck" LABEL_SAMEPOD = K + "/same-pod" LABEL_EMPTYDIRS = K + "/empty-dirs" LABEL_IGNORE = K + "/ignore" + + //deprecated: use LABEL_MAP_ENV instead + LABEL_ENV_SERVICE = K + "/env-to-service" ) // GetLabelsDocumentation returns the documentation for the labels. @@ -24,9 +27,10 @@ func GetLabelsDocumentation() string { # Labels {{.LABEL_IGNORE | printf "%-33s"}}: ignore the container, it will not yied any object in the helm chart {{.LABEL_ENV_SECRET | printf "%-33s"}}: set the given file names as a secret instead of configmap +{{.LABEL_MAP_ENV | printf "%-33s"}}: map environment variable to a template string (yaml style) {{.LABEL_PORT | printf "%-33s"}}: set the ports to expose as a service (coma separated) {{.LABEL_INGRESS | printf "%-33s"}}: set the port to expose in an ingress (coma separated) -{{.LABEL_ENV_SERVICE | printf "%-33s"}}: specifies that the environment variable points on a service name (coma separated) +{{.LABEL_ENV_SERVICE | printf "%-33s"}}: DEPRECATED use {{ .LABEL_MAP_ENV }} instead - specifies that the environment variable points on a service name (coma separated) {{.LABEL_VOL_CM | printf "%-33s"}}: specifies that the volumes points on a configmap (coma separated) {{.LABEL_SAMEPOD | printf "%-33s"}}: specifies that the pod should be deployed in the same pod than the given service name {{.LABEL_EMPTYDIRS | printf "%-33s"}}: specifies that the given volume names should be "emptyDir" instead of persistentVolumeClaim (coma separated) @@ -47,6 +51,7 @@ func GetLabelsDocumentation() string { "LABEL_SAMEPOD": LABEL_SAMEPOD, "LABEL_EMPTYDIRS": LABEL_EMPTYDIRS, "LABEL_IGNORE": LABEL_IGNORE, + "LABEL_MAP_ENV": LABEL_MAP_ENV, }) return buff.String() }