Skip to content

Commit 6bd4b56

Browse files
committed
Capture the apply status for individual resources after each apply.
If the inventory object supports status field, it is updated after setting the inventory list at then of one apply process. BREAKING CHANGE: Update the inventory client and inventory interfaces to pass the apply/reconcile status.
1 parent b450b79 commit 6bd4b56

File tree

10 files changed

+243
-39
lines changed

10 files changed

+243
-39
lines changed

pkg/apply/prune/prune_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func createInventoryInfo(children ...*unstructured.Unstructured) inventory.Info
126126
inventoryObjCopy := inventoryObj.DeepCopy()
127127
wrappedInv := inventory.WrapInventoryObj(inventoryObjCopy)
128128
objs := object.UnstructuredSetToObjMetadataSet(children)
129-
if err := wrappedInv.Store(objs); err != nil {
129+
if err := wrappedInv.Store(objs, nil); err != nil {
130130
return nil
131131
}
132132
obj, err := wrappedInv.GetObject()

pkg/apply/task/inv_set_task.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,11 @@ func (i *InvSetTask) Start(taskContext *taskrunner.TaskContext) {
114114
klog.V(4).Infof("keep in inventory %d invalid objects", len(invalidObjects))
115115
invObjs = invObjs.Union(invalidObjects)
116116

117+
klog.V(4).Infof("get the apply status for %d objects", len(invObjs))
118+
objStatus := taskContext.InventoryManager().Inventory().Status.Objects
119+
117120
klog.V(4).Infof("set inventory %d total objects", len(invObjs))
118-
err := i.InvClient.Replace(i.InvInfo, invObjs, i.DryRun)
121+
err := i.InvClient.Replace(i.InvInfo, invObjs, objStatus, i.DryRun)
119122

120123
klog.V(2).Infof("inventory set task completing (name: %q)", i.Name())
121124
taskContext.TaskChannel() <- taskrunner.TaskResult{Err: err}

pkg/inventory/fake-inventory-client.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@ package inventory
66
import (
77
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
88
cmdutil "k8s.io/kubectl/pkg/cmd/util"
9+
"sigs.k8s.io/cli-utils/pkg/apis/actuation"
910
"sigs.k8s.io/cli-utils/pkg/common"
1011
"sigs.k8s.io/cli-utils/pkg/object"
1112
)
1213

1314
// FakeClient is a testing implementation of the Client interface.
1415
type FakeClient struct {
15-
Objs object.ObjMetadataSet
16-
Err error
16+
Objs object.ObjMetadataSet
17+
Status []actuation.ObjectStatus
18+
Err error
1719
}
1820

1921
var (
@@ -57,12 +59,13 @@ func (fic *FakeClient) Merge(_ Info, objs object.ObjMetadataSet, _ common.DryRun
5759

5860
// Replace the stored cluster inventory objs with the passed obj, or an
5961
// error if one is set up.
60-
61-
func (fic *FakeClient) Replace(_ Info, objs object.ObjMetadataSet, _ common.DryRunStrategy) error {
62+
func (fic *FakeClient) Replace(_ Info, objs object.ObjMetadataSet, status []actuation.ObjectStatus,
63+
_ common.DryRunStrategy) error {
6264
if fic.Err != nil {
6365
return fic.Err
6466
}
6567
fic.Objs = objs
68+
fic.Status = status
6669
return nil
6770
}
6871

pkg/inventory/inventory-client.go

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"k8s.io/klog/v2"
1717
cmdutil "k8s.io/kubectl/pkg/cmd/util"
1818
"k8s.io/kubectl/pkg/util"
19+
"sigs.k8s.io/cli-utils/pkg/apis/actuation"
1920
"sigs.k8s.io/cli-utils/pkg/common"
2021
"sigs.k8s.io/cli-utils/pkg/object"
2122
)
@@ -34,7 +35,7 @@ type Client interface {
3435
Merge(inv Info, objs object.ObjMetadataSet, dryRun common.DryRunStrategy) (object.ObjMetadataSet, error)
3536
// Replace replaces the set of objects stored in the inventory
3637
// object with the passed set of objects, or an error if one occurs.
37-
Replace(inv Info, objs object.ObjMetadataSet, dryRun common.DryRunStrategy) error
38+
Replace(inv Info, objs object.ObjMetadataSet, status []actuation.ObjectStatus, dryRun common.DryRunStrategy) error
3839
// DeleteInventoryObj deletes the passed inventory object from the APIServer.
3940
DeleteInventoryObj(inv Info, dryRun common.DryRunStrategy) error
4041
// ApplyInventoryNamespace applies the Namespace that the inventory object should be in.
@@ -100,8 +101,9 @@ func (cic *ClusterClient) Merge(localInv Info, objs object.ObjMetadataSet, dryRu
100101
}
101102
if clusterInv == nil {
102103
// Wrap inventory object and store the inventory in it.
104+
status := getObjStatus(nil, objs)
103105
inv := cic.InventoryFactoryFunc(invObj)
104-
if err := inv.Store(objs); err != nil {
106+
if err := inv.Store(objs, status); err != nil {
105107
return nil, err
106108
}
107109
invInfo, err := inv.GetObject()
@@ -126,10 +128,11 @@ func (cic *ClusterClient) Merge(localInv Info, objs object.ObjMetadataSet, dryRu
126128
}
127129
pruneIds = clusterObjs.Diff(objs)
128130
unionObjs := clusterObjs.Union(objs)
131+
status := getObjStatus(pruneIds, unionObjs)
129132
klog.V(4).Infof("num objects to prune: %d", len(pruneIds))
130133
klog.V(4).Infof("num merged objects to store in inventory: %d", len(unionObjs))
131134
wrappedInv := cic.InventoryFactoryFunc(clusterInv)
132-
if err = wrappedInv.Store(unionObjs); err != nil {
135+
if err = wrappedInv.Store(unionObjs, status); err != nil {
133136
return pruneIds, err
134137
}
135138
clusterInv, err = wrappedInv.GetObject()
@@ -158,7 +161,8 @@ func (cic *ClusterClient) Merge(localInv Info, objs object.ObjMetadataSet, dryRu
158161

159162
// Replace stores the passed objects in the cluster inventory object, or
160163
// an error if one occurred.
161-
func (cic *ClusterClient) Replace(localInv Info, objs object.ObjMetadataSet, dryRun common.DryRunStrategy) error {
164+
func (cic *ClusterClient) Replace(localInv Info, objs object.ObjMetadataSet, status []actuation.ObjectStatus,
165+
dryRun common.DryRunStrategy) error {
162166
// Skip entire function for dry-run.
163167
if dryRun.ClientOrServerDryRun() {
164168
klog.V(4).Infoln("dry-run replace inventory object: not applied")
@@ -172,7 +176,7 @@ func (cic *ClusterClient) Replace(localInv Info, objs object.ObjMetadataSet, dry
172176
if err != nil {
173177
return fmt.Errorf("failed to read inventory from cluster: %w", err)
174178
}
175-
clusterInv, err = cic.replaceInventory(clusterInv, objs)
179+
clusterInv, err = cic.replaceInventory(clusterInv, objs, status)
176180
if err != nil {
177181
return err
178182
}
@@ -193,9 +197,10 @@ func (cic *ClusterClient) Replace(localInv Info, objs object.ObjMetadataSet, dry
193197
}
194198

195199
// replaceInventory stores the passed objects into the passed inventory object.
196-
func (cic *ClusterClient) replaceInventory(inv *unstructured.Unstructured, objs object.ObjMetadataSet) (*unstructured.Unstructured, error) {
200+
func (cic *ClusterClient) replaceInventory(inv *unstructured.Unstructured, objs object.ObjMetadataSet,
201+
status []actuation.ObjectStatus) (*unstructured.Unstructured, error) {
197202
wrappedInv := cic.InventoryFactoryFunc(inv)
198-
if err := wrappedInv.Store(objs); err != nil {
203+
if err := wrappedInv.Store(objs, status); err != nil {
199204
return nil, err
200205
}
201206
clusterInv, err := wrappedInv.GetObject()
@@ -493,3 +498,28 @@ func (cic *ClusterClient) hasSubResource(groupVersion, resource, subresource str
493498
}
494499
return false, nil
495500
}
501+
502+
// getObjStatus returns the list of object status
503+
// at the beginning of an apply process.
504+
func getObjStatus(pruneIds, unionIds []object.ObjMetadata) []actuation.ObjectStatus {
505+
status := []actuation.ObjectStatus{}
506+
for _, obj := range unionIds {
507+
status = append(status,
508+
actuation.ObjectStatus{
509+
ObjectReference: ObjectReferenceFromObjMetadata(obj),
510+
Strategy: actuation.ActuationStrategyApply,
511+
Actuation: actuation.ActuationPending,
512+
Reconcile: actuation.ReconcilePending,
513+
})
514+
}
515+
for _, obj := range pruneIds {
516+
status = append(status,
517+
actuation.ObjectStatus{
518+
ObjectReference: ObjectReferenceFromObjMetadata(obj),
519+
Strategy: actuation.ActuationStrategyDelete,
520+
Actuation: actuation.ActuationPending,
521+
Reconcile: actuation.ReconcilePending,
522+
})
523+
}
524+
return status
525+
}

0 commit comments

Comments
 (0)