Fix issue with overrides for env-vars in store

The store deploy command was passing multiple environment
variables to the deployment endpoint, instead of implementing
the intended precedence.

Tested with a local faasd instance. This is important for
when people test with things like the sleep function from the
openfaas function store.

Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
This commit is contained in:
Alex Ellis (OpenFaaS Ltd)
2022-10-11 09:33:25 +01:00
parent d188521ee1
commit 10815dfbdb
2 changed files with 27 additions and 11 deletions

View File

@@ -444,15 +444,18 @@ func parseMap(envvars []string, keyName string) (map[string]string, error) {
return result, nil
}
func mergeMap(i map[string]string, j map[string]string) map[string]string {
// mergeMap merges two maps, with the overlay taking precedence.
// The return value allocates a new map.
func mergeMap(base map[string]string, overlay map[string]string) map[string]string {
merged := make(map[string]string)
for k, v := range i {
for k, v := range base {
merged[k] = v
}
for k, v := range j {
for k, v := range overlay {
merged[k] = v
}
return merged
}

View File

@@ -57,14 +57,19 @@ var storeDeployCmd = &cobra.Command{
faas-cli store deploy figlet \
--gateway=http://127.0.0.1:8080 \
--env=MYVAR=myval`,
RunE: runStoreDeploy,
RunE: runStoreDeploy,
PreRunE: preRunEStoreDeploy,
}
func runStoreDeploy(cmd *cobra.Command, args []string) error {
func preRunEStoreDeploy(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return fmt.Errorf("please provide the function name")
}
return nil
}
func runStoreDeploy(cmd *cobra.Command, args []string) error {
targetPlatform := getTargetPlatform(platformValue)
storeItems, err := storeList(storeAddress)
if err != nil {
@@ -79,14 +84,22 @@ func runStoreDeploy(cmd *cobra.Command, args []string) error {
return fmt.Errorf("function '%s' not found for platform '%s'", requestedStoreFn, targetPlatform)
}
// Add the store environment variables to the provided ones from cmd
if item.Environment != nil {
for k, v := range item.Environment {
env := fmt.Sprintf("%s=%s", k, v)
storeDeployFlags.envvarOpts = append(storeDeployFlags.envvarOpts, env)
}
flagEnvs, err := parseMap(storeDeployFlags.envvarOpts, "env")
if err != nil {
return err
}
// Add the store environment variables to the provided ones from cmd
mergedEnvs := mergeMap(item.Environment, flagEnvs)
envs := []string{}
for k, v := range mergedEnvs {
env := fmt.Sprintf("%s=%s", k, v)
storeDeployFlags.envvarOpts = append(envs, env)
}
storeDeployFlags.envvarOpts = envs
// Add the store labels to the provided ones from cmd
if item.Labels != nil {
for k, v := range item.Labels {