Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read env variables from KfDef Parameters #180

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion pkg/kfapp/kustomize/kustomize.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"os"
"path"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -846,7 +847,7 @@ func MergeKustomization(compDir string, targetDir string, kfDef *kfconfig.KfConf

paramMap := make(map[string]string)
for _, nv := range params {
paramMap[nv.Name] = nv.Value
paramMap[nv.Name] = getParameterValue(nv.Value)
}
updateParamFiles := func() error {
paramFile := filepath.Join(targetDir, kftypesv3.KustomizationParamFile)
Expand Down Expand Up @@ -1462,3 +1463,23 @@ func GenerateYamlWithOperatorAnnotation(resMap resmap.ResMap, instance *unstruct
}
return buf.Bytes(), nil
}

func getParameterValue(value string) string {
// Check if there is a environment variable
// Supported format: $VAR and ${VAR}
var re = regexp.MustCompile(`(?m)^\$({(.*)}|\w)`)
envVariable := ""
if re.Match([]byte(value)){
if strings.HasPrefix(value, "${"){
envVariable = value[2:len(value)-1]
}else if strings.HasPrefix(value, "$") {
envVariable = value[1:]
}
return os.Getenv(envVariable)

}else{
log.Info("env variables are not used or are invalid in the parameters." +
" Supported Usage: $VAR and ${VAR}")
}
return value
}
51 changes: 51 additions & 0 deletions pkg/kfapp/kustomize/kustomize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,54 @@ func TestCreateStackAppKustomization(t *testing.T) {
}
}
}


func TestGetParameterValue(t *testing.T) {
type testCase struct {
Name string
Input string
Expected string
}
// Set EXAMPLE_VAR env variable
err := os.Setenv("EXAMPLE_VAR", "example")
if err !=nil {
t.Fatalf("Error setting env variable: %v", err)
}

testCases := []testCase{
{
Name: "Input env variable of type $VAR",
Input: "$EXAMPLE_VAR",
Expected: "example",
},
{
Name: "Input env variable of type ${VAR}",
Input: "${EXAMPLE_VAR}",
Expected: "example",
},
{
Name: "Input string without env variable",
Input: "example",
Expected: "example",
},
}

for _, c := range testCases {

t.Log(c.Name)
result := getParameterValue(c.Input)

if diff := cmp.Diff(c.Expected, result); diff != "" {
t.Fatalf("Result string is different from expected. (-want, +got):\n%s", diff)
}

}

// Unset the env variable
err = os.Unsetenv("EXAMPLE_VAR")
if err !=nil {
t.Fatalf("Error unsetting env variable: %v", err)
}


}