fix: backend auto-detection should be consistent (#1618)

Closes https://github.com/woodpecker-ci/woodpecker/issues/1617

The `woodpecker exec` auto-detects the backend by iterating over a map
of backends. However, since Go 1 the runtime randomizes map iteration
order, so a random backend might be chosen during each execution.

PR changes to auto-detection to iterate over the backends list with
predefined priority: `docker`, `local`, `ssh`, `kubernetes`.

---------

Signed-off-by: Alexander Matyushentsev <AMatyushentsev@gmail.com>
This commit is contained in:
Alexander Matyushentsev 2023-03-13 00:07:41 -07:00 committed by GitHub
parent 5e1171d7a7
commit ee969979c6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -11,19 +11,22 @@ import (
"github.com/woodpecker-ci/woodpecker/pipeline/backend/types" "github.com/woodpecker-ci/woodpecker/pipeline/backend/types"
) )
var engines map[string]types.Engine var (
enginesByName map[string]types.Engine
engines []types.Engine
)
func Init(ctx context.Context) { func Init(ctx context.Context) {
loadedEngines := []types.Engine{ engines = []types.Engine{
docker.New(), docker.New(),
local.New(), local.New(),
ssh.New(), ssh.New(),
kubernetes.New(ctx), kubernetes.New(ctx),
} }
engines = make(map[string]types.Engine) enginesByName = make(map[string]types.Engine)
for _, engine := range loadedEngines { for _, engine := range engines {
engines[engine.Name()] = engine enginesByName[engine.Name()] = engine
} }
} }
@ -38,7 +41,7 @@ func FindEngine(engineName string) (types.Engine, error) {
return nil, fmt.Errorf("Can't detect an available backend engine") return nil, fmt.Errorf("Can't detect an available backend engine")
} }
engine, ok := engines[engineName] engine, ok := enginesByName[engineName]
if !ok { if !ok {
return nil, fmt.Errorf("Backend engine '%s' not found", engineName) return nil, fmt.Errorf("Backend engine '%s' not found", engineName)
} }