forked from argoproj/gitops-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kube.go
389 lines (355 loc) · 12.1 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
// Package kube provides helper utilities common for kubernetes
package kube
import (
"bytes"
"context"
"fmt"
"io"
"regexp"
"strings"
"time"
"github.com/go-logr/logr"
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/util/wait"
kubeyaml "k8s.io/apimachinery/pkg/util/yaml"
"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"
"sigs.k8s.io/yaml"
)
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"
NamespaceKind = "Namespace"
HorizontalPodAutoscalerKind = "HorizontalPodAutoscaler"
)
type ResourceInfoProvider interface {
IsNamespaced(gk schema.GroupKind) (bool, error)
}
func IsNamespacedOrUnknown(provider ResourceInfoProvider, gk schema.GroupKind) bool {
namespaced, err := provider.IsNamespaced(gk)
return namespaced || err != nil
}
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)
}
}
}
}
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 {
return &r, nil
}
}
return nil, apierr.NewNotFound(schema.GroupResource{Group: gvk.Group, Resource: gvk.Kind}, "")
}
var (
kubectlErrOutRegexp = regexp.MustCompile(`^(error: )?(error validating|error when creating|error when creating) "\S+": `)
// See ApplyOpts::Run()
// cmdutil.AddSourceToErr(fmt.Sprintf("applying patch:\n%s\nto:\n%v\nfor:", patchBytes, info), info.Source, err)
kubectlApplyPatchErrOutRegexp = regexp.MustCompile(`(?s)^error when applying patch:.*\nfor: "\S+": `)
)
// cleanKubectlOutput makes the error output of kubectl a little better to read
func cleanKubectlOutput(s string) string {
s = strings.TrimSpace(s)
s = kubectlErrOutRegexp.ReplaceAllString(s, "")
s = kubectlApplyPatchErrOutRegexp.ReplaceAllString(s, "")
s = strings.Replace(s, "; if you choose to ignore these errors, turn validation off with --validate=false", "", -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,
TLSServerName: restConfig.TLSClientConfig.ServerName,
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
}
// SplitYAML splits a YAML file into unstructured objects. Returns list of all unstructured objects
// found in the yaml. If an error occurs, returns objects that have been parsed so far too.
func SplitYAML(yamlData []byte) ([]*unstructured.Unstructured, error) {
// Similar way to what kubectl does
// https://github.com/kubernetes/cli-runtime/blob/master/pkg/resource/visitor.go#L573-L600
// Ideally k8s.io/cli-runtime/pkg/resource.Builder should be used instead of this method.
// E.g. Builder does list unpacking and flattening and this code does not.
d := kubeyaml.NewYAMLOrJSONDecoder(bytes.NewReader(yamlData), 4096)
var objs []*unstructured.Unstructured
for {
ext := runtime.RawExtension{}
if err := d.Decode(&ext); err != nil {
if err == io.EOF {
break
}
return objs, fmt.Errorf("failed to unmarshal manifest: %v", err)
}
ext.Raw = bytes.TrimSpace(ext.Raw)
if len(ext.Raw) == 0 || bytes.Equal(ext.Raw, []byte("null")) {
continue
}
u := &unstructured.Unstructured{}
if err := yaml.Unmarshal(ext.Raw, u); err != nil {
return objs, fmt.Errorf("failed to unmarshal manifest: %v", err)
}
objs = append(objs, u)
}
return objs, nil
}
// 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
}
// RetryUntilSucceed keep retrying given action with specified interval until action succeed or specified context is done.
func RetryUntilSucceed(ctx context.Context, interval time.Duration, desc string, log logr.Logger, action func() error) {
pollErr := wait.PollImmediateUntil(interval, func() (bool /*done*/, error) {
log.V(1).Info(fmt.Sprintf("Start %s", desc))
err := action()
if err == nil {
log.V(1).Info(fmt.Sprintf("Completed %s", desc))
return true, nil
}
log.V(1).Info(fmt.Sprintf("Failed to %s: %+v, retrying in %v", desc, err, interval))
return false, nil
}, ctx.Done())
if pollErr != nil {
// The only error that can happen here is wait.ErrWaitTimeout if ctx is done.
log.V(1).Info(fmt.Sprintf("Stop retrying %s", desc))
}
}