@@ -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