From 5393aa5d3b3524e1788b7099997fec57ac81c2c1 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sun, 9 Jul 2023 21:03:19 +0200 Subject: [PATCH] Move docker specific volume & network settings into backend code (#1956) ... if we want to make them be changed ... it should be an agent-backend-option --- pipeline/backend/docker/docker.go | 22 +++++++++++------ pipeline/backend/types/network.go | 4 +--- pipeline/backend/types/volume.go | 4 +--- pipeline/frontend/yaml/compiler/compiler.go | 26 ++++----------------- 4 files changed, 21 insertions(+), 35 deletions(-) diff --git a/pipeline/backend/docker/docker.go b/pipeline/backend/docker/docker.go index f93b7f1c1..9f7fe8800 100644 --- a/pipeline/backend/docker/docker.go +++ b/pipeline/backend/docker/docker.go @@ -18,6 +18,7 @@ import ( "context" "io" "os" + "runtime" "strings" "github.com/docker/docker/api/types" @@ -41,6 +42,12 @@ type docker struct { volumes []string } +const ( + networkDriverNAT = "nat" + networkDriverBridge = "bridge" + volumeDriver = "local" +) + // New returns a new Docker Engine. func New() backend.Engine { return &docker{ @@ -97,21 +104,22 @@ func (e *docker) Load(ctx context.Context) 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, - Driver: vol.Driver, - DriverOpts: vol.DriverOpts, - // Labels: defaultLabels, + Name: vol.Name, + Driver: volumeDriver, }) if err != nil { return err } } + + networkDriver := networkDriverBridge + if runtime.GOOS == "windows" { + networkDriver = networkDriverNAT + } for _, n := range conf.Networks { _, err := e.client.NetworkCreate(noContext, n.Name, types.NetworkCreate{ - Driver: n.Driver, - Options: n.DriverOpts, + Driver: networkDriver, EnableIPv6: e.enableIPv6, - // Labels: defaultLabels, }) if err != nil { return err diff --git a/pipeline/backend/types/network.go b/pipeline/backend/types/network.go index 86095f40d..f67454150 100644 --- a/pipeline/backend/types/network.go +++ b/pipeline/backend/types/network.go @@ -2,7 +2,5 @@ package types // Network defines a container network. type Network struct { - Name string `json:"name,omitempty"` - Driver string `json:"driver,omitempty"` - DriverOpts map[string]string `json:"driver_opts,omitempty"` + Name string `json:"name,omitempty"` } diff --git a/pipeline/backend/types/volume.go b/pipeline/backend/types/volume.go index cc9f8654a..f3a944e4d 100644 --- a/pipeline/backend/types/volume.go +++ b/pipeline/backend/types/volume.go @@ -2,7 +2,5 @@ package types // Volume defines a container volume. type Volume struct { - Name string `json:"name,omitempty"` - Driver string `json:"driver,omitempty"` - DriverOpts map[string]string `json:"driver_opts,omitempty"` + Name string `json:"name,omitempty"` } diff --git a/pipeline/frontend/yaml/compiler/compiler.go b/pipeline/frontend/yaml/compiler/compiler.go index d89f6d638..e73c2e843 100644 --- a/pipeline/frontend/yaml/compiler/compiler.go +++ b/pipeline/frontend/yaml/compiler/compiler.go @@ -3,7 +3,6 @@ package compiler import ( "fmt" "path" - "strings" backend_types "github.com/woodpecker-ci/woodpecker/pipeline/backend/types" "github.com/woodpecker-ci/woodpecker/pipeline/frontend/metadata" @@ -11,17 +10,9 @@ import ( "github.com/woodpecker-ci/woodpecker/shared/constant" ) -// TODO(bradrydzewski) compiler should handle user-defined volumes from YAML -// TODO(bradrydzewski) compiler should handle user-defined networks from YAML - const ( - windowsPrefix = "windows/" - defaultCloneName = "clone" - networkDriverNAT = "nat" - networkDriverBridge = "bridge" - nameServices = "services" namePipeline = "pipeline" ) @@ -114,22 +105,13 @@ func (c *Compiler) Compile(conf *yaml_types.Workflow) (*backend_types.Config, er // create a default volume config.Volumes = append(config.Volumes, &backend_types.Volume{ - Name: fmt.Sprintf("%s_default", c.prefix), - Driver: "local", + Name: fmt.Sprintf("%s_default", c.prefix), }) // create a default network - if strings.HasPrefix(c.metadata.Sys.Platform, windowsPrefix) { - config.Networks = append(config.Networks, &backend_types.Network{ - Name: fmt.Sprintf("%s_default", c.prefix), - Driver: networkDriverNAT, - }) - } else { - config.Networks = append(config.Networks, &backend_types.Network{ - Name: fmt.Sprintf("%s_default", c.prefix), - Driver: networkDriverBridge, - }) - } + config.Networks = append(config.Networks, &backend_types.Network{ + Name: fmt.Sprintf("%s_default", c.prefix), + }) // create secrets for mask for _, sec := range c.secrets {