forked from argoproj/argo-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kube.go
516 lines (482 loc) · 15.7 KB
/
kube.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
// Package kube provides helper utilities common for kubernetes
package kube
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
"sync"
"time"
"github.com/argoproj/argo-cd/util/cache"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/api/equality"
apierr "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/discovery"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
)
const (
listVerb = "list"
deleteVerb = "delete"
deleteCollectionVerb = "deletecollection"
watchVerb = "watch"
)
const (
ServiceKind = "Service"
EndpointsKind = "Endpoints"
DeploymentKind = "Deployment"
)
const (
apiResourceCacheDuration = 10 * time.Minute
)
var (
// location to use for generating temporary files, such as the kubeconfig needed by kubectl
kubectlTempDir string
// apiResourceCache is a in-memory cache of api resources supported by a k8s server
apiResourceCache = cache.NewInMemoryCache(apiResourceCacheDuration)
)
func init() {
fileInfo, err := os.Stat("/dev/shm")
if err == nil && fileInfo.IsDir() {
kubectlTempDir = "/dev/shm"
}
}
// TestConfig tests to make sure the REST config is usable
func TestConfig(config *rest.Config) error {
kubeclientset, err := kubernetes.NewForConfig(config)
if err != nil {
return fmt.Errorf("REST config invalid: %s", err)
}
_, err = kubeclientset.ServerVersion()
if err != nil {
return fmt.Errorf("REST config invalid: %s", err)
}
return nil
}
// ToUnstructured converts a concrete K8s API type to a un unstructured object
func ToUnstructured(obj interface{}) (*unstructured.Unstructured, error) {
uObj, err := runtime.NewTestUnstructuredConverter(equality.Semantic).ToUnstructured(obj)
if err != nil {
return nil, err
}
return &unstructured.Unstructured{Object: uObj}, nil
}
// MustToUnstructured converts a concrete K8s API type to a un unstructured object and panics if not successful
func MustToUnstructured(obj interface{}) *unstructured.Unstructured {
uObj, err := ToUnstructured(obj)
if err != nil {
panic(err)
}
return uObj
}
// GetCachedServerResources discovers API resources supported by a Kube API server.
// Caches the results for apiResourceCacheDuration (per host)
func GetCachedServerResources(host string, disco discovery.DiscoveryInterface) ([]*metav1.APIResourceList, error) {
var resList []*metav1.APIResourceList
cacheKey := fmt.Sprintf("apires|%s", host)
err := apiResourceCache.Get(cacheKey, &resList)
if err == nil {
log.Debugf("cache hit: %s", cacheKey)
return resList, nil
}
if err == cache.ErrCacheMiss {
log.Infof("cache miss: %s", cacheKey)
} else {
log.Warnf("cache error %s: %v", cacheKey, err)
}
resList, err = disco.ServerResources()
if err != nil {
return nil, errors.WithStack(err)
}
err = apiResourceCache.Set(&cache.Item{
Key: cacheKey,
Object: resList,
})
if err != nil {
log.Warnf("Failed to cache %s: %v", cacheKey, err)
}
return resList, nil
}
// GetLiveResource returns the corresponding live resource from a unstructured object
func GetLiveResource(dclient dynamic.Interface, obj *unstructured.Unstructured, apiResource *metav1.APIResource, namespace string) (*unstructured.Unstructured, error) {
resourceName := obj.GetName()
if resourceName == "" {
return nil, fmt.Errorf("resource was supplied without a name")
}
reIf := dclient.Resource(apiResource, namespace)
liveObj, err := reIf.Get(resourceName, metav1.GetOptions{})
if err != nil {
if apierr.IsNotFound(err) {
log.Infof("No live counterpart to %s/%s/%s/%s in namespace: '%s'", apiResource.Group, apiResource.Version, apiResource.Name, resourceName, namespace)
return nil, nil
}
return nil, errors.WithStack(err)
}
return liveObj, nil
}
func WatchResourcesWithLabel(ctx context.Context, config *rest.Config, namespace string, labelName string) (chan watch.Event, error) {
log.Infof("Start watching for resources changes with label %s in cluster %s", labelName, config.Host)
dynClientPool := dynamic.NewDynamicClientPool(config)
disco, err := discovery.NewDiscoveryClientForConfig(config)
if err != nil {
return nil, err
}
serverResources, err := GetCachedServerResources(config.Host, disco)
if err != nil {
return nil, err
}
resources := make([]dynamic.ResourceInterface, 0)
for _, apiResourcesList := range serverResources {
for i := range apiResourcesList.APIResources {
apiResource := apiResourcesList.APIResources[i]
watchSupported := false
for _, verb := range apiResource.Verbs {
if verb == watchVerb {
watchSupported = true
break
}
}
if watchSupported {
dclient, err := dynClientPool.ClientForGroupVersionKind(schema.FromAPIVersionAndKind(apiResourcesList.GroupVersion, apiResource.Kind))
if err != nil {
return nil, err
}
resources = append(resources, dclient.Resource(&apiResource, namespace))
}
}
}
ch := make(chan watch.Event)
go func() {
var wg sync.WaitGroup
wg.Add(len(resources))
for i := 0; i < len(resources); i++ {
resource := resources[i]
go func() {
defer wg.Done()
watch, err := resource.Watch(metav1.ListOptions{LabelSelector: labelName})
go func() {
select {
case <-ctx.Done():
watch.Stop()
}
}()
if err == nil {
for event := range watch.ResultChan() {
ch <- event
}
}
}()
}
wg.Wait()
close(ch)
log.Infof("Stop watching for resources changes with label %s in cluster %s", labelName, config.ServerName)
}()
return ch, nil
}
// GetResourcesWithLabel returns all kubernetes resources with specified label
func GetResourcesWithLabel(config *rest.Config, namespace string, labelName string, labelValue string) ([]*unstructured.Unstructured, error) {
dynClientPool := dynamic.NewDynamicClientPool(config)
disco, err := discovery.NewDiscoveryClientForConfig(config)
if err != nil {
return nil, err
}
resources, err := GetCachedServerResources(config.Host, disco)
if err != nil {
return nil, err
}
var resourceInterfaces []dynamic.ResourceInterface
for _, apiResourcesList := range resources {
for i := range apiResourcesList.APIResources {
apiResource := apiResourcesList.APIResources[i]
listSupported := false
for _, verb := range apiResource.Verbs {
if verb == listVerb {
listSupported = true
break
}
}
if listSupported {
dclient, err := dynClientPool.ClientForGroupVersionKind(schema.FromAPIVersionAndKind(apiResourcesList.GroupVersion, apiResource.Kind))
if err != nil {
return nil, err
}
resourceInterfaces = append(resourceInterfaces, dclient.Resource(&apiResource, namespace))
}
}
}
var asyncErr error
var result []*unstructured.Unstructured
var wg sync.WaitGroup
wg.Add(len(resourceInterfaces))
for i := range resourceInterfaces {
client := resourceInterfaces[i]
go func() {
defer wg.Done()
list, err := client.List(metav1.ListOptions{
LabelSelector: fmt.Sprintf("%s=%s", labelName, labelValue),
})
if err != nil {
asyncErr = err
return
}
// apply client side filtering since not every kubernetes API supports label filtering
for i := range list.(*unstructured.UnstructuredList).Items {
item := list.(*unstructured.UnstructuredList).Items[i]
labels := item.GetLabels()
if labels != nil {
if value, ok := labels[labelName]; ok && value == labelValue {
result = append(result, &item)
}
}
}
}()
}
wg.Wait()
return result, asyncErr
}
// DeleteResourceWithLabel delete all resources which match to specified label selector
func DeleteResourceWithLabel(config *rest.Config, namespace string, labelName string, labelValue string) error {
dynClientPool := dynamic.NewDynamicClientPool(config)
disco, err := discovery.NewDiscoveryClientForConfig(config)
if err != nil {
return err
}
resources, err := GetCachedServerResources(config.Host, disco)
if err != nil {
return err
}
var resourceInterfaces []struct {
dynamic.ResourceInterface
bool
}
for _, apiResourcesList := range resources {
for i := range apiResourcesList.APIResources {
apiResource := apiResourcesList.APIResources[i]
deleteCollectionSupported := false
deleteSupported := false
for _, verb := range apiResource.Verbs {
if verb == deleteCollectionVerb {
deleteCollectionSupported = true
} else if verb == deleteVerb {
deleteSupported = true
}
}
dclient, err := dynClientPool.ClientForGroupVersionKind(schema.FromAPIVersionAndKind(apiResourcesList.GroupVersion, apiResource.Kind))
if err != nil {
return err
}
if deleteCollectionSupported || deleteSupported {
resourceInterfaces = append(resourceInterfaces, struct {
dynamic.ResourceInterface
bool
}{dclient.Resource(&apiResource, namespace), deleteCollectionSupported})
}
}
}
var asyncErr error
propagationPolicy := metav1.DeletePropagationForeground
var wg sync.WaitGroup
wg.Add(len(resourceInterfaces))
for i := range resourceInterfaces {
client := resourceInterfaces[i].ResourceInterface
deleteCollectionSupported := resourceInterfaces[i].bool
go func() {
defer wg.Done()
if deleteCollectionSupported {
err = client.DeleteCollection(&metav1.DeleteOptions{
PropagationPolicy: &propagationPolicy,
}, metav1.ListOptions{LabelSelector: fmt.Sprintf("%s=%s", labelName, labelValue)})
if err != nil && !apierr.IsNotFound(err) {
asyncErr = err
}
} else {
items, err := client.List(metav1.ListOptions{LabelSelector: fmt.Sprintf("%s=%s", labelName, labelValue)})
if err != nil {
asyncErr = err
return
}
for _, item := range items.(*unstructured.UnstructuredList).Items {
// apply client side filtering since not every kubernetes API supports label filtering
labels := item.GetLabels()
if labels != nil {
if value, ok := labels[labelName]; ok && value == labelValue {
err = client.Delete(item.GetName(), &metav1.DeleteOptions{
PropagationPolicy: &propagationPolicy,
})
if err != nil && !apierr.IsNotFound(err) {
asyncErr = err
return
}
}
}
}
}
}()
}
wg.Wait()
return asyncErr
}
// See: https://github.com/ksonnet/ksonnet/blob/master/utils/client.go
func ServerResourceForGroupVersionKind(disco discovery.DiscoveryInterface, gvk schema.GroupVersionKind) (*metav1.APIResource, error) {
resources, err := disco.ServerResourcesForGroupVersion(gvk.GroupVersion().String())
if err != nil {
return nil, err
}
for _, r := range resources.APIResources {
if r.Kind == gvk.Kind {
log.Debugf("Chose API '%s' for %s", r.Name, gvk)
return &r, nil
}
}
return nil, fmt.Errorf("Server is unable to handle %s", gvk)
}
type listResult struct {
Items []*unstructured.Unstructured `json:"items"`
}
// ListResources returns a list of resources of a particular API type using the dynamic client
func ListResources(dclient dynamic.Interface, apiResource metav1.APIResource, namespace string, listOpts metav1.ListOptions) ([]*unstructured.Unstructured, error) {
reIf := dclient.Resource(&apiResource, namespace)
liveObjs, err := reIf.List(listOpts)
if err != nil {
return nil, errors.WithStack(err)
}
liveObjsBytes, err := json.Marshal(liveObjs)
if err != nil {
return nil, errors.WithStack(err)
}
var objList listResult
err = json.Unmarshal(liveObjsBytes, &objList)
if err != nil {
return nil, errors.WithStack(err)
}
return objList.Items, nil
}
// deleteFile is best effort deletion of a file
func deleteFile(path string) {
if _, err := os.Stat(path); os.IsNotExist(err) {
return
}
_ = os.Remove(path)
}
// DeleteResource deletes resource
func DeleteResource(config *rest.Config, obj *unstructured.Unstructured, namespace string) error {
dynClientPool := dynamic.NewDynamicClientPool(config)
disco, err := discovery.NewDiscoveryClientForConfig(config)
if err != nil {
return err
}
gvk := obj.GroupVersionKind()
dclient, err := dynClientPool.ClientForGroupVersionKind(gvk)
if err != nil {
return err
}
apiResource, err := ServerResourceForGroupVersionKind(disco, gvk)
if err != nil {
return err
}
reIf := dclient.Resource(apiResource, namespace)
propagationPolicy := metav1.DeletePropagationForeground
return reIf.Delete(obj.GetName(), &metav1.DeleteOptions{PropagationPolicy: &propagationPolicy})
}
// ApplyResource performs an apply of a unstructured resource
func ApplyResource(config *rest.Config, obj *unstructured.Unstructured, namespace string, dryRun bool) (string, error) {
log.Infof("Applying resource %s/%s in cluster: %s, namespace: %s", obj.GetKind(), obj.GetName(), config.Host, namespace)
f, err := ioutil.TempFile(kubectlTempDir, "")
if err != nil {
return "", fmt.Errorf("Failed to generate temp file for kubeconfig: %v", err)
}
_ = f.Close()
err = WriteKubeConfig(config, namespace, f.Name())
if err != nil {
return "", fmt.Errorf("Failed to write kubeconfig: %v", err)
}
defer deleteFile(f.Name())
manifestBytes, err := json.Marshal(obj)
if err != nil {
return "", err
}
applyArgs := []string{"--kubeconfig", f.Name(), "-n", namespace, "apply", "-f", "-"}
if dryRun {
applyArgs = append(applyArgs, "--dry-run")
}
cmd := exec.Command("kubectl", applyArgs...)
log.Info(cmd.Args)
cmd.Stdin = bytes.NewReader(manifestBytes)
out, err := cmd.Output()
if err != nil {
if exErr, ok := err.(*exec.ExitError); ok {
// this makes the output a little better to read
errMsg := strings.Replace(strings.TrimSpace(string(exErr.Stderr)), ": error when creating \"STDIN\"", "", -1)
return "", errors.New(errMsg)
}
return "", err
}
return strings.TrimSpace(string(out)), nil
}
// WriteKubeConfig takes a rest.Config and writes it as a kubeconfig at the specified path
func WriteKubeConfig(restConfig *rest.Config, namespace, filename string) error {
var kubeConfig = clientcmdapi.Config{
CurrentContext: restConfig.Host,
Contexts: map[string]*clientcmdapi.Context{
restConfig.Host: {
Cluster: restConfig.Host,
AuthInfo: restConfig.Host,
Namespace: namespace,
},
},
Clusters: map[string]*clientcmdapi.Cluster{
restConfig.Host: {
Server: restConfig.Host,
},
},
AuthInfos: map[string]*clientcmdapi.AuthInfo{
restConfig.Host: {},
},
}
// Set Cluster info
if restConfig.TLSClientConfig.Insecure {
kubeConfig.Clusters[restConfig.Host].InsecureSkipTLSVerify = true
}
if restConfig.TLSClientConfig.CAFile != "" {
kubeConfig.Clusters[restConfig.Host].CertificateAuthority = restConfig.TLSClientConfig.CAFile
}
// Set AuthInfo
if len(restConfig.TLSClientConfig.CAData) > 0 {
kubeConfig.Clusters[restConfig.Host].CertificateAuthorityData = restConfig.TLSClientConfig.CAData
}
if restConfig.TLSClientConfig.CertFile != "" {
kubeConfig.AuthInfos[restConfig.Host].ClientCertificate = restConfig.TLSClientConfig.CertFile
}
if len(restConfig.TLSClientConfig.CertData) > 0 {
kubeConfig.AuthInfos[restConfig.Host].ClientCertificateData = restConfig.TLSClientConfig.CertData
}
if restConfig.TLSClientConfig.KeyFile != "" {
kubeConfig.AuthInfos[restConfig.Host].ClientKey = restConfig.TLSClientConfig.KeyFile
}
if len(restConfig.TLSClientConfig.KeyData) > 0 {
kubeConfig.AuthInfos[restConfig.Host].ClientKeyData = restConfig.TLSClientConfig.KeyData
}
if restConfig.Username != "" {
kubeConfig.AuthInfos[restConfig.Host].Username = restConfig.Username
}
if restConfig.Password != "" {
kubeConfig.AuthInfos[restConfig.Host].Password = restConfig.Password
}
if restConfig.BearerToken != "" {
kubeConfig.AuthInfos[restConfig.Host].Token = restConfig.BearerToken
}
return clientcmd.WriteToFile(kubeConfig, filename)
}