Skip to content

Commit

Permalink
Clean up prune package
Browse files Browse the repository at this point in the history
- Rename PruneOptions to Pruner (it does the work itself)
- Rename variables and internal methods for clarity/consistency
- Extract deleteObject method to improve readability
- Rename GetObject to getObject (only used internally and by test)
- Modify log messages for readability
- Add log support for prune tests
- Make log messages more consistent
  • Loading branch information
karlkfi committed Oct 20, 2021
1 parent d97a96b commit a5030ff
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 128 deletions.
20 changes: 10 additions & 10 deletions pkg/apply/applier.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (

// NewApplier returns a new Applier.
func NewApplier(factory cmdutil.Factory, invClient inventory.InventoryClient, statusPoller poller.Poller) (*Applier, error) {
pruneOpts, err := prune.NewPruneOptions(factory, invClient)
pruner, err := prune.NewPruner(factory, invClient)
if err != nil {
return nil, err
}
Expand All @@ -40,7 +40,7 @@ func NewApplier(factory cmdutil.Factory, invClient inventory.InventoryClient, st
return nil, err
}
return &Applier{
pruneOptions: pruneOpts,
pruner: pruner,
statusPoller: statusPoller,
factory: factory,
invClient: invClient,
Expand All @@ -59,7 +59,7 @@ func NewApplier(factory cmdutil.Factory, invClient inventory.InventoryClient, st
// parameters and/or the set of resources that needs to be applied to the
// cluster, different sets of tasks might be needed.
type Applier struct {
pruneOptions *prune.PruneOptions
pruner *prune.Pruner
statusPoller poller.Poller
factory cmdutil.Factory
invClient inventory.InventoryClient
Expand Down Expand Up @@ -100,7 +100,7 @@ func (a *Applier) prepareObjects(localInv inventory.InventoryInfo, localObjs obj
}
}
}
pruneObjs, err := a.pruneOptions.GetPruneObjs(localInv, localObjs, prune.Options{
pruneObjs, err := a.pruner.GetPruneObjs(localInv, localObjs, prune.Options{
DryRunStrategy: o.DryRunStrategy,
})
if err != nil {
Expand Down Expand Up @@ -155,12 +155,12 @@ func (a *Applier) Run(ctx context.Context, invInfo inventory.InventoryInfo, obje
// Fetch the queue (channel) of tasks that should be executed.
klog.V(4).Infoln("applier building task queue...")
taskBuilder := &solver.TaskQueueBuilder{
PruneOptions: a.pruneOptions,
Factory: a.factory,
InfoHelper: a.infoHelper,
Mapper: mapper,
InvClient: a.invClient,
Destroy: false,
Pruner: a.pruner,
Factory: a.factory,
InfoHelper: a.infoHelper,
Mapper: mapper,
InvClient: a.invClient,
Destroy: false,
}
opts := solver.Options{
ServerSideOptions: options.ServerSideOptions,
Expand Down
18 changes: 9 additions & 9 deletions pkg/apply/destroyer.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ import (
// handled by a separate printer with the KubectlPrinterAdapter bridging
// between the two.
func NewDestroyer(factory cmdutil.Factory, invClient inventory.InventoryClient, statusPoller poller.Poller) (*Destroyer, error) {
pruneOpts, err := prune.NewPruneOptions(factory, invClient)
pruner, err := prune.NewPruner(factory, invClient)
if err != nil {
return nil, fmt.Errorf("error setting up PruneOptions: %w", err)
}
return &Destroyer{
pruneOptions: pruneOpts,
pruner: pruner,
statusPoller: statusPoller,
factory: factory,
invClient: invClient,
Expand All @@ -45,7 +45,7 @@ func NewDestroyer(factory cmdutil.Factory, invClient inventory.InventoryClient,
// Destroyer performs the step of grabbing all the previous inventory objects and
// prune them. This also deletes all the previous inventory objects
type Destroyer struct {
pruneOptions *prune.PruneOptions
pruner *prune.Pruner
statusPoller poller.Poller
factory cmdutil.Factory
invClient inventory.InventoryClient
Expand Down Expand Up @@ -97,7 +97,7 @@ func (d *Destroyer) Run(ctx context.Context, inv inventory.InventoryInfo, option
// Retrieve the objects to be deleted from the cluster. Second parameter is empty
// because no local objects returns all inventory objects for deletion.
emptyLocalObjs := object.UnstructuredSet{}
deleteObjs, err := d.pruneOptions.GetPruneObjs(inv, emptyLocalObjs, prune.Options{
deleteObjs, err := d.pruner.GetPruneObjs(inv, emptyLocalObjs, prune.Options{
DryRunStrategy: options.DryRunStrategy,
})
if err != nil {
Expand All @@ -111,11 +111,11 @@ func (d *Destroyer) Run(ctx context.Context, inv inventory.InventoryInfo, option
}
klog.V(4).Infoln("destroyer building task queue...")
taskBuilder := &solver.TaskQueueBuilder{
PruneOptions: d.pruneOptions,
Factory: d.factory,
Mapper: mapper,
InvClient: d.invClient,
Destroy: true,
Pruner: d.pruner,
Factory: d.factory,
Mapper: mapper,
InvClient: d.invClient,
Destroy: true,
}
opts := solver.Options{
Prune: true,
Expand Down
19 changes: 19 additions & 0 deletions pkg/apply/prune/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2021 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0

package prune

import (
"os"
"testing"

"k8s.io/klog/v2"
)

// TestMain executes the tests for this package, with optional logging.
// To see all logs, use:
// go test sigs.k8s.io/cli-utils/pkg/apply/prune -v -args -v=5
func TestMain(m *testing.M) {
klog.InitFlags(nil)
os.Exit(m.Run())
}
Loading

0 comments on commit a5030ff

Please sign in to comment.