sigs.k8s.io/controller-runtime@v0.18.2/pkg/envtest/komega/komega.go (about) 1 /* 2 Copyright 2021 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package komega 18 19 import ( 20 "context" 21 22 "k8s.io/apimachinery/pkg/types" 23 "sigs.k8s.io/controller-runtime/pkg/client" 24 ) 25 26 // komega is a collection of utilites for writing tests involving a mocked 27 // Kubernetes API. 28 type komega struct { 29 ctx context.Context 30 client client.Client 31 } 32 33 var _ Komega = &komega{} 34 35 // New creates a new Komega instance with the given client. 36 func New(c client.Client) Komega { 37 return &komega{ 38 client: c, 39 ctx: context.Background(), 40 } 41 } 42 43 // WithContext returns a copy that uses the given context. 44 func (k komega) WithContext(ctx context.Context) Komega { 45 k.ctx = ctx 46 return &k 47 } 48 49 // Get returns a function that fetches a resource and returns the occurring error. 50 func (k *komega) Get(obj client.Object) func() error { 51 key := types.NamespacedName{ 52 Name: obj.GetName(), 53 Namespace: obj.GetNamespace(), 54 } 55 return func() error { 56 return k.client.Get(k.ctx, key, obj) 57 } 58 } 59 60 // List returns a function that lists resources and returns the occurring error. 61 func (k *komega) List(obj client.ObjectList, opts ...client.ListOption) func() error { 62 return func() error { 63 return k.client.List(k.ctx, obj, opts...) 64 } 65 } 66 67 // Update returns a function that fetches a resource, applies the provided update function and then updates the resource. 68 func (k *komega) Update(obj client.Object, updateFunc func(), opts ...client.UpdateOption) func() error { 69 key := types.NamespacedName{ 70 Name: obj.GetName(), 71 Namespace: obj.GetNamespace(), 72 } 73 return func() error { 74 err := k.client.Get(k.ctx, key, obj) 75 if err != nil { 76 return err 77 } 78 updateFunc() 79 return k.client.Update(k.ctx, obj, opts...) 80 } 81 } 82 83 // UpdateStatus returns a function that fetches a resource, applies the provided update function and then updates the resource's status. 84 func (k *komega) UpdateStatus(obj client.Object, updateFunc func(), opts ...client.SubResourceUpdateOption) func() error { 85 key := types.NamespacedName{ 86 Name: obj.GetName(), 87 Namespace: obj.GetNamespace(), 88 } 89 return func() error { 90 err := k.client.Get(k.ctx, key, obj) 91 if err != nil { 92 return err 93 } 94 updateFunc() 95 return k.client.Status().Update(k.ctx, obj, opts...) 96 } 97 } 98 99 // Object returns a function that fetches a resource and returns the object. 100 func (k *komega) Object(obj client.Object) func() (client.Object, error) { 101 key := types.NamespacedName{ 102 Name: obj.GetName(), 103 Namespace: obj.GetNamespace(), 104 } 105 return func() (client.Object, error) { 106 err := k.client.Get(k.ctx, key, obj) 107 return obj, err 108 } 109 } 110 111 // ObjectList returns a function that fetches a resource and returns the object. 112 func (k *komega) ObjectList(obj client.ObjectList, opts ...client.ListOption) func() (client.ObjectList, error) { 113 return func() (client.ObjectList, error) { 114 err := k.client.List(k.ctx, obj, opts...) 115 return obj, err 116 } 117 }