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

fix #3261 Sort constraint status audit results #3277

Merged
merged 4 commits into from
Feb 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 34 additions & 0 deletions pkg/audit/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io"
"os"
"path"
"sort"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -138,6 +139,36 @@ type updateListEntry struct {
enforcementAction util.EnforcementAction
}

// ByGVKNNMsg implements sort.Interface based on the group, version, kind, name, namespace, and msg fields.
type byGVKNNMsg []updateListEntry

func (a byGVKNNMsg) Len() int {
return len(a)
}

func (a byGVKNNMsg) Less(i, j int) bool {
if a[i].group != a[j].group {
return a[i].group < a[j].group
}
if a[i].version != a[j].version {
return a[i].version < a[j].version
}
if a[i].kind != a[j].kind {
return a[i].kind < a[j].kind
}
if a[i].namespace != a[j].namespace {
return a[i].namespace < a[j].namespace
}
if a[i].name != a[j].name {
return a[i].name < a[j].name
}
return a[i].msg < a[j].msg
}

func (a byGVKNNMsg) Swap(i, j int) {
a[i], a[j] = a[j], a[i]
}

// nsCache is used for caching namespaces and their labels.
type nsCache struct {
cache map[string]corev1.Namespace
Expand Down Expand Up @@ -863,6 +894,9 @@ func (am *Manager) skipExcludedNamespace(obj *unstructured.Unstructured) (bool,
func (ucloop *updateConstraintLoop) updateConstraintStatus(ctx context.Context, instance *unstructured.Unstructured, auditResults []updateListEntry, timestamp string, totalViolations int64) error {
constraintName := instance.GetName()
ucloop.log.Info("updating constraint status", "constraintName", constraintName)
// sort audit results
sort.Sort(byGVKNNMsg(auditResults))

// create constraint status violations
var statusViolations []interface{}
for i := range auditResults {
Expand Down