diff --git a/pkg/engine/variables/vars.go b/pkg/engine/variables/vars.go index 3f519a9a73..b57bfbfd7b 100644 --- a/pkg/engine/variables/vars.go +++ b/pkg/engine/variables/vars.go @@ -70,6 +70,15 @@ func subArray(log logr.Logger, ctx context.EvalInterface, patternList []interfac return patternList, nil } +type NotFoundVariableErr struct { + variable string + path string +} + +func (n NotFoundVariableErr) Error() string { + return fmt.Sprintf("could not find variable %v at path %v", n.variable, n.path) +} + // subValR resolves the variables if defined func subValR(ctx context.EvalInterface, valuePattern string, path string) (interface{}, error) { originalPattern := valuePattern @@ -92,7 +101,10 @@ func subValR(ctx context.EvalInterface, valuePattern string, path string) (inter } return nil, fmt.Errorf("failed to resolve %v at path %s", underlyingVariable, path) } - return nil, fmt.Errorf("could not find variable %v at path %v", underlyingVariable, path) + return nil, NotFoundVariableErr{ + variable: underlyingVariable, + path: path, + } } } } else { diff --git a/pkg/policy/background.go b/pkg/policy/background.go index 639281f075..a495cfaa60 100644 --- a/pkg/policy/background.go +++ b/pkg/policy/background.go @@ -2,7 +2,6 @@ package policy import ( "fmt" - "strings" kyverno "github.com/nirmata/kyverno/pkg/api/kyverno/v1" "github.com/nirmata/kyverno/pkg/engine/context" @@ -69,12 +68,14 @@ func ContainsVariablesOtherThanObject(policy kyverno.ClusterPolicy) error { func checkNotFoundErr(err error) bool { if err != nil { - if strings.HasPrefix(err.Error(), "could not find variable") { + switch err.(type) { + case variables.NotFoundVariableErr: return true - } else { + default: return false } } + return true }