Skip to content

Commit c100b12

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 c100b12

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-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)

test/cmd/admin.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ os::cmd::expect_success_and_not_text 'oadm manage-node --selector= --schedulable
7878
os::cmd::expect_success_and_not_text 'oc get node -o yaml' 'unschedulable: true'
7979
os::cmd::expect_success_and_text 'oadm manage-node --selector= --schedulable=false' 'SchedulingDisabled'
8080
os::cmd::expect_success_and_text 'oc get node -o yaml' 'unschedulable: true'
81+
# ensure correct serialization of podList output
82+
os::cmd::expect_success_and_text "oadm manage-node --list-pods --selector= -o jsonpath='{ .kind }'" 'List'
8183
echo "manage-node: ok"
8284
os::test::junit::declare_suite_end
8385

0 commit comments

Comments
 (0)