|
| 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 | +// deployment := appsv1.Deployment{ ... } |
| 31 | +// gomega.Eventually(komega.Get(&deployment)).To(gomega.Succeed()) |
| 32 | +// By calling the returned function directly it can also be used with gomega.Expect(komega.Get(...)()).To(...) |
| 33 | +func Get(obj client.Object) func() error { |
| 34 | + checkDefaultClient() |
| 35 | + return defaultK.Get(obj) |
| 36 | +} |
| 37 | + |
| 38 | +// List returns a function that lists resources and returns the occurring error. |
| 39 | +// It can be used with gomega.Eventually() like this |
| 40 | +// deployments := v1.DeploymentList{ ... } |
| 41 | +// gomega.Eventually(k.List(&deployments)).To(gomega.Succeed()) |
| 42 | +// By calling the returned function directly it can also be used as gomega.Expect(k.List(...)()).To(...) |
| 43 | +func List(list client.ObjectList, opts ...client.ListOption) func() error { |
| 44 | + checkDefaultClient() |
| 45 | + return defaultK.List(list, opts...) |
| 46 | +} |
| 47 | + |
| 48 | +// Update returns a function that fetches a resource, applies the provided update function and then updates the resource. |
| 49 | +// It can be used with gomega.Eventually() like this: |
| 50 | +// deployment := appsv1.Deployment{ ... } |
| 51 | +// gomega.Eventually(k.Update(&deployment, func (o client.Object) { |
| 52 | +// deployment.Spec.Replicas = 3 |
| 53 | +// return &deployment |
| 54 | +// })).To(gomega.Scucceed()) |
| 55 | +// By calling the returned function directly it can also be used as gomega.Expect(k.Update(...)()).To(...) |
| 56 | +func Update(obj client.Object, f func(), opts ...client.UpdateOption) func() error { |
| 57 | + checkDefaultClient() |
| 58 | + return defaultK.Update(obj, f, opts...) |
| 59 | +} |
| 60 | + |
| 61 | +// UpdateStatus returns a function that fetches a resource, applies the provided update function and then updates the resource's status. |
| 62 | +// It can be used with gomega.Eventually() like this: |
| 63 | +// deployment := appsv1.Deployment{ ... } |
| 64 | +// gomega.Eventually(k.Update(&deployment, func (o client.Object) { |
| 65 | +// deployment.Status.AvailableReplicas = 1 |
| 66 | +// return &deployment |
| 67 | +// })).To(gomega.Scucceed()) |
| 68 | +// By calling the returned function directly it can also be used as gomega.Expect(k.UpdateStatus(...)()).To(...) |
| 69 | +func UpdateStatus(obj client.Object, f func(), opts ...client.UpdateOption) func() error { |
| 70 | + checkDefaultClient() |
| 71 | + return defaultK.UpdateStatus(obj, f, opts...) |
| 72 | +} |
| 73 | + |
| 74 | +// Object returns a function that fetches a resource and returns the object. |
| 75 | +// It can be used with gomega.Eventually() like this: |
| 76 | +// deployment := appsv1.Deployment{ ... } |
| 77 | +// gomega.Eventually(k.Object(&deployment)).To(HaveField("Spec.Replicas", gomega.Equal(pointer.Int32(3)))) |
| 78 | +// By calling the returned function directly it can also be used as gomega.Expect(k.Object(...)()).To(...) |
| 79 | +func Object(obj client.Object) func() (client.Object, error) { |
| 80 | + checkDefaultClient() |
| 81 | + return defaultK.Object(obj) |
| 82 | +} |
| 83 | + |
| 84 | +// ObjectList returns a function that fetches a resource and returns the object. |
| 85 | +// It can be used with gomega.Eventually() like this: |
| 86 | +// deployments := appsv1.DeploymentList{ ... } |
| 87 | +// gomega.Eventually(k.ObjectList(&deployments)).To(HaveField("Items", HaveLen(1))) |
| 88 | +// By calling the returned function directly it can also be used as gomega.Expect(k.ObjectList(...)()).To(...) |
| 89 | +func ObjectList(list client.ObjectList, opts ...client.ListOption) func() (client.ObjectList, error) { |
| 90 | + checkDefaultClient() |
| 91 | + return defaultK.ObjectList(list, opts...) |
| 92 | +} |
0 commit comments