forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
listpods.go
76 lines (65 loc) · 1.98 KB
/
listpods.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package node
import (
"fmt"
"github.com/spf13/cobra"
kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/fields"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl"
kcmdutil "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
kerrors "github.com/GoogleCloudPlatform/kubernetes/pkg/util/errors"
)
type ListPodsOptions struct {
Options *NodeOptions
}
func (l *ListPodsOptions) AddFlags(cmd *cobra.Command) {
kcmdutil.AddPrinterFlags(cmd)
}
func (l *ListPodsOptions) Run() error {
nodes, err := l.Options.GetNodes()
if err != nil {
return err
}
errList := []error{}
for _, node := range nodes {
err := l.RunListPods(node)
if err != nil {
// Don't bail out if one node fails
errList = append(errList, err)
}
}
return kerrors.NewAggregate(errList)
}
func (l *ListPodsOptions) RunListPods(node *kapi.Node) error {
labelSelector, err := labels.Parse(l.Options.PodSelector)
if err != nil {
return err
}
fieldSelector := fields.Set{GetPodHostFieldLabel(node.TypeMeta.APIVersion): node.ObjectMeta.Name}.AsSelector()
// Filter all pods that satisfies pod label selector and belongs to the given node
pods, err := l.Options.Kclient.Pods(kapi.NamespaceAll).List(labelSelector, fieldSelector)
if err != nil {
return err
}
var printerWithHeaders, printerNoHeaders kubectl.ResourcePrinter
if l.Options.CmdPrinterOutput {
printerWithHeaders = l.Options.CmdPrinter
printerNoHeaders = l.Options.CmdPrinter
} else {
printerWithHeaders, printerNoHeaders, err = l.Options.GetPrintersByResource("pod")
if err != nil {
return err
}
}
firstPod := true
for _, pod := range pods.Items {
if firstPod {
fmt.Fprintln(l.Options.Writer, "\nListing matched pods on node: ", node.ObjectMeta.Name, "\n")
printerWithHeaders.PrintObj(&pod, l.Options.Writer)
firstPod = false
} else {
printerNoHeaders.PrintObj(&pod, l.Options.Writer)
}
}
return err
}