Skip to content

Commit

Permalink
Move function to kustomize
Browse files Browse the repository at this point in the history
  • Loading branch information
VaishnaviHire committed Oct 17, 2022
1 parent c29f2a6 commit 01652f2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
27 changes: 26 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 @@ -835,6 +836,30 @@ func WriteKfDef(kfdef *kfdefsv3.KfDef, kfdefpath string) error {
return nil
}

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

}
return value
}


// MergeKustomization will merge the child into the parent
// if the child has no bases, then the parent just needs to add the child as base
// otherwise the parent needs to merge with behaviors
Expand All @@ -846,7 +871,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
21 changes: 1 addition & 20 deletions pkg/kfconfig/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"os"
"path"
"path/filepath"
"regexp"
"sigs.k8s.io/kustomize/v3/pkg/types"
"strings"
)
Expand Down Expand Up @@ -1032,31 +1031,13 @@ func IsAppNotFound(e error) bool {
func getParameter(parameters []NameValue, paramName string) (string, bool) {
for _, p := range parameters {
if p.Name == paramName {
return getParameterValue(p.Value), true
return p.Value, true
}
}

return "", false
}

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

func setParameter(parameters []NameValue, paramName string, value string) []NameValue {
pIndex := -1

Expand Down

0 comments on commit 01652f2

Please sign in to comment.