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

Let the backend engine report the current platform #2688

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
2 changes: 1 addition & 1 deletion cli/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func execWithAxis(c *cli.Context, file, repoPath string, axis matrix.Axis) error
return err
}

if err = engine.Load(backendCtx); err != nil {
if _, err = engine.Load(backendCtx); err != nil {
return err
}

Expand Down
26 changes: 12 additions & 14 deletions cmd/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
"fmt"
"net/http"
"os"
"runtime"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -57,8 +56,6 @@
hostname, _ = os.Hostname()
}

platform := runtime.GOOS + "/" + runtime.GOARCH

counter.Polling = c.Int("max-workflows")
counter.Running = 0

Expand Down Expand Up @@ -155,7 +152,15 @@
return err
}

agentConfig.AgentID, err = client.RegisterAgent(ctx, platform, engine.Name(), version.String(), parallel)
// load engine (e.g. init api client)
engInfo, err := engine.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())

agentConfig.AgentID, err = client.RegisterAgent(ctx, engInfo.Platform, engInfo.Backend, version.String(), parallel)

Check warning on line 163 in cmd/agent/agent.go

View check run for this annotation

Codecov / codecov/patch

cmd/agent/agent.go#L156-L163

Added lines #L156 - L163 were not covered by tests
if err != nil {
return err
}
Expand All @@ -164,8 +169,8 @@

labels := map[string]string{
"hostname": hostname,
"platform": platform,
"backend": engine.Name(),
"platform": engInfo.Platform,
"backend": engInfo.Backend,

Check warning on line 173 in cmd/agent/agent.go

View check run for this annotation

Codecov / codecov/patch

cmd/agent/agent.go#L172-L173

Added lines #L172 - L173 were not covered by tests
"repo": "*", // allow all repos by default
}

Expand Down Expand Up @@ -195,13 +200,6 @@
}
}()

// load engine (e.g. init api client)
if err := engine.Load(backendCtx); err != nil {
log.Error().Err(err).Msg("cannot load backend engine")
return err
}
log.Debug().Msgf("loaded %s backend engine", engine.Name())

for i := 0; i < parallel; i++ {
i := i
go func() {
Expand All @@ -226,7 +224,7 @@

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(), platform, parallel)
version.String(), engInfo.Backend, engInfo.Platform, parallel)

Check warning on line 227 in cmd/agent/agent.go

View check run for this annotation

Codecov / codecov/patch

cmd/agent/agent.go#L227

Added line #L227 was not covered by tests

wg.Wait()
return nil
Expand Down
5 changes: 2 additions & 3 deletions pipeline/backend/common/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@

import (
"encoding/base64"
"runtime"
)

func GenerateContainerConf(commands []string) (env map[string]string, entry, cmd []string) {
func GenerateContainerConf(commands []string, goos string) (env map[string]string, entry, cmd []string) {

Check warning on line 21 in pipeline/backend/common/script.go

View check run for this annotation

Codecov / codecov/patch

pipeline/backend/common/script.go#L21

Added line #L21 was not covered by tests
env = make(map[string]string)
if runtime.GOOS == "windows" {
if goos == "windows" {

Check warning on line 23 in pipeline/backend/common/script.go

View check run for this annotation

Codecov / codecov/patch

pipeline/backend/common/script.go#L23

Added line #L23 was not covered by tests
env["CI_SCRIPT"] = base64.StdEncoding.EncodeToString([]byte(generateScriptWindows(commands)))
env["HOME"] = "c:\\root"
env["SHELL"] = "powershell.exe"
Expand Down
7 changes: 2 additions & 5 deletions pipeline/backend/docker/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
)

// returns a container configuration.
func toConfig(step *types.Step) *container.Config {
func (e *docker) toConfig(step *types.Step) *container.Config {

Check warning on line 30 in pipeline/backend/docker/convert.go

View check run for this annotation

Codecov / codecov/patch

pipeline/backend/docker/convert.go#L30

Added line #L30 was not covered by tests
config := &container.Config{
Image: step.Image,
Labels: map[string]string{"wp_uuid": step.UUID},
Expand All @@ -37,7 +37,7 @@
}

if len(step.Commands) != 0 {
env, entry, cmd := common.GenerateContainerConf(step.Commands)
env, entry, cmd := common.GenerateContainerConf(step.Commands, e.info.OSType)

Check warning on line 40 in pipeline/backend/docker/convert.go

View check run for this annotation

Codecov / codecov/patch

pipeline/backend/docker/convert.go#L40

Added line #L40 was not covered by tests
for k, v := range env {
step.Environment[k] = v
}
Expand Down Expand Up @@ -76,9 +76,6 @@
Sysctls: step.Sysctls,
}

// if len(step.VolumesFrom) != 0 {
// config.VolumesFrom = step.VolumesFrom
// }
if len(step.NetworkMode) != 0 {
config.NetworkMode = container.NetworkMode(step.NetworkMode)
}
Expand Down
37 changes: 27 additions & 10 deletions pipeline/backend/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"net/http"
"os"
"path/filepath"
"runtime"
"strings"

"github.com/docker/docker/api/types"
Expand All @@ -43,6 +42,7 @@
enableIPv6 bool
network string
volumes []string
info types.Info
}

const (
Expand Down Expand Up @@ -94,10 +94,10 @@
}

// Load new client for Docker Engine using environment variables.
func (e *docker) Load(ctx context.Context) error {
func (e *docker) Load(ctx context.Context) (*backend.EngineInfo, error) {

Check warning on line 97 in pipeline/backend/docker/docker.go

View check run for this annotation

Codecov / codecov/patch

pipeline/backend/docker/docker.go#L97

Added line #L97 was not covered by tests
c, ok := ctx.Value(backend.CliContext).(*cli.Context)
if !ok {
return backend.ErrNoCliContextFound
return nil, backend.ErrNoCliContextFound

Check warning on line 100 in pipeline/backend/docker/docker.go

View check run for this annotation

Codecov / codecov/patch

pipeline/backend/docker/docker.go#L100

Added line #L100 was not covered by tests
}

var dockerClientOpts []client.Opt
Expand All @@ -115,10 +115,15 @@

cl, err := client.NewClientWithOpts(dockerClientOpts...)
if err != nil {
return err
return nil, err

Check warning on line 118 in pipeline/backend/docker/docker.go

View check run for this annotation

Codecov / codecov/patch

pipeline/backend/docker/docker.go#L118

Added line #L118 was not covered by tests
}
e.client = cl

e.info, err = cl.Info(ctx)
if err != nil {
return nil, err
}

Check warning on line 125 in pipeline/backend/docker/docker.go

View check run for this annotation

Codecov / codecov/patch

pipeline/backend/docker/docker.go#L122-L125

Added lines #L122 - L125 were not covered by tests

e.enableIPv6 = c.Bool("backend-docker-ipv6")
e.network = c.String("backend-docker-network")

Expand All @@ -137,7 +142,10 @@
e.volumes = append(e.volumes, strings.Join(parts, ":"))
}

return nil
return &backend.EngineInfo{
Backend: e.Name(),
Platform: e.info.OSType + "/" + normalizeArchType(e.info.Architecture),
}, nil

Check warning on line 148 in pipeline/backend/docker/docker.go

View check run for this annotation

Codecov / codecov/patch

pipeline/backend/docker/docker.go#L145-L148

Added lines #L145 - L148 were not covered by tests
}

func (e *docker) SetupWorkflow(_ context.Context, conf *backend.Config, taskUUID string) error {
Expand All @@ -154,7 +162,7 @@
}

networkDriver := networkDriverBridge
if runtime.GOOS == "windows" {
if e.info.OSType == "windows" {

Check warning on line 165 in pipeline/backend/docker/docker.go

View check run for this annotation

Codecov / codecov/patch

pipeline/backend/docker/docker.go#L165

Added line #L165 was not covered by tests
networkDriver = networkDriverNAT
}
for _, n := range conf.Networks {
Expand All @@ -172,7 +180,7 @@
func (e *docker) StartStep(ctx context.Context, step *backend.Step, taskUUID string) error {
log.Trace().Str("taskUUID", taskUUID).Msgf("start step %s", step.Name)

config := toConfig(step)
config := e.toConfig(step)

Check warning on line 183 in pipeline/backend/docker/docker.go

View check run for this annotation

Codecov / codecov/patch

pipeline/backend/docker/docker.go#L183

Added line #L183 was not covered by tests
hostConfig := toHostConfig(step)
containerName := toContainerName(step)

Expand Down Expand Up @@ -262,9 +270,6 @@
if err != nil {
return nil, err
}
// if info.State.Running {
// TODO
// }

return &backend.State{
Exited: true,
Expand Down Expand Up @@ -344,3 +349,15 @@
// Error: No such container: ...
return err != nil && (strings.Contains(err.Error(), "No such container") || strings.Contains(err.Error(), "is not running"))
}

// normalizeArchType converts what docker info reports als arch
// and convert it into the runtime.GOARCH format
// TODO: find out if we we need to convert other arch types too
6543 marked this conversation as resolved.
Show resolved Hide resolved
func normalizeArchType(s string) string {
switch s {
case "x86_64":
return "amd64"
default:
return s

Check warning on line 361 in pipeline/backend/docker/docker.go

View check run for this annotation

Codecov / codecov/patch

pipeline/backend/docker/docker.go#L356-L361

Added lines #L356 - L361 were not covered by tests
}
}
17 changes: 12 additions & 5 deletions pipeline/backend/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"fmt"
"io"
"os"
"runtime"
"strings"
"time"

Expand Down Expand Up @@ -46,6 +47,7 @@
ctx context.Context
client kubernetes.Interface
config *Config
goos string
}

type Config struct {
Expand Down Expand Up @@ -104,10 +106,10 @@
return len(host) > 0
}

func (e *kube) Load(context.Context) error {
func (e *kube) Load(context.Context) (*types.EngineInfo, error) {

Check warning on line 109 in pipeline/backend/kubernetes/kubernetes.go

View check run for this annotation

Codecov / codecov/patch

pipeline/backend/kubernetes/kubernetes.go#L109

Added line #L109 was not covered by tests
config, err := configFromCliContext(e.ctx)
if err != nil {
return err
return nil, err

Check warning on line 112 in pipeline/backend/kubernetes/kubernetes.go

View check run for this annotation

Codecov / codecov/patch

pipeline/backend/kubernetes/kubernetes.go#L112

Added line #L112 was not covered by tests
}
e.config = config

Expand All @@ -120,12 +122,17 @@
}

if err != nil {
return err
return nil, err

Check warning on line 125 in pipeline/backend/kubernetes/kubernetes.go

View check run for this annotation

Codecov / codecov/patch

pipeline/backend/kubernetes/kubernetes.go#L125

Added line #L125 was not covered by tests
}

e.client = kubeClient

return nil
// TODO: use info resp of kubeClient to define platform var
anbraten marked this conversation as resolved.
Show resolved Hide resolved
6543 marked this conversation as resolved.
Show resolved Hide resolved
e.goos = runtime.GOOS
return &types.EngineInfo{
Backend: e.Name(),
Platform: runtime.GOOS + "/" + runtime.GOARCH,
}, nil

Check warning on line 135 in pipeline/backend/kubernetes/kubernetes.go

View check run for this annotation

Codecov / codecov/patch

pipeline/backend/kubernetes/kubernetes.go#L130-L135

Added lines #L130 - L135 were not covered by tests
}

// Setup the pipeline environment.
Expand Down Expand Up @@ -183,7 +190,7 @@

// Start the pipeline step.
func (e *kube) StartStep(ctx context.Context, step *types.Step, taskUUID string) error {
pod, err := Pod(e.config.Namespace, step, e.config.PodLabels, e.config.PodAnnotations)
pod, err := Pod(e.config.Namespace, step, e.config.PodLabels, e.config.PodAnnotations, e.goos)

Check warning on line 193 in pipeline/backend/kubernetes/kubernetes.go

View check run for this annotation

Codecov / codecov/patch

pipeline/backend/kubernetes/kubernetes.go#L193

Added line #L193 was not covered by tests
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions pipeline/backend/kubernetes/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"github.com/woodpecker-ci/woodpecker/pipeline/backend/types"
)

func Pod(namespace string, step *types.Step, labels, annotations map[string]string) (*v1.Pod, error) {
func Pod(namespace string, step *types.Step, labels, annotations map[string]string, goos string) (*v1.Pod, error) {

Check warning on line 31 in pipeline/backend/kubernetes/pod.go

View check run for this annotation

Codecov / codecov/patch

pipeline/backend/kubernetes/pod.go#L31

Added line #L31 was not covered by tests
var (
vols []v1.Volume
volMounts []v1.VolumeMount
Expand Down Expand Up @@ -66,7 +66,7 @@
}

if len(step.Commands) != 0 {
scriptEnv, entry, cmds := common.GenerateContainerConf(step.Commands)
scriptEnv, entry, cmds := common.GenerateContainerConf(step.Commands, goos)

Check warning on line 69 in pipeline/backend/kubernetes/pod.go

View check run for this annotation

Codecov / codecov/patch

pipeline/backend/kubernetes/pod.go#L69

Added line #L69 was not covered by tests
for k, v := range scriptEnv {
step.Environment[k] = v
}
Expand Down
7 changes: 5 additions & 2 deletions pipeline/backend/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,13 @@
return true
}

func (e *local) Load(context.Context) error {
func (e *local) Load(context.Context) (*types.EngineInfo, error) {

Check warning on line 63 in pipeline/backend/local/local.go

View check run for this annotation

Codecov / codecov/patch

pipeline/backend/local/local.go#L63

Added line #L63 was not covered by tests
e.loadClone()

return nil
return &types.EngineInfo{
Backend: e.Name(),
Platform: runtime.GOOS + "/" + runtime.GOARCH,
}, nil

Check warning on line 69 in pipeline/backend/local/local.go

View check run for this annotation

Codecov / codecov/patch

pipeline/backend/local/local.go#L66-L69

Added lines #L66 - L69 were not covered by tests
}

// SetupWorkflow the pipeline environment.
Expand Down
8 changes: 7 additions & 1 deletion pipeline/backend/types/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type Engine interface {
IsAvailable(ctx context.Context) bool

// Load the backend engine.
Load(ctx context.Context) error
Load(ctx context.Context) (*EngineInfo, error)

// SetupWorkflow the workflow environment.
SetupWorkflow(ctx context.Context, conf *Config, taskUUID string) error
Expand All @@ -47,3 +47,9 @@ type Engine interface {
// DestroyWorkflow 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
Backend string
6543 marked this conversation as resolved.
Show resolved Hide resolved
}