From dbe05585d3538a953d6e3a765a99fc2871404e51 Mon Sep 17 00:00:00 2001 From: Kostiantyn Kalynovskyi Date: Fri, 30 Apr 2021 01:40:08 +0000 Subject: [PATCH] Close events channel on executor level This will fix a bug, when go routine wasn't finished when program has exited. Results of the bug was that not all defered statemnets were executed, which lead to temp kubeconfig not being cleaned up Relates-To: #541 Closes: #541 Change-Id: If314886d9bed04b2c0f8a5006bc058395f4fdf7a --- pkg/k8s/applier/applier.go | 1 - pkg/k8s/applier/applier_test.go | 9 ++++++++- pkg/phase/executors/k8s_applier.go | 2 +- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/pkg/k8s/applier/applier.go b/pkg/k8s/applier/applier.go index d9f38b62f..4fd8269c9 100644 --- a/pkg/k8s/applier/applier.go +++ b/pkg/k8s/applier/applier.go @@ -71,7 +71,6 @@ func NewApplier(eventCh chan events.Event, f cmdutil.Factory) *Applier { // ApplyBundle apply bundle to kubernetes cluster func (a *Applier) ApplyBundle(bundle document.Bundle, ao ApplyOptions) { - defer close(a.eventChannel) log.Debugf("Getting infos for bundle, inventory id is %s", ao.BundleName) objects, err := a.getObjects(ao.BundleName, bundle, ao.DryRunStrategy) if err != nil { diff --git a/pkg/k8s/applier/applier_test.go b/pkg/k8s/applier/applier_test.go index 5be0d95a6..981ca8a59 100644 --- a/pkg/k8s/applier/applier_test.go +++ b/pkg/k8s/applier/applier_test.go @@ -125,11 +125,18 @@ func TestApplierRun(t *testing.T) { a.Poller = tt.poller } // start writing to channel - go a.ApplyBundle(tt.bundle, opts) + go func(bundle document.Bundle, applyOpts applier.ApplyOptions) { + // since applier doesn't close channel anymore, we need to close it + // after it applier is finished + defer close(eventChan) + a.ApplyBundle(bundle, applyOpts) + }(tt.bundle, opts) + var airEvents []events.Event for e := range eventChan { airEvents = append(airEvents, e) } + var errs []error for _, e := range airEvents { if e.Type == events.ErrorType { diff --git a/pkg/phase/executors/k8s_applier.go b/pkg/phase/executors/k8s_applier.go index 25eac8161..168037918 100644 --- a/pkg/phase/executors/k8s_applier.go +++ b/pkg/phase/executors/k8s_applier.go @@ -75,10 +75,10 @@ func NewKubeApplierExecutor(cfg ifc.ExecutorConfig) (ifc.Executor, error) { // Run executor, should be performed in separate go routine func (e *KubeApplierExecutor) Run(ch chan events.Event, runOpts ifc.RunOptions) { + defer close(ch) applier, filteredBundle, err := e.prepareApplier(ch) if err != nil { handleError(ch, err) - close(ch) return } defer e.cleanup()