Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename engine to backend #2950

Merged
merged 4 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions agent/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions cli/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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",
}),
Expand Down
18 changes: 9 additions & 9 deletions cmd/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cmd/agent/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
}
22 changes: 11 additions & 11 deletions pipeline/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions pipeline/backend/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down Expand Up @@ -93,7 +93,7 @@ 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) {
c, ok := ctx.Value(backend.CliContext).(*cli.Context)
if !ok {
Expand Down
7 changes: 4 additions & 3 deletions pipeline/backend/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
}
Expand Down
4 changes: 2 additions & 2 deletions pipeline/backend/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
42 changes: 42 additions & 0 deletions pipeline/backend/types/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) (*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 {
6543 marked this conversation as resolved.
Show resolved Hide resolved
Platform string
}

// BackendOptions defines advanced options for specific backends
type BackendOptions struct {
Kubernetes KubernetesBackendOptions `json:"kubernetes,omitempty"`
Expand Down
57 changes: 0 additions & 57 deletions pipeline/backend/types/engine.go

This file was deleted.

6 changes: 3 additions & 3 deletions pipeline/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
2 changes: 1 addition & 1 deletion pipeline/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type (
type Runtime struct {
err error
spec *backend.Config
engine backend.Engine
engine backend.Backend
started int64

ctx context.Context
Expand Down