forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_options.go
192 lines (169 loc) · 5.3 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
package node
import (
"errors"
"fmt"
"io"
"reflect"
"strings"
"github.com/spf13/cobra"
kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/meta"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl"
kcmdutil "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/resource"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
kerrors "github.com/GoogleCloudPlatform/kubernetes/pkg/util/errors"
"github.com/openshift/origin/pkg/cmd/util/clientcmd"
)
type NodeOptions struct {
DefaultNamespace string
Kclient *client.Client
Writer io.Writer
Mapper meta.RESTMapper
Typer runtime.ObjectTyper
RESTClientFactory func(mapping *meta.RESTMapping) (resource.RESTClient, error)
Printer func(mapping *meta.RESTMapping, noHeaders, withNamespace bool, columnLabels []string) (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 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()
n.DefaultNamespace = defaultNamespace
n.Kclient = kc
n.Writer = out
n.Mapper = mapper
n.Typer = typer
n.RESTClientFactory = f.Factory.RESTClient
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)).
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) error {
node, ok := info.Object.(*kapi.Node)
if !ok {
err := fmt.Errorf("cannot convert input to Node: ", 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 := util.NewStringSet(n.NodeNames...)
foundNodeNames := util.StringSet{}
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) {
version, kind, err := kapi.Scheme.ObjectVersionAndKind(obj)
if err != nil {
return nil, nil, err
}
return n.GetPrinters(kind, version)
}
func (n *NodeOptions) GetPrintersByResource(resource string) (kubectl.ResourcePrinter, kubectl.ResourcePrinter, error) {
version, kind, err := n.Mapper.VersionAndKindForResource(resource)
if err != nil {
return nil, nil, err
}
return n.GetPrinters(kind, version)
}
func (n *NodeOptions) GetPrinters(kind, version string) (kubectl.ResourcePrinter, kubectl.ResourcePrinter, error) {
mapping, err := n.Mapper.RESTMapping(kind, version)
if err != nil {
return nil, nil, err
}
printerWithHeaders, err := n.Printer(mapping, false, true, []string{})
if err != nil {
return nil, nil, err
}
printerNoHeaders, err := n.Printer(mapping, true, true, []string{})
if err != nil {
return nil, nil, err
}
return printerWithHeaders, printerNoHeaders, nil
}
func GetPodHostFieldLabel(apiVersion string) string {
switch apiVersion {
default:
return "spec.host"
}
}