-
Notifications
You must be signed in to change notification settings - Fork 82
/
kubernetes.go
1323 lines (1139 loc) · 41.1 KB
/
kubernetes.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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package utils
// Contains methods helpful for interacting with and manipulating Kubernetes resources from YAML.
import (
"bufio"
"bytes"
"context"
ejson "encoding/json"
"errors"
"fmt"
"io"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"testing"
"time"
"github.com/google/shlex"
"github.com/pmezard/go-difflib/difflib"
"github.com/spf13/pflag"
appsv1 "k8s.io/api/apps/v1"
batchv1 "k8s.io/api/batch/v1"
batchv1beta1 "k8s.io/api/batch/v1beta1"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apiextv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
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/runtime/serializer/json"
"k8s.io/apimachinery/pkg/types"
apijson "k8s.io/apimachinery/pkg/util/json"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/discovery"
fakediscovery "k8s.io/client-go/discovery/fake"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes/scheme"
_ "k8s.io/client-go/plugin/pkg/client/auth" // package needed for auth providers like GCP
"k8s.io/client-go/rest"
"k8s.io/client-go/restmapper"
coretesting "k8s.io/client-go/testing"
api "k8s.io/client-go/tools/clientcmd/api/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
"sigs.k8s.io/controller-runtime/pkg/envtest"
"github.com/kudobuilder/kuttl/pkg/apis"
harness "github.com/kudobuilder/kuttl/pkg/apis/testharness/v1beta1"
"github.com/kudobuilder/kuttl/pkg/env"
)
// ensure that we only add to the scheme once.
var schemeLock sync.Once
// TODO (kensipe): need to consider options around AlwaysAdmin https://github.com/kudobuilder/kudo/pull/1420/files#r391449597
// IsJSONSyntaxError returns true if the error is a JSON syntax error.
func IsJSONSyntaxError(err error) bool {
_, ok := err.(*ejson.SyntaxError)
return ok
}
// ValidateErrors accepts an error as its first argument and passes it to each function in the errValidationFuncs slice,
// if any of the methods returns true, the method returns nil, otherwise it returns the original error.
func ValidateErrors(err error, errValidationFuncs ...func(error) bool) error {
for _, errFunc := range errValidationFuncs {
if errFunc(err) {
return nil
}
}
return err
}
// Retry retries a method until the context expires or the method returns an unvalidated error.
func Retry(ctx context.Context, fn func(context.Context) error, errValidationFuncs ...func(error) bool) error {
var lastErr error
errCh := make(chan error)
doneCh := make(chan struct{})
if fn == nil {
log.Println("retry func is nil and will be ignored")
return nil
}
// do { } while (err != nil): https://stackoverflow.com/a/32844744/10892393
for ok := true; ok; ok = lastErr != nil {
// run the function in a goroutine and close it once it is finished so that
// we can use select to wait for both the function return and the context deadline.
go func() {
// if the func we are calling panics, clean up and call it done
// the common case is when a shared reference, like a client, is nil and is called in the function
defer func() {
if r := recover(); r != nil {
errCh <- errors.New("func passed to retry panicked. expected if testsuite is shutting down")
}
}()
if err := fn(ctx); err != nil {
errCh <- err
} else {
doneCh <- struct{}{}
}
}()
select {
// the callback finished
case <-doneCh:
lastErr = nil
case err := <-errCh:
// check if we tolerate the error, return it if not.
if e := ValidateErrors(err, errValidationFuncs...); e != nil {
return e
}
lastErr = err
// timeout exceeded
case <-ctx.Done():
if lastErr == nil {
// there's no previous error, so just return the timeout error
return ctx.Err()
}
// return the most recent error
return lastErr
}
}
return lastErr
}
// RetryClient implements the Client interface, with retries built in.
type RetryClient struct {
Client client.Client
dynamic dynamic.Interface
discovery discovery.DiscoveryInterface
}
// RetryStatusWriter implements the StatusWriter interface, with retries built in.
type RetryStatusWriter struct {
StatusWriter client.StatusWriter
}
// NewRetryClient initializes a new Kubernetes client that automatically retries on network-related errors.
func NewRetryClient(cfg *rest.Config, opts client.Options) (*RetryClient, error) {
dynamicClient, err := dynamic.NewForConfig(cfg)
if err != nil {
return nil, err
}
discovery, err := discovery.NewDiscoveryClientForConfig(cfg)
if err != nil {
return nil, err
}
if opts.Mapper == nil {
httpClient, err := rest.HTTPClientFor(cfg)
if err != nil {
return nil, err
}
opts.Mapper, err = apiutil.NewDynamicRESTMapper(cfg, httpClient)
if err != nil {
return nil, err
}
}
client, err := client.New(cfg, opts)
return &RetryClient{Client: client, dynamic: dynamicClient, discovery: discovery}, err
}
// Scheme returns the scheme this client is using.
func (r *RetryClient) Scheme() *runtime.Scheme {
return r.Client.Scheme()
}
// RESTMapper returns the rest mapper this client is using.
func (r *RetryClient) RESTMapper() meta.RESTMapper {
return r.Client.RESTMapper()
}
// SubResource returns a subresource client for the named subResource.
func (r *RetryClient) SubResource(subResource string) client.SubResourceClient {
return r.Client.SubResource(subResource)
}
// GroupVersionKindFor returns the GroupVersionKind for the provided object.
func (r *RetryClient) GroupVersionKindFor(obj runtime.Object) (schema.GroupVersionKind, error) {
return r.Client.GroupVersionKindFor(obj)
}
// IsObjectNamespaced returns true if the object is namespaced.
func (r *RetryClient) IsObjectNamespaced(obj runtime.Object) (bool, error) {
return r.Client.IsObjectNamespaced(obj)
}
// Create saves the object obj in the Kubernetes cluster.
func (r *RetryClient) Create(ctx context.Context, obj client.Object, opts ...client.CreateOption) error {
return Retry(ctx, func(ctx context.Context) error {
return r.Client.Create(ctx, obj, opts...)
}, IsJSONSyntaxError)
}
// Delete deletes the given obj from Kubernetes cluster.
func (r *RetryClient) Delete(ctx context.Context, obj client.Object, opts ...client.DeleteOption) error {
return Retry(ctx, func(ctx context.Context) error {
return r.Client.Delete(ctx, obj, opts...)
}, IsJSONSyntaxError)
}
// DeleteAllOf deletes the given obj from Kubernetes cluster.
func (r *RetryClient) DeleteAllOf(ctx context.Context, obj client.Object, opts ...client.DeleteAllOfOption) error {
return Retry(ctx, func(ctx context.Context) error {
return r.Client.DeleteAllOf(ctx, obj, opts...)
}, IsJSONSyntaxError)
}
// Update updates the given obj in the Kubernetes cluster. obj must be a
// struct pointer so that obj can be updated with the content returned by the Server.
func (r *RetryClient) Update(ctx context.Context, obj client.Object, opts ...client.UpdateOption) error {
return Retry(ctx, func(ctx context.Context) error {
return r.Client.Update(ctx, obj, opts...)
}, IsJSONSyntaxError)
}
// Patch patches the given obj in the Kubernetes cluster. obj must be a
// struct pointer so that obj can be updated with the content returned by the Server.
func (r *RetryClient) Patch(ctx context.Context, obj client.Object, patch client.Patch, opts ...client.PatchOption) error {
return Retry(ctx, func(ctx context.Context) error {
return r.Client.Patch(ctx, obj, patch, opts...)
}, IsJSONSyntaxError)
}
// Get retrieves an obj for the given object key from the Kubernetes Cluster.
// obj must be a struct pointer so that obj can be updated with the response
// returned by the Server.
func (r *RetryClient) Get(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error {
return Retry(ctx, func(ctx context.Context) error {
return r.Client.Get(ctx, key, obj, opts...)
}, IsJSONSyntaxError)
}
// List retrieves list of objects for a given namespace and list options. On a
// successful call, Items field in the list will be populated with the
// result returned from the server.
func (r *RetryClient) List(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error {
return Retry(ctx, func(ctx context.Context) error {
return r.Client.List(ctx, list, opts...)
}, IsJSONSyntaxError)
}
// Watch watches a specific object and returns all events for it.
func (r *RetryClient) Watch(_ context.Context, obj runtime.Object) (watch.Interface, error) {
meta, err := meta.Accessor(obj)
if err != nil {
return nil, err
}
gvk := obj.GetObjectKind().GroupVersionKind()
groupResources, err := restmapper.GetAPIGroupResources(r.discovery)
if err != nil {
return nil, err
}
mapping, err := restmapper.NewDiscoveryRESTMapper(groupResources).RESTMapping(gvk.GroupKind(), gvk.Version)
if err != nil {
return nil, err
}
return r.dynamic.Resource(mapping.Resource).Watch(context.TODO(), metav1.SingleObject(metav1.ObjectMeta{
Name: meta.GetName(),
Namespace: meta.GetNamespace(),
}))
}
// Status returns a client which can update status subresource for kubernetes objects.
func (r *RetryClient) Status() client.StatusWriter {
return &RetryStatusWriter{
StatusWriter: r.Client.Status(),
}
}
// Create saves the subResource object in the Kubernetes cluster. obj must be a
// struct pointer so that obj can be updated with the content returned by the Server.
func (r *RetryStatusWriter) Create(ctx context.Context, obj client.Object, subResource client.Object, opts ...client.SubResourceCreateOption) error {
return Retry(ctx, func(ctx context.Context) error {
return r.StatusWriter.Create(ctx, obj, subResource, opts...)
}, IsJSONSyntaxError)
}
// Update updates the given obj in the Kubernetes cluster. obj must be a
// struct pointer so that obj can be updated with the content returned by the Server.
func (r *RetryStatusWriter) Update(ctx context.Context, obj client.Object, opts ...client.SubResourceUpdateOption) error {
return Retry(ctx, func(ctx context.Context) error {
return r.StatusWriter.Update(ctx, obj, opts...)
}, IsJSONSyntaxError)
}
// Patch patches the given obj in the Kubernetes cluster. obj must be a
// struct pointer so that obj can be updated with the content returned by the Server.
func (r *RetryStatusWriter) Patch(ctx context.Context, obj client.Object, patch client.Patch, opts ...client.SubResourcePatchOption) error {
return Retry(ctx, func(ctx context.Context) error {
return r.StatusWriter.Patch(ctx, obj, patch, opts...)
}, IsJSONSyntaxError)
}
// Scheme returns an initialized Kubernetes Scheme.
func Scheme() *runtime.Scheme {
schemeLock.Do(func() {
if err := apis.AddToScheme(scheme.Scheme); err != nil {
fmt.Printf("failed to add API resources to the scheme: %v", err)
os.Exit(-1)
}
if err := apiextv1.AddToScheme(scheme.Scheme); err != nil {
fmt.Printf("failed to add V1 API extension resources to the scheme: %v", err)
os.Exit(-1)
}
if err := apiextv1beta1.AddToScheme(scheme.Scheme); err != nil {
fmt.Printf("failed to add V1beta1 API extension resources to the scheme: %v", err)
os.Exit(-1)
}
})
return scheme.Scheme
}
// ResourceID returns a human readable identifier indicating the object kind, name, and namespace.
func ResourceID(obj runtime.Object) string {
m, err := meta.Accessor(obj)
if err != nil {
return ""
}
gvk := obj.GetObjectKind().GroupVersionKind()
return fmt.Sprintf("%s:%s/%s", gvk.Kind, m.GetNamespace(), m.GetName())
}
// Namespaced sets the namespace on an object to namespace, if it is a namespace scoped resource.
// If the resource is cluster scoped, then it is ignored and the namespace is not set.
// If it is a namespaced resource and a namespace is already set, then the namespace is unchanged.
func Namespaced(dClient discovery.DiscoveryInterface, obj runtime.Object, namespace string) (string, string, error) {
m, err := meta.Accessor(obj)
if err != nil {
return "", "", err
}
if m.GetNamespace() != "" {
return m.GetName(), m.GetNamespace(), nil
}
resource, err := GetAPIResource(dClient, obj.GetObjectKind().GroupVersionKind())
if err != nil {
return "", "", fmt.Errorf("retrieving API resource for %v failed: %v", obj.GetObjectKind().GroupVersionKind(), err)
}
if !resource.Namespaced {
return m.GetName(), "", nil
}
m.SetNamespace(namespace)
return m.GetName(), namespace, nil
}
func pruneLargeAdditions(expected *unstructured.Unstructured, actual *unstructured.Unstructured) runtime.Object {
pruned := actual.DeepCopy()
prune(expected.Object, pruned.Object)
return pruned
}
// prune replaces some fields in the actual tree to make it smaller for display.
//
// The goal is to make diffs on large objects much less verbose but not any less useful,
// by omitting these fields in the object which are not specified in the assertion and are at least
// moderately long when serialized.
//
// This way, for example when asserting on status.availableReplicas of a Deployment
// (which is missing if zero replicas are available) will still show the status.unavailableReplicas
// for example, but will omit spec completely unless the assertion also mentions it.
//
// This saves hundreds to thousands of lines of logs to scroll when debugging failures of some operator tests.
func prune(expected map[string]interface{}, actual map[string]interface{}) {
// This value was chosen so that it is low enough to hide huge fields like `metadata.managedFields`,
// but large enough such that for example a typical `metadata.labels` still shows,
// since it might be useful for identifying reported objects like pods.
// This could potentially be turned into a knob in the future.
const maxLines = 10
var toRemove []string
for k, v := range actual {
if _, inExpected := expected[k]; inExpected {
expectedMap, isExpectedMap := expected[k].(map[string]interface{})
actualMap, isActualMap := actual[k].(map[string]interface{})
if isActualMap && isExpectedMap {
prune(expectedMap, actualMap)
}
continue
}
numLines, err := countLines(k, v)
if err != nil || numLines < maxLines {
continue
}
toRemove = append(toRemove, k)
}
for _, s := range toRemove {
actual[s] = fmt.Sprintf("[... elided field over %d lines long ...]", maxLines)
}
}
func countLines(k string, v interface{}) (int, error) {
buf := strings.Builder{}
dummyObj := &unstructured.Unstructured{
Object: map[string]interface{}{k: v}}
err := MarshalObject(dummyObj, &buf)
if err != nil {
return 0, fmt.Errorf("cannot marshal field %s to compute its length in lines: %w", k, err)
}
return strings.Count(buf.String(), "\n"), nil
}
// PrettyDiff creates a unified diff highlighting the differences between two Kubernetes resources
func PrettyDiff(expected *unstructured.Unstructured, actual *unstructured.Unstructured) (string, error) {
actualPruned := pruneLargeAdditions(expected, actual)
expectedBuf := &bytes.Buffer{}
actualBuf := &bytes.Buffer{}
if err := MarshalObject(expected, expectedBuf); err != nil {
return "", err
}
if err := MarshalObject(actualPruned, actualBuf); err != nil {
return "", err
}
diffed := difflib.UnifiedDiff{
A: difflib.SplitLines(expectedBuf.String()),
B: difflib.SplitLines(actualBuf.String()),
FromFile: ResourceID(expected),
ToFile: ResourceID(actual),
Context: 3,
}
return difflib.GetUnifiedDiffString(diffed)
}
// ConvertUnstructured converts an unstructured object to the known struct. If the type is not known, then
// the unstructured object is returned unmodified.
func ConvertUnstructured(in client.Object) (client.Object, error) {
unstruct, err := runtime.DefaultUnstructuredConverter.ToUnstructured(in)
if err != nil {
return nil, fmt.Errorf("error converting %s to unstructured error: %w", ResourceID(in), err)
}
var converted client.Object
kind := in.GetObjectKind().GroupVersionKind().Kind
group := in.GetObjectKind().GroupVersionKind().Group
kuttlGroup := "kuttl.dev"
if group != kuttlGroup {
return in, nil
}
switch {
case kind == "TestFile":
converted = &harness.TestFile{}
case kind == "TestStep":
converted = &harness.TestStep{}
case kind == "TestAssert":
converted = &harness.TestAssert{}
case kind == "TestSuite":
converted = &harness.TestSuite{}
default:
return in, nil
}
err = runtime.DefaultUnstructuredConverter.FromUnstructured(unstruct, converted)
if err != nil {
return nil, fmt.Errorf("error converting %s from unstructured error: %w", ResourceID(in), err)
}
return converted, nil
}
// PatchObject updates expected with the Resource Version from actual.
// In the future, PatchObject may perform a strategic merge of actual into expected.
func PatchObject(actual, expected runtime.Object) error {
actualMeta, err := meta.Accessor(actual)
if err != nil {
return err
}
expectedMeta, err := meta.Accessor(expected)
if err != nil {
return err
}
expectedMeta.SetResourceVersion(actualMeta.GetResourceVersion())
return nil
}
// CleanObjectForMarshalling removes unnecessary object metadata that should not be included in serialization and diffs.
func CleanObjectForMarshalling(o runtime.Object) (runtime.Object, error) {
copied := o.DeepCopyObject()
meta, err := meta.Accessor(copied)
if err != nil {
return nil, err
}
meta.SetResourceVersion("")
meta.SetCreationTimestamp(metav1.Time{})
meta.SetSelfLink("")
meta.SetUID(types.UID(""))
meta.SetGeneration(0)
annotations := meta.GetAnnotations()
delete(annotations, "deployment.kubernetes.io/revision")
if len(annotations) > 0 {
meta.SetAnnotations(annotations)
} else {
meta.SetAnnotations(nil)
}
return copied, nil
}
// MarshalObject marshals a Kubernetes object to a YAML string.
func MarshalObject(o runtime.Object, w io.Writer) error {
copied, err := CleanObjectForMarshalling(o)
if err != nil {
return err
}
return json.NewYAMLSerializer(json.DefaultMetaFactory, nil, nil).Encode(copied, w)
}
// MarshalObjectJSON marshals a Kubernetes object to a JSON string.
func MarshalObjectJSON(o runtime.Object, w io.Writer) error {
copied, err := CleanObjectForMarshalling(o)
if err != nil {
return err
}
return json.NewSerializer(json.DefaultMetaFactory, nil, nil, false).Encode(copied, w)
}
// LoadYAMLFromFile loads all objects from a YAML file.
func LoadYAMLFromFile(path string) ([]client.Object, error) {
opened, err := os.Open(path)
if err != nil {
return nil, err
}
defer opened.Close()
return LoadYAML(path, opened)
}
// LoadYAML loads all objects from a reader
func LoadYAML(path string, r io.Reader) ([]client.Object, error) {
yamlReader := yaml.NewYAMLReader(bufio.NewReader(r))
objects := []client.Object{}
for {
data, err := yamlReader.Read()
if err != nil {
if err == io.EOF {
break
}
return nil, fmt.Errorf("error reading yaml %s: %w", path, err)
}
unstructuredObj := &unstructured.Unstructured{}
decoder := yaml.NewYAMLOrJSONDecoder(bytes.NewBuffer(data), len(data))
if err = decoder.Decode(unstructuredObj); err != nil {
return nil, fmt.Errorf("error decoding yaml %s: %w", path, err)
}
obj, err := ConvertUnstructured(unstructuredObj)
if err != nil {
return nil, fmt.Errorf("error converting unstructured object %s (%s): %w", ResourceID(unstructuredObj), path, err)
}
// discovered reader will return empty objects if a number of lines are preceding a yaml separator (---)
// this detects that, logs and continues
if obj.GetObjectKind().GroupVersionKind().Kind == "" {
log.Println("object detected with no GVK Kind for path", path)
} else {
objects = append(objects, obj)
}
}
return objects, nil
}
// MatchesKind returns true if the Kubernetes kind of obj matches any of kinds.
func MatchesKind(obj runtime.Object, kinds ...runtime.Object) bool {
gvk := obj.GetObjectKind().GroupVersionKind()
for _, kind := range kinds {
if kind.GetObjectKind().GroupVersionKind() == gvk {
return true
}
}
return false
}
// InstallManifests recurses over ManifestsDir to install all resources defined in YAML manifests.
func InstallManifests(ctx context.Context, c client.Client, dClient discovery.DiscoveryInterface, manifestsDir string, kinds ...runtime.Object) ([]*apiextv1.CustomResourceDefinition, error) {
crds := []*apiextv1.CustomResourceDefinition{}
if manifestsDir == "" {
return crds, nil
}
return crds, filepath.Walk(manifestsDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
extensions := map[string]bool{
".yaml": true,
".yml": true,
".json": true,
}
if !extensions[filepath.Ext(path)] {
return nil
}
objs, err := LoadYAMLFromFile(path)
if err != nil {
return err
}
for _, obj := range objs {
if len(kinds) > 0 && !MatchesKind(obj, kinds...) {
var expectedKinds []string
// it is expected that it is highly unlikely to be here (an unmatched kind)
// which is the justification for have a loop in a loop
for _, k := range kinds {
expectedKinds = append(expectedKinds, k.GetObjectKind().GroupVersionKind().String())
}
log.Printf("Skipping resource %s because it does not match expected kinds: %s", obj.GetObjectKind().GroupVersionKind().String(), strings.Join(expectedKinds, ","))
continue
}
objectKey := ObjectKey(obj)
if objectKey.Namespace == "" {
if _, _, err := Namespaced(dClient, obj, "default"); err != nil {
return err
}
}
updated, err := CreateOrUpdate(ctx, c, obj, true)
if err != nil {
return fmt.Errorf("error creating resource %s: %w", ResourceID(obj), err)
}
action := "created"
if updated {
action = "updated"
}
// TODO: use test logger instead of Go logger
log.Println(ResourceID(obj), action)
newCrd := apiextv1.CustomResourceDefinition{
TypeMeta: metav1.TypeMeta{
Kind: obj.GetObjectKind().GroupVersionKind().Kind,
APIVersion: obj.GetObjectKind().GroupVersionKind().Version,
},
ObjectMeta: metav1.ObjectMeta{
Name: obj.GetName(),
},
}
crds = append(crds, &newCrd)
}
return nil
})
}
// ObjectKey returns an instantiated ObjectKey for the provided object.
func ObjectKey(obj runtime.Object) client.ObjectKey {
m, _ := meta.Accessor(obj) //nolint:errcheck // runtime.Object don't have the error issues of interface{}
return client.ObjectKey{
Name: m.GetName(),
Namespace: m.GetNamespace(),
}
}
// NewResource generates a Kubernetes object using the provided apiVersion, kind, name, and namespace.
// The name and namespace are omitted if empty.
func NewResource(apiVersion, kind, name, namespace string) *unstructured.Unstructured {
meta := map[string]interface{}{}
if name != "" {
meta["name"] = name
}
if namespace != "" {
meta["namespace"] = namespace
}
return &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": apiVersion,
"kind": kind,
"metadata": meta,
},
}
}
// NewClusterRoleBinding Create a clusterrolebinding for the serviceAccount passed
func NewClusterRoleBinding(apiVersion, kind, name, namespace string, serviceAccount string, roleName string) runtime.Object {
sa := &rbacv1.ClusterRoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
RoleRef: rbacv1.RoleRef{
APIGroup: "rbac.authorization.k8s.io",
Kind: "ClusterRole",
Name: roleName,
},
Subjects: []rbacv1.Subject{{
Kind: "ServiceAccount",
Name: serviceAccount,
Namespace: namespace,
}},
}
return &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": apiVersion,
"kind": kind,
"metadata": sa.ObjectMeta,
"subjects": sa.Subjects,
"roleRef": sa.RoleRef,
},
}
}
// NewPod creates a new pod object.
func NewPod(name, namespace string) *unstructured.Unstructured {
return NewResource("v1", "Pod", name, namespace)
}
// NewV1Pod returns a new corev1.Pod object.
// Each of name, namespace and serviceAccountName are set if non-empty.
func NewV1Pod(name, namespace, serviceAccountName string) *corev1.Pod {
pod := corev1.Pod{}
pod.SetGroupVersionKind(schema.GroupVersionKind{
Group: "",
Version: "v1",
Kind: "Pod",
})
if name != "" {
pod.SetName(name)
}
if namespace != "" {
pod.SetNamespace(namespace)
}
if serviceAccountName != "" {
pod.Spec.ServiceAccountName = serviceAccountName
}
return &pod
}
// WithNamespace naively applies the namespace to the object. Used mainly in tests, otherwise
// use Namespaced.
func WithNamespace(obj *unstructured.Unstructured, namespace string) *unstructured.Unstructured {
res := obj.DeepCopy()
m, _ := meta.Accessor(res) //nolint:errcheck // runtime.Object don't have the error issues of interface{}
m.SetNamespace(namespace)
return res
}
// WithSpec applies the provided spec to the Kubernetes object.
func WithSpec(t *testing.T, obj *unstructured.Unstructured, spec map[string]interface{}) *unstructured.Unstructured {
res, err := WithKeyValue(obj, "spec", spec)
if err != nil {
t.Fatalf("failed to apply spec %v to object %v: %v", spec, obj, err)
}
return res
}
// WithStatus applies the provided status to the Kubernetes object.
func WithStatus(t *testing.T, obj *unstructured.Unstructured, status map[string]interface{}) *unstructured.Unstructured {
res, err := WithKeyValue(obj, "status", status)
if err != nil {
t.Fatalf("failed to apply status %v to object %v: %v", status, obj, err)
}
return res
}
// WithKeyValue sets key in the provided object to value.
func WithKeyValue(obj *unstructured.Unstructured, key string, value map[string]interface{}) (*unstructured.Unstructured, error) {
obj = obj.DeepCopy()
// we need to convert to and from unstructured here so that the types in case_test match when comparing
content, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
if err != nil {
return nil, err
}
content[key] = value
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(content, obj); err != nil {
return nil, err
}
return obj, nil
}
// WithLabels sets the labels on an object.
func WithLabels(t *testing.T, obj *unstructured.Unstructured, labels map[string]string) *unstructured.Unstructured {
obj = obj.DeepCopy()
m, err := meta.Accessor(obj)
if err != nil {
t.Fatalf("failed to apply labels %v to object %v: %v", labels, obj, err)
}
m.SetLabels(labels)
return obj
}
// WithAnnotations sets the annotations on an object.
func WithAnnotations(obj runtime.Object, annotations map[string]string) runtime.Object {
obj = obj.DeepCopyObject()
m, _ := meta.Accessor(obj) //nolint:errcheck // runtime.Object don't have the error issues of interface{}
m.SetAnnotations(annotations)
return obj
}
// FakeDiscoveryClient returns a fake discovery client that is populated with some types for use in
// unit tests.
func FakeDiscoveryClient() discovery.DiscoveryInterface {
return &fakediscovery.FakeDiscovery{
Fake: &coretesting.Fake{
Resources: []*metav1.APIResourceList{
{
GroupVersion: corev1.SchemeGroupVersion.String(),
APIResources: []metav1.APIResource{
{Name: "pod", Namespaced: true, Kind: "Pod"},
{Name: "namespace", Namespaced: false, Kind: "Namespace"},
{Name: "service", Namespaced: true, Kind: "Service"},
},
},
{
GroupVersion: appsv1.SchemeGroupVersion.String(),
APIResources: []metav1.APIResource{
{Name: "statefulset", Namespaced: true, Kind: "StatefulSet"},
{Name: "deployment", Namespaced: true, Kind: "Deployment"},
},
},
{
GroupVersion: batchv1.SchemeGroupVersion.String(),
APIResources: []metav1.APIResource{
{Name: "job", Namespaced: true, Kind: "Job"},
},
},
{
GroupVersion: batchv1beta1.SchemeGroupVersion.String(),
APIResources: []metav1.APIResource{
{Name: "job", Namespaced: true, Kind: "CronJob"},
},
},
{
GroupVersion: apiextv1.SchemeGroupVersion.String(),
APIResources: []metav1.APIResource{
{Name: "customresourcedefinitions", Namespaced: false, Kind: "CustomResourceDefinition"},
},
},
{
GroupVersion: apiextv1beta1.SchemeGroupVersion.String(),
APIResources: []metav1.APIResource{
{Name: "customresourcedefinitions", Namespaced: false, Kind: "CustomResourceDefinition"},
},
},
},
},
}
}
// CreateOrUpdate will create obj if it does not exist and update if it it does.
// retryonerror indicates whether we retry in case of conflict
// Returns true if the object was updated and false if it was created.
func CreateOrUpdate(ctx context.Context, cl client.Client, obj client.Object, retryOnError bool) (updated bool, err error) {
orig := obj.DeepCopyObject()
validators := []func(err error) bool{k8serrors.IsAlreadyExists}
if retryOnError {
validators = append(validators, k8serrors.IsConflict)
}
err = Retry(ctx, func(ctx context.Context) error {
expected := orig.DeepCopyObject()
actual := &unstructured.Unstructured{}
actual.SetGroupVersionKind(obj.GetObjectKind().GroupVersionKind())
err := cl.Get(ctx, ObjectKey(expected), actual)
if err == nil {
if err = PatchObject(actual, expected); err != nil {
return err
}
var expectedBytes []byte
expectedBytes, err = apijson.Marshal(expected)
if err != nil {
return err
}
err = cl.Patch(ctx, actual, client.RawPatch(types.MergePatchType, expectedBytes))
updated = true
} else if k8serrors.IsNotFound(err) {
err = cl.Create(ctx, obj)
updated = false
}
return err
}, validators...)
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
err = errors.New("create/update timeout exceeded")
}
return updated, err
}
// SetAnnotation sets the given key and value in the object's annotations, returning a copy.
func SetAnnotation(obj *unstructured.Unstructured, key, value string) *unstructured.Unstructured {
obj = obj.DeepCopy()
annotations := obj.GetAnnotations()
if annotations == nil {
annotations = map[string]string{}
}
annotations[key] = value
obj.SetAnnotations(annotations)
return obj
}
// GetAPIResource returns the APIResource object for a specific GroupVersionKind.
func GetAPIResource(dClient discovery.DiscoveryInterface, gvk schema.GroupVersionKind) (metav1.APIResource, error) {
resourceTypes, err := dClient.ServerResourcesForGroupVersion(gvk.GroupVersion().String())
if err != nil {
return metav1.APIResource{}, err
}
for _, resource := range resourceTypes.APIResources {
if !strings.EqualFold(resource.Kind, gvk.Kind) {
continue
}
return resource, nil
}
return metav1.APIResource{}, errors.New("resource type not found")
}
// WaitForDelete waits for the provide runtime objects to be deleted from cluster
func WaitForDelete(c *RetryClient, objs []runtime.Object) error {
// Wait for resources to be deleted.
return wait.PollUntilContextTimeout(context.TODO(), 100*time.Millisecond, 10*time.Second, true, func(ctx context.Context) (done bool, err error) {
for _, obj := range objs {
actual := &unstructured.Unstructured{}
actual.SetGroupVersionKind(obj.GetObjectKind().GroupVersionKind())
err = c.Get(ctx, ObjectKey(obj), actual)
if err == nil || !k8serrors.IsNotFound(err) {
return false, err
}
}
return true, nil
})
}
// WaitForSA waits for a service account to be present
func WaitForSA(config *rest.Config, name, namespace string) error {
c, err := NewRetryClient(config, client.Options{
Scheme: Scheme(),
})
if err != nil {
return err
}