forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_options.go
198 lines (175 loc) · 5.25 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package node
import (
"errors"
"fmt"
"io"
"reflect"
"strings"
"github.com/spf13/cobra"
kapi "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/meta"
"k8s.io/kubernetes/pkg/api/unversioned"
client "k8s.io/kubernetes/pkg/client/unversioned"
"k8s.io/kubernetes/pkg/kubectl"
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/kubectl/resource"
"k8s.io/kubernetes/pkg/labels"
"k8s.io/kubernetes/pkg/runtime"
kerrors "k8s.io/kubernetes/pkg/util/errors"
"k8s.io/kubernetes/pkg/util/sets"
"github.com/openshift/origin/pkg/cmd/util/clientcmd"
)
type NodeOptions struct {
DefaultNamespace string
Kclient *client.Client
Writer io.Writer
ErrWriter io.Writer
Mapper meta.RESTMapper
Typer runtime.ObjectTyper
RESTClientFactory func(mapping *meta.RESTMapping) (resource.RESTClient, error)
Printer func(mapping *meta.RESTMapping, printOptions *kubectl.PrintOptions) (kubectl.ResourcePrinter, error)
CmdPrinter kubectl.ResourcePrinter
CmdPrinterOutput bool
NodeNames []string
// Common optional params
Selector string
PodSelector string
}
func (n *NodeOptions) Complete(f *clientcmd.Factory, c *cobra.Command, args []string, out, errout io.Writer) error {
defaultNamespace, _, err := f.DefaultNamespace()
if err != nil {
return err
}
_, kc, err := f.Clients()
if err != nil {
return err
}
cmdPrinter, output, err := kcmdutil.PrinterForCommand(c)
if err != nil {
return err
}
mapper, typer := f.Object(false)
n.DefaultNamespace = defaultNamespace
n.Kclient = kc
n.Writer = out
n.ErrWriter = errout
n.Mapper = mapper
n.Typer = typer
n.RESTClientFactory = f.Factory.ClientForMapping
n.Printer = f.Printer
n.NodeNames = []string{}
n.CmdPrinter = cmdPrinter
n.CmdPrinterOutput = false
if output {
n.CmdPrinterOutput = true
}
if len(args) != 0 {
n.NodeNames = append(n.NodeNames, args...)
}
return nil
}
func (n *NodeOptions) Validate(checkNodeSelector bool) error {
errList := []error{}
if checkNodeSelector {
if len(n.Selector) > 0 {
if _, err := labels.Parse(n.Selector); err != nil {
errList = append(errList, errors.New("--selector=<node_selector> must be a valid label selector"))
}
}
if len(n.NodeNames) != 0 {
errList = append(errList, errors.New("either specify --selector=<node_selector> or nodes but not both"))
}
} else if len(n.NodeNames) == 0 {
errList = append(errList, errors.New("must provide --selector=<node_selector> or nodes"))
}
if len(n.PodSelector) > 0 {
if _, err := labels.Parse(n.PodSelector); err != nil {
errList = append(errList, errors.New("--pod-selector=<pod_selector> must be a valid label selector"))
}
}
return kerrors.NewAggregate(errList)
}
func (n *NodeOptions) GetNodes() ([]*kapi.Node, error) {
nameArgs := []string{"nodes"}
if len(n.NodeNames) != 0 {
nameArgs = append(nameArgs, n.NodeNames...)
}
r := resource.NewBuilder(n.Mapper, n.Typer, resource.ClientMapperFunc(n.RESTClientFactory), kapi.Codecs.UniversalDecoder()).
ContinueOnError().
NamespaceParam(n.DefaultNamespace).
SelectorParam(n.Selector).
ResourceTypeOrNameArgs(true, nameArgs...).
Flatten().
Do()
if r.Err() != nil {
return nil, r.Err()
}
errList := []error{}
nodeList := []*kapi.Node{}
_ = r.Visit(func(info *resource.Info, err error) error {
if err != nil {
return err
}
node, ok := info.Object.(*kapi.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(n.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 (n *NodeOptions) GetPrintersByObject(obj runtime.Object) (kubectl.ResourcePrinter, kubectl.ResourcePrinter, error) {
gvk, _, err := kapi.Scheme.ObjectKinds(obj)
if err != nil {
return nil, nil, err
}
return n.GetPrinters(gvk[0])
}
func (n *NodeOptions) GetPrintersByResource(resource unversioned.GroupVersionResource) (kubectl.ResourcePrinter, kubectl.ResourcePrinter, error) {
gvks, err := n.Mapper.KindsFor(resource)
if err != nil {
return nil, nil, err
}
return n.GetPrinters(gvks[0])
}
func (n *NodeOptions) GetPrinters(gvk unversioned.GroupVersionKind) (kubectl.ResourcePrinter, kubectl.ResourcePrinter, error) {
mapping, err := n.Mapper.RESTMapping(gvk.GroupKind(), gvk.Version)
if err != nil {
return nil, nil, err
}
printerWithHeaders, err := n.Printer(mapping, nil)
if err != nil {
return nil, nil, err
}
printerNoHeaders, err := n.Printer(mapping, &kubectl.PrintOptions{NoHeaders: true})
if err != nil {
return nil, nil, err
}
return printerWithHeaders, printerNoHeaders, nil
}
func GetPodHostFieldLabel(apiVersion string) string {
switch apiVersion {
default:
return "spec.host"
}
}