Skip to content

Commit 3411adc

Browse files
committed
Fix sorting to keep container/pod intervals together in the chart
1 parent 48f03bc commit 3411adc

File tree

9 files changed

+71
-70
lines changed

9 files changed

+71
-70
lines changed

pkg/monitor/monitorapi/types.go

Lines changed: 43 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -328,41 +328,8 @@ func (i Locator) OldLocator() string {
328328
for k := range i.Keys {
329329
keys = append(keys, string(k))
330330
}
331-
// Some components rely on the ordering of keys in the locator for groupings in interval charts.
332-
// (i.e. namespace pod uid container, which groups container events with their pod.
333-
// Blindly going through the keys results in alphabetical ordering, container comes first, and then we've
334-
// got container events separated from their pod events on the intervals chart.
335-
// This will hopefully eventually go away but for now we need it.
336-
337-
// Ensure these keys appear in this order. Other keys can be mixed in and their order will be preserved at the end,
338-
// but we want these specific combos to come in this order so items appear together in interval charts
339-
// which sort on locator.
340-
// This function is courtesy of chatgpt, but unit tested.
341-
keysToEnsure := []string{"namespace", "pod", "uid", "container"}
342-
343-
// Create a map to store the index of each key in the keysToEnsure slice
344-
orderMap := make(map[string]int, len(keysToEnsure))
345-
for i, key := range keysToEnsure {
346-
orderMap[key] = i
347-
}
348-
349-
// Use a custom sorting function to reorder the keys
350-
customSort(keys, func(i, j int) bool {
351-
idxI, foundI := orderMap[keys[i]]
352-
idxJ, foundJ := orderMap[keys[j]]
353-
354-
// If either key is not in the keysToEnsure, keep their relative order
355-
if !foundI && !foundJ {
356-
return i < j
357-
} else if !foundI {
358-
return false
359-
} else if !foundJ {
360-
return true
361-
}
362331

363-
// Compare the indices of keysToEnsure
364-
return idxI < idxJ
365-
})
332+
keys = sortKeys(keys)
366333

367334
annotations := []string{}
368335
for _, k := range keys {
@@ -379,15 +346,49 @@ func (i Locator) OldLocator() string {
379346
return annotationString
380347
}
381348

382-
// customSort sorts the keys slice using a custom sorting function
383-
func customSort(keys []string, less func(i, j int) bool) {
384-
for i := 0; i < len(keys); i++ {
385-
for j := i + 1; j < len(keys); j++ {
386-
if less(j, i) {
387-
keys[i], keys[j] = keys[j], keys[i]
388-
}
389-
}
349+
// sortKeys ensures that some keys appear in the order we require (least specific to most), so rows with locators
350+
// are grouped together. (i.e. keeping containers within the same pod together, or rows for a specific container)
351+
// Blindly going through the keys results in alphabetical ordering, container comes first, and then we've
352+
// got container events separated from their pod events on the intervals chart.
353+
// This will hopefully eventually go away but for now we need it.
354+
// Courtesy of ChatGPT but unit tested.
355+
func sortKeys(keys []string) []string {
356+
357+
// Ensure these keys appear in this order. Other keys can be mixed in and will appear at the end in alphabetical
358+
// order.
359+
orderedKeys := []string{"namespace", "node", "pod", "uid", "server", "container", "shutdown"}
360+
361+
// Create a map to store the indices of keys in the orderedKeys array.
362+
// This will allow us to efficiently check if a key is in orderedKeys and find its position.
363+
orderedKeyIndices := make(map[string]int)
364+
365+
for i, key := range orderedKeys {
366+
orderedKeyIndices[key] = i
390367
}
368+
369+
// Define a custom sorting function that orders the keys based on the orderedKeys array.
370+
sort.Slice(keys, func(i, j int) bool {
371+
// Get the indices of keys i and j in orderedKeys.
372+
indexI, existsI := orderedKeyIndices[keys[i]]
373+
indexJ, existsJ := orderedKeyIndices[keys[j]]
374+
375+
// If both keys exist in orderedKeys, sort them based on their order.
376+
if existsI && existsJ {
377+
return indexI < indexJ
378+
}
379+
380+
// If only one of the keys exists in orderedKeys, move it to the front.
381+
if existsI {
382+
return true
383+
} else if existsJ {
384+
return false
385+
}
386+
387+
// If neither key is in orderedKeys, sort alphabetically so we have predictable ordering
388+
return keys[i] < keys[j]
389+
})
390+
391+
return keys
391392
}
392393

393394
type IntervalFilter func(i Interval) bool

pkg/monitortests/node/watchpods/podTest/container-life/expected.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
},
2828
{
2929
"level": "Info",
30-
"locator": "container/without-label namespace/e2e-kubectl-3271 pod/without-label uid/e185b70c-ea3e-4600-850a-b2370a729a73",
30+
"locator": "namespace/e2e-kubectl-3271 pod/without-label uid/e185b70c-ea3e-4600-850a-b2370a729a73 container/without-label",
3131
"message": "constructed/pod-lifecycle-constructor reason/ContainerWait missed real \"ContainerWait\"",
3232
"tempSource": "PodState",
3333
"tempStructuredLocator": {
@@ -79,7 +79,7 @@
7979
},
8080
{
8181
"level": "Info",
82-
"locator": "container/without-label namespace/e2e-kubectl-3271 pod/without-label uid/e185b70c-ea3e-4600-850a-b2370a729a73",
82+
"locator": "namespace/e2e-kubectl-3271 pod/without-label uid/e185b70c-ea3e-4600-850a-b2370a729a73 container/without-label",
8383
"message": "constructed/pod-lifecycle-constructor reason/NotReady missed real \"NotReady\"",
8484
"tempSource": "PodState",
8585
"tempStructuredLocator": {
@@ -105,7 +105,7 @@
105105
},
106106
{
107107
"level": "Info",
108-
"locator": "container/without-label namespace/e2e-kubectl-3271 pod/without-label uid/e185b70c-ea3e-4600-850a-b2370a729a73",
108+
"locator": "namespace/e2e-kubectl-3271 pod/without-label uid/e185b70c-ea3e-4600-850a-b2370a729a73 container/without-label",
109109
"message": "cause/ constructed/pod-lifecycle-constructor duration/6.00s reason/ContainerStart",
110110
"tempSource": "PodState",
111111
"tempStructuredLocator": {
@@ -133,7 +133,7 @@
133133
},
134134
{
135135
"level": "Info",
136-
"locator": "container/without-label namespace/e2e-kubectl-3271 pod/without-label uid/e185b70c-ea3e-4600-850a-b2370a729a73",
136+
"locator": "namespace/e2e-kubectl-3271 pod/without-label uid/e185b70c-ea3e-4600-850a-b2370a729a73 container/without-label",
137137
"message": "constructed/pod-lifecycle-constructor reason/Ready",
138138
"tempSource": "PodState",
139139
"tempStructuredLocator": {

pkg/monitortests/node/watchpods/podTest/end-before-begin/expected.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
},
5454
{
5555
"level": "Info",
56-
"locator": "container/pruner namespace/openshift-kube-apiserver pod/revision-pruner-7-ip-10-0-214-214.us-west-1.compute.internal uid/6a81964d-169c-47e0-a986-551429370ae9",
56+
"locator": "namespace/openshift-kube-apiserver pod/revision-pruner-7-ip-10-0-214-214.us-west-1.compute.internal uid/6a81964d-169c-47e0-a986-551429370ae9 container/pruner",
5757
"message": "constructed/pod-lifecycle-constructor reason/NotReady",
5858
"tempSource": "PodState",
5959
"tempStructuredLocator": {

pkg/monitortests/node/watchpods/podTest/installer-pod/expected.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
},
2828
{
2929
"level": "Info",
30-
"locator": "container/installer namespace/openshift-etcd pod/installer-9-ci-op-97t906zm-db044-bwrrn-master-0 uid/6fb10c53-7ed9-4f51-88db-f8a689050f21",
30+
"locator": "namespace/openshift-etcd pod/installer-9-ci-op-97t906zm-db044-bwrrn-master-0 uid/6fb10c53-7ed9-4f51-88db-f8a689050f21 container/installer",
3131
"message": "constructed/pod-lifecycle-constructor reason/ContainerWait missed real \"ContainerWait\"",
3232
"tempSource": "PodState",
3333
"tempStructuredLocator": {
@@ -79,7 +79,7 @@
7979
},
8080
{
8181
"level": "Info",
82-
"locator": "container/installer namespace/openshift-etcd pod/installer-9-ci-op-97t906zm-db044-bwrrn-master-0 uid/6fb10c53-7ed9-4f51-88db-f8a689050f21",
82+
"locator": "namespace/openshift-etcd pod/installer-9-ci-op-97t906zm-db044-bwrrn-master-0 uid/6fb10c53-7ed9-4f51-88db-f8a689050f21 container/installer",
8383
"message": "constructed/pod-lifecycle-constructor reason/NotReady",
8484
"tempSource": "PodState",
8585
"tempStructuredLocator": {
@@ -105,7 +105,7 @@
105105
},
106106
{
107107
"level": "Info",
108-
"locator": "container/installer namespace/openshift-etcd pod/installer-9-ci-op-97t906zm-db044-bwrrn-master-0 uid/6fb10c53-7ed9-4f51-88db-f8a689050f21",
108+
"locator": "namespace/openshift-etcd pod/installer-9-ci-op-97t906zm-db044-bwrrn-master-0 uid/6fb10c53-7ed9-4f51-88db-f8a689050f21 container/installer",
109109
"message": "cause/ constructed/pod-lifecycle-constructor duration/3.00s reason/ContainerStart",
110110
"tempSource": "PodState",
111111
"tempStructuredLocator": {
@@ -133,7 +133,7 @@
133133
},
134134
{
135135
"level": "Info",
136-
"locator": "container/installer namespace/openshift-etcd pod/installer-9-ci-op-97t906zm-db044-bwrrn-master-0 uid/6fb10c53-7ed9-4f51-88db-f8a689050f21",
136+
"locator": "namespace/openshift-etcd pod/installer-9-ci-op-97t906zm-db044-bwrrn-master-0 uid/6fb10c53-7ed9-4f51-88db-f8a689050f21 container/installer",
137137
"message": "constructed/pod-lifecycle-constructor reason/Ready",
138138
"tempSource": "PodState",
139139
"tempStructuredLocator": {
@@ -159,7 +159,7 @@
159159
},
160160
{
161161
"level": "Info",
162-
"locator": "container/installer namespace/openshift-etcd pod/installer-9-ci-op-97t906zm-db044-bwrrn-master-0 uid/6fb10c53-7ed9-4f51-88db-f8a689050f21",
162+
"locator": "namespace/openshift-etcd pod/installer-9-ci-op-97t906zm-db044-bwrrn-master-0 uid/6fb10c53-7ed9-4f51-88db-f8a689050f21 container/installer",
163163
"message": "constructed/pod-lifecycle-constructor reason/NotReady",
164164
"tempSource": "PodState",
165165
"tempStructuredLocator": {

pkg/monitortests/node/watchpods/podTest/long-not-ready/expected.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"items": [
33
{
44
"level": "Info",
5-
"locator": "container/fix-audit-permissions namespace/openshift-apiserver pod/apiserver-5b9785f765-qk9hl uid/d5f66519-ca7a-4808-94a0-b889552d411c",
5+
"locator": "namespace/openshift-apiserver pod/apiserver-5b9785f765-qk9hl uid/d5f66519-ca7a-4808-94a0-b889552d411c container/fix-audit-permissions",
66
"message": "constructed/pod-lifecycle-constructor reason/NotReady missed real \"NotReady\"",
77
"tempSource": "PodState",
88
"tempStructuredLocator": {
@@ -53,7 +53,7 @@
5353
},
5454
{
5555
"level": "Info",
56-
"locator": "container/openshift-apiserver namespace/openshift-apiserver pod/apiserver-5b9785f765-qk9hl uid/d5f66519-ca7a-4808-94a0-b889552d411c",
56+
"locator": "namespace/openshift-apiserver pod/apiserver-5b9785f765-qk9hl uid/d5f66519-ca7a-4808-94a0-b889552d411c container/openshift-apiserver",
5757
"message": "constructed/pod-lifecycle-constructor reason/NotReady missed real \"NotReady\"",
5858
"tempSource": "PodState",
5959
"tempStructuredLocator": {
@@ -79,7 +79,7 @@
7979
},
8080
{
8181
"level": "Info",
82-
"locator": "container/openshift-apiserver-check-endpoints namespace/openshift-apiserver pod/apiserver-5b9785f765-qk9hl uid/d5f66519-ca7a-4808-94a0-b889552d411c",
82+
"locator": "namespace/openshift-apiserver pod/apiserver-5b9785f765-qk9hl uid/d5f66519-ca7a-4808-94a0-b889552d411c container/openshift-apiserver-check-endpoints",
8383
"message": "constructed/pod-lifecycle-constructor reason/NotReady missed real \"NotReady\"",
8484
"tempSource": "PodState",
8585
"tempStructuredLocator": {
@@ -105,7 +105,7 @@
105105
},
106106
{
107107
"level": "Info",
108-
"locator": "container/fix-audit-permissions namespace/openshift-apiserver pod/apiserver-5b9785f765-qk9hl uid/d5f66519-ca7a-4808-94a0-b889552d411c",
108+
"locator": "namespace/openshift-apiserver pod/apiserver-5b9785f765-qk9hl uid/d5f66519-ca7a-4808-94a0-b889552d411c container/fix-audit-permissions",
109109
"message": "constructed/pod-lifecycle-constructor reason/Ready",
110110
"tempSource": "PodState",
111111
"tempStructuredLocator": {
@@ -157,7 +157,7 @@
157157
},
158158
{
159159
"level": "Info",
160-
"locator": "container/openshift-apiserver namespace/openshift-apiserver pod/apiserver-5b9785f765-qk9hl uid/d5f66519-ca7a-4808-94a0-b889552d411c",
160+
"locator": "namespace/openshift-apiserver pod/apiserver-5b9785f765-qk9hl uid/d5f66519-ca7a-4808-94a0-b889552d411c container/openshift-apiserver",
161161
"message": "constructed/pod-lifecycle-constructor reason/Ready",
162162
"tempSource": "PodState",
163163
"tempStructuredLocator": {
@@ -183,7 +183,7 @@
183183
},
184184
{
185185
"level": "Info",
186-
"locator": "container/openshift-apiserver-check-endpoints namespace/openshift-apiserver pod/apiserver-5b9785f765-qk9hl uid/d5f66519-ca7a-4808-94a0-b889552d411c",
186+
"locator": "namespace/openshift-apiserver pod/apiserver-5b9785f765-qk9hl uid/d5f66519-ca7a-4808-94a0-b889552d411c container/openshift-apiserver-check-endpoints",
187187
"message": "constructed/pod-lifecycle-constructor reason/Ready",
188188
"tempSource": "PodState",
189189
"tempStructuredLocator": {

pkg/monitortests/node/watchpods/podTest/run-once-done/expected.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
},
5454
{
5555
"level": "Info",
56-
"locator": "container/installer namespace/openshift-kube-scheduler pod/installer-3-ip-10-0-136-132.us-west-2.compute.internal uid/b7d89367-600a-49a3-95e1-a3ef2c91ecb9",
56+
"locator": "namespace/openshift-kube-scheduler pod/installer-3-ip-10-0-136-132.us-west-2.compute.internal uid/b7d89367-600a-49a3-95e1-a3ef2c91ecb9 container/installer",
5757
"message": "constructed/pod-lifecycle-constructor reason/NotReady",
5858
"tempSource": "PodState",
5959
"tempStructuredLocator": {

pkg/monitortests/node/watchpods/podTest/simple/expected.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
},
2828
{
2929
"level": "Info",
30-
"locator": "container/openshift-apiserver-operator namespace/openshift-apiserver-operator pod/openshift-apiserver-operator-845779f5d-975gr uid/d9a5b0ba-6958-44aa-bc32-03d62944f973",
30+
"locator": "namespace/openshift-apiserver-operator pod/openshift-apiserver-operator-845779f5d-975gr uid/d9a5b0ba-6958-44aa-bc32-03d62944f973 container/openshift-apiserver-operator",
3131
"message": "constructed/pod-lifecycle-constructor reason/NotReady missed real \"NotReady\"",
3232
"tempSource": "PodState",
3333
"tempStructuredLocator": {
@@ -53,7 +53,7 @@
5353
},
5454
{
5555
"level": "Info",
56-
"locator": "container/openshift-apiserver-operator namespace/openshift-apiserver-operator pod/openshift-apiserver-operator-845779f5d-975gr uid/d9a5b0ba-6958-44aa-bc32-03d62944f973",
56+
"locator": "namespace/openshift-apiserver-operator pod/openshift-apiserver-operator-845779f5d-975gr uid/d9a5b0ba-6958-44aa-bc32-03d62944f973 container/openshift-apiserver-operator",
5757
"message": "constructed/pod-lifecycle-constructor reason/ContainerWait missed real \"ContainerWait\"",
5858
"tempSource": "PodState",
5959
"tempStructuredLocator": {
@@ -105,7 +105,7 @@
105105
},
106106
{
107107
"level": "Info",
108-
"locator": "container/openshift-apiserver-operator namespace/openshift-apiserver-operator pod/openshift-apiserver-operator-845779f5d-975gr uid/d9a5b0ba-6958-44aa-bc32-03d62944f973",
108+
"locator": "namespace/openshift-apiserver-operator pod/openshift-apiserver-operator-845779f5d-975gr uid/d9a5b0ba-6958-44aa-bc32-03d62944f973 container/openshift-apiserver-operator",
109109
"message": "constructed/pod-lifecycle-constructor reason/Ready",
110110
"tempSource": "PodState",
111111
"tempStructuredLocator": {
@@ -131,7 +131,7 @@
131131
},
132132
{
133133
"level": "Info",
134-
"locator": "container/openshift-apiserver-operator namespace/openshift-apiserver-operator pod/openshift-apiserver-operator-845779f5d-975gr uid/d9a5b0ba-6958-44aa-bc32-03d62944f973",
134+
"locator": "namespace/openshift-apiserver-operator pod/openshift-apiserver-operator-845779f5d-975gr uid/d9a5b0ba-6958-44aa-bc32-03d62944f973 container/openshift-apiserver-operator",
135135
"message": "constructed/pod-lifecycle-constructor reason/NotReady",
136136
"tempSource": "PodState",
137137
"tempStructuredLocator": {

pkg/monitortests/node/watchpods/podTest/trailing-ready-2/expected.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
},
2828
{
2929
"level": "Info",
30-
"locator": "container/machine-config-operator namespace/openshift-machine-config-operator pod/machine-config-operator-7d5bf78cff-bbbwb uid/27e57fd1-c8f9-4528-8a04-0054dad5d38f",
30+
"locator": "namespace/openshift-machine-config-operator pod/machine-config-operator-7d5bf78cff-bbbwb uid/27e57fd1-c8f9-4528-8a04-0054dad5d38f container/machine-config-operator",
3131
"message": "constructed/pod-lifecycle-constructor reason/NotReady missed real \"NotReady\"",
3232
"tempSource": "PodState",
3333
"tempStructuredLocator": {
@@ -79,7 +79,7 @@
7979
},
8080
{
8181
"level": "Info",
82-
"locator": "container/machine-config-operator namespace/openshift-machine-config-operator pod/machine-config-operator-7d5bf78cff-bbbwb uid/27e57fd1-c8f9-4528-8a04-0054dad5d38f",
82+
"locator": "namespace/openshift-machine-config-operator pod/machine-config-operator-7d5bf78cff-bbbwb uid/27e57fd1-c8f9-4528-8a04-0054dad5d38f container/machine-config-operator",
8383
"message": "constructed/pod-lifecycle-constructor reason/Ready",
8484
"tempSource": "PodState",
8585
"tempStructuredLocator": {

pkg/monitortests/node/watchpods/podTest/trailing-ready/expected.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
},
2828
{
2929
"level": "Info",
30-
"locator": "container/registry-server namespace/openshift-marketplace pod/community-operators-sp6lm uid/efb1885a-1fe1-4f5b-ad41-044e55f806a9",
30+
"locator": "namespace/openshift-marketplace pod/community-operators-sp6lm uid/efb1885a-1fe1-4f5b-ad41-044e55f806a9 container/registry-server",
3131
"message": "constructed/pod-lifecycle-constructor reason/ContainerWait missed real \"ContainerWait\"",
3232
"tempSource": "PodState",
3333
"tempStructuredLocator": {
@@ -79,7 +79,7 @@
7979
},
8080
{
8181
"level": "Info",
82-
"locator": "container/registry-server namespace/openshift-marketplace pod/community-operators-sp6lm uid/efb1885a-1fe1-4f5b-ad41-044e55f806a9",
82+
"locator": "namespace/openshift-marketplace pod/community-operators-sp6lm uid/efb1885a-1fe1-4f5b-ad41-044e55f806a9 container/registry-server",
8383
"message": "constructed/pod-lifecycle-constructor reason/NotReady missed real \"NotReady\"",
8484
"tempSource": "PodState",
8585
"tempStructuredLocator": {
@@ -105,7 +105,7 @@
105105
},
106106
{
107107
"level": "Info",
108-
"locator": "container/registry-server namespace/openshift-marketplace pod/community-operators-sp6lm uid/efb1885a-1fe1-4f5b-ad41-044e55f806a9",
108+
"locator": "namespace/openshift-marketplace pod/community-operators-sp6lm uid/efb1885a-1fe1-4f5b-ad41-044e55f806a9 container/registry-server",
109109
"message": "cause/ constructed/pod-lifecycle-constructor duration/3.00s reason/ContainerStart",
110110
"tempSource": "PodState",
111111
"tempStructuredLocator": {
@@ -159,7 +159,7 @@
159159
},
160160
{
161161
"level": "Info",
162-
"locator": "container/registry-server namespace/openshift-marketplace pod/community-operators-sp6lm uid/efb1885a-1fe1-4f5b-ad41-044e55f806a9",
162+
"locator": "namespace/openshift-marketplace pod/community-operators-sp6lm uid/efb1885a-1fe1-4f5b-ad41-044e55f806a9 container/registry-server",
163163
"message": "constructed/pod-lifecycle-constructor reason/Ready",
164164
"tempSource": "PodState",
165165
"tempStructuredLocator": {
@@ -185,7 +185,7 @@
185185
},
186186
{
187187
"level": "Info",
188-
"locator": "container/registry-server namespace/openshift-marketplace pod/community-operators-sp6lm uid/efb1885a-1fe1-4f5b-ad41-044e55f806a9",
188+
"locator": "namespace/openshift-marketplace pod/community-operators-sp6lm uid/efb1885a-1fe1-4f5b-ad41-044e55f806a9 container/registry-server",
189189
"message": "constructed/pod-lifecycle-constructor reason/NotReady",
190190
"tempSource": "PodState",
191191
"tempStructuredLocator": {

0 commit comments

Comments
 (0)