-
Notifications
You must be signed in to change notification settings - Fork 75
/
inventory-client.go
501 lines (449 loc) · 17.7 KB
/
inventory-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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
// Copyright 2020 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package inventory
import (
"context"
"fmt"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/discovery"
"k8s.io/client-go/dynamic"
"k8s.io/klog/v2"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
"k8s.io/kubectl/pkg/util"
"sigs.k8s.io/cli-utils/pkg/apis/actuation"
"sigs.k8s.io/cli-utils/pkg/common"
"sigs.k8s.io/cli-utils/pkg/object"
)
// Client expresses an interface for interacting with
// objects which store references to objects (inventory objects).
type Client interface {
// GetClusterObjs returns the set of previously applied objects as ObjMetadata,
// or an error if one occurred. This set of previously applied object references
// is stored in the inventory objects living in the cluster.
GetClusterObjs(inv Info) (object.ObjMetadataSet, error)
// Merge applies the union of the passed objects with the currently
// stored objects in the inventory object. Returns the set of
// objects which are not in the passed objects (objects to be pruned).
// Otherwise, returns an error if one happened.
Merge(inv Info, objs object.ObjMetadataSet, dryRun common.DryRunStrategy) (object.ObjMetadataSet, error)
// Replace replaces the set of objects stored in the inventory
// object with the passed set of objects, or an error if one occurs.
Replace(inv Info, objs object.ObjMetadataSet, status []actuation.ObjectStatus, dryRun common.DryRunStrategy) error
// DeleteInventoryObj deletes the passed inventory object from the APIServer.
DeleteInventoryObj(inv Info, dryRun common.DryRunStrategy) error
// ApplyInventoryNamespace applies the Namespace that the inventory object should be in.
ApplyInventoryNamespace(invNamespace *unstructured.Unstructured, dryRun common.DryRunStrategy) error
// GetClusterInventoryInfo returns the cluster inventory object.
GetClusterInventoryInfo(inv Info) (*unstructured.Unstructured, error)
// GetClusterInventoryObjs looks up the inventory objects from the cluster.
GetClusterInventoryObjs(inv Info) (object.UnstructuredSet, error)
// ListClusterInventoryObjs returns a map mapping from inventory name to a list of cluster inventory objects
ListClusterInventoryObjs(ctx context.Context) (map[string]object.ObjMetadataSet, error)
}
// ClusterClient is a concrete implementation of the
// Client interface.
type ClusterClient struct {
dc dynamic.Interface
discoveryClient discovery.CachedDiscoveryInterface
mapper meta.RESTMapper
InventoryFactoryFunc StorageFactoryFunc
invToUnstructuredFunc ToUnstructuredFunc
statusPolicy StatusPolicy
gvk schema.GroupVersionKind
}
var _ Client = &ClusterClient{}
// NewClient returns a concrete implementation of the
// Client interface or an error.
func NewClient(factory cmdutil.Factory,
invFunc StorageFactoryFunc,
invToUnstructuredFunc ToUnstructuredFunc,
statusPolicy StatusPolicy,
gvk schema.GroupVersionKind,
) (*ClusterClient, error) {
dc, err := factory.DynamicClient()
if err != nil {
return nil, err
}
mapper, err := factory.ToRESTMapper()
if err != nil {
return nil, err
}
discoveryClinet, err := factory.ToDiscoveryClient()
if err != nil {
return nil, err
}
clusterClient := ClusterClient{
dc: dc,
discoveryClient: discoveryClinet,
mapper: mapper,
InventoryFactoryFunc: invFunc,
invToUnstructuredFunc: invToUnstructuredFunc,
statusPolicy: statusPolicy,
gvk: gvk,
}
return &clusterClient, nil
}
// Merge stores the union of the passed objects with the objects currently
// stored in the cluster inventory object. Retrieves and caches the cluster
// inventory object. Returns the set differrence of the cluster inventory
// objects and the currently applied objects. This is the set of objects
// to prune. Creates the initial cluster inventory object storing the passed
// objects if an inventory object does not exist. Returns an error if one
// occurred.
func (cic *ClusterClient) Merge(localInv Info, objs object.ObjMetadataSet, dryRun common.DryRunStrategy) (object.ObjMetadataSet, error) {
pruneIds := object.ObjMetadataSet{}
invObj := cic.invToUnstructuredFunc(localInv)
clusterInv, err := cic.GetClusterInventoryInfo(localInv)
if err != nil {
return pruneIds, err
}
// Inventory does not exist on the cluster.
if clusterInv == nil {
// Wrap inventory object and store the inventory in it.
var status []actuation.ObjectStatus
if cic.statusPolicy == StatusPolicyAll {
status = getObjStatus(nil, objs)
}
inv := cic.InventoryFactoryFunc(invObj)
if err := inv.Store(objs, status); err != nil {
return nil, err
}
klog.V(4).Infof("creating initial inventory object with %d objects", len(objs))
if dryRun.ClientOrServerDryRun() {
klog.V(4).Infof("dry-run create inventory object: not created")
return nil, nil
}
err = inv.Apply(cic.dc, cic.mapper, cic.statusPolicy)
return nil, err
}
// Update existing cluster inventory with merged union of objects
clusterObjs, err := cic.GetClusterObjs(localInv)
if err != nil {
return pruneIds, err
}
pruneIds = clusterObjs.Diff(objs)
unionObjs := clusterObjs.Union(objs)
var status []actuation.ObjectStatus
if cic.statusPolicy == StatusPolicyAll {
status = getObjStatus(pruneIds, unionObjs)
}
klog.V(4).Infof("num objects to prune: %d", len(pruneIds))
klog.V(4).Infof("num merged objects to store in inventory: %d", len(unionObjs))
wrappedInv := cic.InventoryFactoryFunc(clusterInv)
if err = wrappedInv.Store(unionObjs, status); err != nil {
return pruneIds, err
}
// Update not required when all objects in inventory are the same and
// status does not need to be updated. If status is stored, always update the
// inventory to store the latest status.
if objs.Equal(clusterObjs) && cic.statusPolicy == StatusPolicyNone {
return pruneIds, nil
}
if dryRun.ClientOrServerDryRun() {
klog.V(4).Infof("dry-run create inventory object: not created")
return pruneIds, nil
}
err = wrappedInv.Apply(cic.dc, cic.mapper, cic.statusPolicy)
return pruneIds, err
}
// Replace stores the passed objects in the cluster inventory object, or
// an error if one occurred.
func (cic *ClusterClient) Replace(localInv Info, objs object.ObjMetadataSet, status []actuation.ObjectStatus,
dryRun common.DryRunStrategy) error {
// Skip entire function for dry-run.
if dryRun.ClientOrServerDryRun() {
klog.V(4).Infoln("dry-run replace inventory object: not applied")
return nil
}
clusterInv, err := cic.GetClusterInventoryInfo(localInv)
if err != nil {
return fmt.Errorf("failed to read inventory from cluster: %w", err)
}
clusterObjs, err := cic.GetClusterObjs(localInv)
if err != nil {
return fmt.Errorf("failed to read inventory objects from cluster: %w", err)
}
clusterInv, wrappedInv, err := cic.replaceInventory(clusterInv, objs, status)
if err != nil {
return err
}
// Update not required when all objects in inventory are the same and
// status does not need to be updated. If status is stored, always update the
// inventory to store the latest status.
if objs.Equal(clusterObjs) && cic.statusPolicy == StatusPolicyNone {
return nil
}
klog.V(4).Infof("replace cluster inventory: %s/%s", clusterInv.GetNamespace(), clusterInv.GetName())
klog.V(4).Infof("replace cluster inventory %d objects", len(objs))
if err := wrappedInv.ApplyWithPrune(cic.dc, cic.mapper, cic.statusPolicy, objs); err != nil {
return fmt.Errorf("failed to write updated inventory to cluster: %w", err)
}
return nil
}
// replaceInventory stores the passed objects into the passed inventory object.
func (cic *ClusterClient) replaceInventory(inv *unstructured.Unstructured, objs object.ObjMetadataSet,
status []actuation.ObjectStatus) (*unstructured.Unstructured, Storage, error) {
if cic.statusPolicy == StatusPolicyNone {
status = nil
}
wrappedInv := cic.InventoryFactoryFunc(inv)
if err := wrappedInv.Store(objs, status); err != nil {
return nil, nil, err
}
clusterInv, err := wrappedInv.GetObject()
if err != nil {
return nil, nil, err
}
return clusterInv, wrappedInv, nil
}
// DeleteInventoryObj deletes the inventory object from the cluster.
func (cic *ClusterClient) DeleteInventoryObj(localInv Info, dryRun common.DryRunStrategy) error {
if localInv == nil {
return fmt.Errorf("retrieving cluster inventory object with nil local inventory")
}
switch localInv.Strategy() {
case NameStrategy:
return cic.deleteInventoryObjByName(cic.invToUnstructuredFunc(localInv), dryRun)
case LabelStrategy:
return cic.deleteInventoryObjsByLabel(localInv, dryRun)
default:
panic(fmt.Errorf("unknown inventory strategy: %s", localInv.Strategy()))
}
}
func (cic *ClusterClient) deleteInventoryObjsByLabel(inv Info, dryRun common.DryRunStrategy) error {
clusterInvObjs, err := cic.getClusterInventoryObjsByLabel(inv)
if err != nil {
return err
}
for _, invObj := range clusterInvObjs {
if err := cic.deleteInventoryObjByName(invObj, dryRun); err != nil {
return err
}
}
return nil
}
// GetClusterObjs returns the objects stored in the cluster inventory object, or
// an error if one occurred.
func (cic *ClusterClient) GetClusterObjs(localInv Info) (object.ObjMetadataSet, error) {
var objs object.ObjMetadataSet
clusterInv, err := cic.GetClusterInventoryInfo(localInv)
if err != nil {
return objs, fmt.Errorf("failed to read inventory from cluster: %w", err)
}
// First time; no inventory obj yet.
if clusterInv == nil {
return objs, nil
}
wrapped := cic.InventoryFactoryFunc(clusterInv)
return wrapped.Load()
}
// getClusterInventoryObj returns a pointer to the cluster inventory object, or
// an error if one occurred. Returns the cached cluster inventory object if it
// has been previously retrieved. Uses the ResourceBuilder to retrieve the
// inventory object in the cluster, using the namespace, group resource, and
// inventory label. Merges multiple inventory objects into one if it retrieves
// more than one (this should be very rare).
//
// TODO(seans3): Remove the special case code to merge multiple cluster inventory
// objects once we've determined that this case is no longer possible.
func (cic *ClusterClient) GetClusterInventoryInfo(inv Info) (*unstructured.Unstructured, error) {
clusterInvObjects, err := cic.GetClusterInventoryObjs(inv)
if err != nil {
return nil, fmt.Errorf("failed to read inventory objects from cluster: %w", err)
}
var clusterInv *unstructured.Unstructured
if len(clusterInvObjects) == 1 {
clusterInv = clusterInvObjects[0]
} else if l := len(clusterInvObjects); l > 1 {
return nil, fmt.Errorf("found %d inventory objects with inventory id %s", l, inv.ID())
}
return clusterInv, nil
}
func (cic *ClusterClient) getClusterInventoryObjsByLabel(inv Info) (object.UnstructuredSet, error) {
localInv := cic.invToUnstructuredFunc(inv)
if localInv == nil {
return nil, fmt.Errorf("retrieving cluster inventory object with nil local inventory")
}
localObj := object.UnstructuredToObjMetadata(localInv)
mapping, err := cic.getMapping(localInv)
if err != nil {
return nil, err
}
groupResource := mapping.Resource.GroupResource().String()
namespace := localObj.Namespace
label, err := retrieveInventoryLabel(localInv)
if err != nil {
return nil, err
}
labelSelector := fmt.Sprintf("%s=%s", common.InventoryLabel, label)
klog.V(4).Infof("inventory object fetch by label (group: %q, namespace: %q, selector: %q)", groupResource, namespace, labelSelector)
uList, err := cic.dc.Resource(mapping.Resource).Namespace(namespace).List(context.TODO(), metav1.ListOptions{
LabelSelector: labelSelector,
})
if err != nil {
return nil, err
}
var invList []*unstructured.Unstructured
for i := range uList.Items {
invList = append(invList, &uList.Items[i])
}
return invList, nil
}
func (cic *ClusterClient) getClusterInventoryObjsByName(inv Info) (object.UnstructuredSet, error) {
localInv := cic.invToUnstructuredFunc(inv)
if localInv == nil {
return nil, fmt.Errorf("retrieving cluster inventory object with nil local inventory")
}
mapping, err := cic.getMapping(localInv)
if err != nil {
return nil, err
}
klog.V(4).Infof("inventory object fetch by name (namespace: %q, name: %q)", inv.Namespace(), inv.Name())
clusterInv, err := cic.dc.Resource(mapping.Resource).Namespace(inv.Namespace()).
Get(context.TODO(), inv.Name(), metav1.GetOptions{})
if err != nil && !apierrors.IsNotFound(err) {
return nil, err
}
if apierrors.IsNotFound(err) {
return object.UnstructuredSet{}, nil
}
return object.UnstructuredSet{clusterInv}, nil
}
func (cic *ClusterClient) GetClusterInventoryObjs(inv Info) (object.UnstructuredSet, error) {
if inv == nil {
return nil, fmt.Errorf("inventoryInfo must be specified")
}
var clusterInvObjects object.UnstructuredSet
var err error
switch inv.Strategy() {
case NameStrategy:
clusterInvObjects, err = cic.getClusterInventoryObjsByName(inv)
case LabelStrategy:
clusterInvObjects, err = cic.getClusterInventoryObjsByLabel(inv)
default:
panic(fmt.Errorf("unknown inventory strategy: %s", inv.Strategy()))
}
return clusterInvObjects, err
}
func (cic *ClusterClient) ListClusterInventoryObjs(ctx context.Context) (map[string]object.ObjMetadataSet, error) {
// Define the mapping
mapping, err := cic.mapper.RESTMapping(cic.gvk.GroupKind(), cic.gvk.Version)
if err != nil {
return nil, err
}
// retrieve the list from the cluster
clusterInvs, err := cic.dc.Resource(mapping.Resource).List(ctx, metav1.ListOptions{})
if err != nil && !apierrors.IsNotFound(err) {
return nil, err
}
if apierrors.IsNotFound(err) {
return map[string]object.ObjMetadataSet{}, nil
}
identifiers := make(map[string]object.ObjMetadataSet)
for i, inv := range clusterInvs.Items {
invName := inv.GetName()
identifiers[invName] = object.ObjMetadataSet{}
wrappedInvObjSlice, err := cic.InventoryFactoryFunc(&clusterInvs.Items[i]).Load()
if err != nil {
return nil, err
}
identifiers[invName] = append(identifiers[invName], wrappedInvObjSlice...)
}
return identifiers, nil
}
// createInventoryObj creates the passed inventory object on the APIServer.
func (cic *ClusterClient) createInventoryObj(obj *unstructured.Unstructured, dryRun common.DryRunStrategy) (*unstructured.Unstructured, error) {
if dryRun.ClientOrServerDryRun() {
klog.V(4).Infof("dry-run create inventory object: not created")
return obj.DeepCopy(), nil
}
if obj == nil {
return nil, fmt.Errorf("attempting create a nil inventory object")
}
// Default inventory name gets random suffix. Fixes problem where legacy
// inventory templates within same namespace will collide on name.
err := fixLegacyInventoryName(obj)
if err != nil {
return nil, err
}
mapping, err := cic.getMapping(obj)
if err != nil {
return nil, err
}
klog.V(4).Infof("creating inventory object: %s/%s", obj.GetNamespace(), obj.GetName())
return cic.dc.Resource(mapping.Resource).Namespace(obj.GetNamespace()).
Create(context.TODO(), obj, metav1.CreateOptions{})
}
// deleteInventoryObjByName deletes the passed inventory object from the APIServer, or
// an error if one occurs.
func (cic *ClusterClient) deleteInventoryObjByName(obj *unstructured.Unstructured, dryRun common.DryRunStrategy) error {
if dryRun.ClientOrServerDryRun() {
klog.V(4).Infof("dry-run delete inventory object: not deleted")
return nil
}
if obj == nil {
return fmt.Errorf("attempting delete a nil inventory object")
}
mapping, err := cic.getMapping(obj)
if err != nil {
return err
}
klog.V(4).Infof("deleting inventory object: %s/%s", obj.GetNamespace(), obj.GetName())
return cic.dc.Resource(mapping.Resource).Namespace(obj.GetNamespace()).
Delete(context.TODO(), obj.GetName(), metav1.DeleteOptions{})
}
// ApplyInventoryNamespace creates the passed namespace if it does not already
// exist, or returns an error if one happened. NOTE: No error if already exists.
func (cic *ClusterClient) ApplyInventoryNamespace(obj *unstructured.Unstructured, dryRun common.DryRunStrategy) error {
if dryRun.ClientOrServerDryRun() {
klog.V(4).Infof("dry-run apply inventory namespace (%s): not applied", obj.GetName())
return nil
}
invNamespace := obj.DeepCopy()
klog.V(4).Infof("applying inventory namespace: %s", obj.GetName())
object.StripKyamlAnnotations(invNamespace)
if err := util.CreateApplyAnnotation(invNamespace, unstructured.UnstructuredJSONScheme); err != nil {
return err
}
mapping, err := cic.getMapping(obj)
if err != nil {
return err
}
_, err = cic.dc.Resource(mapping.Resource).Create(context.TODO(), invNamespace, metav1.CreateOptions{})
if apierrors.IsAlreadyExists(err) {
return nil
}
return err
}
// getMapping returns the RESTMapping for the provided resource.
func (cic *ClusterClient) getMapping(obj *unstructured.Unstructured) (*meta.RESTMapping, error) {
return cic.mapper.RESTMapping(obj.GroupVersionKind().GroupKind(), obj.GroupVersionKind().Version)
}
// getObjStatus returns the list of object status
// at the beginning of an apply process.
func getObjStatus(pruneIds, unionIds []object.ObjMetadata) []actuation.ObjectStatus {
status := []actuation.ObjectStatus{}
for _, obj := range unionIds {
status = append(status,
actuation.ObjectStatus{
ObjectReference: ObjectReferenceFromObjMetadata(obj),
Strategy: actuation.ActuationStrategyApply,
Actuation: actuation.ActuationPending,
Reconcile: actuation.ReconcilePending,
})
}
for _, obj := range pruneIds {
status = append(status,
actuation.ObjectStatus{
ObjectReference: ObjectReferenceFromObjMetadata(obj),
Strategy: actuation.ActuationStrategyDelete,
Actuation: actuation.ActuationPending,
Reconcile: actuation.ReconcilePending,
})
}
return status
}