Skip to content

Commit 8e98780

Browse files
committed
Fixes bug in destroy command
1 parent 882303d commit 8e98780

File tree

4 files changed

+6
-90
lines changed

4 files changed

+6
-90
lines changed

pkg/apply/destroyer.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ func (d *Destroyer) Run() <-chan event.Event {
8484

8585
go func() {
8686
defer close(ch)
87+
d.invClient.SetDryRunStrategy(d.DryRunStrategy)
8788
infos, err := d.ApplyOptions.GetObjects()
8889
if err != nil {
8990
ch <- event.Event{
@@ -94,12 +95,10 @@ func (d *Destroyer) Run() <-chan event.Event {
9495
}
9596
return
9697
}
97-
// Clear the data/inventory section of the inventory object configmap,
98-
// so the prune will calculate the prune set as all the objects,
99-
// deleting everything. We can ignore the error, since the Prune
100-
// will catch the same problems.
101-
invInfo, nonInvInfos, _ := inventory.SplitInfos(infos)
102-
invInfo, err = d.invClient.ClearInventoryObj(invInfo)
98+
// Force a pruning of all cluster resources by clearing out the
99+
// local resources, and sending only the inventory object to the
100+
// prune.
101+
invInfo, _, err := inventory.SplitInfos(infos)
103102
if err != nil {
104103
ch <- event.Event{
105104
Type: event.ErrorType,
@@ -109,7 +108,7 @@ func (d *Destroyer) Run() <-chan event.Event {
109108
}
110109
return
111110
}
112-
infos = append([]*resource.Info{invInfo}, nonInvInfos...)
111+
infos = []*resource.Info{invInfo}
113112

114113
// Start the event transformer goroutine so we can transform
115114
// the Prune events emitted from the Prune function to Delete
@@ -123,7 +122,6 @@ func (d *Destroyer) Run() <-chan event.Event {
123122
// Now delete the inventory object as well.
124123
inv := inventory.FindInventoryObj(infos)
125124
if inv != nil {
126-
d.invClient.SetDryRunStrategy(d.DryRunStrategy)
127125
_ = d.invClient.DeleteInventoryObj(inv)
128126
}
129127

pkg/inventory/fake-inventory-client.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,6 @@ func (fic *FakeInventoryClient) DeleteInventoryObj(inv *resource.Info) error {
6363
return nil
6464
}
6565

66-
// ClearInventoryObj implements InventoryClient interface function. It does nothing for now.
67-
func (fic *FakeInventoryClient) ClearInventoryObj(inv *resource.Info) (*resource.Info, error) {
68-
return inv, nil
69-
}
70-
7166
func (fic *FakeInventoryClient) SetDryRunStrategy(drs common.DryRunStrategy) {}
7267

7368
func (fic *FakeInventoryClient) SetInventoryFactoryFunc(fn InventoryFactoryFunc) {}

pkg/inventory/inventory-client.go

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ type InventoryClient interface {
3434
Replace(inv *resource.Info, objs []object.ObjMetadata) error
3535
// DeleteInventoryObj deletes the passed inventory object from the APIServer.
3636
DeleteInventoryObj(inv *resource.Info) error
37-
// ClearInventoryObj clears all obj references from the passed inventory object,
38-
// returning the cleared inventory object or an error.
39-
ClearInventoryObj(inv *resource.Info) (*resource.Info, error)
4037
// SetDryRunStrategy sets the dry run strategy on whether this we actually mutate.
4138
SetDryRunStrategy(drs common.DryRunStrategy)
4239
// Sets the function to create the Inventory object.
@@ -389,22 +386,6 @@ func (cic *ClusterInventoryClient) DeleteInventoryObj(info *resource.Info) error
389386
return err
390387
}
391388

392-
// ClearInventoryObj sets an empty inventory, which is used in destroy. Returns the
393-
// cleared inventory or an error if one occurred.
394-
func (cic *ClusterInventoryClient) ClearInventoryObj(invInfo *resource.Info) (*resource.Info, error) {
395-
if invInfo == nil {
396-
return nil, fmt.Errorf("clearing nil inventory object")
397-
}
398-
if !IsInventoryObject(invInfo) {
399-
return nil, fmt.Errorf("attempting to clear non-inventory object")
400-
}
401-
wrapped := cic.InventoryFactoryFunc(invInfo)
402-
if err := wrapped.Store([]object.ObjMetadata{}); err != nil {
403-
return nil, err
404-
}
405-
return wrapped.GetObject()
406-
}
407-
408389
// SetDryRun sets whether the inventory client will mutate the inventory
409390
// object in the cluster.
410391
func (cic *ClusterInventoryClient) SetDryRunStrategy(drs common.DryRunStrategy) {

pkg/inventory/inventory-client_test.go

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -472,64 +472,6 @@ func TestDeleteInventoryObj(t *testing.T) {
472472
}
473473
}
474474

475-
func TestClearInventoryObject(t *testing.T) {
476-
pod1 := ignoreErrInfoToObjMeta(pod1Info)
477-
pod3 := ignoreErrInfoToObjMeta(pod3Info)
478-
inv := storeObjsInInventory(invInfo, []object.ObjMetadata{pod1, pod3})
479-
tests := map[string]struct {
480-
invInfo *resource.Info
481-
isError bool
482-
}{
483-
"Nil info should error": {
484-
invInfo: nil,
485-
isError: true,
486-
},
487-
"Info with nil Object should error": {
488-
invInfo: nilInfo,
489-
isError: true,
490-
},
491-
"Single non-inventory object should error": {
492-
invInfo: pod1Info,
493-
isError: true,
494-
},
495-
"Single inventory object without data should stay cleared": {
496-
invInfo: invInfo,
497-
isError: false,
498-
},
499-
"Single inventory object with data should be cleared": {
500-
invInfo: inv,
501-
isError: false,
502-
},
503-
}
504-
tf := cmdtesting.NewTestFactory().WithNamespace(testNamespace)
505-
defer tf.Cleanup()
506-
507-
for name, tc := range tests {
508-
t.Run(name, func(t *testing.T) {
509-
invClient, _ := NewInventoryClient(tf)
510-
invInfo, err := invClient.ClearInventoryObj(tc.invInfo)
511-
if tc.isError {
512-
if err == nil {
513-
t.Errorf("Should have produced an error, but returned none.")
514-
}
515-
}
516-
if !tc.isError {
517-
if err != nil {
518-
t.Fatalf("Received unexpected error: %s", err)
519-
}
520-
wrapped := WrapInventoryObj(invInfo)
521-
objs, err := wrapped.Load()
522-
if err != nil {
523-
t.Fatalf("Received unexpected error: %s", err)
524-
}
525-
if len(objs) > 0 {
526-
t.Errorf("Inventory object inventory not cleared: %#v\n", objs)
527-
}
528-
}
529-
})
530-
}
531-
}
532-
533475
type invAndObjs struct {
534476
inv *resource.Info
535477
invObjs []object.ObjMetadata

0 commit comments

Comments
 (0)