Skip to content

Commit

Permalink
added deduction for system variables (#4075)
Browse files Browse the repository at this point in the history
  • Loading branch information
subhashish-devtron authored and ashishdevtron committed Oct 12, 2023
1 parent 8089f0d commit 1d8e76f
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 8 deletions.
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
}

0 comments on commit 1d8e76f

Please sign in to comment.