Skip to content

Commit

Permalink
Merge pull request #379 from seans3/apply-prune-sort
Browse files Browse the repository at this point in the history
Adds object sorting for apply, prune, and delete
  • Loading branch information
k8s-ci-robot authored Jul 2, 2021
2 parents a8bb60c + 0777e62 commit d0a5567
Show file tree
Hide file tree
Showing 23 changed files with 873 additions and 468 deletions.
10 changes: 5 additions & 5 deletions cmd/apply/cmdapply.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,13 @@ func (r *ApplyRunner) RunE(cmd *cobra.Command, args []string) error {
return err
}

// Only emit status events if we are waiting for status.
// Only print status events if we are waiting for status.
//TODO: This is not the right way to do this. There are situations where
// we do need status events event if we are not waiting for status. The
// printers should be updated to handle this.
var emitStatusEvents bool
var printStatusEvents bool
if r.reconcileTimeout != time.Duration(0) || r.pruneTimeout != time.Duration(0) {
emitStatusEvents = true
printStatusEvents = true
}

// TODO: Fix DemandOneDirectory to no longer return FileNameFlags
Expand Down Expand Up @@ -153,7 +153,7 @@ func (r *ApplyRunner) RunE(cmd *cobra.Command, args []string) error {
ReconcileTimeout: r.reconcileTimeout,
// If we are not waiting for status, tell the applier to not
// emit the events.
EmitStatusEvents: emitStatusEvents,
EmitStatusEvents: printStatusEvents,
NoPrune: r.noPrune,
DryRunStrategy: common.DryRunNone,
PrunePropagationPolicy: prunePropPolicy,
Expand All @@ -164,5 +164,5 @@ func (r *ApplyRunner) RunE(cmd *cobra.Command, args []string) error {
// The printer will print updates from the channel. It will block
// until the channel is closed.
printer := printers.GetPrinter(r.output, r.ioStreams)
return printer.Print(ch, common.DryRunNone)
return printer.Print(ch, common.DryRunNone, printStatusEvents)
}
5 changes: 3 additions & 2 deletions cmd/destroy/cmddestroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,16 @@ func (r *DestroyRunner) RunE(cmd *cobra.Command, args []string) error {
}
// Run the destroyer. It will return a channel where we can receive updates
// to keep track of progress and any issues.
printStatusEvents := r.deleteTimeout != time.Duration(0)
ch := d.Run(inv, apply.DestroyerOptions{
DeleteTimeout: r.deleteTimeout,
DeletePropagationPolicy: deletePropPolicy,
InventoryPolicy: inventoryPolicy,
EmitStatusEvents: r.deleteTimeout != time.Duration(0),
EmitStatusEvents: printStatusEvents,
})

// The printer will print updates from the channel. It will block
// until the channel is closed.
printer := printers.GetPrinter(r.output, r.ioStreams)
return printer.Print(ch, common.DryRunNone)
return printer.Print(ch, common.DryRunNone, printStatusEvents)
}
2 changes: 1 addition & 1 deletion cmd/preview/cmdpreview.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,5 +165,5 @@ func (r *PreviewRunner) RunE(cmd *cobra.Command, args []string) error {
// The printer will print updates from the channel. It will block
// until the channel is closed.
printer := printers.GetPrinter(r.output, r.ioStreams)
return printer.Print(ch, drs)
return printer.Print(ch, drs, false) // Do not print status
}
12 changes: 9 additions & 3 deletions cmd/printers/events/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ func (ef *formatter) FormatErrorEvent(_ event.ErrorEvent) error {

func (ef *formatter) FormatActionGroupEvent(age event.ActionGroupEvent, ags []event.ActionGroup,
as *list.ApplyStats, ps *list.PruneStats, ds *list.DeleteStats, c list.Collector) error {
if age.Action == event.ApplyAction && age.Type == event.Finished {
if age.Action == event.ApplyAction &&
age.Type == event.Finished &&
list.IsLastActionGroup(age, ags) {
output := fmt.Sprintf("%d resource(s) applied. %d created, %d unchanged, %d configured, %d failed",
as.Sum(), as.Created, as.Unchanged, as.Configured, as.Failed)
// Only print information about serverside apply if some of the
Expand All @@ -99,11 +101,15 @@ func (ef *formatter) FormatActionGroupEvent(age event.ActionGroupEvent, ags []ev
ef.print(output)
}

if age.Action == event.PruneAction && age.Type == event.Finished {
if age.Action == event.PruneAction &&
age.Type == event.Finished &&
list.IsLastActionGroup(age, ags) {
ef.print("%d resource(s) pruned, %d skipped, %d failed", ps.Pruned, ps.Skipped, ps.Failed)
}

if age.Action == event.DeleteAction && age.Type == event.Finished {
if age.Action == event.DeleteAction &&
age.Type == event.Finished &&
list.IsLastActionGroup(age, ags) {
ef.print("%d resource(s) deleted, %d skipped", ds.Deleted, ds.Skipped)
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/printers/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ import (
)

type Printer interface {
Print(ch <-chan event.Event, previewStrategy common.DryRunStrategy) error
Print(ch <-chan event.Event, previewStrategy common.DryRunStrategy, printStatus bool) error
}
2 changes: 1 addition & 1 deletion cmd/printers/table/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type Printer struct {
IOStreams genericclioptions.IOStreams
}

func (t *Printer) Print(ch <-chan event.Event, _ common.DryRunStrategy) error {
func (t *Printer) Print(ch <-chan event.Event, _ common.DryRunStrategy, printStatus bool) error {
// Wait for the init event that will give us the set of
// resources.
var initEvent event.InitEvent
Expand Down
4 changes: 0 additions & 4 deletions examples/alphaTestExamples/helloapp.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,6 @@ rm $BASE/configMap.yaml
kapply apply $BASE --reconcile-timeout=120s > $OUTPUT/status;
expectedOutputLine "deployment.apps/the-deployment is Current: Deployment is available. Replicas: 3"
expectedOutputLine "service/the-service is Current: Service is ready"
expectedOutputLine "configmap/the-map2 is Current: Resource is always ready"
expectedOutputLine "configmap/the-map1 pruned"
Expand Down
1 change: 0 additions & 1 deletion examples/alphaTestExamples/inventoryNamespace.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ kapply apply $BASE --reconcile-timeout=1m > $OUTPUT/status
expectedOutputLine "namespace/test-namespace unchanged"
expectedOutputLine "configmap/cm-a created"
expectedOutputLine "2 resource(s) applied. 1 created, 1 unchanged, 0 configured"
expectedOutputLine "0 resource(s) pruned, 0 skipped"
# There should be only one inventory object
kubectl get cm -n test-namespace --selector='cli-utils.sigs.k8s.io/inventory-id' --no-headers | wc -l > $OUTPUT/status
Expand Down
1 change: 0 additions & 1 deletion examples/alphaTestExamples/pruneBasic.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ kapply apply $BASE --reconcile-timeout=1m > $OUTPUT/status
expectedOutputLine "configmap/cm-a created"
expectedOutputLine "configmap/cm-b created"
expectedOutputLine "configmap/cm-c created"
expectedOutputLine "0 resource(s) pruned, 0 skipped"
# There should be only one inventory object
kubectl get cm --selector='cli-utils.sigs.k8s.io/inventory-id' --no-headers | wc -l > $OUTPUT/status
Expand Down
1 change: 0 additions & 1 deletion examples/alphaTestExamples/pruneNamespace.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ expectedOutputLine "configmap/cm-a created"
expectedOutputLine "configmap/cm-b created"
expectedOutputLine "configmap/cm-c created"
expectedOutputLine "4 resource(s) applied. 4 created, 0 unchanged, 0 configured"
expectedOutputLine "0 resource(s) pruned, 0 skipped"
# There should be only one inventory object
kubectl get cm --selector='cli-utils.sigs.k8s.io/inventory-id' --no-headers | wc -l > $OUTPUT/status
Expand Down
29 changes: 16 additions & 13 deletions pkg/apply/applier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,42 +315,41 @@ func TestApplier(t *testing.T) {
testutil.Unstructured(t, resources["deployment"], testutil.AddOwningInv(t, "test")),
testutil.Unstructured(t, resources["secret"], testutil.AddOwningInv(t, "test")),
},
reconcileTimeout: time.Minute,
prune: true,
inventoryPolicy: inventory.InventoryPolicyMustMatch,
statusEvents: []pollevent.Event{},
prune: true,
inventoryPolicy: inventory.InventoryPolicyMustMatch,
statusEvents: []pollevent.Event{},
expectedEvents: []testutil.ExpEvent{
{
EventType: event.InitType,
},
{
// Inventory task starting
EventType: event.ActionGroupType,
},
{
// Inventory task finished
EventType: event.ActionGroupType,
},
{
// Prune task starting
EventType: event.ActionGroupType,
},
{
EventType: event.ActionGroupType,
},
{
EventType: event.ActionGroupType,
},
{
// Prune object
EventType: event.PruneType,
PruneEvent: &testutil.ExpPruneEvent{
Operation: event.Pruned,
},
},
{
// Prune object
EventType: event.PruneType,
PruneEvent: &testutil.ExpPruneEvent{
Operation: event.Pruned,
},
},
{
// Prune task finished
EventType: event.ActionGroupType,
},
},
Expand Down Expand Up @@ -576,7 +575,7 @@ func TestApplier(t *testing.T) {
})

var events []event.Event
timer := time.NewTimer(30 * time.Second)
timer := time.NewTimer(10 * time.Second)

loop:
for {
Expand All @@ -586,9 +585,13 @@ func TestApplier(t *testing.T) {
break loop
}
if e.Type == event.ActionGroupType &&
e.ActionGroupEvent.Action == event.ApplyAction &&
e.ActionGroupEvent.Type == event.Finished {
close(poller.start)
// If we do not also check for PruneAction, then the tests
// hang, timeout, and fail.
if e.ActionGroupEvent.Action == event.ApplyAction ||
e.ActionGroupEvent.Action == event.PruneAction {
close(poller.start)
}
}
events = append(events, e)
case <-timer.C:
Expand Down
Loading

0 comments on commit d0a5567

Please sign in to comment.