Skip to content
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
68 changes: 60 additions & 8 deletions pkg/variables/ScopedVariableService.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package variables

import (
"fmt"
"github.com/caarlos0/env"
"github.com/devtron-labs/devtron/pkg/devtronResource"
"github.com/devtron-labs/devtron/pkg/resourceQualifiers"

"github.com/devtron-labs/devtron/pkg/sql"
"github.com/devtron-labs/devtron/pkg/variables/cache"
"github.com/devtron-labs/devtron/pkg/variables/helper"
Expand All @@ -14,6 +14,8 @@ import (
"github.com/go-pg/pg"
"go.uber.org/zap"
"golang.org/x/exp/slices"
"regexp"
"strings"
"sync"
"time"
)
Expand Down Expand Up @@ -320,8 +322,9 @@ func (impl *ScopedVariableServiceImpl) selectScopeForCompoundQualifier(scopes []
func (impl *ScopedVariableServiceImpl) GetScopedVariables(scope resourceQualifiers.Scope, varNames []string, maskSensitiveData bool) (scopedVariableDataObj []*models.ScopedVariableData, err error) {

//populating system variables from system metadata
var systemVariableData, allSystemVariables []*models.ScopedVariableData
if scope.SystemMetadata != nil {
systemVariableData := impl.getSystemVariablesData(scope.SystemMetadata, varNames)
systemVariableData, allSystemVariables = impl.getSystemVariablesData(scope.SystemMetadata, varNames)
scopedVariableDataObj = append(scopedVariableDataObj, systemVariableData...)
}

Expand Down Expand Up @@ -429,22 +432,71 @@ func (impl *ScopedVariableServiceImpl) GetScopedVariables(scope resourceQualifie
}
}
}

impl.deduceVariables(scopedVariableDataObj, allSystemVariables)
return scopedVariableDataObj, err
}

func resolveExpressionWithVariableValues(expr string, varNameToData map[string]*models.ScopedVariableData) (string, error) {
// regex to find variable placeholder and extracts a variable name which is alphanumeric
// and can contain hyphen, underscore and whitespaces. white spaces will be trimmed on lookup
variableRegex := `@{{([a-zA-Z0-9-_\s]+)}}`

re := regexp.MustCompile(variableRegex)
matches := re.FindAllStringSubmatch(expr, -1)

for _, match := range matches {
if len(match) == 2 {
originalMatch := match[0]
innerContent := match[1]

variableName := strings.TrimSpace(innerContent)

if data, ok := varNameToData[variableName]; ok {
value := data.VariableValue.StringValue()
expr = strings.Replace(expr, originalMatch, value, 1)
} else {
return expr, fmt.Errorf("variable not found %s", variableName)
}
}
}
return expr, nil
}

func (impl *ScopedVariableServiceImpl) getSystemVariablesData(metadata *resourceQualifiers.SystemMetadata, varNames []string) []*models.ScopedVariableData {
func (impl *ScopedVariableServiceImpl) deduceVariables(scopedVariableDataList []*models.ScopedVariableData, systemVariables []*models.ScopedVariableData) {
varNameToData := make(map[string]*models.ScopedVariableData)
for _, variable := range systemVariables {
varNameToData[variable.VariableName] = variable
}

for _, data := range scopedVariableDataList {
value := data.VariableValue.Value
if utils.IsStringType(value) {
resolvedValue, err := resolveExpressionWithVariableValues(value.(string), varNameToData)
if err != nil {
impl.logger.Warnw("variables not resolved", "err", err, "value", value)
continue
}
data.VariableValue.Value = resolvedValue
}
}
}

func (impl *ScopedVariableServiceImpl) getSystemVariablesData(metadata *resourceQualifiers.SystemMetadata, varNames []string) ([]*models.ScopedVariableData, []*models.ScopedVariableData) {
systemVariables := make([]*models.ScopedVariableData, 0)
allSystemVariables := make([]*models.ScopedVariableData, 0)
for _, variable := range resourceQualifiers.SystemVariables {
if len(metadata.GetDataFromSystemVariable(variable)) > 0 && slices.Contains(varNames, string(variable)) {
systemVariables = append(systemVariables, &models.ScopedVariableData{
if len(metadata.GetDataFromSystemVariable(variable)) > 0 {
systemVariable := &models.ScopedVariableData{
VariableName: string(variable),
VariableValue: &models.VariableValue{Value: metadata.GetDataFromSystemVariable(variable)},
})
}
allSystemVariables = append(allSystemVariables, systemVariable)
if slices.Contains(varNames, string(variable)) {
systemVariables = append(systemVariables, systemVariable)
}
}
}
return systemVariables
return systemVariables, allSystemVariables
}

func (impl *ScopedVariableServiceImpl) GetJsonForVariables() (*models.Payload, error) {
Expand Down
4 changes: 4 additions & 0 deletions pkg/variables/utils/type-utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,7 @@ func IsPrimitiveType(value interface{}) bool {
kind == reflect.Uint64 || kind == reflect.Float32 || kind == reflect.Float64 ||
kind == reflect.Bool
}

func IsStringType(val interface{}) bool {
return reflect.TypeOf(val).Kind() == reflect.String
}