From e178b7b4b2b649dbcb543ab0dca5cf2ab9e7bbf6 Mon Sep 17 00:00:00 2001 From: Anbraten Date: Tue, 8 Mar 2022 16:21:43 +0100 Subject: [PATCH] Improve agent backend loading and suppress expectable errors (#818) * improve agent loop loading backend once * supress container not found or stopped warnings --- cmd/agent/agent.go | 37 +++++++++++++++++-------------- pipeline/backend/backend.go | 17 +++++++------- pipeline/backend/docker/docker.go | 12 ++++++++-- 3 files changed, 38 insertions(+), 28 deletions(-) diff --git a/cmd/agent/agent.go b/cmd/agent/agent.go index a52ceb268..3f6096eca 100644 --- a/cmd/agent/agent.go +++ b/cmd/agent/agent.go @@ -132,28 +132,31 @@ func loop(c *cli.Context) error { for i := 0; i < parallel; i++ { go func() { defer wg.Done() + + // new engine + engine, err := backend.FindEngine(c.String("backend-engine")) + if err != nil { + log.Error().Err(err).Msgf("cannot find backend engine '%s'", c.String("backend-engine")) + return + } + + // load engine (e.g. init api client) + err = engine.Load() + if err != nil { + log.Error().Err(err).Msg("cannot load backend engine") + return + } + + r := agent.NewRunner(client, filter, hostname, counter, &engine) + + log.Debug().Msgf("loaded %s backend engine", engine.Name()) + for { if sigterm.IsSet() { return } - // new engine - engine, err := backend.FindEngine(c.String("backend-engine")) - if err != nil { - log.Error().Err(err).Msgf("cannot find backend engine '%s'", c.String("backend-engine")) - return - } - - // load engine (e.g. init api client) - err = engine.Load() - if err != nil { - log.Error().Err(err).Msg("cannot load backend engine") - return - } - - log.Debug().Msgf("loaded %s backend engine", engine.Name()) - - r := agent.NewRunner(client, filter, hostname, counter, &engine) + log.Debug().Msg("polling new jobs") if err := r.Run(ctx); err != nil { log.Error().Err(err).Msg("pipeline done with error") return diff --git a/pipeline/backend/backend.go b/pipeline/backend/backend.go index 699682fef..4d4ea33b3 100644 --- a/pipeline/backend/backend.go +++ b/pipeline/backend/backend.go @@ -10,16 +10,15 @@ import ( var engines map[string]types.Engine func init() { + loadedEngines := []types.Engine{ + docker.New(), + // kubernetes.New(), // TODO: disabled for now as kubernetes backend has not been implemented yet + } + engines = make(map[string]types.Engine) - - // TODO: disabled for now as kubernetes backend has not been implemented yet - // kubernetes - // engine = kubernetes.New("", "", "") - // engines[engine.Name()] = engine - - // docker - engine := docker.New() - engines[engine.Name()] = engine + for _, engine := range loadedEngines { + engines[engine.Name()] = engine + } } func FindEngine(engineName string) (types.Engine, error) { diff --git a/pipeline/backend/docker/docker.go b/pipeline/backend/docker/docker.go index 575807053..003643dca 100644 --- a/pipeline/backend/docker/docker.go +++ b/pipeline/backend/docker/docker.go @@ -4,6 +4,7 @@ import ( "context" "io" "os" + "strings" "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/network" @@ -192,10 +193,10 @@ func (e *docker) Tail(ctx context.Context, proc *backend.Step) (io.ReadCloser, e func (e *docker) Destroy(_ context.Context, conf *backend.Config) error { for _, stage := range conf.Stages { for _, step := range stage.Steps { - if err := e.client.ContainerKill(noContext, step.Name, "9"); err != nil { + if err := e.client.ContainerKill(noContext, step.Name, "9"); err != nil && !isErrContainerNotFoundOrNotRunning(err) { log.Error().Err(err).Msgf("could not kill container '%s'", stage.Name) } - if err := e.client.ContainerRemove(noContext, step.Name, removeOpts); err != nil { + if err := e.client.ContainerRemove(noContext, step.Name, removeOpts); err != nil && !isErrContainerNotFoundOrNotRunning(err) { log.Error().Err(err).Msgf("could not remove container '%s'", stage.Name) } } @@ -232,3 +233,10 @@ var ( Timestamps: false, } ) + +func isErrContainerNotFoundOrNotRunning(err error) bool { + // Error response from daemon: Cannot kill container: ...: No such container: ... + // Error response from daemon: Cannot kill container: ...: Container ... is not running" + // Error: No such container: ... + return err != nil && (strings.Contains(err.Error(), "No such container") || strings.Contains(err.Error(), "is not running")) +}