1 package komega 2 3 import ( 4 "context" 5 6 "sigs.k8s.io/controller-runtime/pkg/client" 7 ) 8 9 // defaultK is the Komega used by the package global functions. 10 var defaultK = &komega{ctx: context.Background()} 11 12 // SetClient sets the client used by the package global functions. 13 func SetClient(c client.Client) { 14 defaultK.client = c 15 } 16 17 // SetContext sets the context used by the package global functions. 18 func SetContext(c context.Context) { 19 defaultK.ctx = c 20 } 21 22 func checkDefaultClient() { 23 if defaultK.client == nil { 24 panic("Default Komega's client is not set. Use SetClient to set it.") 25 } 26 } 27 28 // Get returns a function that fetches a resource and returns the occurring error. 29 // It can be used with gomega.Eventually() like this 30 // 31 // deployment := appsv1.Deployment{ ... } 32 // gomega.Eventually(komega.Get(&deployment)).Should(gomega.Succeed()) 33 // 34 // By calling the returned function directly it can also be used with gomega.Expect(komega.Get(...)()).To(...) 35 func Get(obj client.Object) func() error { 36 checkDefaultClient() 37 return defaultK.Get(obj) 38 } 39 40 // List returns a function that lists resources and returns the occurring error. 41 // It can be used with gomega.Eventually() like this 42 // 43 // deployments := v1.DeploymentList{ ... } 44 // gomega.Eventually(k.List(&deployments)).Should(gomega.Succeed()) 45 // 46 // By calling the returned function directly it can also be used as gomega.Expect(k.List(...)()).To(...) 47 func List(list client.ObjectList, opts ...client.ListOption) func() error { 48 checkDefaultClient() 49 return defaultK.List(list, opts...) 50 } 51 52 // Update returns a function that fetches a resource, applies the provided update function and then updates the resource. 53 // It can be used with gomega.Eventually() like this: 54 // 55 // deployment := appsv1.Deployment{ ... } 56 // gomega.Eventually(k.Update(&deployment, func() { 57 // deployment.Spec.Replicas = 3 58 // })).Should(gomega.Succeed()) 59 // 60 // By calling the returned function directly it can also be used as gomega.Expect(k.Update(...)()).To(...) 61 func Update(obj client.Object, f func(), opts ...client.UpdateOption) func() error { 62 checkDefaultClient() 63 return defaultK.Update(obj, f, opts...) 64 } 65 66 // UpdateStatus returns a function that fetches a resource, applies the provided update function and then updates the resource's status. 67 // It can be used with gomega.Eventually() like this: 68 // 69 // deployment := appsv1.Deployment{ ... } 70 // gomega.Eventually(k.UpdateStatus(&deployment, func() { 71 // deployment.Status.AvailableReplicas = 1 72 // })).Should(gomega.Succeed()) 73 // 74 // By calling the returned function directly it can also be used as gomega.Expect(k.UpdateStatus(...)()).To(...) 75 func UpdateStatus(obj client.Object, f func(), opts ...client.SubResourceUpdateOption) func() error { 76 checkDefaultClient() 77 return defaultK.UpdateStatus(obj, f, opts...) 78 } 79 80 // Object returns a function that fetches a resource and returns the object. 81 // It can be used with gomega.Eventually() like this: 82 // 83 // deployment := appsv1.Deployment{ ... } 84 // gomega.Eventually(k.Object(&deployment)).Should(HaveField("Spec.Replicas", gomega.Equal(ptr.To(3)))) 85 // 86 // By calling the returned function directly it can also be used as gomega.Expect(k.Object(...)()).To(...) 87 func Object(obj client.Object) func() (client.Object, error) { 88 checkDefaultClient() 89 return defaultK.Object(obj) 90 } 91 92 // ObjectList returns a function that fetches a resource and returns the object. 93 // It can be used with gomega.Eventually() like this: 94 // 95 // deployments := appsv1.DeploymentList{ ... } 96 // gomega.Eventually(k.ObjectList(&deployments)).Should(HaveField("Items", HaveLen(1))) 97 // 98 // By calling the returned function directly it can also be used as gomega.Expect(k.ObjectList(...)()).To(...) 99 func ObjectList(list client.ObjectList, opts ...client.ListOption) func() (client.ObjectList, error) { 100 checkDefaultClient() 101 return defaultK.ObjectList(list, opts...) 102 } 103