Rename engine to backend (#2950)

rename based on https://woodpecker-ci.org/docs/usage/terminiology

---------

Co-authored-by: 6543 <6543@obermui.de>
This commit is contained in:
qwerty287 2023-12-14 19:20:47 +01:00 committed by GitHub
parent 0099ff5d26
commit ff1f51d6a9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 89 additions and 103 deletions

View file

@ -37,16 +37,16 @@ type Runner struct {
filter rpc.Filter filter rpc.Filter
hostname string hostname string
counter *State 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{ return Runner{
client: workEngine, client: workEngine,
filter: f, filter: f,
hostname: h, hostname: h,
counter: state, 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.WithTaskUUID(fmt.Sprint(work.ID)),
pipeline.WithLogger(r.createLogger(logger, &uploads, work)), pipeline.WithLogger(r.createLogger(logger, &uploads, work)),
pipeline.WithTracer(r.createTracer(ctxmeta, logger, work)), pipeline.WithTracer(r.createTracer(ctxmeta, logger, work)),
pipeline.WithEngine(*r.engine), pipeline.WithBackend(*r.backend),
pipeline.WithDescription(map[string]string{ pipeline.WithDescription(map[string]string{
"ID": work.ID, "ID": work.ID,
"Repo": repoName, "Repo": repoName,

View file

@ -215,12 +215,12 @@ func execWithAxis(c *cli.Context, file, repoPath string, axis matrix.Axis) error
backendCtx := context.WithValue(c.Context, backendTypes.CliContext, c) backendCtx := context.WithValue(c.Context, backendTypes.CliContext, c)
backend.Init(backendCtx) backend.Init(backendCtx)
engine, err := backend.FindEngine(backendCtx, c.String("backend-engine")) backendEngine, err := backend.FindBackend(backendCtx, c.String("backend-engine"))
if err != nil { if err != nil {
return err return err
} }
if _, err = engine.Load(backendCtx); err != nil { if _, err = backendEngine.Load(backendCtx); err != nil {
return err return err
} }
@ -234,7 +234,7 @@ func execWithAxis(c *cli.Context, file, repoPath string, axis matrix.Axis) error
pipeline.WithContext(ctx), pipeline.WithContext(ctx),
pipeline.WithTracer(pipeline.DefaultTracer), pipeline.WithTracer(pipeline.DefaultTracer),
pipeline.WithLogger(defaultLogger), pipeline.WithLogger(defaultLogger),
pipeline.WithEngine(engine), pipeline.WithBackend(backendEngine),
pipeline.WithDescription(map[string]string{ pipeline.WithDescription(map[string]string{
"CLI": "exec", "CLI": "exec",
}), }),

View file

@ -156,22 +156,22 @@ func run(c *cli.Context) error {
parallel := c.Int("max-workflows") parallel := c.Int("max-workflows")
wg.Add(parallel) wg.Add(parallel)
// new engine // new backend
engine, err := backend.FindEngine(backendCtx, c.String("backend-engine")) backendEngine, err := backend.FindBackend(backendCtx, c.String("backend-engine"))
if err != nil { if err != nil {
log.Error().Err(err).Msgf("cannot find backend engine '%s'", c.String("backend-engine")) log.Error().Err(err).Msgf("cannot find backend engine '%s'", c.String("backend-engine"))
return err return err
} }
// load engine (e.g. init api client) // load backend (e.g. init api client)
engInfo, err := engine.Load(backendCtx) engInfo, err := backendEngine.Load(backendCtx)
if err != nil { if err != nil {
log.Error().Err(err).Msg("cannot load backend engine") log.Error().Err(err).Msg("cannot load backend engine")
return err 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 { if err != nil {
return err return err
} }
@ -185,7 +185,7 @@ func run(c *cli.Context) error {
labels := map[string]string{ labels := map[string]string{
"hostname": hostname, "hostname": hostname,
"platform": engInfo.Platform, "platform": engInfo.Platform,
"backend": engine.Name(), "backend": backendEngine.Name(),
"repo": "*", // allow all repos by default "repo": "*", // allow all repos by default
} }
@ -221,7 +221,7 @@ func run(c *cli.Context) error {
go func() { go func() {
defer wg.Done() 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) log.Debug().Msgf("created new runner %d", i)
for { for {
@ -241,7 +241,7 @@ func run(c *cli.Context) error {
log.Info().Msgf( log.Info().Msgf(
"Starting Woodpecker agent with version '%s' and backend '%s' using platform '%s' running up to %d pipelines in parallel", "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() wg.Wait()
return nil return nil

View file

@ -94,7 +94,7 @@ var flags = []cli.Flag{
&cli.StringFlag{ &cli.StringFlag{
EnvVars: []string{"WOODPECKER_BACKEND"}, EnvVars: []string{"WOODPECKER_BACKEND"},
Name: "backend-engine", Name: "backend-engine",
Usage: "backend engine to run pipelines on", Usage: "backend to run pipelines on",
Value: "auto-detect", Value: "auto-detect",
}, },
} }

View file

@ -25,26 +25,26 @@ import (
) )
var ( var (
enginesByName map[string]types.Engine backendsByName map[string]types.Backend
engines []types.Engine backends []types.Backend
) )
func Init(ctx context.Context) { func Init(ctx context.Context) {
engines = []types.Engine{ backends = []types.Backend{
docker.New(), docker.New(),
local.New(), local.New(),
kubernetes.New(ctx), kubernetes.New(ctx),
} }
enginesByName = make(map[string]types.Engine) backendsByName = make(map[string]types.Backend)
for _, engine := range engines { for _, engine := range backends {
enginesByName[engine.Name()] = engine backendsByName[engine.Name()] = engine
} }
} }
func FindEngine(ctx context.Context, engineName string) (types.Engine, error) { func FindBackend(ctx context.Context, backendName string) (types.Backend, error) {
if engineName == "auto-detect" { if backendName == "auto-detect" {
for _, engine := range engines { for _, engine := range backends {
if engine.IsAvailable(ctx) { if engine.IsAvailable(ctx) {
return engine, nil 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") return nil, fmt.Errorf("can't detect an available backend engine")
} }
engine, ok := enginesByName[engineName] engine, ok := backendsByName[backendName]
if !ok { 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 return engine, nil

View file

@ -51,8 +51,8 @@ const (
volumeDriver = "local" volumeDriver = "local"
) )
// New returns a new Docker Engine. // New returns a new Docker Backend.
func New() backend.Engine { func New() backend.Backend {
return &docker{ return &docker{
client: nil, client: nil,
} }
@ -93,8 +93,8 @@ func httpClientOfOpts(dockerCertPath string, verifyTLS bool) *http.Client {
} }
} }
// Load new client for Docker Engine using environment variables. // Load new client for Docker Backend using environment variables.
func (e *docker) Load(ctx context.Context) (*backend.EngineInfo, error) { func (e *docker) Load(ctx context.Context) (*backend.BackendInfo, error) {
c, ok := ctx.Value(backend.CliContext).(*cli.Context) c, ok := ctx.Value(backend.CliContext).(*cli.Context)
if !ok { if !ok {
return nil, backend.ErrNoCliContextFound 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, ":")) e.volumes = append(e.volumes, strings.Join(parts, ":"))
} }
return &backend.EngineInfo{ return &backend.BackendInfo{
Platform: e.info.OSType + "/" + normalizeArchType(e.info.Architecture), Platform: e.info.OSType + "/" + normalizeArchType(e.info.Architecture),
}, nil }, nil
} }

View file

@ -24,9 +24,10 @@ import (
"time" "time"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"go.woodpecker-ci.org/woodpecker/v2/pipeline/backend/types"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
"go.woodpecker-ci.org/woodpecker/v2/pipeline/backend/types"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
v1 "k8s.io/api/core/v1" v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/errors"
@ -101,8 +102,8 @@ func configFromCliContext(ctx context.Context) (*Config, error) {
return nil, types.ErrNoCliContextFound return nil, types.ErrNoCliContextFound
} }
// New returns a new Kubernetes Engine. // New returns a new Kubernetes Backend.
func New(ctx context.Context) types.Engine { func New(ctx context.Context) types.Backend {
return &kube{ return &kube{
ctx: ctx, ctx: ctx,
} }
@ -117,7 +118,7 @@ func (e *kube) IsAvailable(context.Context) bool {
return len(host) > 0 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) config, err := configFromCliContext(e.ctx)
if err != nil { if err != nil {
return nil, err 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 // TODO(2693): use info resp of kubeClient to define platform var
e.goos = runtime.GOOS e.goos = runtime.GOOS
return &types.EngineInfo{ return &types.BackendInfo{
Platform: runtime.GOOS + "/" + runtime.GOARCH, Platform: runtime.GOOS + "/" + runtime.GOARCH,
}, nil }, nil
} }

View file

@ -50,8 +50,8 @@ type local struct {
os, arch string os, arch string
} }
// New returns a new local Engine. // New returns a new local Backend.
func New() types.Engine { func New() types.Backend {
return &local{ return &local{
os: runtime.GOOS, os: runtime.GOOS,
arch: runtime.GOARCH, arch: runtime.GOARCH,
@ -66,7 +66,7 @@ func (e *local) IsAvailable(context.Context) bool {
return true 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) c, ok := ctx.Value(types.CliContext).(*cli.Context)
if ok { if ok {
e.tempDir = c.String("backend-local-temp-dir") e.tempDir = c.String("backend-local-temp-dir")
@ -74,7 +74,7 @@ func (e *local) Load(ctx context.Context) (*types.EngineInfo, error) {
e.loadClone() e.loadClone()
return &types.EngineInfo{ return &types.BackendInfo{
Platform: e.os + "/" + e.arch, Platform: e.os + "/" + e.arch,
}, nil }, nil
} }

View file

@ -14,6 +14,48 @@
package types 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 // BackendOptions defines advanced options for specific backends
type BackendOptions struct { type BackendOptions struct {
Kubernetes KubernetesBackendOptions `json:"kubernetes,omitempty"` Kubernetes KubernetesBackendOptions `json:"kubernetes,omitempty"`

View file

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

View file

@ -23,10 +23,10 @@ import (
// Option configures a runtime option. // Option configures a runtime option.
type Option func(*Runtime) type Option func(*Runtime)
// WithEngine returns an option configured with a runtime engine. // WithBackend returns an option configured with a runtime engine.
func WithEngine(engine backend.Engine) Option { func WithBackend(backend backend.Backend) Option {
return func(r *Runtime) { return func(r *Runtime) {
r.engine = engine r.engine = backend
} }
} }

View file

@ -55,7 +55,7 @@ type (
type Runtime struct { type Runtime struct {
err error err error
spec *backend.Config spec *backend.Config
engine backend.Engine engine backend.Backend
started int64 started int64
ctx context.Context ctx context.Context