@@ -16,6 +16,7 @@ import (
16
16
"k8s.io/klog/v2"
17
17
cmdutil "k8s.io/kubectl/pkg/cmd/util"
18
18
"k8s.io/kubectl/pkg/util"
19
+ "sigs.k8s.io/cli-utils/pkg/apis/actuation"
19
20
"sigs.k8s.io/cli-utils/pkg/common"
20
21
"sigs.k8s.io/cli-utils/pkg/object"
21
22
)
@@ -34,7 +35,7 @@ type Client interface {
34
35
Merge (inv Info , objs object.ObjMetadataSet , dryRun common.DryRunStrategy ) (object.ObjMetadataSet , error )
35
36
// Replace replaces the set of objects stored in the inventory
36
37
// 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
38
39
// DeleteInventoryObj deletes the passed inventory object from the APIServer.
39
40
DeleteInventoryObj (inv Info , dryRun common.DryRunStrategy ) error
40
41
// 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
100
101
}
101
102
if clusterInv == nil {
102
103
// Wrap inventory object and store the inventory in it.
104
+ status := getObjStatus (nil , objs )
103
105
inv := cic .InventoryFactoryFunc (invObj )
104
- if err := inv .Store (objs ); err != nil {
106
+ if err := inv .Store (objs , status ); err != nil {
105
107
return nil , err
106
108
}
107
109
invInfo , err := inv .GetObject ()
@@ -126,10 +128,11 @@ func (cic *ClusterClient) Merge(localInv Info, objs object.ObjMetadataSet, dryRu
126
128
}
127
129
pruneIds = clusterObjs .Diff (objs )
128
130
unionObjs := clusterObjs .Union (objs )
131
+ status := getObjStatus (pruneIds , unionObjs )
129
132
klog .V (4 ).Infof ("num objects to prune: %d" , len (pruneIds ))
130
133
klog .V (4 ).Infof ("num merged objects to store in inventory: %d" , len (unionObjs ))
131
134
wrappedInv := cic .InventoryFactoryFunc (clusterInv )
132
- if err = wrappedInv .Store (unionObjs ); err != nil {
135
+ if err = wrappedInv .Store (unionObjs , status ); err != nil {
133
136
return pruneIds , err
134
137
}
135
138
clusterInv , err = wrappedInv .GetObject ()
@@ -158,7 +161,8 @@ func (cic *ClusterClient) Merge(localInv Info, objs object.ObjMetadataSet, dryRu
158
161
159
162
// Replace stores the passed objects in the cluster inventory object, or
160
163
// 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 {
162
166
// Skip entire function for dry-run.
163
167
if dryRun .ClientOrServerDryRun () {
164
168
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
172
176
if err != nil {
173
177
return fmt .Errorf ("failed to read inventory from cluster: %w" , err )
174
178
}
175
- clusterInv , err = cic .replaceInventory (clusterInv , objs )
179
+ clusterInv , err = cic .replaceInventory (clusterInv , objs , status )
176
180
if err != nil {
177
181
return err
178
182
}
@@ -193,9 +197,10 @@ func (cic *ClusterClient) Replace(localInv Info, objs object.ObjMetadataSet, dry
193
197
}
194
198
195
199
// 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 ) {
197
202
wrappedInv := cic .InventoryFactoryFunc (inv )
198
- if err := wrappedInv .Store (objs ); err != nil {
203
+ if err := wrappedInv .Store (objs , status ); err != nil {
199
204
return nil , err
200
205
}
201
206
clusterInv , err := wrappedInv .GetObject ()
@@ -493,3 +498,28 @@ func (cic *ClusterClient) hasSubResource(groupVersion, resource, subresource str
493
498
}
494
499
return false , nil
495
500
}
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