Fix workflow volume and network config (#4650)

This commit is contained in:
Anbraten 2025-01-01 16:21:57 +01:00 committed by GitHub
parent 04f691ebd6
commit 5d3300824d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 72 additions and 52 deletions

View file

@ -144,23 +144,32 @@ func (e *docker) Load(ctx context.Context) (*backend.BackendInfo, error) {
func (e *docker) SetupWorkflow(ctx context.Context, conf *backend.Config, taskUUID string) error { func (e *docker) SetupWorkflow(ctx context.Context, conf *backend.Config, taskUUID string) error {
log.Trace().Str("taskUUID", taskUUID).Msg("create workflow environment") log.Trace().Str("taskUUID", taskUUID).Msg("create workflow environment")
_, err := e.client.VolumeCreate(ctx, volume.CreateOptions{ for _, vol := range conf.Volumes {
Name: conf.Volume.Name, _, err := e.client.VolumeCreate(ctx, volume.CreateOptions{
Driver: volumeDriver, Name: vol.Name,
}) Driver: volumeDriver,
if err != nil { })
return err if err != nil {
return err
}
} }
networkDriver := networkDriverBridge networkDriver := networkDriverBridge
if e.info.OSType == "windows" { if e.info.OSType == "windows" {
networkDriver = networkDriverNAT networkDriver = networkDriverNAT
} }
_, err = e.client.NetworkCreate(ctx, conf.Network.Name, network.CreateOptions{
Driver: networkDriver, for _, n := range conf.Networks {
EnableIPv6: &e.config.enableIPv6, _, err := e.client.NetworkCreate(ctx, n.Name, network.CreateOptions{
}) Driver: networkDriver,
return err EnableIPv6: &e.config.enableIPv6,
})
if err != nil {
return err
}
}
return nil
} }
func (e *docker) StartStep(ctx context.Context, step *backend.Step, taskUUID string) error { func (e *docker) StartStep(ctx context.Context, step *backend.Step, taskUUID string) error {
@ -323,12 +332,19 @@ func (e *docker) DestroyWorkflow(ctx context.Context, conf *backend.Config, task
} }
} }
} }
if err := e.client.VolumeRemove(ctx, conf.Volume.Name, true); err != nil {
log.Error().Err(err).Msgf("could not remove volume '%s'", conf.Volume.Name) for _, v := range conf.Volumes {
if err := e.client.VolumeRemove(ctx, v.Name, true); err != nil {
log.Error().Err(err).Msgf("could not remove volume '%s'", v.Name)
}
} }
if err := e.client.NetworkRemove(ctx, conf.Network.Name); err != nil {
log.Error().Err(err).Msgf("could not remove network '%s'", conf.Network.Name) for _, n := range conf.Networks {
if err := e.client.NetworkRemove(ctx, n.Name); err != nil {
log.Error().Err(err).Msgf("could not remove network '%s'", n.Name)
}
} }
return nil return nil
} }

View file

@ -191,9 +191,11 @@ func (e *kube) getConfig() *config {
func (e *kube) SetupWorkflow(ctx context.Context, conf *types.Config, taskUUID string) error { func (e *kube) SetupWorkflow(ctx context.Context, conf *types.Config, taskUUID string) error {
log.Trace().Str("taskUUID", taskUUID).Msgf("Setting up Kubernetes primitives") log.Trace().Str("taskUUID", taskUUID).Msgf("Setting up Kubernetes primitives")
_, err := startVolume(ctx, e, conf.Volume.Name) for _, vol := range conf.Volumes {
if err != nil { _, err := startVolume(ctx, e, vol.Name)
return err if err != nil {
return err
}
} }
var extraHosts []types.HostAlias var extraHosts []types.HostAlias
@ -425,9 +427,11 @@ func (e *kube) DestroyWorkflow(ctx context.Context, conf *types.Config, taskUUID
} }
} }
err := stopVolume(ctx, e, conf.Volume.Name, defaultDeleteOptions) for _, vol := range conf.Volumes {
if err != nil { err := stopVolume(ctx, e, vol.Name, defaultDeleteOptions)
return err if err != nil {
return err
}
} }
return nil return nil

View file

@ -16,10 +16,10 @@ package types
// Config defines the runtime configuration of a workflow. // Config defines the runtime configuration of a workflow.
type Config struct { type Config struct {
Stages []*Stage `json:"pipeline"` // workflow stages Stages []*Stage `json:"pipeline"` // workflow stages
Network *Network `json:"network"` // network definitions Networks []*Network `json:"network"` // network definitions
Volume *Volume `json:"volume"` // volume definition Volumes []*Volume `json:"volume"` // volume definitions
Secrets []*Secret `json:"secrets"` // secret definitions Secrets []*Secret `json:"secrets"` // secret definitions
} }
// CliCommand is the context key to pass cli context to backends if needed. // CliCommand is the context key to pass cli context to backends if needed.

View file

@ -129,14 +129,14 @@ func (c *Compiler) Compile(conf *yaml_types.Workflow) (*backend_types.Config, er
} }
// create a default volume // create a default volume
config.Volume = &backend_types.Volume{ config.Volumes = append(config.Volumes, &backend_types.Volume{
Name: fmt.Sprintf("%s_default", c.prefix), Name: fmt.Sprintf("%s_default", c.prefix),
} })
// create a default network // create a default network
config.Network = &backend_types.Network{ config.Networks = append(config.Networks, &backend_types.Network{
Name: fmt.Sprintf("%s_default", c.prefix), Name: fmt.Sprintf("%s_default", c.prefix),
} })
// create secrets for mask // create secrets for mask
for _, sec := range c.secrets { for _, sec := range c.secrets {

View file

@ -81,12 +81,12 @@ func TestCompilerCompile(t *testing.T) {
WithWorkspaceFromURL("/test", repoURL), WithWorkspaceFromURL("/test", repoURL),
) )
defaultNetwork := &backend_types.Network{ defaultNetworks := []*backend_types.Network{{
Name: "test_default", Name: "test_default",
} }}
defaultVolume := &backend_types.Volume{ defaultVolumes := []*backend_types.Volume{{
Name: "test_default", Name: "test_default",
} }}
defaultCloneStage := &backend_types.Stage{ defaultCloneStage := &backend_types.Stage{
Steps: []*backend_types.Step{{ Steps: []*backend_types.Step{{
@ -95,7 +95,7 @@ func TestCompilerCompile(t *testing.T) {
Image: constant.DefaultClonePlugin, Image: constant.DefaultClonePlugin,
OnSuccess: true, OnSuccess: true,
Failure: "fail", Failure: "fail",
Volumes: []string{defaultVolume.Name + ":/woodpecker"}, Volumes: []string{defaultVolumes[0].Name + ":/woodpecker"},
WorkingDir: "/woodpecker/src/github.com/octocat/hello-world", WorkingDir: "/woodpecker/src/github.com/octocat/hello-world",
WorkspaceBase: "/woodpecker", WorkspaceBase: "/woodpecker",
Networks: []backend_types.Conn{{Name: "test_default", Aliases: []string{"clone"}}}, Networks: []backend_types.Conn{{Name: "test_default", Aliases: []string{"clone"}}},
@ -113,17 +113,17 @@ func TestCompilerCompile(t *testing.T) {
name: "empty workflow, no clone", name: "empty workflow, no clone",
fronConf: &yaml_types.Workflow{SkipClone: true}, fronConf: &yaml_types.Workflow{SkipClone: true},
backConf: &backend_types.Config{ backConf: &backend_types.Config{
Network: defaultNetwork, Networks: defaultNetworks,
Volume: defaultVolume, Volumes: defaultVolumes,
}, },
}, },
{ {
name: "empty workflow, default clone", name: "empty workflow, default clone",
fronConf: &yaml_types.Workflow{}, fronConf: &yaml_types.Workflow{},
backConf: &backend_types.Config{ backConf: &backend_types.Config{
Network: defaultNetwork, Networks: defaultNetworks,
Volume: defaultVolume, Volumes: defaultVolumes,
Stages: []*backend_types.Stage{defaultCloneStage}, Stages: []*backend_types.Stage{defaultCloneStage},
}, },
}, },
{ {
@ -133,8 +133,8 @@ func TestCompilerCompile(t *testing.T) {
Image: "dummy_img", Image: "dummy_img",
}}}}, }}}},
backConf: &backend_types.Config{ backConf: &backend_types.Config{
Network: defaultNetwork, Networks: defaultNetworks,
Volume: defaultVolume, Volumes: defaultVolumes,
Stages: []*backend_types.Stage{defaultCloneStage, { Stages: []*backend_types.Stage{defaultCloneStage, {
Steps: []*backend_types.Step{{ Steps: []*backend_types.Step{{
Name: "dummy", Name: "dummy",
@ -142,7 +142,7 @@ func TestCompilerCompile(t *testing.T) {
Image: "dummy_img", Image: "dummy_img",
OnSuccess: true, OnSuccess: true,
Failure: "fail", Failure: "fail",
Volumes: []string{defaultVolume.Name + ":/woodpecker"}, Volumes: []string{defaultVolumes[0].Name + ":/woodpecker"},
WorkingDir: "/woodpecker/src/github.com/octocat/hello-world", WorkingDir: "/woodpecker/src/github.com/octocat/hello-world",
WorkspaceBase: "/woodpecker", WorkspaceBase: "/woodpecker",
Networks: []backend_types.Conn{{Name: "test_default", Aliases: []string{"dummy"}}}, Networks: []backend_types.Conn{{Name: "test_default", Aliases: []string{"dummy"}}},
@ -167,8 +167,8 @@ func TestCompilerCompile(t *testing.T) {
Commands: []string{"echo 2"}, Commands: []string{"echo 2"},
}}}}, }}}},
backConf: &backend_types.Config{ backConf: &backend_types.Config{
Network: defaultNetwork, Networks: defaultNetworks,
Volume: defaultVolume, Volumes: defaultVolumes,
Stages: []*backend_types.Stage{ Stages: []*backend_types.Stage{
defaultCloneStage, { defaultCloneStage, {
Steps: []*backend_types.Step{{ Steps: []*backend_types.Step{{
@ -178,7 +178,7 @@ func TestCompilerCompile(t *testing.T) {
Commands: []string{"env"}, Commands: []string{"env"},
OnSuccess: true, OnSuccess: true,
Failure: "fail", Failure: "fail",
Volumes: []string{defaultVolume.Name + ":/test"}, Volumes: []string{defaultVolumes[0].Name + ":/test"},
WorkingDir: "/test/src/github.com/octocat/hello-world", WorkingDir: "/test/src/github.com/octocat/hello-world",
WorkspaceBase: "/test", WorkspaceBase: "/test",
Networks: []backend_types.Conn{{Name: "test_default", Aliases: []string{"echo env"}}}, Networks: []backend_types.Conn{{Name: "test_default", Aliases: []string{"echo env"}}},
@ -192,7 +192,7 @@ func TestCompilerCompile(t *testing.T) {
Commands: []string{"echo 1"}, Commands: []string{"echo 1"},
OnSuccess: true, OnSuccess: true,
Failure: "fail", Failure: "fail",
Volumes: []string{defaultVolume.Name + ":/test"}, Volumes: []string{defaultVolumes[0].Name + ":/test"},
WorkingDir: "/test/src/github.com/octocat/hello-world", WorkingDir: "/test/src/github.com/octocat/hello-world",
WorkspaceBase: "/test", WorkspaceBase: "/test",
Networks: []backend_types.Conn{{Name: "test_default", Aliases: []string{"parallel echo 1"}}}, Networks: []backend_types.Conn{{Name: "test_default", Aliases: []string{"parallel echo 1"}}},
@ -206,7 +206,7 @@ func TestCompilerCompile(t *testing.T) {
Commands: []string{"echo 2"}, Commands: []string{"echo 2"},
OnSuccess: true, OnSuccess: true,
Failure: "fail", Failure: "fail",
Volumes: []string{defaultVolume.Name + ":/test"}, Volumes: []string{defaultVolumes[0].Name + ":/test"},
WorkingDir: "/test/src/github.com/octocat/hello-world", WorkingDir: "/test/src/github.com/octocat/hello-world",
WorkspaceBase: "/test", WorkspaceBase: "/test",
Networks: []backend_types.Conn{{Name: "test_default", Aliases: []string{"parallel echo 2"}}}, Networks: []backend_types.Conn{{Name: "test_default", Aliases: []string{"parallel echo 2"}}},
@ -233,8 +233,8 @@ func TestCompilerCompile(t *testing.T) {
Commands: []string{"echo 2"}, Commands: []string{"echo 2"},
}}}}, }}}},
backConf: &backend_types.Config{ backConf: &backend_types.Config{
Network: defaultNetwork, Networks: defaultNetworks,
Volume: defaultVolume, Volumes: defaultVolumes,
Stages: []*backend_types.Stage{defaultCloneStage, { Stages: []*backend_types.Stage{defaultCloneStage, {
Steps: []*backend_types.Step{{ Steps: []*backend_types.Step{{
Name: "echo env", Name: "echo env",
@ -243,7 +243,7 @@ func TestCompilerCompile(t *testing.T) {
Commands: []string{"env"}, Commands: []string{"env"},
OnSuccess: true, OnSuccess: true,
Failure: "fail", Failure: "fail",
Volumes: []string{defaultVolume.Name + ":/test"}, Volumes: []string{defaultVolumes[0].Name + ":/test"},
WorkingDir: "/test/src/github.com/octocat/hello-world", WorkingDir: "/test/src/github.com/octocat/hello-world",
WorkspaceBase: "/test", WorkspaceBase: "/test",
Networks: []backend_types.Conn{{Name: "test_default", Aliases: []string{"echo env"}}}, Networks: []backend_types.Conn{{Name: "test_default", Aliases: []string{"echo env"}}},
@ -255,7 +255,7 @@ func TestCompilerCompile(t *testing.T) {
Commands: []string{"echo 2"}, Commands: []string{"echo 2"},
OnSuccess: true, OnSuccess: true,
Failure: "fail", Failure: "fail",
Volumes: []string{defaultVolume.Name + ":/test"}, Volumes: []string{defaultVolumes[0].Name + ":/test"},
WorkingDir: "/test/src/github.com/octocat/hello-world", WorkingDir: "/test/src/github.com/octocat/hello-world",
WorkspaceBase: "/test", WorkspaceBase: "/test",
Networks: []backend_types.Conn{{Name: "test_default", Aliases: []string{"echo 2"}}}, Networks: []backend_types.Conn{{Name: "test_default", Aliases: []string{"echo 2"}}},
@ -269,7 +269,7 @@ func TestCompilerCompile(t *testing.T) {
Commands: []string{"echo 1"}, Commands: []string{"echo 1"},
OnSuccess: true, OnSuccess: true,
Failure: "fail", Failure: "fail",
Volumes: []string{defaultVolume.Name + ":/test"}, Volumes: []string{defaultVolumes[0].Name + ":/test"},
WorkingDir: "/test/src/github.com/octocat/hello-world", WorkingDir: "/test/src/github.com/octocat/hello-world",
WorkspaceBase: "/test", WorkspaceBase: "/test",
Networks: []backend_types.Conn{{Name: "test_default", Aliases: []string{"echo 1"}}}, Networks: []backend_types.Conn{{Name: "test_default", Aliases: []string{"echo 1"}}},