From e072e4cce710bc5647c468ddc6dec131ee88ee45 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sat, 27 Nov 2021 02:29:14 +0100 Subject: [PATCH] Fix pipeline backend autodetect (#545) * refactor: - rename IsAvivable -> IsAvailable - drop depricated Kill - make sure backends implement interface - rename backend struct for ide (better info) * docker backend fix autodetect --- pipeline/backend/backend.go | 2 +- pipeline/backend/docker/docker.go | 31 +++++++++++------------ pipeline/backend/docker/pool.go | 8 +++--- pipeline/backend/kubernetes/kubernetes.go | 31 ++++++++++------------- pipeline/backend/types/engine.go | 5 +--- 5 files changed, 35 insertions(+), 42 deletions(-) diff --git a/pipeline/backend/backend.go b/pipeline/backend/backend.go index 06c93a8d4..8bc9dad19 100644 --- a/pipeline/backend/backend.go +++ b/pipeline/backend/backend.go @@ -27,7 +27,7 @@ func init() { func FindEngine(engineName string) (types.Engine, error) { if engineName == "auto-detect" { for _, engine := range engines { - if engine.IsAvivable() { + if engine.IsAvailable() { return engine, nil } } diff --git a/pipeline/backend/docker/docker.go b/pipeline/backend/docker/docker.go index 1bb6915da..baf92762f 100644 --- a/pipeline/backend/docker/docker.go +++ b/pipeline/backend/docker/docker.go @@ -17,28 +17,31 @@ import ( backend "github.com/woodpecker-ci/woodpecker/pipeline/backend/types" ) -type engine struct { +type docker struct { client client.APIClient } +// make sure docker implements Engine +var _ backend.Engine = &docker{} + // New returns a new Docker Engine. func New() backend.Engine { - return &engine{ + return &docker{ client: nil, } } -func (e *engine) Name() string { +func (e *docker) Name() string { return "docker" } -func (e *engine) IsAvivable() bool { - _, err := os.Stat("/.dockerenv") - return os.IsNotExist(err) +func (e *docker) IsAvailable() bool { + _, err := os.Stat("/var/run/docker.sock") + return err == nil } // Load new client for Docker Engine using environment variables. -func (e *engine) Load() error { +func (e *docker) Load() error { cli, err := client.NewClientWithOpts(client.FromEnv) if err != nil { return err @@ -48,7 +51,7 @@ func (e *engine) Load() error { return nil } -func (e *engine) Setup(_ context.Context, conf *backend.Config) error { +func (e *docker) Setup(_ context.Context, conf *backend.Config) error { for _, vol := range conf.Volumes { _, err := e.client.VolumeCreate(noContext, volume.VolumeCreateBody{ Name: vol.Name, @@ -73,7 +76,7 @@ func (e *engine) Setup(_ context.Context, conf *backend.Config) error { return nil } -func (e *engine) Exec(ctx context.Context, proc *backend.Step) error { +func (e *docker) Exec(ctx context.Context, proc *backend.Step) error { config := toConfig(proc) hostConfig := toHostConfig(proc) @@ -144,11 +147,7 @@ func (e *engine) Exec(ctx context.Context, proc *backend.Step) error { return e.client.ContainerStart(ctx, proc.Name, startOpts) } -func (e *engine) Kill(_ context.Context, proc *backend.Step) error { - return e.client.ContainerKill(noContext, proc.Name, "9") -} - -func (e *engine) Wait(ctx context.Context, proc *backend.Step) (*backend.State, error) { +func (e *docker) Wait(ctx context.Context, proc *backend.Step) (*backend.State, error) { wait, errc := e.client.ContainerWait(ctx, proc.Name, "") select { case <-wait: @@ -170,7 +169,7 @@ func (e *engine) Wait(ctx context.Context, proc *backend.Step) (*backend.State, }, nil } -func (e *engine) Tail(ctx context.Context, proc *backend.Step) (io.ReadCloser, error) { +func (e *docker) Tail(ctx context.Context, proc *backend.Step) (io.ReadCloser, error) { logs, err := e.client.ContainerLogs(ctx, proc.Name, logsOpts) if err != nil { return nil, err @@ -187,7 +186,7 @@ func (e *engine) Tail(ctx context.Context, proc *backend.Step) (io.ReadCloser, e return rc, nil } -func (e *engine) Destroy(_ context.Context, conf *backend.Config) error { +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 { diff --git a/pipeline/backend/docker/pool.go b/pipeline/backend/docker/pool.go index 31ab40e4d..79ae88cff 100644 --- a/pipeline/backend/docker/pool.go +++ b/pipeline/backend/docker/pool.go @@ -20,15 +20,15 @@ package docker // func (p *Pool) Reserve(c context.Context) backend.Engine { // select { // case <-c.Done(): -// case engine := <-p.queue: -// return engine +// case docker := <-p.queue: +// return docker // } // return nil // } // // // Release releases the Docker client back to the pool. -// func (p *Pool) Release(engine backend.Engine) { -// p.queue <- engine +// func (p *Pool) Release(docker backend.Engine) { +// p.queue <- docker // } // pool := docker.Pool( diff --git a/pipeline/backend/kubernetes/kubernetes.go b/pipeline/backend/kubernetes/kubernetes.go index 63370803a..a6ddfa272 100644 --- a/pipeline/backend/kubernetes/kubernetes.go +++ b/pipeline/backend/kubernetes/kubernetes.go @@ -8,68 +8,65 @@ import ( "github.com/woodpecker-ci/woodpecker/pipeline/backend/types" ) -type engine struct { +type kube struct { namespace string endpoint string token string } +// make sure kube implements Engine +var _ types.Engine = &kube{} + // New returns a new Kubernetes Engine. func New(namespace, endpoint, token string) types.Engine { - return &engine{ + return &kube{ namespace: namespace, endpoint: endpoint, token: token, } } -func (e *engine) Name() string { +func (e *kube) Name() string { return "kubernetes" } -func (e *engine) IsAvivable() bool { +func (e *kube) IsAvailable() bool { host := os.Getenv("KUBERNETES_SERVICE_HOST") return len(host) > 0 } -func (e *engine) Load() error { +func (e *kube) Load() error { return nil } // Setup the pipeline environment. -func (e *engine) Setup(context.Context, *types.Config) error { +func (e *kube) Setup(context.Context, *types.Config) error { // POST /api/v1/namespaces return nil } -// Start the pipeline step. -func (e *engine) Exec(context.Context, *types.Step) error { +// Exec the pipeline step. +func (e *kube) Exec(context.Context, *types.Step) error { // POST /api/v1/namespaces/{namespace}/pods return nil } -// DEPRECATED -// Kill the pipeline step. -func (e *engine) Kill(context.Context, *types.Step) error { - return nil -} - // Wait for the pipeline step to complete and returns // the completion results. -func (e *engine) Wait(context.Context, *types.Step) (*types.State, error) { +func (e *kube) Wait(context.Context, *types.Step) (*types.State, error) { // GET /api/v1/watch/namespaces/{namespace}/pods // GET /api/v1/watch/namespaces/{namespace}/pods/{name} return nil, nil } // Tail the pipeline step logs. -func (e *engine) Tail(context.Context, *types.Step) (io.ReadCloser, error) { +func (e *kube) Tail(context.Context, *types.Step) (io.ReadCloser, error) { // GET /api/v1/namespaces/{namespace}/pods/{name}/log return nil, nil } // Destroy the pipeline environment. -func (e *engine) Destroy(context.Context, *types.Config) error { +func (e *kube) Destroy(context.Context, *types.Config) error { // DELETE /api/v1/namespaces/{name} return nil } diff --git a/pipeline/backend/types/engine.go b/pipeline/backend/types/engine.go index 7e71773b7..33c09d55a 100644 --- a/pipeline/backend/types/engine.go +++ b/pipeline/backend/types/engine.go @@ -10,7 +10,7 @@ import ( type Engine interface { Name() string - IsAvivable() bool + IsAvailable() bool Load() error @@ -20,9 +20,6 @@ type Engine interface { // Exec start the pipeline step. Exec(context.Context, *Step) error - // Kill the pipeline step. - Kill(context.Context, *Step) error - // Wait for the pipeline step to complete and returns // the completion results. Wait(context.Context, *Step) (*State, error)