forked from kubevirt/kubevirt
/
kubecli.go
277 lines (237 loc) · 9.36 KB
/
kubecli.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
* This file is part of the KubeVirt project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2018 Red Hat, Inc.
* Copyright 2018 The Kubernetes Authors.
*
*/
package kubecli
import (
"flag"
"os"
"sync"
secv1 "github.com/openshift/client-go/security/clientset/versioned/typed/security/v1"
"github.com/spf13/pflag"
extclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/client-go/discovery"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
restclient "k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
networkclient "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/client/clientset/versioned"
cdiclient "kubevirt.io/containerized-data-importer/pkg/client/clientset/versioned"
v1 "kubevirt.io/kubevirt/pkg/api/v1"
)
var (
kubeconfig string
master string
)
var virtclient KubevirtClient
var once sync.Once
func init() {
flag.StringVar(&kubeconfig, "kubeconfig", "", "absolute path to the kubeconfig file")
flag.StringVar(&master, "master", "", "master url")
}
func GetKubevirtSubresourceClientFromFlags(master string, kubeconfig string) (KubevirtClient, error) {
config, err := clientcmd.BuildConfigFromFlags(master, kubeconfig)
if err != nil {
return nil, err
}
config.GroupVersion = &v1.SubresourceGroupVersion
config.NegotiatedSerializer = serializer.DirectCodecFactory{CodecFactory: scheme.Codecs}
config.APIPath = "/apis"
config.ContentType = runtime.ContentTypeJSON
restClient, err := rest.RESTClientFor(config)
if err != nil {
return nil, err
}
coreClient, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
}
cdiClient, err := cdiclient.NewForConfig(config)
if err != nil {
return nil, err
}
networkClient, err := networkclient.NewForConfig(config)
if err != nil {
return nil, err
}
extensionsClient, err := extclient.NewForConfig(config)
if err != nil {
return nil, err
}
secClient, err := secv1.NewForConfig(config)
if err != nil {
return nil, err
}
discoveryClient, err := discovery.NewDiscoveryClientForConfig(config)
if err != nil {
return nil, err
}
return &kubevirt{
master,
kubeconfig,
restClient,
config,
cdiClient,
networkClient,
extensionsClient,
secClient,
discoveryClient,
coreClient,
}, nil
}
// DefaultClientConfig creates a clientcmd.ClientConfig with the following hierarchy:
// 1. Use the kubeconfig builder. The number of merges and overrides here gets a little crazy. Stay with me.
// 1. Merge the kubeconfig itself. This is done with the following hierarchy rules:
// 1. CommandLineLocation - this parsed from the command line, so it must be late bound. If you specify this,
// then no other kubeconfig files are merged. This file must exist.
// 2. If $KUBECONFIG is set, then it is treated as a list of files that should be merged.
// 3. HomeDirectoryLocation
// Empty filenames are ignored. Files with non-deserializable content produced errors.
// The first file to set a particular value or map key wins and the value or map key is never changed.
// This means that the first file to set CurrentContext will have its context preserved. It also means
// that if two files specify a "red-user", only values from the first file's red-user are used. Even
// non-conflicting entries from the second file's "red-user" are discarded.
// 2. Determine the context to use based on the first hit in this chain
// 1. command line argument - again, parsed from the command line, so it must be late bound
// 2. CurrentContext from the merged kubeconfig file
// 3. Empty is allowed at this stage
// 3. Determine the cluster info and auth info to use. At this point, we may or may not have a context. They
// are built based on the first hit in this chain. (run it twice, once for auth, once for cluster)
// 1. command line argument
// 2. If context is present, then use the context value
// 3. Empty is allowed
// 4. Determine the actual cluster info to use. At this point, we may or may not have a cluster info. Build
// each piece of the cluster info based on the chain:
// 1. command line argument
// 2. If cluster info is present and a value for the attribute is present, use it.
// 3. If you don't have a server location, bail.
// 5. Auth info is build using the same rules as cluster info, EXCEPT that you can only have one authentication
// technique per auth info. The following conditions result in an error:
// 1. If there are two conflicting techniques specified from the command line, fail.
// 2. If the command line does not specify one, and the auth info has conflicting techniques, fail.
// 3. If the command line specifies one and the auth info specifies another, honor the command line technique.
// 2. Use default values and potentially prompt for auth information
//
// However, if it appears that we're running in a kubernetes cluster
// container environment, then run with the auth info kubernetes mounted for
// us. Specifically:
// The env vars KUBERNETES_SERVICE_HOST and KUBERNETES_SERVICE_PORT are
// set, and the file /var/run/secrets/kubernetes.io/serviceaccount/token
// exists and is not a directory.
// Initially copied from https://github.com/kubernetes/kubernetes/blob/09f321c80bfc9bca63a5530b56d7a1a3ba80ba9b/pkg/kubectl/cmd/util/factory_client_access.go#L174
func DefaultClientConfig(flags *pflag.FlagSet) clientcmd.ClientConfig {
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
// use the standard defaults for this client command
// DEPRECATED: remove and replace with something more accurate
loadingRules.DefaultClientConfig = &clientcmd.DefaultClientConfig
flags.StringVar(&loadingRules.ExplicitPath, "kubeconfig", "", "Path to the kubeconfig file to use for CLI requests.")
overrides := &clientcmd.ConfigOverrides{ClusterDefaults: clientcmd.ClusterDefaults}
flagNames := clientcmd.RecommendedConfigOverrideFlags("")
// short flagnames are disabled by default. These are here for compatibility with existing scripts
flagNames.ClusterOverrideFlags.APIServer.ShortName = "s"
clientcmd.BindOverrideFlags(overrides, flags, flagNames)
clientConfig := clientcmd.NewInteractiveDeferredLoadingClientConfig(loadingRules, overrides, os.Stdin)
return clientConfig
}
// this function is defined as a closure so iut could be overwritten by unit tests
var GetKubevirtClientFromClientConfig = func(cmdConfig clientcmd.ClientConfig) (KubevirtClient, error) {
config, err := cmdConfig.ClientConfig()
if err != nil {
return nil, err
}
return GetKubevirtClientFromRESTConfig(config)
}
func GetKubevirtClientFromRESTConfig(config *rest.Config) (KubevirtClient, error) {
config.GroupVersion = &v1.GroupVersion
config.NegotiatedSerializer = serializer.DirectCodecFactory{CodecFactory: v1.Codecs}
config.APIPath = "/apis"
config.ContentType = runtime.ContentTypeJSON
if config.UserAgent == "" {
config.UserAgent = restclient.DefaultKubernetesUserAgent()
}
restClient, err := rest.RESTClientFor(config)
if err != nil {
return nil, err
}
coreClient, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
}
cdiClient, err := cdiclient.NewForConfig(config)
if err != nil {
return nil, err
}
networkClient, err := networkclient.NewForConfig(config)
if err != nil {
return nil, err
}
extensionsClient, err := extclient.NewForConfig(config)
if err != nil {
return nil, err
}
secClient, err := secv1.NewForConfig(config)
if err != nil {
return nil, err
}
discoveryClient, err := discovery.NewDiscoveryClientForConfig(config)
if err != nil {
return nil, err
}
return &kubevirt{
master,
kubeconfig,
restClient,
config,
cdiClient,
networkClient,
extensionsClient,
secClient,
discoveryClient,
coreClient,
}, nil
}
func GetKubevirtClientFromFlags(master string, kubeconfig string) (KubevirtClient, error) {
config, err := clientcmd.BuildConfigFromFlags(master, kubeconfig)
if err != nil {
return nil, err
}
return GetKubevirtClientFromRESTConfig(config)
}
func GetKubevirtClient() (KubevirtClient, error) {
var err error
once.Do(func() {
virtclient, err = GetKubevirtClientFromFlags(master, kubeconfig)
})
return virtclient, err
}
func GetKubevirtSubresourceClient() (KubevirtClient, error) {
return GetKubevirtSubresourceClientFromFlags(master, kubeconfig)
}
func GetConfig() (*restclient.Config, error) {
return clientcmd.BuildConfigFromFlags(master, kubeconfig)
}
func GetKubevirtClientConfig() (*rest.Config, error) {
config, err := clientcmd.BuildConfigFromFlags(master, kubeconfig)
if err != nil {
return nil, err
}
return config, nil
}