From 5f14c3e0d6935c7801e82bcdaaa34e43f9739eac Mon Sep 17 00:00:00 2001 From: Aditya Thebe Date: Wed, 6 Nov 2024 10:31:25 +0545 Subject: [PATCH] feat: sort .status.conditions --- scrapers/kubernetes/kubernetes.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/scrapers/kubernetes/kubernetes.go b/scrapers/kubernetes/kubernetes.go index 154144be..b1385c26 100644 --- a/scrapers/kubernetes/kubernetes.go +++ b/scrapers/kubernetes/kubernetes.go @@ -3,6 +3,7 @@ package kubernetes import ( "encoding/json" "fmt" + "sort" "strconv" "strings" "time" @@ -595,6 +596,19 @@ func cleanKubernetesObject(obj map[string]any) (map[string]any, error) { o.Delete("status", "observedGeneration") o.Delete("status", "lastTransitionTime") + if cData := o.S("status", "conditions").Data(); cData != nil { + if conditions, ok := cData.([]any); ok { + // sort conditions to prevent unnecessary diffs + sort.Slice(conditions, func(i, j int) bool { + return gabs.Wrap(conditions[i]).S("type").Data().(string) < gabs.Wrap(conditions[j]).S("type").Data().(string) + }) + + if _, err := o.Set(conditions, "status", "conditions"); err != nil { + return nil, fmt.Errorf("failed to set sorted status.conditions: %w", err) + } + } + } + c, _ := o.ArrayCount("status", "conditions") for i := 0; i < c; i += 1 { o.Delete("status", "conditions", strconv.Itoa(i), "lastTransitionTime")