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

feat: Support list expansions #2068

Closed
wants to merge 2 commits into from
Closed
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
26 changes: 26 additions & 0 deletions docs/customresourcestate-metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,32 @@ Examples:

# For generally matching against a field in an object schema, use the following syntax:
[metadata, "name=foo"] # if v, ok := metadata[name]; ok && v == "foo" { return v; } else { /* ignore */ }

# expand a list
rexagod marked this conversation as resolved.
Show resolved Hide resolved
[spec, order, "*", value] # syntax
# for the object snippet below
...
status:
namespaces:
- namespace: "foo"
status:
available:
resourceA: '10'
resourceB: '20'
pending:
resourceA: '0'
resourceB: '6'
...
# resolves to: [status, namespaces, <range len(namespaces)>, status] (a multi-dimensional array)
path: [status, namespaces, "*", status]
labelsFromPath:
# this can be combined with the wildcard prefixes feature introduced in #2052
"available_*": [available]
dgrisonnet marked this conversation as resolved.
Show resolved Hide resolved
"lorem_*": [pending]
# this can be combined with dynamic valueFrom expressions introduced in #2068
valueFrom: [lorem_resourceB] # or [available_resourceB]
# outputs:
...{...,available_resourceA="10",available_resourceB="20",lorem_resourceA="0",lorem_resourceB="6"...} 6
```

### Wildcard matching of version and kind fields
Expand Down
187 changes: 182 additions & 5 deletions pkg/customresourcestate/registry_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,22 @@ type compiledCommon struct {
t metric.Type
}

func (c *compiledCommon) SetPath(p valuePath) {
c.path = p
}

func (c compiledCommon) Path() valuePath {
return c.path
}

func (c *compiledCommon) SetLabelFromPath(parr map[string]valuePath) {
c.labelFromPath = parr
}

func (c compiledCommon) LabelFromPath() map[string]valuePath {
return c.labelFromPath
}

func (c compiledCommon) Type() metric.Type {
return c.t
}
Expand All @@ -146,7 +156,9 @@ type eachValue struct {
type compiledMetric interface {
Values(v interface{}) (result []eachValue, err []error)
Path() valuePath
SetPath(valuePath)
LabelFromPath() map[string]valuePath
SetLabelFromPath(map[string]valuePath)
Type() metric.Type
}

Expand Down Expand Up @@ -216,6 +228,44 @@ type compiledGauge struct {
labelFromKey string
}

func underscoresToIndices(extractedValueFrom string, it interface{}) interface{} {
// `it` is the search space.
m, isResolvableMap := it.(map[string]interface{})
arr, isResolvableArr := it.([]interface{})
if !isResolvableMap && !isResolvableArr {
return nil
}
// `extractedValueFrom` is the search term.
// Split `extractedValueFrom` by underscores.
terms := strings.Split(extractedValueFrom, "_")
resolvedTerm := interface{}(terms[0])
for _, term := range terms[1:] {
if isResolvableMap {
t, ok := m[term]
if !ok {
return resolvedTerm
}
resolvedTerm = t
if _, isResolvableMap = t.(map[string]interface{}); isResolvableMap {
m = t.(map[string]interface{})
}
} else if isResolvableArr {
for _, el := range arr {
t, ok := el.(map[string]interface{})
if !ok {
continue
}
if v, ok := t[term]; ok {
resolvedTerm = v
m = t
break
}
}
}
}
return resolvedTerm
}

func (c *compiledGauge) Values(v interface{}) (result []eachValue, errs []error) {
onError := func(err error) {
errs = append(errs, fmt.Errorf("%s: %v", c.Path(), err))
Expand All @@ -237,9 +287,41 @@ func (c *compiledGauge) Values(v interface{}) (result []eachValue, errs []error)
sValueFrom[0] == '[' && sValueFrom[len(sValueFrom)-1] == ']' &&
// "[...]" and not "[]".
len(sValueFrom) > 2 {
// remove the brackets
extractedValueFrom := sValueFrom[1 : len(sValueFrom)-1]
if key == extractedValueFrom {
gotFloat, err := toFloat64(it, c.NilIsZero)
// search space to resolve the dynamic valueFrom from the wildcard labels
dynamicValueFromScope := c.compiledCommon.labelFromPath
lastUnderscoreIndex := strings.LastIndex(extractedValueFrom, "_")
if lastUnderscoreIndex != -1 {
// For:
// labelsFromPath:
// "foo_*": ["spec", "fooObj"]
unresolvedKey := extractedValueFrom[:lastUnderscoreIndex] + "_*"
dynamicPaths, ok := dynamicValueFromScope[unresolvedKey]
if ok {
var resolvedKeyArr []string
for _, dynamicPath := range dynamicPaths {
resolvedKeyArr = append(resolvedKeyArr, dynamicPath.part)
}
// resolvedKey will map to unresolved key "foo_*"'s corresponding valid path string, i.e., "spec_fooObj_*".
resolvedKey := strings.Join(resolvedKeyArr, "_")
extractedValueFrom = resolvedKey + extractedValueFrom[lastUnderscoreIndex:]
}
}
Comment on lines +292 to +310
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we do that?

As far as I can tell this is made to propagate the wildcard to the value no? Like:

"foo_*": ["spec", "fooObj"]

yields the following path:

spec_fooObj_*

no?

if strings.HasPrefix(extractedValueFrom, key) {
var gotFloat float64
var err error
if strings.Contains(extractedValueFrom, "_") {
resolvedExtractedValueFrom := underscoresToIndices(extractedValueFrom, it)
if _, didResolveFullPath := resolvedExtractedValueFrom.(string); didResolveFullPath {
gotFloat, err = toFloat64(resolvedExtractedValueFrom, c.NilIsZero)
}
if _, isFloat := resolvedExtractedValueFrom.(float64); isFloat {
gotFloat = resolvedExtractedValueFrom.(float64)
}
} else {
gotFloat, err = toFloat64(it, c.NilIsZero)
}
if err != nil {
onError(fmt.Errorf("[%s]: %w", key, err))
continue
Expand Down Expand Up @@ -554,11 +636,35 @@ type pathOp struct {
type valuePath []pathOp

func (p valuePath) Get(obj interface{}) interface{} {
handleNil := func(object interface{}, part string) interface{} {
switch tobj := object.(type) {
case map[string]interface{}:
return tobj[part]
case []interface{}:
if part == "*" {
return tobj
}
idx, err := strconv.Atoi(part)
if err != nil {
return nil
}
if idx < 0 || idx >= len(tobj) {
return nil
}
return tobj[idx]
default:
return nil
}
}
Comment on lines +639 to +658
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you make that its own function so that the code is easier to read?

for _, op := range p {
if obj == nil {
return nil
}
obj = op.op(obj)
if op.op == nil {
obj = handleNil(obj, op.part)
} else {
obj = op.op(obj)
}
}
return obj
}
Expand Down Expand Up @@ -674,14 +780,85 @@ func famGen(f compiledFamily) generator.FamilyGenerator {
}
}

func findWildcard(path valuePath, i *int) bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not a fan of modifying the index and returning whether we find the wildcard or not. A more idiomatic approach would be similar to the search functions in the strings package where if found you return the index, otherwise -1

for ; *i < len(path); *i++ {
if path[*i].part == "*" {
return true
}
}
return false
}

func resolveWildcard(path valuePath, object map[string]interface{}) []valuePath {
if path == nil {
return nil
}
checkpoint := object
var expandedPaths []valuePath
var list []interface{}
var l int
for i, j := 0, 0; findWildcard(path, &i); /* i is at "*" now */ {
for ; j < i; j++ {
maybeCheckpoint, ok := checkpoint[path[j].part]
if !ok {
// path[j] is not in the object, so we can't expand the wildcard
return []valuePath{path}
}
// store (persist) last checkpoint
if c, ok := maybeCheckpoint.([]interface{}); ok {
list = c
break
}
checkpoint = maybeCheckpoint.(map[string]interface{})
}
if j > i {
break
}
// i is at "*", j is at the last part before "*", checkpoint is at the value of the last part before "*"
l = len(list) // number of elements in the list
pathCopyStart := make(valuePath, i)
copy(pathCopyStart, path[:i])
pathCopyWildcard := make(valuePath, len(path)-i-1)
copy(pathCopyWildcard, path[i+1:])
for k := 0; k < l; k++ {
pathCopyStart = append(pathCopyStart, pathOp{part: strconv.Itoa(k)})
pathCopyStart = append(pathCopyStart, pathCopyWildcard...)
expandedPaths = append(expandedPaths, pathCopyStart)
}
Comment on lines +819 to +827
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since we know the size of the path from the get go, it would be less expensive in terms of allocations to just allocate one slice and then fill it correctly

j++ // skip "*"
}
return expandedPaths[:l]
}

// generate generates the metrics for a custom resource.
func generate(u *unstructured.Unstructured, f compiledFamily, errLog klog.Verbose) *metric.Family {
klog.V(10).InfoS("Checked", "compiledFamilyName", f.Name, "unstructuredName", u.GetName())
var metrics []*metric.Metric
baseLabels := f.BaseLabels(u.Object)
if f.Each.Path() != nil {
fPaths := resolveWildcard(f.Each.Path(), u.Object)
for _, fPath := range fPaths {
f.Each.SetPath(fPath)
}
}

if f.Each.LabelFromPath() != nil {
labelsFromPath := make(map[string]valuePath)
flfp := f.Each.LabelFromPath()
for k, flfpPath := range flfp {
fLPaths := resolveWildcard(flfpPath, u.Object)
for i, fPath := range fLPaths {
genLabel := k + strconv.Itoa(i)
labelsFromPath[genLabel] = fPath
}
}
if len(labelsFromPath) > 0 {
f.Each.SetLabelFromPath(labelsFromPath)
}
}

values, errors := scrapeValuesFor(f.Each, u.Object)
for _, err := range errors {
values, errorSet := scrapeValuesFor(f.Each, u.Object)
for _, err := range errorSet {
errLog.ErrorS(err, f.Name)
}

Expand Down
Loading