Skip to content

Commit

Permalink
Recursively add metadata JSON fields to the tree
Browse files Browse the repository at this point in the history
Some annotations contain a JSON string. This string may include
nested arrays and maps. This patch updates addPodInfoToTree()
to recursively add these fields to the output tree using pretty print.

Before:

        ├── kubectl.kubernetes.io/last-applied-configuration
        │   ├── metadata: map[annotations:map[] name:counter-pod namespace:default]
        │   ├── spec: map[containers:[map[args:[-c i=0; while true; do echo $i; i=$(expr $i + 1); sleep 1; done] command:[sh] image:busybox:latest name:counter-container]]]

After:

        ├── kubectl.kubernetes.io/last-applied-configuration
        │   ├── apiVersion: v1
        │   ├── kind: Pod
        │   ├── metadata:
        │   │   ├── annotations:
        │   │   ├── name: counter-pod
        │   │   └── namespace: default
        │   └── spec:
        │       └── containers:
        │           └── [0]:
        │               ├── args:
        │               │   ├── [0]: -c
        │               │   └── [1]: i=0; while true; do echo $i; i=$(expr $i + 1); sleep 1; done
        │               ├── command:
        │               │   └── [0]: sh
        │               ├── image: busybox:latest
        │               └── name: counter-container

Signed-off-by: Radostin Stoyanov <[email protected]>
  • Loading branch information
rst0git committed Oct 23, 2024
1 parent c770752 commit 2e305b9
Showing 1 changed file with 36 additions and 3 deletions.
39 changes: 36 additions & 3 deletions internal/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,9 +335,8 @@ func addPodInfoToTree(tree treeprint.Tree, info *checkpointInfo) {
continue
}
localTree := annotationTree.AddBranch(key)
for labelKey := range local {
localTree.AddBranch(fmt.Sprintf("%s: %s", labelKey, local[labelKey]))
}
// Recursively add array/map JSON fields to the tree
addMapToTree(localTree, local)
case "io.kubernetes.cri-o.Volumes":
// We know that some annotations contain a JSON string we can pretty print
var local []mountAnnotations
Expand All @@ -359,3 +358,37 @@ func addPodInfoToTree(tree treeprint.Tree, info *checkpointInfo) {
}
}
}

func addMapToTree(tree treeprint.Tree, data map[string]interface{}) {
for key, value := range data {
switch v := value.(type) {
case map[string]interface{}:
// Recursively add nested maps
subTree := tree.AddBranch(fmt.Sprintf("%s:", key))
addMapToTree(subTree, v)
case []interface{}:
// Handle arrays recursively
arrayTree := tree.AddBranch(fmt.Sprintf("%s: ", key))
addArrayToTree(arrayTree, v)
default:
tree.AddBranch(fmt.Sprintf("%s: %v", key, v))
}
}
}

func addArrayToTree(tree treeprint.Tree, data []interface{}) {
for i, value := range data {
switch v := value.(type) {
case map[string]interface{}:
// Recursively add maps inside arrays
subTree := tree.AddBranch(fmt.Sprintf("[%d]:", i))
addMapToTree(subTree, v)
case []interface{}:
// Recursively add arrays inside arrays
subArrayTree := tree.AddBranch(fmt.Sprintf("[%d]: ", i))
addArrayToTree(subArrayTree, v)
default:
tree.AddBranch(fmt.Sprintf("[%d]: %v", i, v))
}
}
}

0 comments on commit 2e305b9

Please sign in to comment.