Fix backend detection (#3353)

closes #3352
This commit is contained in:
Anbraten 2024-02-09 00:04:43 +01:00 committed by GitHub
parent 0b91317cde
commit 6785806873
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 13 additions and 26 deletions

View file

@ -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
}

View file

@ -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

View file

@ -24,8 +24,8 @@ import (
func main() {
core.RunAgent([]backendTypes.Backend{
kubernetes.New(),
docker.New(),
local.New(),
kubernetes.New(),
})
}

View file

@ -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)
}