Skip to content

Commit d09af58

Browse files
committed
Creates helper function to create prune events
1 parent af9a037 commit d09af58

File tree

1 file changed

+16
-24
lines changed

1 file changed

+16
-24
lines changed

pkg/apply/prune/prune.go

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
apierrors "k8s.io/apimachinery/pkg/api/errors"
1818
"k8s.io/apimachinery/pkg/api/meta"
1919
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
20+
"k8s.io/apimachinery/pkg/runtime"
2021
"k8s.io/apimachinery/pkg/util/sets"
2122
"k8s.io/cli-runtime/pkg/resource"
2223
"k8s.io/client-go/dynamic"
@@ -133,14 +134,7 @@ func (po *PruneOptions) Prune(currentObjects []*resource.Info, eventChannel chan
133134
// Handle lifecycle directive preventing deletion.
134135
if preventDeleteAnnotation(metadata.GetAnnotations()) {
135136
klog.V(7).Infof("prune object lifecycle directive; do not prune: %s", uid)
136-
eventChannel <- event.Event{
137-
Type: event.PruneType,
138-
PruneEvent: event.PruneEvent{
139-
Type: event.PruneEventResourceUpdate,
140-
Operation: event.PruneSkipped,
141-
Object: obj,
142-
},
143-
}
137+
eventChannel <- createPruneEvent(obj, event.PruneSkipped)
144138
continue
145139
}
146140
if !o.DryRun {
@@ -150,14 +144,7 @@ func (po *PruneOptions) Prune(currentObjects []*resource.Info, eventChannel chan
150144
return err
151145
}
152146
}
153-
eventChannel <- event.Event{
154-
Type: event.PruneType,
155-
PruneEvent: event.PruneEvent{
156-
Type: event.PruneEventResourceUpdate,
157-
Operation: event.Pruned,
158-
Object: obj,
159-
},
160-
}
147+
eventChannel <- createPruneEvent(obj, event.Pruned)
161148
}
162149
// Delete previous inventory objects.
163150
pastInventories, err := po.invClient.GetPreviousInventoryObjects(currentInventoryObject)
@@ -177,14 +164,7 @@ func (po *PruneOptions) Prune(currentObjects []*resource.Info, eventChannel chan
177164
return err
178165
}
179166
}
180-
eventChannel <- event.Event{
181-
Type: event.PruneType,
182-
PruneEvent: event.PruneEvent{
183-
Type: event.PruneEventResourceUpdate,
184-
Operation: event.Pruned,
185-
Object: pastGroupInfo.Object,
186-
},
187-
}
167+
eventChannel <- createPruneEvent(pastGroupInfo.Object, event.Pruned)
188168
}
189169
return nil
190170
}
@@ -201,3 +181,15 @@ func preventDeleteAnnotation(annotations map[string]string) bool {
201181
}
202182
return false
203183
}
184+
185+
// createPruneEvent is a helper function to package a prune event.
186+
func createPruneEvent(obj runtime.Object, op event.PruneEventOperation) event.Event {
187+
return event.Event{
188+
Type: event.PruneType,
189+
PruneEvent: event.PruneEvent{
190+
Type: event.PruneEventResourceUpdate,
191+
Operation: op,
192+
Object: obj,
193+
},
194+
}
195+
}

0 commit comments

Comments
 (0)