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
This commit is contained in:
6543 2023-07-09 21:03:19 +02:00 committed by GitHub
parent 89623471fb
commit 5393aa5d3b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 35 deletions

View file

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

View file

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

View file

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

View file

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