Skip to content

Commit

Permalink
example of simpler kyverno validation (#54)
Browse files Browse the repository at this point in the history
Co-authored-by: sachin chauhan <[email protected]>
Co-authored-by: sachin chauhan <[email protected]>
  • Loading branch information
3 people authored Dec 2, 2024
1 parent 52950da commit b8610b4
Showing 1 changed file with 8 additions and 21 deletions.
29 changes: 8 additions & 21 deletions targets.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package magekubernetes

import (
"fmt"
"os"
"strings"

"github.com/magefile/mage/mg" // mg contains helpful utility functions, like Deps
Expand Down Expand Up @@ -120,30 +119,18 @@ func validateKyvernoPolicies(paths string) error {
return nil
}

// Iterate over all policies and apply them
policyFiles, err := os.ReadDir(policyDir)
cmdOptions := append([]string{"apply", policyDir, "-t", "--detailed-results", "--continue-on-fail"}, resourceArgs...)

output, err := sh.Output("kyverno", cmdOptions...)
if err != nil {
return fmt.Errorf("failed to read Kyverno policies: %w", err)
fmt.Println(output)
return fmt.Errorf("Kyverno validation failed for policy %w", err)
}

for _, policyFile := range policyFiles {
if !strings.HasSuffix(policyFile.Name(), ".yaml") {
continue
}

policyFilePath := fmt.Sprintf("%s/%s", policyDir, policyFile.Name())
cmdOptions := append([]string{"apply", policyFilePath, "--policy-report", "--output", "yaml"}, resourceArgs...)
fmt.Printf("Kyverno validation completed.\n")

output, err := sh.Output("kyverno", cmdOptions...)
if err != nil {
return fmt.Errorf("Kyverno validation failed for policy '%s': %w", policyFilePath, err)
}

fmt.Printf("Kyverno validation with policy '%s' completed.\n", policyFilePath)

if strings.Contains(output, "violation") || strings.Contains(output, "failed") {
return fmt.Errorf("Kyverno validation issues found with policy '%s': %s", policyFilePath, output)
}
if strings.Contains(output, "violation") || strings.Contains(output, "failed") {
return fmt.Errorf("Kyverno validation issues found with policy: %s", output)
}

return nil
Expand Down

0 comments on commit b8610b4

Please sign in to comment.