/
komega.go
127 lines (109 loc) · 3.6 KB
/
komega.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
/*
Copyright 2021 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package komega
import (
"context"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// komega is a collection of
type komega struct {
ctx context.Context
client client.Client
}
var _ Komega = &komega{}
// New creates a new Komega instance with the given client.
func New(c client.Client) Komega {
return &komega{
client: c,
}
}
// WithContext returns a copy that uses the given context.
func (k komega) WithContext(ctx context.Context) Komega {
k.ctx = ctx
return &k
}
// WithClient returns a copy that uses the given client.
func (k komega) WithClient(c client.Client) Komega {
k.client = c
return &k
}
// context returns the matcher context if one has been set or context.Background() otherwise.
func (k *komega) context() context.Context {
if k.ctx == nil {
return context.Background()
}
return k.ctx
}
// Get returns a function that fetches a resource and returns the occuring error.
func (k *komega) Get(obj client.Object) func() error {
key := types.NamespacedName{
Name: obj.GetName(),
Namespace: obj.GetNamespace(),
}
return func() error {
return k.client.Get(k.context(), key, obj)
}
}
// List returns a function that lists resources and returns the occuring error.
func (k *komega) List(obj client.ObjectList, opts ...client.ListOption) func() error {
return func() error {
return k.client.List(k.context(), obj, opts...)
}
}
// Update returns a function that fetches a resource, applies the provided update function and then updates the resource.
func (k *komega) Update(obj client.Object, updateFunc UpdateFunc, opts ...client.UpdateOption) func() error {
key := types.NamespacedName{
Name: obj.GetName(),
Namespace: obj.GetNamespace(),
}
return func() error {
err := k.client.Get(k.context(), key, obj)
if err != nil {
return err
}
return k.client.Update(k.context(), updateFunc(obj), opts...)
}
}
// UpdateStatus returns a function that fetches a resource, applies the provided update function and then updates the resource's status.
func (k *komega) UpdateStatus(obj client.Object, updateFunc UpdateFunc, opts ...client.UpdateOption) func() error {
key := types.NamespacedName{
Name: obj.GetName(),
Namespace: obj.GetNamespace(),
}
return func() error {
err := k.client.Get(k.context(), key, obj)
if err != nil {
return err
}
return k.client.Status().Update(k.context(), updateFunc(obj), opts...)
}
}
// Object returns a function that fetches a resource and returns the object.
func (k *komega) Object(obj client.Object) func() (client.Object, error) {
key := types.NamespacedName{
Name: obj.GetName(),
Namespace: obj.GetNamespace(),
}
return func() (client.Object, error) {
err := k.client.Get(k.context(), key, obj)
return obj, err
}
}
// ObjectList returns a function that fetches a resource and returns the object.
func (k *komega) ObjectList(obj client.ObjectList, opts ...client.ListOption) func() (client.ObjectList, error) {
return func() (client.ObjectList, error) {
err := k.client.List(k.context(), obj, opts...)
return obj, err
}
}