forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
node_options.go
212 lines (184 loc) · 5.48 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package node
import (
"errors"
"fmt"
"reflect"
"strings"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
kerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/kubernetes"
"k8s.io/kubernetes/pkg/api/legacyscheme"
kapi "k8s.io/kubernetes/pkg/apis/core"
kclientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
"k8s.io/kubernetes/pkg/kubectl/genericclioptions/resource"
kprinters "k8s.io/kubernetes/pkg/printers"
)
type NodeOptions struct {
DefaultNamespace string
ExternalKubeClient kubernetes.Interface
KubeClient kclientset.Interface
Mapper meta.RESTMapper
Typer runtime.ObjectTyper
Printer func(mapping *meta.RESTMapping, withNamespace bool) (kprinters.ResourcePrinter, error)
CmdPrinter kprinters.ResourcePrinter
CmdPrinterOutput bool
Builder *resource.Builder
NodeNames []string
// Common optional params
Selector string
PodSelector string
genericclioptions.IOStreams
}
func NewNodeOptions(streams genericclioptions.IOStreams) *NodeOptions {
return &NodeOptions{
IOStreams: streams,
}
}
func (n *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
}
kc, err := kclientset.NewForConfig(config)
if err != nil {
return err
}
externalkc, err := kubernetes.NewForConfig(config)
if err != nil {
return err
}
cmdPrinter, err := kcmdutil.PrinterForOptions(kcmdutil.ExtractCmdPrintOptions(c, false))
if err != nil {
return err
}
typer := legacyscheme.Scheme
mapper, err := f.ToRESTMapper()
if err != nil {
return err
}
n.Builder = f.NewBuilder()
n.DefaultNamespace = defaultNamespace
n.ExternalKubeClient = externalkc
n.KubeClient = kc
n.Mapper = mapper
n.Typer = typer
n.NodeNames = []string{}
n.CmdPrinter = cmdPrinter
n.CmdPrinterOutput = false
n.Printer = func(mapping *meta.RESTMapping, withNamespace bool) (kprinters.ResourcePrinter, error) {
return kcmdutil.PrinterForOptions(kcmdutil.ExtractCmdPrintOptions(c, withNamespace))
}
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 := n.Builder.
WithScheme(legacyscheme.Scheme).
ContinueOnError().
NamespaceParam(n.DefaultNamespace).
LabelSelectorParam(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) (kprinters.ResourcePrinter, error) {
gvk, _, err := legacyscheme.Scheme.ObjectKinds(obj)
if err != nil {
return nil, err
}
return n.GetPrinters(gvk[0], false)
}
func (n *NodeOptions) GetPrintersByResource(resource schema.GroupVersionResource, withNamespace bool) (kprinters.ResourcePrinter, error) {
gvks, err := n.Mapper.KindsFor(resource)
if err != nil {
return nil, err
}
return n.GetPrinters(gvks[0], withNamespace)
}
func (n *NodeOptions) GetPrinters(gvk schema.GroupVersionKind, withNamespace bool) (kprinters.ResourcePrinter, error) {
mapping, err := n.Mapper.RESTMapping(gvk.GroupKind(), gvk.Version)
if err != nil {
return nil, err
}
return n.Printer(mapping, withNamespace)
}
func GetPodHostFieldLabel(apiVersion string) string {
switch apiVersion {
default:
return "spec.host"
}
}