forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
node_options.go
175 lines (149 loc) · 4.29 KB
/
node_options.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package node
import (
"errors"
"fmt"
"reflect"
"strings"
"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
kerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/kubernetes"
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
"k8s.io/kubernetes/pkg/kubectl/genericclioptions/printers"
"k8s.io/kubernetes/pkg/kubectl/genericclioptions/resource"
"k8s.io/kubernetes/pkg/kubectl/scheme"
)
const (
tabWriterMinWidth = 0
tabWriterWidth = 7
tabWriterPadding = 3
tabWriterPadChar = ' '
tabWriterFlags = 0
)
type NodeOptions struct {
PrintFlags *genericclioptions.PrintFlags
DefaultNamespace string
KubeClient kubernetes.Interface
ToPrinter func(string) (printers.ResourcePrinter, error)
Builder *resource.Builder
NoHeaders bool
NodeNames []string
// Common optional params
Selector string
PodSelector string
CheckNodeSelector bool
genericclioptions.IOStreams
}
func NewNodeOptions(streams genericclioptions.IOStreams) *NodeOptions {
return &NodeOptions{
PrintFlags: genericclioptions.NewPrintFlags("").WithTypeSetter(scheme.Scheme),
IOStreams: streams,
}
}
func (o *NodeOptions) Complete(f kcmdutil.Factory, c *cobra.Command, args []string) error {
defaultNamespace, _, err := f.ToRawKubeConfigLoader().Namespace()
if err != nil {
return err
}
config, err := f.ToRESTConfig()
if err != nil {
return err
}
o.KubeClient, err = kubernetes.NewForConfig(config)
if err != nil {
return err
}
o.Builder = f.NewBuilder()
o.DefaultNamespace = defaultNamespace
o.NodeNames = []string{}
o.CheckNodeSelector = c.Flag("selector").Changed
o.NoHeaders = kcmdutil.GetFlagBool(c, "no-headers")
o.ToPrinter = func(operation string) (printers.ResourcePrinter, error) {
o.PrintFlags.NamePrintFlags.Operation = operation
return o.PrintFlags.ToPrinter()
}
if len(args) > 0 {
o.NodeNames = append(o.NodeNames, args...)
}
return nil
}
func (o *NodeOptions) Validate() error {
errList := []error{}
if o.CheckNodeSelector {
if len(o.Selector) > 0 {
if _, err := labels.Parse(o.Selector); err != nil {
errList = append(errList, errors.New("--selector=<node_selector> must be a valid label selector"))
}
}
if len(o.NodeNames) != 0 {
errList = append(errList, errors.New("either specify --selector=<node_selector> or nodes but not both"))
}
} else if len(o.NodeNames) == 0 {
errList = append(errList, errors.New("must provide --selector=<node_selector> or nodes"))
}
if len(o.PodSelector) > 0 {
if _, err := labels.Parse(o.PodSelector); err != nil {
errList = append(errList, errors.New("--pod-selector=<pod_selector> must be a valid label selector"))
}
}
return kerrors.NewAggregate(errList)
}
func (o *NodeOptions) GetNodes() ([]*corev1.Node, error) {
nameArgs := []string{"nodes"}
if len(o.NodeNames) != 0 {
nameArgs = append(nameArgs, o.NodeNames...)
}
r := o.Builder.
WithScheme(scheme.Scheme, scheme.Scheme.PrioritizedVersionsAllGroups()...).
ContinueOnError().
NamespaceParam(o.DefaultNamespace).
LabelSelectorParam(o.Selector).
ResourceTypeOrNameArgs(true, nameArgs...).
Flatten().
Do()
if r.Err() != nil {
return nil, r.Err()
}
errList := []error{}
nodeList := []*corev1.Node{}
_ = r.Visit(func(info *resource.Info, err error) error {
if err != nil {
return err
}
node, ok := info.Object.(*corev1.Node)
if !ok {
err = fmt.Errorf("cannot convert input to Node: %v", reflect.TypeOf(info.Object))
errList = append(errList, err)
// Don't bail out if one node fails
return nil
}
nodeList = append(nodeList, node)
return nil
})
if len(errList) != 0 {
return nodeList, kerrors.NewAggregate(errList)
}
if len(nodeList) == 0 {
return nodeList, fmt.Errorf("No nodes found")
} else {
givenNodeNames := sets.NewString(o.NodeNames...)
foundNodeNames := sets.String{}
for _, node := range nodeList {
foundNodeNames.Insert(node.ObjectMeta.Name)
}
skippedNodeNames := givenNodeNames.Difference(foundNodeNames)
if skippedNodeNames.Len() > 0 {
return nodeList, fmt.Errorf("Nodes %v not found", strings.Join(skippedNodeNames.List(), ", "))
}
}
return nodeList, nil
}
func GetPodHostFieldLabel(apiVersion string) string {
switch apiVersion {
default:
return "spec.host"
}
}