Fix secret that disapeared from Values

- must fix #24
- optimisation on memory locks
- add `AddEnvironment` function to help management
This commit is contained in:
2022-06-22 10:55:11 +02:00
parent 16c8e3dd20
commit 9c449eefab
3 changed files with 50 additions and 20 deletions

View File

@@ -31,7 +31,6 @@ const (
ICON_CRON = "🕒" ICON_CRON = "🕒"
) )
// Values is kept in memory to create a values.yaml file.
var ( var (
EmptyDirs = []string{} EmptyDirs = []string{}
servicesMap = make(map[string]int) servicesMap = make(map[string]int)
@@ -267,8 +266,6 @@ func buildCommandProbe(s *types.ServiceConfig) *helm.Probe {
} }
func setSecretVar(name string, s *types.ServiceConfig, c *helm.Container) *helm.Secret { func setSecretVar(name string, s *types.ServiceConfig, c *helm.Container) *helm.Secret {
locker.Lock()
defer locker.Unlock()
// get the list of secret vars // get the list of secret vars
secretvars, ok := s.Labels[helm.LABEL_SECRETVARS] secretvars, ok := s.Labels[helm.LABEL_SECRETVARS]
if !ok { if !ok {
@@ -285,14 +282,23 @@ func setSecretVar(name string, s *types.ServiceConfig, c *helm.Container) *helm.
} }
// add the secret // add the secret
store.AddEnv(secretvar, ".Values."+name+".environment."+secretvar) store.AddEnv(secretvar, ".Values."+name+".environment."+secretvar)
for i, env := range c.Env { AddEnvironment(name, secretvar, *s.Environment[secretvar])
if env.Name == secretvar {
c.Env = append(c.Env[:i], c.Env[i+1:]...) // Finally remove the secret var from the environment on the service
i-- // and the helm container definition.
defer func(secretvar string) { // defered because AddEnvironment locks the memory
locker.Lock()
defer locker.Unlock()
for i, env := range c.Env {
if env.Name == secretvar {
c.Env = append(c.Env[:i], c.Env[i+1:]...)
i--
}
} }
}
// remove env from ServiceConfig delete(s.Environment, secretvar)
delete(s.Environment, secretvar) }(secretvar)
} }
return store return store
} }

View File

@@ -7,6 +7,11 @@ import (
"github.com/compose-spec/compose-go/types" "github.com/compose-spec/compose-go/types"
) )
var (
// Values is kept in memory to create a values.yaml file.
Values = make(map[string]map[string]interface{})
)
// AddValues adds values to the values.yaml map. // AddValues adds values to the values.yaml map.
func AddValues(servicename string, values map[string]EnvVal) { func AddValues(servicename string, values map[string]EnvVal) {
locker.Lock() locker.Lock()
@@ -21,15 +26,19 @@ func AddValues(servicename string, values map[string]EnvVal) {
} }
} }
// AddVolumeValues add a volume to the values.yaml map for the given deployment name. func AddEnvironment(servicename string, key string, val EnvVal) {
func AddVolumeValues(deployment string, volname string, values map[string]EnvVal) {
locker.Lock() locker.Lock()
defer locker.Unlock() defer locker.Unlock()
if _, ok := VolumeValues[deployment]; !ok { if _, ok := Values[servicename]; !ok {
VolumeValues[deployment] = make(map[string]map[string]EnvVal) Values[servicename] = make(map[string]interface{})
} }
VolumeValues[deployment][volname] = values
if _, ok := Values[servicename]["environment"]; !ok {
Values[servicename]["environment"] = make(map[string]EnvVal)
}
Values[servicename]["environment"].(map[string]EnvVal)[key] = val
} }
// setEnvToValues will set the environment variables to the values.yaml map. // setEnvToValues will set the environment variables to the values.yaml map.
@@ -44,13 +53,12 @@ func setEnvToValues(name string, s *types.ServiceConfig, c *helm.Container) {
return return
} }
valuesEnv := make(map[string]interface{})
for k, v := range env { for k, v := range env {
k = strings.ReplaceAll(k, ".", "_") k = strings.ReplaceAll(k, ".", "_")
valuesEnv[k] = v AddEnvironment(name, k, v)
} }
AddValues(name, map[string]EnvVal{"environment": valuesEnv}) //AddValues(name, map[string]EnvVal{"environment": valuesEnv})
for k := range env { for k := range env {
fixedK := strings.ReplaceAll(k, ".", "_") fixedK := strings.ReplaceAll(k, ".", "_")
v := "{{ tpl .Values." + name + ".environment." + fixedK + " . }}" v := "{{ tpl .Values." + name + ".environment." + fixedK + " . }}"

View File

@@ -13,10 +13,22 @@ import (
) )
var ( var (
Values = make(map[string]map[string]interface{}) // VolumeValues is the map of volumes for each deployment
// containing volume configuration
VolumeValues = make(map[string]map[string]map[string]EnvVal) VolumeValues = make(map[string]map[string]map[string]EnvVal)
) )
// AddVolumeValues add a volume to the values.yaml map for the given deployment name.
func AddVolumeValues(deployment string, volname string, values map[string]EnvVal) {
locker.Lock()
defer locker.Unlock()
if _, ok := VolumeValues[deployment]; !ok {
VolumeValues[deployment] = make(map[string]map[string]EnvVal)
}
VolumeValues[deployment][volname] = values
}
// addVolumeFrom takes the LABEL_VOLUMEFROM to get volumes from another container. This can only work with // addVolumeFrom takes the LABEL_VOLUMEFROM to get volumes from another container. This can only work with
// container that has got LABEL_SAMEPOD as we need to get the volumes from another container in the same deployment. // container that has got LABEL_SAMEPOD as we need to get the volumes from another container in the same deployment.
func addVolumeFrom(deployment *helm.Deployment, container *helm.Container, s *types.ServiceConfig) { func addVolumeFrom(deployment *helm.Deployment, container *helm.Container, s *types.ServiceConfig) {
@@ -86,7 +98,11 @@ func addVolumeFrom(deployment *helm.Deployment, container *helm.Container, s *ty
} }
// prepareVolumes add the volumes of a service. // prepareVolumes add the volumes of a service.
func prepareVolumes(deployment, name string, s *types.ServiceConfig, container *helm.Container, fileGeneratorChan HelmFileGenerator) []map[string]interface{} { func prepareVolumes(
deployment, name string,
s *types.ServiceConfig,
container *helm.Container,
fileGeneratorChan HelmFileGenerator) []map[string]interface{} {
volumes := make([]map[string]interface{}, 0) volumes := make([]map[string]interface{}, 0)
mountPoints := make([]interface{}, 0) mountPoints := make([]interface{}, 0)