-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
kube.go
431 lines (399 loc) · 13.3 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
// Package kube provides helper utilities common for kubernetes
package kube
import (
"context"
"fmt"
"regexp"
"strings"
"time"
"github.com/ghodss/yaml"
log "github.com/sirupsen/logrus"
v1 "k8s.io/api/core/v1"
"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"
"github.com/argoproj/argo-cd/common"
)
const (
listVerb = "list"
watchVerb = "watch"
)
const (
SecretKind = "Secret"
ServiceKind = "Service"
ServiceAccountKind = "ServiceAccount"
EndpointsKind = "Endpoints"
DeploymentKind = "Deployment"
ReplicaSetKind = "ReplicaSet"
StatefulSetKind = "StatefulSet"
DaemonSetKind = "DaemonSet"
IngressKind = "Ingress"
JobKind = "Job"
PersistentVolumeClaimKind = "PersistentVolumeClaim"
CustomResourceDefinitionKind = "CustomResourceDefinition"
PodKind = "Pod"
APIServiceKind = "APIService"
)
type ResourceKey struct {
Group string
Kind string
Namespace string
Name string
}
func (k *ResourceKey) String() string {
return fmt.Sprintf("%s/%s/%s/%s", k.Group, k.Kind, k.Namespace, k.Name)
}
func (k ResourceKey) GroupKind() schema.GroupKind {
return schema.GroupKind{Group: k.Group, Kind: k.Kind}
}
func NewResourceKey(group string, kind string, namespace string, name string) ResourceKey {
return ResourceKey{Group: group, Kind: kind, Namespace: namespace, Name: name}
}
func GetResourceKey(obj *unstructured.Unstructured) ResourceKey {
gvk := obj.GroupVersionKind()
return NewResourceKey(gvk.Group, gvk.Kind, obj.GetNamespace(), obj.GetName())
}
func GetObjectRef(obj *unstructured.Unstructured) v1.ObjectReference {
return v1.ObjectReference{
UID: obj.GetUID(),
APIVersion: obj.GetAPIVersion(),
Kind: obj.GetKind(),
Name: obj.GetName(),
Namespace: obj.GetNamespace(),
}
}
// 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
}
// GetAppInstanceLabel returns the application instance name from labels
func GetAppInstanceLabel(un *unstructured.Unstructured, key string) string {
if labels := un.GetLabels(); labels != nil {
return labels[key]
}
return ""
}
// UnsetLabel removes our app labels from an unstructured object
func UnsetLabel(target *unstructured.Unstructured, key string) {
if labels := target.GetLabels(); labels != nil {
if _, ok := labels[key]; ok {
delete(labels, key)
if len(labels) == 0 {
unstructured.RemoveNestedField(target.Object, "metadata", "labels")
} else {
target.SetLabels(labels)
}
}
}
}
// SetAppInstanceLabel the recommended app.kubernetes.io/instance label against an unstructured object
// Uses the legacy labeling if environment variable is set
func SetAppInstanceLabel(target *unstructured.Unstructured, key, val string) error {
labels := target.GetLabels()
if labels == nil {
labels = make(map[string]string)
}
labels[key] = val
target.SetLabels(labels)
if key != common.LabelKeyLegacyApplicationName {
// we no longer label the pod template sub resources in v0.11
return nil
}
gvk := schema.FromAPIVersionAndKind(target.GetAPIVersion(), target.GetKind())
// special case for deployment and job types: make sure that derived replicaset, and pod has
// the application label
switch gvk.Group {
case "apps", "extensions":
switch gvk.Kind {
case DeploymentKind, ReplicaSetKind, StatefulSetKind, DaemonSetKind:
templateLabels, ok, err := unstructured.NestedMap(target.UnstructuredContent(), "spec", "template", "metadata", "labels")
if err != nil {
return err
}
if !ok || templateLabels == nil {
templateLabels = make(map[string]interface{})
}
templateLabels[key] = val
err = unstructured.SetNestedMap(target.UnstructuredContent(), templateLabels, "spec", "template", "metadata", "labels")
if err != nil {
return err
}
// The following is a workaround for issue #335. In API version extensions/v1beta1 or
// apps/v1beta1, if a spec omits spec.selector then k8s will default the
// spec.selector.matchLabels to match spec.template.metadata.labels. This means Argo CD
// labels can potentially make their way into spec.selector.matchLabels, which is a bad
// thing. The following logic prevents this behavior.
switch target.GetAPIVersion() {
case "apps/v1beta1", "extensions/v1beta1":
selector, _, err := unstructured.NestedMap(target.UnstructuredContent(), "spec", "selector")
if err != nil {
return err
}
if len(selector) == 0 {
// If we get here, user did not set spec.selector in their manifest. We do not want
// our Argo CD labels to get defaulted by kubernetes, so we explicitly set the labels
// for them (minus the Argo CD labels).
delete(templateLabels, key)
err = unstructured.SetNestedMap(target.UnstructuredContent(), templateLabels, "spec", "selector", "matchLabels")
if err != nil {
return err
}
}
}
}
case "batch":
switch gvk.Kind {
case JobKind:
templateLabels, ok, err := unstructured.NestedMap(target.UnstructuredContent(), "spec", "template", "metadata", "labels")
if err != nil {
return err
}
if !ok || templateLabels == nil {
templateLabels = make(map[string]interface{})
}
templateLabels[key] = val
err = unstructured.SetNestedMap(target.UnstructuredContent(), templateLabels, "spec", "template", "metadata", "labels")
if err != nil {
return err
}
}
}
return nil
}
func ToGroupVersionResource(groupVersion string, apiResource *metav1.APIResource) schema.GroupVersionResource {
gvk := schema.FromAPIVersionAndKind(groupVersion, apiResource.Kind)
gv := gvk.GroupVersion()
return gv.WithResource(apiResource.Name)
}
func ToResourceInterface(dynamicIf dynamic.Interface, apiResource *metav1.APIResource, resource schema.GroupVersionResource, namespace string) dynamic.ResourceInterface {
if apiResource.Namespaced {
return dynamicIf.Resource(resource).Namespace(namespace)
}
return dynamicIf.Resource(resource)
}
func IsCRDGroupVersionKind(gvk schema.GroupVersionKind) bool {
return gvk.Kind == CustomResourceDefinitionKind && gvk.Group == "apiextensions.k8s.io"
}
func IsCRD(obj *unstructured.Unstructured) bool {
return IsCRDGroupVersionKind(obj.GroupVersionKind())
}
// 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, apierr.NewNotFound(schema.GroupResource{Group: gvk.Group, Resource: gvk.Kind}, "")
}
// cleanKubectlOutput makes the error output of kubectl a little better to read
func cleanKubectlOutput(s string) string {
s = strings.TrimSpace(s)
s = strings.Replace(s, ": error validating \"STDIN\"", "", -1)
s = strings.Replace(s, ": unable to recognize \"STDIN\"", "", -1)
s = strings.Replace(s, ": error when creating \"STDIN\"", "", -1)
s = strings.Replace(s, "; if you choose to ignore these errors, turn validation off with --validate=false", "", -1)
s = strings.Replace(s, "error: error", "error", -1)
return s
}
// WriteKubeConfig takes a rest.Config and writes it as a kubeconfig at the specified path
func WriteKubeConfig(restConfig *rest.Config, namespace, filename string) error {
kubeConfig := NewKubeConfig(restConfig, namespace)
return clientcmd.WriteToFile(*kubeConfig, filename)
}
// NewKubeConfig converts a clientcmdapi.Config (kubeconfig) from a rest.Config
func NewKubeConfig(restConfig *rest.Config, namespace string) *clientcmdapi.Config {
return &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,
InsecureSkipTLSVerify: restConfig.TLSClientConfig.Insecure,
CertificateAuthority: restConfig.TLSClientConfig.CAFile,
CertificateAuthorityData: restConfig.TLSClientConfig.CAData,
},
},
AuthInfos: map[string]*clientcmdapi.AuthInfo{
restConfig.Host: newAuthInfo(restConfig),
},
}
}
// newAuthInfo returns an AuthInfo from a rest config, detecting if the rest.Config is an
// in-cluster config and automatically setting the token path appropriately.
func newAuthInfo(restConfig *rest.Config) *clientcmdapi.AuthInfo {
authInfo := clientcmdapi.AuthInfo{}
haveCredentials := false
if restConfig.TLSClientConfig.CertFile != "" {
authInfo.ClientCertificate = restConfig.TLSClientConfig.CertFile
haveCredentials = true
}
if len(restConfig.TLSClientConfig.CertData) > 0 {
authInfo.ClientCertificateData = restConfig.TLSClientConfig.CertData
haveCredentials = true
}
if restConfig.TLSClientConfig.KeyFile != "" {
authInfo.ClientKey = restConfig.TLSClientConfig.KeyFile
haveCredentials = true
}
if len(restConfig.TLSClientConfig.KeyData) > 0 {
authInfo.ClientKeyData = restConfig.TLSClientConfig.KeyData
haveCredentials = true
}
if restConfig.Username != "" {
authInfo.Username = restConfig.Username
haveCredentials = true
}
if restConfig.Password != "" {
authInfo.Password = restConfig.Password
haveCredentials = true
}
if restConfig.BearerToken != "" {
authInfo.Token = restConfig.BearerToken
haveCredentials = true
}
if restConfig.ExecProvider != nil {
authInfo.Exec = restConfig.ExecProvider
haveCredentials = true
}
if restConfig.ExecProvider == nil && !haveCredentials {
// If no credentials were set (or there was no exec provider), we assume in-cluster config.
// In-cluster configs from the go-client will no longer set bearer tokens, so we set the
// well known token path. See issue #774
authInfo.TokenFile = "/var/run/secrets/kubernetes.io/serviceaccount/token"
}
return &authInfo
}
var diffSeparator = regexp.MustCompile(`\n---`)
// SplitYAML splits a YAML file into unstructured objects. Returns list of all unstructured objects
// found in the yaml. If any errors occurred, returns the first one
func SplitYAML(out string) ([]*unstructured.Unstructured, error) {
parts := diffSeparator.Split(out, -1)
var objs []*unstructured.Unstructured
var firstErr error
for _, part := range parts {
var objMap map[string]interface{}
err := yaml.Unmarshal([]byte(part), &objMap)
if err != nil {
if firstErr == nil {
firstErr = fmt.Errorf("Failed to unmarshal manifest: %v", err)
}
continue
}
if len(objMap) == 0 {
// handles case where theres no content between `---`
continue
}
var obj unstructured.Unstructured
err = yaml.Unmarshal([]byte(part), &obj)
if err != nil {
if firstErr == nil {
firstErr = fmt.Errorf("Failed to unmarshal manifest: %v", err)
}
continue
}
objs = append(objs, &obj)
}
return objs, firstErr
}
// WatchWithRetry returns channel of watch events or errors of failed to call watch API.
func WatchWithRetry(ctx context.Context, getWatch func() (watch.Interface, error)) chan struct {
*watch.Event
Error error
} {
ch := make(chan struct {
*watch.Event
Error error
})
execute := func() (bool, error) {
w, err := getWatch()
if err != nil {
return false, err
}
defer w.Stop()
for {
select {
case event, ok := <-w.ResultChan():
if ok {
ch <- struct {
*watch.Event
Error error
}{Event: &event, Error: nil}
} else {
return true, nil
}
case <-ctx.Done():
return false, nil
}
}
}
go func() {
defer close(ch)
for {
retry, err := execute()
if err != nil {
ch <- struct {
*watch.Event
Error error
}{Error: err}
}
if !retry {
return
}
time.Sleep(time.Second)
}
}()
return ch
}
func GetDeploymentReplicas(u *unstructured.Unstructured) *int64 {
val, found, err := unstructured.NestedInt64(u.Object, "spec", "replicas")
if !found || err != nil {
return nil
}
return &val
}