From 6785806873e3284cde566e5d1c2c388caf1dc8a6 Mon Sep 17 00:00:00 2001 From: Anbraten <6918444+anbraten@users.noreply.github.com> Date: Fri, 9 Feb 2024 00:04:43 +0100 Subject: [PATCH] Fix backend detection (#3353) closes #3352 --- cli/exec/exec.go | 9 ++++----- cmd/agent/core/agent.go | 5 ++--- cmd/agent/main.go | 2 +- pipeline/backend/backend.go | 23 ++++++----------------- 4 files changed, 13 insertions(+), 26 deletions(-) diff --git a/cli/exec/exec.go b/cli/exec/exec.go index 9fc374a39..aa13b37bf 100644 --- a/cli/exec/exec.go +++ b/cli/exec/exec.go @@ -213,13 +213,12 @@ func execWithAxis(c *cli.Context, file, repoPath string, axis matrix.Axis) error } backendCtx := context.WithValue(c.Context, backendTypes.CliContext, c) - backend.Init([]backendTypes.Backend{ + backends := []backendTypes.Backend{ + kubernetes.New(), docker.New(), local.New(), - kubernetes.New(), - }) - - backendEngine, err := backend.FindBackend(backendCtx, c.String("backend-engine")) + } + backendEngine, err := backend.FindBackend(backendCtx, backends, c.String("backend-engine")) if err != nil { return err } diff --git a/cmd/agent/core/agent.go b/cmd/agent/core/agent.go index 98fa21516..8bfe00fe0 100644 --- a/cmd/agent/core/agent.go +++ b/cmd/agent/core/agent.go @@ -47,7 +47,7 @@ import ( "go.woodpecker-ci.org/woodpecker/v2/version" ) -func run(c *cli.Context, backendEngines []types.Backend) error { +func run(c *cli.Context, backends []types.Backend) error { agentConfigPath := c.String("agent-config") hostname := c.String("hostname") if len(hostname) == 0 { @@ -153,9 +153,8 @@ func run(c *cli.Context, backendEngines []types.Backend) error { // new engine backendCtx := context.WithValue(ctx, types.CliContext, c) - backend.Init(backendEngines) backendName := c.String("backend-engine") - backendEngine, err := backend.FindBackend(backendCtx, backendName) + backendEngine, err := backend.FindBackend(backendCtx, backends, backendName) if err != nil { log.Error().Err(err).Msgf("cannot find backend engine '%s'", backendName) return err diff --git a/cmd/agent/main.go b/cmd/agent/main.go index d83ba1400..062fc0729 100644 --- a/cmd/agent/main.go +++ b/cmd/agent/main.go @@ -24,8 +24,8 @@ import ( func main() { core.RunAgent([]backendTypes.Backend{ + kubernetes.New(), docker.New(), local.New(), - kubernetes.New(), }) } diff --git a/pipeline/backend/backend.go b/pipeline/backend/backend.go index 16688e9a3..ffb370ed7 100644 --- a/pipeline/backend/backend.go +++ b/pipeline/backend/backend.go @@ -21,19 +21,7 @@ import ( "go.woodpecker-ci.org/woodpecker/v2/pipeline/backend/types" ) -var ( - backendsByName map[string]types.Backend - backends []types.Backend -) - -func Init(backends []types.Backend) { - backendsByName = make(map[string]types.Backend) - for _, engine := range backends { - backendsByName[engine.Name()] = engine - } -} - -func FindBackend(ctx context.Context, backendName string) (types.Backend, error) { +func FindBackend(ctx context.Context, backends []types.Backend, backendName string) (types.Backend, error) { if backendName == "auto-detect" { for _, engine := range backends { if engine.IsAvailable(ctx) { @@ -44,10 +32,11 @@ func FindBackend(ctx context.Context, backendName string) (types.Backend, error) return nil, fmt.Errorf("can't detect an available backend engine") } - engine, ok := backendsByName[backendName] - if !ok { - return nil, fmt.Errorf("backend engine '%s' not found", backendName) + for _, engine := range backends { + if engine.Name() == backendName { + return engine, nil + } } - return engine, nil + return nil, fmt.Errorf("backend engine '%s' not found", backendName) }