Skip to content

Commit 922ffef

Browse files
committed
change prune to be continue on error
1 parent 165888d commit 922ffef

File tree

2 files changed

+296
-51
lines changed

2 files changed

+296
-51
lines changed

pkg/apply/prune/prune.go

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ func (po *PruneOptions) Prune(localInv inventory.InventoryInfo, localObjs []*uns
8989
}
9090
invNamespace := localInv.Namespace()
9191
klog.V(4).Infof("prune local inventory object: %s/%s", invNamespace, localInv.Name())
92+
// Get the list of Object Meta from the local objects.
93+
localIds := object.UnstructuredsToObjMetas(localObjs)
9294
// Create the set of namespaces for currently (locally) applied objects, including
9395
// the namespace the inventory object lives in (if it's not cluster-scoped). When
9496
// pruning, check this set of namespaces to ensure these namespaces are not deleted.
@@ -108,7 +110,9 @@ func (po *PruneOptions) Prune(localInv inventory.InventoryInfo, localObjs []*uns
108110
for _, clusterObj := range clusterObjs {
109111
mapping, err := po.mapper.RESTMapping(clusterObj.GroupKind)
110112
if err != nil {
111-
return err
113+
localIds = append(localIds, clusterObj)
114+
eventChannel <- createPruneFailedEvent(clusterObj, err)
115+
continue
112116
}
113117
namespacedClient := po.client.Resource(mapping.Resource).Namespace(clusterObj.Namespace)
114118
obj, err := namespacedClient.Get(context.TODO(), clusterObj.Name, metav1.GetOptions{})
@@ -117,11 +121,15 @@ func (po *PruneOptions) Prune(localInv inventory.InventoryInfo, localObjs []*uns
117121
if apierrors.IsNotFound(err) {
118122
continue
119123
}
120-
return err
124+
localIds = append(localIds, clusterObj)
125+
eventChannel <- createPruneFailedEvent(clusterObj, err)
126+
continue
121127
}
122128
metadata, err := meta.Accessor(obj)
123129
if err != nil {
124-
return err
130+
localIds = append(localIds, clusterObj)
131+
eventChannel <- createPruneFailedEvent(clusterObj, err)
132+
continue
125133
}
126134
// If this cluster object is not also a currently applied
127135
// object, then it has been omitted--prune it. If the cluster
@@ -135,29 +143,32 @@ func (po *PruneOptions) Prune(localInv inventory.InventoryInfo, localObjs []*uns
135143
// Handle lifecycle directive preventing deletion.
136144
if preventDeleteAnnotation(metadata.GetAnnotations()) {
137145
klog.V(7).Infof("prune object lifecycle directive; do not prune: %s", uid)
138-
eventChannel <- createPruneEvent(obj, event.PruneSkipped)
146+
eventChannel <- createPruneEvent(clusterObj, obj, event.PruneSkipped)
147+
localIds = append(localIds, clusterObj)
139148
continue
140149
}
141150
// If regular pruning (not destroying), skip deleting namespace containing
142151
// currently applied objects.
143152
if !po.Destroy {
144153
if clusterObj.GroupKind == object.CoreV1Namespace.GroupKind() &&
145-
clusterObj.Name == localInv.Namespace() {
154+
localNamespaces.Has(clusterObj.Name) {
146155
klog.V(7).Infof("skip pruning inventory namespace: %s", obj)
147-
eventChannel <- createPruneEvent(obj, event.PruneSkipped)
156+
eventChannel <- createPruneEvent(clusterObj, obj, event.PruneSkipped)
157+
localIds = append(localIds, clusterObj)
148158
continue
149159
}
150160
}
151161
if !o.DryRunStrategy.ClientOrServerDryRun() {
152162
klog.V(4).Infof("prune object delete: %s/%s", clusterObj.Namespace, clusterObj.Name)
153163
err = namespacedClient.Delete(context.TODO(), clusterObj.Name, metav1.DeleteOptions{})
154164
if err != nil {
155-
return err
165+
eventChannel <- createPruneFailedEvent(clusterObj, err)
166+
localIds = append(localIds, clusterObj)
167+
continue
156168
}
157169
}
158-
eventChannel <- createPruneEvent(obj, event.Pruned)
170+
eventChannel <- createPruneEvent(clusterObj, obj, event.Pruned)
159171
}
160-
localIds := object.UnstructuredsToObjMetas(localObjs)
161172
return po.InvClient.Replace(localInv, localIds)
162173
}
163174

@@ -187,13 +198,26 @@ func preventDeleteAnnotation(annotations map[string]string) bool {
187198
}
188199

189200
// createPruneEvent is a helper function to package a prune event.
190-
func createPruneEvent(obj *unstructured.Unstructured, op event.PruneEventOperation) event.Event {
201+
func createPruneEvent(id object.ObjMetadata, obj *unstructured.Unstructured, op event.PruneEventOperation) event.Event {
202+
return event.Event{
203+
Type: event.PruneType,
204+
PruneEvent: event.PruneEvent{
205+
Type: event.PruneEventResourceUpdate,
206+
Operation: op,
207+
Object: obj,
208+
Identifier: id,
209+
},
210+
}
211+
}
212+
213+
// createPruneEvent is a helper function to package a prune event for a failure.
214+
func createPruneFailedEvent(objMeta object.ObjMetadata, err error) event.Event {
191215
return event.Event{
192216
Type: event.PruneType,
193217
PruneEvent: event.PruneEvent{
194-
Type: event.PruneEventResourceUpdate,
195-
Operation: op,
196-
Object: obj,
218+
Type: event.PruneEventFailed,
219+
Identifier: objMeta,
220+
Error: err,
197221
},
198222
}
199223
}

0 commit comments

Comments
 (0)