/
client.go
364 lines (284 loc) · 7.56 KB
/
client.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
package cmd
import (
"context"
"encoding/json"
"fmt"
"slices"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/yaml"
corev1alpha1 "package-operator.run/apis/core/v1alpha1"
manifestsv1alpha1 "package-operator.run/apis/manifests/v1alpha1"
)
func NewClient(client client.Client) *Client {
return &Client{
client: client,
}
}
type Client struct {
client client.Client
}
func (c *Client) GetPackage(ctx context.Context, name string, opts ...GetPackageOption) (*Package, error) {
var cfg GetPackageConfig
cfg.Option(opts...)
var obj client.Object
if cfg.Namespace != "" {
obj = &corev1alpha1.Package{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: cfg.Namespace,
},
}
} else {
obj = &corev1alpha1.ClusterPackage{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
}
}
if err := c.client.Get(ctx, client.ObjectKeyFromObject(obj), obj); err != nil {
return nil, fmt.Errorf("getting package object: %w", err)
}
return &Package{
client: c.client,
obj: obj,
}, nil
}
type GetPackageConfig struct {
Namespace string
}
func (c *GetPackageConfig) Option(opts ...GetPackageOption) {
for _, opt := range opts {
opt.ConfigureGetPackage(c)
}
}
type GetPackageOption interface{ ConfigureGetPackage(*GetPackageConfig) }
func (c *Client) GetObjectDeployment(
ctx context.Context, name string, opts ...GetObjectDeploymentOption,
) (*ObjectDeployment, error) {
var cfg GetObjectDeploymentConfig
cfg.Option(opts...)
var obj client.Object
if cfg.Namespace != "" {
obj = &corev1alpha1.ObjectDeployment{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: cfg.Namespace,
},
}
} else {
obj = &corev1alpha1.ClusterObjectDeployment{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
}
}
if err := c.client.Get(ctx, client.ObjectKeyFromObject(obj), obj); err != nil {
return nil, fmt.Errorf("getting objectdeployment object: %w", err)
}
return &ObjectDeployment{
client: c.client,
obj: obj,
}, nil
}
type GetObjectDeploymentConfig struct {
Namespace string
}
func (c *GetObjectDeploymentConfig) Option(opts ...GetObjectDeploymentOption) {
for _, opt := range opts {
opt.ConfigureGetObjectDeployment(c)
}
}
type GetObjectDeploymentOption interface {
ConfigureGetObjectDeployment(*GetObjectDeploymentConfig)
}
type Package struct {
client client.Client
obj client.Object
}
func (p *Package) Name() string {
return p.obj.GetName()
}
func (p *Package) Namespace() string {
return p.obj.GetNamespace()
}
func (p *Package) CurrentRevision() int64 {
if cpkg, ok := p.obj.(*corev1alpha1.ClusterPackage); ok {
return cpkg.Status.Revision
}
return p.obj.(*corev1alpha1.Package).Status.Revision
}
func (p *Package) ObjectSets(ctx context.Context) (ObjectSetList, error) {
opts := []findObjectSetsOption{
withSelector{
Selector: labels.SelectorFromSet(labels.Set{
manifestsv1alpha1.PackageInstanceLabel: p.Name(),
}),
},
}
if _, ok := p.obj.(*corev1alpha1.Package); ok {
opts = append(opts, withNamespace(p.Namespace()))
}
return findObjectSets(ctx, p.client, opts...)
}
type ObjectDeployment struct {
client client.Client
obj client.Object
}
func (d *ObjectDeployment) Name() string {
return d.obj.GetName()
}
func (d *ObjectDeployment) Namespace() string {
return d.obj.GetNamespace()
}
func (d *ObjectDeployment) CurrentRevision() int64 {
if cod, ok := d.obj.(*corev1alpha1.ClusterObjectDeployment); ok {
return cod.Status.Revision
}
return d.obj.(*corev1alpha1.ObjectDeployment).Status.Revision
}
func (d *ObjectDeployment) ObjectSets(ctx context.Context) (ObjectSetList, error) {
opts := []findObjectSetsOption{
withSelector{
Selector: labels.SelectorFromSet(d.obj.GetLabels()),
},
}
if _, ok := d.obj.(*corev1alpha1.ObjectDeployment); ok {
opts = append(opts, withNamespace(d.Namespace()))
}
return findObjectSets(ctx, d.client, opts...)
}
func findObjectSets(ctx context.Context, c client.Client, opts ...findObjectSetsOption) (ObjectSetList, error) {
var cfg findObjectSetsConfig
cfg.Option(opts...)
listOpts := []client.ListOption{
client.MatchingLabelsSelector{
Selector: cfg.Selector,
},
}
if cfg.Namespace != "" {
listOpts = append(listOpts, client.InNamespace(cfg.Namespace))
var sets corev1alpha1.ObjectSetList
if err := c.List(ctx, &sets, listOpts...); err != nil {
return nil, fmt.Errorf("listing ObjectSets: %w", err)
}
revs := make(ObjectSetList, 0, len(sets.Items))
for i := range sets.Items {
revs = append(revs, NewObjectSet(&sets.Items[i]))
}
return revs, nil
}
var sets corev1alpha1.ClusterObjectSetList
if err := c.List(ctx, &sets, listOpts...); err != nil {
return nil, fmt.Errorf("listing ClusterObjectSets: %w", err)
}
revs := make(ObjectSetList, 0, len(sets.Items))
for i := range sets.Items {
revs = append(revs, NewObjectSet(&sets.Items[i]))
}
return revs, nil
}
type findObjectSetsConfig struct {
Namespace string
Selector labels.Selector
}
func (c *findObjectSetsConfig) Option(opts ...findObjectSetsOption) {
for _, opt := range opts {
opt.ConfigureFindObjectSets(c)
}
}
type findObjectSetsOption interface {
ConfigureFindObjectSets(*findObjectSetsConfig)
}
type withNamespace string
func (w withNamespace) ConfigureFindObjectSets(c *findObjectSetsConfig) {
c.Namespace = string(w)
}
type withSelector struct{ Selector labels.Selector }
func (w withSelector) ConfigureFindObjectSets(c *findObjectSetsConfig) {
c.Selector = w.Selector
}
func NewObjectSet(obj client.Object) ObjectSet {
return ObjectSet{
obj: obj,
}
}
type ObjectSet struct {
obj client.Object
}
func (s *ObjectSet) Name() string {
return s.obj.GetName()
}
func (s *ObjectSet) Namespace() string {
return s.obj.GetNamespace()
}
func (s *ObjectSet) HasSucceeded() bool {
return meta.IsStatusConditionTrue(s.getConditions(), corev1alpha1.ObjectSetSucceeded)
}
func (s *ObjectSet) getConditions() []metav1.Condition {
if cos, ok := s.obj.(*corev1alpha1.ClusterObjectSet); ok {
return cos.Status.Conditions
}
return s.obj.(*corev1alpha1.ObjectSet).Status.Conditions
}
func (s *ObjectSet) Revision() int64 {
if cos, ok := s.obj.(*corev1alpha1.ClusterObjectSet); ok {
return cos.Status.Revision
}
return s.obj.(*corev1alpha1.ObjectSet).Status.Revision
}
func (s *ObjectSet) ChangeCause() string {
const changeCauseKey = "kubernetes.io/change-cause"
return s.obj.GetAnnotations()[changeCauseKey]
}
func (s *ObjectSet) MarshalYAML() ([]byte, error) {
return yaml.Marshal(s.obj)
}
func (s *ObjectSet) MarshalJSON() ([]byte, error) {
return json.Marshal(s.obj)
}
type ObjectSetList []ObjectSet
func (l ObjectSetList) Sort() {
slices.SortFunc(l, func(a, b ObjectSet) int {
return int(a.Revision() - b.Revision())
})
}
func (l ObjectSetList) FindRevision(rev int64) (ObjectSet, bool) {
idx := slices.IndexFunc(l, func(os ObjectSet) bool {
return os.Revision() == rev
})
if idx < 0 {
return ObjectSet{}, false
}
return l[idx], true
}
func (l ObjectSetList) RenderJSON() ([]byte, error) {
return json.MarshalIndent(l, "", " ")
}
func (l ObjectSetList) RenderYAML() ([]byte, error) {
return yaml.Marshal(l)
}
func (l ObjectSetList) RenderTable(headers ...string) Table {
table := NewDefaultTable(
WithHeaders(headers),
)
for _, rev := range l {
table.AddRow(
Field{
Name: "Revision",
Value: rev.Revision(),
},
Field{
Name: "Successful",
Value: rev.HasSucceeded(),
},
Field{
Name: "Change-Cause",
Value: rev.ChangeCause(),
},
)
}
return table
}