From ff1f51d6a9a67bf9c62acd2496882f3136b5bad5 Mon Sep 17 00:00:00 2001 From: qwerty287 <80460567+qwerty287@users.noreply.github.com> Date: Thu, 14 Dec 2023 19:20:47 +0100 Subject: [PATCH] Rename `engine` to `backend` (#2950) rename based on https://woodpecker-ci.org/docs/usage/terminiology --------- Co-authored-by: 6543 <6543@obermui.de> --- agent/runner.go | 8 ++-- cli/exec/exec.go | 6 +-- cmd/agent/agent.go | 18 +++---- cmd/agent/flags.go | 2 +- pipeline/backend/backend.go | 22 ++++----- pipeline/backend/docker/docker.go | 10 ++-- pipeline/backend/kubernetes/kubernetes.go | 11 +++-- pipeline/backend/local/local.go | 8 ++-- pipeline/backend/types/backend.go | 42 +++++++++++++++++ pipeline/backend/types/engine.go | 57 ----------------------- pipeline/option.go | 6 +-- pipeline/pipeline.go | 2 +- 12 files changed, 89 insertions(+), 103 deletions(-) delete mode 100644 pipeline/backend/types/engine.go diff --git a/agent/runner.go b/agent/runner.go index 991751fcc..753785dcd 100644 --- a/agent/runner.go +++ b/agent/runner.go @@ -37,16 +37,16 @@ type Runner struct { filter rpc.Filter hostname string counter *State - engine *backend.Engine + backend *backend.Backend } -func NewRunner(workEngine rpc.Peer, f rpc.Filter, h string, state *State, backend *backend.Engine) Runner { +func NewRunner(workEngine rpc.Peer, f rpc.Filter, h string, state *State, backend *backend.Backend) Runner { return Runner{ client: workEngine, filter: f, hostname: h, counter: state, - engine: backend, + backend: backend, } } @@ -144,7 +144,7 @@ func (r *Runner) Run(runnerCtx context.Context) error { pipeline.WithTaskUUID(fmt.Sprint(work.ID)), pipeline.WithLogger(r.createLogger(logger, &uploads, work)), pipeline.WithTracer(r.createTracer(ctxmeta, logger, work)), - pipeline.WithEngine(*r.engine), + pipeline.WithBackend(*r.backend), pipeline.WithDescription(map[string]string{ "ID": work.ID, "Repo": repoName, diff --git a/cli/exec/exec.go b/cli/exec/exec.go index 96feb4dcc..7e8c7fc66 100644 --- a/cli/exec/exec.go +++ b/cli/exec/exec.go @@ -215,12 +215,12 @@ func execWithAxis(c *cli.Context, file, repoPath string, axis matrix.Axis) error backendCtx := context.WithValue(c.Context, backendTypes.CliContext, c) backend.Init(backendCtx) - engine, err := backend.FindEngine(backendCtx, c.String("backend-engine")) + backendEngine, err := backend.FindBackend(backendCtx, c.String("backend-engine")) if err != nil { return err } - if _, err = engine.Load(backendCtx); err != nil { + if _, err = backendEngine.Load(backendCtx); err != nil { return err } @@ -234,7 +234,7 @@ func execWithAxis(c *cli.Context, file, repoPath string, axis matrix.Axis) error pipeline.WithContext(ctx), pipeline.WithTracer(pipeline.DefaultTracer), pipeline.WithLogger(defaultLogger), - pipeline.WithEngine(engine), + pipeline.WithBackend(backendEngine), pipeline.WithDescription(map[string]string{ "CLI": "exec", }), diff --git a/cmd/agent/agent.go b/cmd/agent/agent.go index 198f1a4bb..66241f856 100644 --- a/cmd/agent/agent.go +++ b/cmd/agent/agent.go @@ -156,22 +156,22 @@ func run(c *cli.Context) error { parallel := c.Int("max-workflows") wg.Add(parallel) - // new engine - engine, err := backend.FindEngine(backendCtx, c.String("backend-engine")) + // new backend + backendEngine, err := backend.FindBackend(backendCtx, c.String("backend-engine")) if err != nil { log.Error().Err(err).Msgf("cannot find backend engine '%s'", c.String("backend-engine")) return err } - // load engine (e.g. init api client) - engInfo, err := engine.Load(backendCtx) + // load backend (e.g. init api client) + engInfo, err := backendEngine.Load(backendCtx) if err != nil { log.Error().Err(err).Msg("cannot load backend engine") return err } - log.Debug().Msgf("loaded %s backend engine", engine.Name()) + log.Debug().Msgf("loaded %s backend engine", backendEngine.Name()) - agentConfig.AgentID, err = client.RegisterAgent(ctx, engInfo.Platform, engine.Name(), version.String(), parallel) + agentConfig.AgentID, err = client.RegisterAgent(ctx, engInfo.Platform, backendEngine.Name(), version.String(), parallel) if err != nil { return err } @@ -185,7 +185,7 @@ func run(c *cli.Context) error { labels := map[string]string{ "hostname": hostname, "platform": engInfo.Platform, - "backend": engine.Name(), + "backend": backendEngine.Name(), "repo": "*", // allow all repos by default } @@ -221,7 +221,7 @@ func run(c *cli.Context) error { go func() { defer wg.Done() - r := agent.NewRunner(client, filter, hostname, counter, &engine) + r := agent.NewRunner(client, filter, hostname, counter, &backendEngine) log.Debug().Msgf("created new runner %d", i) for { @@ -241,7 +241,7 @@ func run(c *cli.Context) error { log.Info().Msgf( "Starting Woodpecker agent with version '%s' and backend '%s' using platform '%s' running up to %d pipelines in parallel", - version.String(), engine.Name(), engInfo.Platform, parallel) + version.String(), backendEngine.Name(), engInfo.Platform, parallel) wg.Wait() return nil diff --git a/cmd/agent/flags.go b/cmd/agent/flags.go index fa43297fe..fed4a3d3b 100644 --- a/cmd/agent/flags.go +++ b/cmd/agent/flags.go @@ -94,7 +94,7 @@ var flags = []cli.Flag{ &cli.StringFlag{ EnvVars: []string{"WOODPECKER_BACKEND"}, Name: "backend-engine", - Usage: "backend engine to run pipelines on", + Usage: "backend to run pipelines on", Value: "auto-detect", }, } diff --git a/pipeline/backend/backend.go b/pipeline/backend/backend.go index 13ef009d3..56d3291d7 100644 --- a/pipeline/backend/backend.go +++ b/pipeline/backend/backend.go @@ -25,26 +25,26 @@ import ( ) var ( - enginesByName map[string]types.Engine - engines []types.Engine + backendsByName map[string]types.Backend + backends []types.Backend ) func Init(ctx context.Context) { - engines = []types.Engine{ + backends = []types.Backend{ docker.New(), local.New(), kubernetes.New(ctx), } - enginesByName = make(map[string]types.Engine) - for _, engine := range engines { - enginesByName[engine.Name()] = engine + backendsByName = make(map[string]types.Backend) + for _, engine := range backends { + backendsByName[engine.Name()] = engine } } -func FindEngine(ctx context.Context, engineName string) (types.Engine, error) { - if engineName == "auto-detect" { - for _, engine := range engines { +func FindBackend(ctx context.Context, backendName string) (types.Backend, error) { + if backendName == "auto-detect" { + for _, engine := range backends { if engine.IsAvailable(ctx) { return engine, nil } @@ -53,9 +53,9 @@ func FindEngine(ctx context.Context, engineName string) (types.Engine, error) { return nil, fmt.Errorf("can't detect an available backend engine") } - engine, ok := enginesByName[engineName] + engine, ok := backendsByName[backendName] if !ok { - return nil, fmt.Errorf("backend engine '%s' not found", engineName) + return nil, fmt.Errorf("backend engine '%s' not found", backendName) } return engine, nil diff --git a/pipeline/backend/docker/docker.go b/pipeline/backend/docker/docker.go index c942c2ceb..bd8dd73ce 100644 --- a/pipeline/backend/docker/docker.go +++ b/pipeline/backend/docker/docker.go @@ -51,8 +51,8 @@ const ( volumeDriver = "local" ) -// New returns a new Docker Engine. -func New() backend.Engine { +// New returns a new Docker Backend. +func New() backend.Backend { return &docker{ client: nil, } @@ -93,8 +93,8 @@ func httpClientOfOpts(dockerCertPath string, verifyTLS bool) *http.Client { } } -// Load new client for Docker Engine using environment variables. -func (e *docker) Load(ctx context.Context) (*backend.EngineInfo, error) { +// Load new client for Docker Backend using environment variables. +func (e *docker) Load(ctx context.Context) (*backend.BackendInfo, error) { c, ok := ctx.Value(backend.CliContext).(*cli.Context) if !ok { return nil, backend.ErrNoCliContextFound @@ -142,7 +142,7 @@ func (e *docker) Load(ctx context.Context) (*backend.EngineInfo, error) { e.volumes = append(e.volumes, strings.Join(parts, ":")) } - return &backend.EngineInfo{ + return &backend.BackendInfo{ Platform: e.info.OSType + "/" + normalizeArchType(e.info.Architecture), }, nil } diff --git a/pipeline/backend/kubernetes/kubernetes.go b/pipeline/backend/kubernetes/kubernetes.go index bdb543bfc..c508c2103 100644 --- a/pipeline/backend/kubernetes/kubernetes.go +++ b/pipeline/backend/kubernetes/kubernetes.go @@ -24,9 +24,10 @@ import ( "time" "github.com/rs/zerolog/log" - "go.woodpecker-ci.org/woodpecker/v2/pipeline/backend/types" "gopkg.in/yaml.v3" + "go.woodpecker-ci.org/woodpecker/v2/pipeline/backend/types" + "github.com/urfave/cli/v2" v1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/errors" @@ -101,8 +102,8 @@ func configFromCliContext(ctx context.Context) (*Config, error) { return nil, types.ErrNoCliContextFound } -// New returns a new Kubernetes Engine. -func New(ctx context.Context) types.Engine { +// New returns a new Kubernetes Backend. +func New(ctx context.Context) types.Backend { return &kube{ ctx: ctx, } @@ -117,7 +118,7 @@ func (e *kube) IsAvailable(context.Context) bool { return len(host) > 0 } -func (e *kube) Load(context.Context) (*types.EngineInfo, error) { +func (e *kube) Load(context.Context) (*types.BackendInfo, error) { config, err := configFromCliContext(e.ctx) if err != nil { return nil, err @@ -140,7 +141,7 @@ func (e *kube) Load(context.Context) (*types.EngineInfo, error) { // TODO(2693): use info resp of kubeClient to define platform var e.goos = runtime.GOOS - return &types.EngineInfo{ + return &types.BackendInfo{ Platform: runtime.GOOS + "/" + runtime.GOARCH, }, nil } diff --git a/pipeline/backend/local/local.go b/pipeline/backend/local/local.go index ca67c1616..8b3cd5231 100644 --- a/pipeline/backend/local/local.go +++ b/pipeline/backend/local/local.go @@ -50,8 +50,8 @@ type local struct { os, arch string } -// New returns a new local Engine. -func New() types.Engine { +// New returns a new local Backend. +func New() types.Backend { return &local{ os: runtime.GOOS, arch: runtime.GOARCH, @@ -66,7 +66,7 @@ func (e *local) IsAvailable(context.Context) bool { return true } -func (e *local) Load(ctx context.Context) (*types.EngineInfo, error) { +func (e *local) Load(ctx context.Context) (*types.BackendInfo, error) { c, ok := ctx.Value(types.CliContext).(*cli.Context) if ok { e.tempDir = c.String("backend-local-temp-dir") @@ -74,7 +74,7 @@ func (e *local) Load(ctx context.Context) (*types.EngineInfo, error) { e.loadClone() - return &types.EngineInfo{ + return &types.BackendInfo{ Platform: e.os + "/" + e.arch, }, nil } diff --git a/pipeline/backend/types/backend.go b/pipeline/backend/types/backend.go index e4cfd5dd1..d6cfd0990 100644 --- a/pipeline/backend/types/backend.go +++ b/pipeline/backend/types/backend.go @@ -14,6 +14,48 @@ package types +import ( + "context" + "io" +) + +// Backend defines a container orchestration backend and is used +// to create and manage container resources. +type Backend interface { + // Name returns the name of the backend. + Name() string + + // IsAvailable check if the backend is available. + IsAvailable(ctx context.Context) bool + + // Load loads the backend engine. + Load(ctx context.Context) (*BackendInfo, error) + + // SetupWorkflow sets up the workflow environment. + SetupWorkflow(ctx context.Context, conf *Config, taskUUID string) error + + // StartStep starts the workflow step. + StartStep(ctx context.Context, step *Step, taskUUID string) error + + // WaitStep waits for the workflow step to complete and returns + // the completion results. + WaitStep(ctx context.Context, step *Step, taskUUID string) (*State, error) + + // TailStep tails the workflow step logs. + TailStep(ctx context.Context, step *Step, taskUUID string) (io.ReadCloser, error) + + // DestroyStep destroys the workflow step. + DestroyStep(ctx context.Context, step *Step, taskUUID string) error + + // DestroyWorkflow destroys the workflow environment. + DestroyWorkflow(ctx context.Context, conf *Config, taskUUID string) error +} + +// BackendInfo represents the reported information of a loaded backend +type BackendInfo struct { + Platform string +} + // BackendOptions defines advanced options for specific backends type BackendOptions struct { Kubernetes KubernetesBackendOptions `json:"kubernetes,omitempty"` diff --git a/pipeline/backend/types/engine.go b/pipeline/backend/types/engine.go deleted file mode 100644 index d35abeb47..000000000 --- a/pipeline/backend/types/engine.go +++ /dev/null @@ -1,57 +0,0 @@ -// Copyright 2023 Woodpecker Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package types - -import ( - "context" - "io" -) - -// Engine defines a container orchestration backend and is used -// to create and manage container resources. -type Engine interface { - // Name returns the name of the backend. - Name() string - - // IsAvailable check if the backend is available. - IsAvailable(ctx context.Context) bool - - // Load loads the backend engine. - Load(ctx context.Context) (*EngineInfo, error) - - // SetupWorkflow sets up the workflow environment. - SetupWorkflow(ctx context.Context, conf *Config, taskUUID string) error - - // StartStep starts the workflow step. - StartStep(ctx context.Context, step *Step, taskUUID string) error - - // WaitStep waits for the workflow step to complete and returns - // the completion results. - WaitStep(ctx context.Context, step *Step, taskUUID string) (*State, error) - - // TailStep tails the workflow step logs. - TailStep(ctx context.Context, step *Step, taskUUID string) (io.ReadCloser, error) - - // DestroyStep destroys the workflow step. - DestroyStep(ctx context.Context, step *Step, taskUUID string) error - - // DestroyWorkflow destroys the workflow environment. - DestroyWorkflow(ctx context.Context, conf *Config, taskUUID string) error -} - -// EngineInfo represents the reported information of a loaded engine -type EngineInfo struct { - Platform string -} diff --git a/pipeline/option.go b/pipeline/option.go index 27bcd9c68..6be8f3c3d 100644 --- a/pipeline/option.go +++ b/pipeline/option.go @@ -23,10 +23,10 @@ import ( // Option configures a runtime option. type Option func(*Runtime) -// WithEngine returns an option configured with a runtime engine. -func WithEngine(engine backend.Engine) Option { +// WithBackend returns an option configured with a runtime engine. +func WithBackend(backend backend.Backend) Option { return func(r *Runtime) { - r.engine = engine + r.engine = backend } } diff --git a/pipeline/pipeline.go b/pipeline/pipeline.go index f777acfe2..4c0ef2106 100644 --- a/pipeline/pipeline.go +++ b/pipeline/pipeline.go @@ -55,7 +55,7 @@ type ( type Runtime struct { err error spec *backend.Config - engine backend.Engine + engine backend.Backend started int64 ctx context.Context