Skip to content

Commit f0d52c1

Browse files
committed
print typed podlist for correct serialization
Pass a typed list of pods to the printer in order to have correctly serialized output.
1 parent 8833a3a commit f0d52c1

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

pkg/cmd/admin/node/listpods.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package node
22

33
import (
4+
"encoding/json"
45
"fmt"
56

67
"github.com/spf13/cobra"
@@ -11,6 +12,7 @@ import (
1112
"k8s.io/apimachinery/pkg/runtime/schema"
1213
kerrors "k8s.io/apimachinery/pkg/util/errors"
1314
kapi "k8s.io/kubernetes/pkg/api"
15+
kapiv1 "k8s.io/kubernetes/pkg/api/v1"
1416
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
1517
kprinters "k8s.io/kubernetes/pkg/printers"
1618
)
@@ -90,7 +92,12 @@ func (l *ListPodsOptions) runListPods(node *kapi.Node, printer kprinters.Resourc
9092
// objects for every node, into a single list. This allows output containing multiple nodes to be
9193
// printed to a single writer, and be easily parsed as a single data format.
9294
func (l *ListPodsOptions) handleRESTOutput(nodes []*kapi.Node, printer kprinters.ResourcePrinter) []error {
93-
unifiedPodList := &kapi.PodList{}
95+
unifiedPodList := &kapiv1.PodList{
96+
TypeMeta: metav1.TypeMeta{
97+
Kind: "List",
98+
APIVersion: "v1",
99+
},
100+
}
94101

95102
errList := []error{}
96103
for _, node := range nodes {
@@ -107,7 +114,23 @@ func (l *ListPodsOptions) handleRESTOutput(nodes []*kapi.Node, printer kprinters
107114
continue
108115
}
109116

110-
unifiedPodList.Items = append(unifiedPodList.Items, pods.Items...)
117+
unifiedPodList.ListMeta.SelfLink = pods.ListMeta.SelfLink
118+
119+
for _, pod := range pods.Items {
120+
typedPod := &kapiv1.Pod{}
121+
b, err := json.Marshal(pod)
122+
if err != nil {
123+
errList = append(errList, err)
124+
continue
125+
}
126+
err = json.Unmarshal(b, typedPod)
127+
if err != nil {
128+
errList = append(errList, err)
129+
continue
130+
}
131+
132+
unifiedPodList.Items = append(unifiedPodList.Items, *typedPod)
133+
}
111134
}
112135

113136
printer.PrintObj(unifiedPodList, l.Options.Writer)

0 commit comments

Comments
 (0)