@@ -89,6 +89,8 @@ func (po *PruneOptions) Prune(localInv inventory.InventoryInfo, localObjs []*uns
89
89
}
90
90
invNamespace := localInv .Namespace ()
91
91
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 )
92
94
// Create the set of namespaces for currently (locally) applied objects, including
93
95
// the namespace the inventory object lives in (if it's not cluster-scoped). When
94
96
// 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
108
110
for _ , clusterObj := range clusterObjs {
109
111
mapping , err := po .mapper .RESTMapping (clusterObj .GroupKind )
110
112
if err != nil {
111
- return err
113
+ localIds = append (localIds , clusterObj )
114
+ eventChannel <- createPruneFailedEvent (clusterObj , err )
115
+ continue
112
116
}
113
117
namespacedClient := po .client .Resource (mapping .Resource ).Namespace (clusterObj .Namespace )
114
118
obj , err := namespacedClient .Get (context .TODO (), clusterObj .Name , metav1.GetOptions {})
@@ -117,11 +121,15 @@ func (po *PruneOptions) Prune(localInv inventory.InventoryInfo, localObjs []*uns
117
121
if apierrors .IsNotFound (err ) {
118
122
continue
119
123
}
120
- return err
124
+ localIds = append (localIds , clusterObj )
125
+ eventChannel <- createPruneFailedEvent (clusterObj , err )
126
+ continue
121
127
}
122
128
metadata , err := meta .Accessor (obj )
123
129
if err != nil {
124
- return err
130
+ localIds = append (localIds , clusterObj )
131
+ eventChannel <- createPruneFailedEvent (clusterObj , err )
132
+ continue
125
133
}
126
134
// If this cluster object is not also a currently applied
127
135
// object, then it has been omitted--prune it. If the cluster
@@ -135,29 +143,32 @@ func (po *PruneOptions) Prune(localInv inventory.InventoryInfo, localObjs []*uns
135
143
// Handle lifecycle directive preventing deletion.
136
144
if preventDeleteAnnotation (metadata .GetAnnotations ()) {
137
145
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 )
139
148
continue
140
149
}
141
150
// If regular pruning (not destroying), skip deleting namespace containing
142
151
// currently applied objects.
143
152
if ! po .Destroy {
144
153
if clusterObj .GroupKind == object .CoreV1Namespace .GroupKind () &&
145
- clusterObj .Name == localInv . Namespace ( ) {
154
+ localNamespaces . Has ( clusterObj .Name ) {
146
155
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 )
148
158
continue
149
159
}
150
160
}
151
161
if ! o .DryRunStrategy .ClientOrServerDryRun () {
152
162
klog .V (4 ).Infof ("prune object delete: %s/%s" , clusterObj .Namespace , clusterObj .Name )
153
163
err = namespacedClient .Delete (context .TODO (), clusterObj .Name , metav1.DeleteOptions {})
154
164
if err != nil {
155
- return err
165
+ eventChannel <- createPruneFailedEvent (clusterObj , err )
166
+ localIds = append (localIds , clusterObj )
167
+ continue
156
168
}
157
169
}
158
- eventChannel <- createPruneEvent (obj , event .Pruned )
170
+ eventChannel <- createPruneEvent (clusterObj , obj , event .Pruned )
159
171
}
160
- localIds := object .UnstructuredsToObjMetas (localObjs )
161
172
return po .InvClient .Replace (localInv , localIds )
162
173
}
163
174
@@ -187,13 +198,26 @@ func preventDeleteAnnotation(annotations map[string]string) bool {
187
198
}
188
199
189
200
// 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 {
191
215
return event.Event {
192
216
Type : event .PruneType ,
193
217
PruneEvent : event.PruneEvent {
194
- Type : event .PruneEventResourceUpdate ,
195
- Operation : op ,
196
- Object : obj ,
218
+ Type : event .PruneEventFailed ,
219
+ Identifier : objMeta ,
220
+ Error : err ,
197
221
},
198
222
}
199
223
}
0 commit comments