Skip to content

Commit

Permalink
feat: matchLabels
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed Jan 24, 2025
1 parent 062a9ec commit 4246321
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 7 deletions.
18 changes: 18 additions & 0 deletions coll/coll.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"sort"
"strings"

"github.com/flanksource/commons/collections"
"github.com/flanksource/gomplate/v3/conv"
iconv "github.com/flanksource/gomplate/v3/internal/conv"
)
Expand Down Expand Up @@ -370,3 +371,20 @@ func KeyValToMap(s string) (map[string]string, error) {
}
return m, nil
}

func MatchLabel(labels map[string]any, key, valuePattern string) bool {
for k, v := range labels {
if k != key {
continue
}

if vStr, ok := v.(string); ok {
if collections.MatchItems(vStr, valuePattern) {
return true
}
}

}

return false
}
1 change: 1 addition & 0 deletions funcs/cel_exports.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions funcs/coll.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"context"

"github.com/flanksource/gomplate/v3/conv"
"github.com/google/cel-go/cel"
"github.com/google/cel-go/common/types"
"github.com/google/cel-go/common/types/ref"

"github.com/flanksource/gomplate/v3/coll"
"github.com/pkg/errors"
Expand Down Expand Up @@ -43,6 +46,8 @@ func CreateCollFuncs(ctx context.Context) map[string]interface{} {
f["sort"] = ns.Sort
f["jq"] = ns.JQ
f["flatten"] = ns.Flatten

f["matchLabel"] = coll.MatchLabel
f["mapToKeyVal"] = coll.MapToKeyVal[any]
f["keyValToMap"] = coll.KeyValToMap
f["jsonpath"] = coll.JSONPath
Expand Down Expand Up @@ -189,3 +194,24 @@ func (CollFuncs) Omit(args ...interface{}) (map[string]interface{}, error) {
}
return coll.Omit(m, keys...), nil
}

var celLabelsMatch = cel.Function("matchLabel",
cel.Overload("matchLabel_map_string_string",
[]*cel.Type{
cel.MapType(cel.StringType, cel.DynType), cel.StringType, cel.StringType,
},
cel.BoolType,
cel.FunctionBinding(func(args ...ref.Val) ref.Val {
key := conv.ToString(args[1])
valuePattern := conv.ToString(args[2])

labels, err := convertMap(args[0])
if err != nil {
return types.WrapErr(errors.New("matchLabel expects the first argument to be a map[string]any"))
}

result := coll.MatchLabel(labels, key, valuePattern)
return types.DefaultTypeAdapter.NativeToValue(result)
}),
),
)
6 changes: 0 additions & 6 deletions metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,3 @@ type MetricsType struct {
TemplatesProcessed int
Errors int
}

func newMetrics() *MetricsType {
return &MetricsType{
RenderDuration: make(map[string]time.Duration),
}
}
2 changes: 1 addition & 1 deletion template.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func RunExpressionContext(ctx commonsContext.Context, _environment map[string]an

ast, issues := env.Compile(strings.ReplaceAll(template.Expression, "\n", " "))
if issues != nil && issues.Err() != nil {
return "", oops.With("template", template.Expression).Errorf(issues.String())
return "", oops.With("template", template.Expression).Errorf("issues: %s", issues.String())
}

prg, err = env.Program(ast, cel.Globals(data))
Expand Down
20 changes: 20 additions & 0 deletions tests/cel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,26 @@ func TestCelK8sMemoryResourceUnits(t *testing.T) {
}
}

func TestMatchLabel(t *testing.T) {
config := map[string]any{
"labels": map[string]string{
"environment": "production",
"region": "us-east-1",
},
"tags": map[string]string{
"cluster": "aws-prod",
},
}

runTests(t, []Test{
{map[string]any{"config": config}, "matchLabel(config.labels, 'region', 'us-*')", "true"},
{map[string]any{"config": config}, "matchLabel(config.labels, 'region', 'eu-*')", "false"},
{map[string]any{"config": config}, "matchLabel(config.labels, 'environment', 'production')", "true"},
{map[string]any{"config": config}, "matchLabel(config.tags, 'cluster', 'aws-*')", "true"},
{map[string]any{"config": config}, "matchLabel(config.tags, 'cluster', '*-prod')", "true"},
})
}

func TestCelYAML(t *testing.T) {
person := Person{
Name: "Aditya",
Expand Down

0 comments on commit 4246321

Please sign in to comment.