Skip to content

Commit 3316070

Browse files
authored
Merge pull request #553 from Andreagit97/improve-debugger-logs
feat(debugger): improve debugger logs
2 parents 6f9ec92 + ddc6546 commit 3316070

2 files changed

Lines changed: 25 additions & 31 deletions

File tree

cmd/debugger/validate.go

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,20 @@ const staticPodAnnotation = "kubernetes.io/config.mirror"
2222

2323
type nodeName = string
2424

25-
func printAgentCache(w io.Writer,
26-
agentCachePerNode map[nodeName][]*agentv1.PodView) {
27-
nodes := make([]string, 0, len(agentCachePerNode))
28-
for n := range agentCachePerNode {
29-
nodes = append(nodes, n)
30-
}
31-
sort.Strings(nodes)
32-
33-
fmt.Fprintln(w, "=== Agent Caches Dump ===")
34-
// For each node
35-
for _, node := range nodes {
36-
cache := agentCachePerNode[node]
37-
fmt.Fprintf(w, "\nNode: %s (%d pods)\n", node, len(cache))
38-
for _, view := range cache {
39-
fmt.Fprintf(
40-
w,
41-
"- pod: (%s/%s)-%s\n",
42-
view.GetMeta().GetNamespace(),
43-
view.GetMeta().GetName(),
44-
view.GetMeta().GetId(),
45-
)
46-
for containerID, containerMeta := range view.GetContainers() {
47-
fmt.Fprintf(w, "-- container: (id=%s) %s\n", containerID, containerMeta.GetName())
48-
}
25+
func printNodeAgentCache(w io.Writer,
26+
nodeName string,
27+
nodeCache []*agentv1.PodView) {
28+
fmt.Fprintf(w, "=== Node '%s' cache dump (%d pods) ===\n", nodeName, len(nodeCache))
29+
for _, view := range nodeCache {
30+
fmt.Fprintf(
31+
w,
32+
"- pod: (%s/%s)-%s\n",
33+
view.GetMeta().GetNamespace(),
34+
view.GetMeta().GetName(),
35+
view.GetMeta().GetId(),
36+
)
37+
for containerID, containerMeta := range view.GetContainers() {
38+
fmt.Fprintf(w, "-- container: (id=%s) %s\n", containerID, containerMeta.GetName())
4939
}
5040
}
5141
fmt.Fprintln(w)
@@ -58,17 +48,21 @@ func validateAgentCache(w io.Writer,
5848
hasDiff := false
5949

6050
if len(expectedCachePerNode) != len(agentCachePerNode) {
61-
fmt.Fprintf(w, "- Mismatch on nodes number: expected %d nodes, got %d\n",
62-
len(expectedCachePerNode), len(agentCachePerNode))
63-
hasDiff = true
51+
// This is possible for example when the runtime enforcer is not installed on some agents.
52+
fmt.Fprintf(
53+
w,
54+
"- Some nodes in the cluster don't have an agent cache. Nodes in the cluster: %d, agent caches: %d.\n",
55+
len(expectedCachePerNode),
56+
len(agentCachePerNode),
57+
)
6458
}
6559

6660
// We are sure the number of nodes is the same
6761
for nodeName, expectedPods := range expectedCachePerNode {
6862
// Skip nodes without an agent cache
6963
actualPods, ok := agentCachePerNode[nodeName]
7064
if !ok {
71-
fmt.Fprintf(w, "- node %s: no agent cache available\n", nodeName)
65+
fmt.Fprintf(w, "- node '%s': no agent cache available\n", nodeName)
7266
continue
7367
}
7468

@@ -84,15 +78,15 @@ func validateAgentCache(w io.Writer,
8478
fmt.Fprintf(w, "- cache on node '%s' is not aligned\n%s\n",
8579
nodeName, diff)
8680
hasDiff = true
81+
// In case of diff, print the agent cache
82+
printNodeAgentCache(w, nodeName, agentCachePerNode[nodeName])
8783
}
8884
}
8985

9086
if !hasDiff {
9187
fmt.Fprintln(w, "caches are aligned")
9288
} else {
9389
fmt.Fprintln(w, "caches are not aligned")
94-
// In case of diff, print the agent cache
95-
printAgentCache(w, agentCachePerNode)
9690
}
9791
}
9892

cmd/debugger/validate_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ func TestValidateAgentCache(t *testing.T) {
254254
nodeName1: {podA},
255255
nodeName2: {podB},
256256
},
257-
expectedOutput: []string{"Mismatch on nodes number", "caches are not aligned"},
257+
expectedOutput: []string{"Some nodes in the cluster don't have an agent cache", "caches are aligned"},
258258
},
259259
{
260260
name: "pod mismatch",

0 commit comments

Comments
 (0)