diff --git a/cmd/cli/kubectl-kyverno/commands/apply/command.go b/cmd/cli/kubectl-kyverno/commands/apply/command.go index c72038a6a0..7281d4caad 100644 --- a/cmd/cli/kubectl-kyverno/commands/apply/command.go +++ b/cmd/cli/kubectl-kyverno/commands/apply/command.go @@ -237,6 +237,7 @@ func (c *ApplyCommandConfig) applyPolicytoResource( if vars != nil { vars.SetInStore(store) } + var rc processor.ResultCounts // validate policies var validPolicies []kyvernov1.PolicyInterface for _, pol := range policies { @@ -244,6 +245,7 @@ func (c *ApplyCommandConfig) applyPolicytoResource( _, err := policyvalidation.Validate(pol, nil, nil, true, config.KyvernoUserName(config.KyvernoServiceAccountName())) if err != nil { log.Log.Error(err, "policy validation error") + rc.IncrementError(1) if strings.HasPrefix(err.Error(), "variable 'element.name'") { skipInvalidPolicies.invalid = append(skipInvalidPolicies.invalid, pol.GetName()) } else { @@ -254,7 +256,6 @@ func (c *ApplyCommandConfig) applyPolicytoResource( validPolicies = append(validPolicies, pol) } - var rc processor.ResultCounts var responses []engineapi.EngineResponse for _, resource := range resources { processor := processor.PolicyProcessor{ diff --git a/cmd/cli/kubectl-kyverno/processor/result.go b/cmd/cli/kubectl-kyverno/processor/result.go index 0196f56a00..173ed08b16 100644 --- a/cmd/cli/kubectl-kyverno/processor/result.go +++ b/cmd/cli/kubectl-kyverno/processor/result.go @@ -21,6 +21,10 @@ func (rc ResultCounts) Warn() int { return rc.warn } func (rc ResultCounts) Error() int { return rc.err } func (rc ResultCounts) Skip() int { return rc.skip } +func (rc *ResultCounts) IncrementError(inc int) { + rc.err += inc +} + func (rc *ResultCounts) addEngineResponses(auditWarn bool, responses ...engineapi.EngineResponse) { for _, response := range responses { rc.addEngineResponse(auditWarn, response)