Skip to content

Commit

Permalink
Merge pull request #530 from Liujingfang1/master
Browse files Browse the repository at this point in the history
capture the apply/reconcile status in the inventory object
  • Loading branch information
k8s-ci-robot authored Feb 16, 2022
2 parents b450b79 + 6bd4b56 commit cb75cc8
Show file tree
Hide file tree
Showing 10 changed files with 243 additions and 39 deletions.
2 changes: 1 addition & 1 deletion pkg/apply/prune/prune_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func createInventoryInfo(children ...*unstructured.Unstructured) inventory.Info
inventoryObjCopy := inventoryObj.DeepCopy()
wrappedInv := inventory.WrapInventoryObj(inventoryObjCopy)
objs := object.UnstructuredSetToObjMetadataSet(children)
if err := wrappedInv.Store(objs); err != nil {
if err := wrappedInv.Store(objs, nil); err != nil {
return nil
}
obj, err := wrappedInv.GetObject()
Expand Down
5 changes: 4 additions & 1 deletion pkg/apply/task/inv_set_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,11 @@ func (i *InvSetTask) Start(taskContext *taskrunner.TaskContext) {
klog.V(4).Infof("keep in inventory %d invalid objects", len(invalidObjects))
invObjs = invObjs.Union(invalidObjects)

klog.V(4).Infof("get the apply status for %d objects", len(invObjs))
objStatus := taskContext.InventoryManager().Inventory().Status.Objects

klog.V(4).Infof("set inventory %d total objects", len(invObjs))
err := i.InvClient.Replace(i.InvInfo, invObjs, i.DryRun)
err := i.InvClient.Replace(i.InvInfo, invObjs, objStatus, i.DryRun)

klog.V(2).Infof("inventory set task completing (name: %q)", i.Name())
taskContext.TaskChannel() <- taskrunner.TaskResult{Err: err}
Expand Down
11 changes: 7 additions & 4 deletions pkg/inventory/fake-inventory-client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ package inventory
import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
"sigs.k8s.io/cli-utils/pkg/apis/actuation"
"sigs.k8s.io/cli-utils/pkg/common"
"sigs.k8s.io/cli-utils/pkg/object"
)

// FakeClient is a testing implementation of the Client interface.
type FakeClient struct {
Objs object.ObjMetadataSet
Err error
Objs object.ObjMetadataSet
Status []actuation.ObjectStatus
Err error
}

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

// Replace the stored cluster inventory objs with the passed obj, or an
// error if one is set up.

func (fic *FakeClient) Replace(_ Info, objs object.ObjMetadataSet, _ common.DryRunStrategy) error {
func (fic *FakeClient) Replace(_ Info, objs object.ObjMetadataSet, status []actuation.ObjectStatus,
_ common.DryRunStrategy) error {
if fic.Err != nil {
return fic.Err
}
fic.Objs = objs
fic.Status = status
return nil
}

Expand Down
44 changes: 37 additions & 7 deletions pkg/inventory/inventory-client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"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"
)
Expand All @@ -34,7 +35,7 @@ type Client interface {
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, dryRun common.DryRunStrategy) error
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.
Expand Down Expand Up @@ -100,8 +101,9 @@ func (cic *ClusterClient) Merge(localInv Info, objs object.ObjMetadataSet, dryRu
}
if clusterInv == nil {
// Wrap inventory object and store the inventory in it.
status := getObjStatus(nil, objs)
inv := cic.InventoryFactoryFunc(invObj)
if err := inv.Store(objs); err != nil {
if err := inv.Store(objs, status); err != nil {
return nil, err
}
invInfo, err := inv.GetObject()
Expand All @@ -126,10 +128,11 @@ func (cic *ClusterClient) Merge(localInv Info, objs object.ObjMetadataSet, dryRu
}
pruneIds = clusterObjs.Diff(objs)
unionObjs := clusterObjs.Union(objs)
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); err != nil {
if err = wrappedInv.Store(unionObjs, status); err != nil {
return pruneIds, err
}
clusterInv, err = wrappedInv.GetObject()
Expand Down Expand Up @@ -158,7 +161,8 @@ func (cic *ClusterClient) Merge(localInv Info, objs object.ObjMetadataSet, dryRu

// 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, dryRun common.DryRunStrategy) error {
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")
Expand All @@ -172,7 +176,7 @@ func (cic *ClusterClient) Replace(localInv Info, objs object.ObjMetadataSet, dry
if err != nil {
return fmt.Errorf("failed to read inventory from cluster: %w", err)
}
clusterInv, err = cic.replaceInventory(clusterInv, objs)
clusterInv, err = cic.replaceInventory(clusterInv, objs, status)
if err != nil {
return err
}
Expand All @@ -193,9 +197,10 @@ func (cic *ClusterClient) Replace(localInv Info, objs object.ObjMetadataSet, dry
}

// replaceInventory stores the passed objects into the passed inventory object.
func (cic *ClusterClient) replaceInventory(inv *unstructured.Unstructured, objs object.ObjMetadataSet) (*unstructured.Unstructured, error) {
func (cic *ClusterClient) replaceInventory(inv *unstructured.Unstructured, objs object.ObjMetadataSet,
status []actuation.ObjectStatus) (*unstructured.Unstructured, error) {
wrappedInv := cic.InventoryFactoryFunc(inv)
if err := wrappedInv.Store(objs); err != nil {
if err := wrappedInv.Store(objs, status); err != nil {
return nil, err
}
clusterInv, err := wrappedInv.GetObject()
Expand Down Expand Up @@ -493,3 +498,28 @@ func (cic *ClusterClient) hasSubResource(groupVersion, resource, subresource str
}
return false, nil
}

// 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
}
Loading

0 comments on commit cb75cc8

Please sign in to comment.