kyverno / chainsaw

Declarative K8s e2e testing
https://kyverno.github.io/chainsaw/
Apache License 2.0
312 stars 46 forks source link

fix: template-functions now correctly work for name, namespace etc. #2140

Open bertbaron opened 2 weeks ago

bertbaron commented 2 weeks ago

…bels

Explanation

JMESPath conditions for name or namespace are ignored. When a JMESPath condition is used for a label, all labels are ignored. In our setup this resulted in strange sporadic failures where totally different resources from other tests were matched.

Related issue

Fixes #2138

Proposed Changes

The current code uses the getters and setters from Unstructured, i.e.:

    temp.SetNamespace(obj.GetNamespace())
    temp.SetLabels(obj.GetLabels())

    ... perform templating on temp

    obj.SetNamespace(temp.GetNamespace())
    obj.SetLabels(temp.GetLabels())

The problem here is that the Get... methods will return empty values if the values are not strings, and by setting them back to obj the existing structured values are overwritten.

The fix involves two changes:

  1. Instead of using obj.GetLabels(), which returns nil if any label value is not a string, filter all string labels ignoring any other label.
  2. Instead of copying the properties back using the setters, use obj.Object = maps.Merge(obj.Object, merged.UnstructuredContent()) to avoid that structured values will be removed from obj simply because they are absent in the templated object.

Checklist

Further Comments