diff --git a/pipeline/backend/kubernetes/pod.go b/pipeline/backend/kubernetes/pod.go index 7a70aa5b0f..6c9d8529c2 100644 --- a/pipeline/backend/kubernetes/pod.go +++ b/pipeline/backend/kubernetes/pod.go @@ -135,9 +135,11 @@ func podSpec(step *types.Step, config *config, options BackendOptions) (v1.PodSp func podContainer(step *types.Step, podName, goos string, options BackendOptions) (v1.Container, error) { var err error container := v1.Container{ - Name: podName, - Image: step.Image, - WorkingDir: step.WorkingDir, + Name: podName, + Image: step.Image, + WorkingDir: step.WorkingDir, + Ports: containerPorts(step.Ports), + SecurityContext: containerSecurityContext(options.SecurityContext, step.Privileged), } if step.Pull { @@ -155,8 +157,6 @@ func podContainer(step *types.Step, podName, goos string, options BackendOptions } container.Env = mapToEnvVars(step.Environment) - container.Ports = containerPorts(step.Ports) - container.SecurityContext = containerSecurityContext(options.SecurityContext, step.Privileged) container.Resources, err = resourceRequirements(options.Resources) if err != nil { diff --git a/pipeline/frontend/yaml/compiler/cacher.go b/pipeline/frontend/yaml/compiler/cacher.go deleted file mode 100644 index 93c279f312..0000000000 --- a/pipeline/frontend/yaml/compiler/cacher.go +++ /dev/null @@ -1,117 +0,0 @@ -// Copyright 2023 Woodpecker Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package compiler - -import ( - "path" - "strings" - - yaml_types "go.woodpecker-ci.org/woodpecker/v2/pipeline/frontend/yaml/types" -) - -// Cacher defines a compiler transform that can be used -// to implement default caching for a repository. -type Cacher interface { - Restore(repo, branch string, mounts []string) *yaml_types.Container - Rebuild(repo, branch string, mounts []string) *yaml_types.Container -} - -type volumeCacher struct { - base string -} - -func (c *volumeCacher) Restore(repo, branch string, mounts []string) *yaml_types.Container { - return &yaml_types.Container{ - Name: "rebuild_cache", - Image: "plugins/volume-cache:1.0.0", - Settings: map[string]any{ - "mount": mounts, - "path": "/cache", - "restore": true, - "file": strings.ReplaceAll(branch, "/", "_") + ".tar", - "fallback_to": "main.tar", - }, - Volumes: yaml_types.Volumes{ - Volumes: []*yaml_types.Volume{ - { - Source: path.Join(c.base, repo), - Destination: "/cache", - // TODO add access mode - }, - }, - }, - } -} - -func (c *volumeCacher) Rebuild(repo, branch string, mounts []string) *yaml_types.Container { - return &yaml_types.Container{ - Name: "rebuild_cache", - Image: "plugins/volume-cache:1.0.0", - Settings: map[string]any{ - "mount": mounts, - "path": "/cache", - "rebuild": true, - "flush": true, - "file": strings.ReplaceAll(branch, "/", "_") + ".tar", - }, - Volumes: yaml_types.Volumes{ - Volumes: []*yaml_types.Volume{ - { - Source: path.Join(c.base, repo), - Destination: "/cache", - // TODO add access mode - }, - }, - }, - } -} - -type s3Cacher struct { - bucket string - access string - secret string - region string -} - -func (c *s3Cacher) Restore(_, _ string, mounts []string) *yaml_types.Container { - return &yaml_types.Container{ - Name: "rebuild_cache", - Image: "plugins/s3-cache:latest", - Settings: map[string]any{ - "mount": mounts, - "access_key": c.access, - "secret_key": c.secret, - "bucket": c.bucket, - "region": c.region, - "rebuild": true, - }, - } -} - -func (c *s3Cacher) Rebuild(_, _ string, mounts []string) *yaml_types.Container { - return &yaml_types.Container{ - Name: "rebuild_cache", - Image: "plugins/s3-cache:latest", - Settings: map[string]any{ - "mount": mounts, - "access_key": c.access, - "secret_key": c.secret, - "bucket": c.bucket, - "region": c.region, - "rebuild": true, - "flush": true, - }, - } -} diff --git a/pipeline/frontend/yaml/compiler/compiler.go b/pipeline/frontend/yaml/compiler/compiler.go index 8c36df2f74..2a7803349a 100644 --- a/pipeline/frontend/yaml/compiler/compiler.go +++ b/pipeline/frontend/yaml/compiler/compiler.go @@ -16,7 +16,6 @@ package compiler import ( "fmt" - "path" backend_types "go.woodpecker-ci.org/woodpecker/v2/pipeline/backend/types" "go.woodpecker-ci.org/woodpecker/v2/pipeline/frontend/metadata" @@ -34,7 +33,6 @@ type Registry struct { Hostname string Username string Password string - Token string } type Secret struct { @@ -68,7 +66,7 @@ func (s *Secret) Match(event string) bool { if len(s.Events) == 0 { return true } - // tread all pull events the same way + // treat all pull events the same way if event == "pull_request_closed" { event = "pull_request" } @@ -82,8 +80,6 @@ func (s *Secret) Match(event string) bool { return false } -type secretMap map[string]Secret - type ResourceLimit struct { MemSwapLimit int64 MemLimit int64 @@ -106,8 +102,7 @@ type Compiler struct { path string metadata metadata.Metadata registries []Registry - secrets secretMap - cacher Cacher + secrets map[string]Secret reslimit ResourceLimit defaultCloneImage string trustedPipeline bool @@ -224,11 +219,6 @@ func (c *Compiler) Compile(conf *yaml_types.Workflow) (*backend_types.Config, er } } - err := c.setupCache(conf, config) - if err != nil { - return nil, err - } - // add services steps if len(conf.Services.ContainerList) != 0 { stage := new(backend_types.Stage) @@ -297,48 +287,5 @@ func (c *Compiler) Compile(conf *yaml_types.Workflow) (*backend_types.Config, er config.Stages = append(config.Stages, stepStages...) - err = c.setupCacheRebuild(conf, config) - if err != nil { - return nil, err - } - return config, nil } - -func (c *Compiler) setupCache(conf *yaml_types.Workflow, ir *backend_types.Config) error { - if c.local || len(conf.Cache) == 0 || c.cacher == nil { - return nil - } - - container := c.cacher.Restore(path.Join(c.metadata.Repo.Owner, c.metadata.Repo.Name), c.metadata.Curr.Commit.Branch, conf.Cache) - step, err := c.createProcess(container, backend_types.StepTypeCache) - if err != nil { - return err - } - - stage := new(backend_types.Stage) - stage.Steps = append(stage.Steps, step) - - ir.Stages = append(ir.Stages, stage) - - return nil -} - -func (c *Compiler) setupCacheRebuild(conf *yaml_types.Workflow, ir *backend_types.Config) error { - if c.local || len(conf.Cache) == 0 || c.metadata.Curr.Event != metadata.EventPush || c.cacher == nil { - return nil - } - container := c.cacher.Rebuild(path.Join(c.metadata.Repo.Owner, c.metadata.Repo.Name), c.metadata.Curr.Commit.Branch, conf.Cache) - - step, err := c.createProcess(container, backend_types.StepTypeCache) - if err != nil { - return err - } - - stage := new(backend_types.Stage) - stage.Steps = append(stage.Steps, step) - - ir.Stages = append(ir.Stages, stage) - - return nil -} diff --git a/pipeline/frontend/yaml/compiler/option.go b/pipeline/frontend/yaml/compiler/option.go index 3fb651756e..e4730d4978 100644 --- a/pipeline/frontend/yaml/compiler/option.go +++ b/pipeline/frontend/yaml/compiler/option.go @@ -149,34 +149,6 @@ func WithEnviron(env map[string]string) Option { } } -// WithCacher configures the compiler with default cache settings. -func WithCacher(cacher Cacher) Option { - return func(compiler *Compiler) { - compiler.cacher = cacher - } -} - -// WithVolumeCacher configures the compiler with default local volume -// caching enabled. -func WithVolumeCacher(base string) Option { - return func(compiler *Compiler) { - compiler.cacher = &volumeCacher{base: base} - } -} - -// WithS3Cacher configures the compiler with default amazon s3 -// caching enabled. -func WithS3Cacher(access, secret, region, bucket string) Option { - return func(compiler *Compiler) { - compiler.cacher = &s3Cacher{ - access: access, - secret: secret, - bucket: bucket, - region: region, - } - } -} - // WithNetworks configures the compiler with additional networks // to be connected to pipeline containers func WithNetworks(networks ...string) Option { diff --git a/pipeline/frontend/yaml/compiler/option_test.go b/pipeline/frontend/yaml/compiler/option_test.go index 77b26638b3..4f8e08e5d5 100644 --- a/pipeline/frontend/yaml/compiler/option_test.go +++ b/pipeline/frontend/yaml/compiler/option_test.go @@ -166,30 +166,9 @@ func TestWithEnviron(t *testing.T) { assert.Equal(t, "true", compiler.env["SHOW"]) } -func TestWithVolumeCacher(t *testing.T) { - compiler := New( - WithVolumeCacher("/cache"), - ) - cacher, ok := compiler.cacher.(*volumeCacher) - assert.True(t, ok) - assert.Equal(t, "/cache", cacher.base) -} - func TestWithDefaultCloneImage(t *testing.T) { compiler := New( WithDefaultCloneImage("not-an-image"), ) assert.Equal(t, "not-an-image", compiler.defaultCloneImage) } - -func TestWithS3Cacher(t *testing.T) { - compiler := New( - WithS3Cacher("some-access-key", "some-secret-key", "some-region", "some-bucket"), - ) - cacher, ok := compiler.cacher.(*s3Cacher) - assert.True(t, ok) - assert.Equal(t, "some-bucket", cacher.bucket) - assert.Equal(t, "some-access-key", cacher.access) - assert.Equal(t, "some-region", cacher.region) - assert.Equal(t, "some-secret-key", cacher.secret) -} diff --git a/pipeline/frontend/yaml/types/workflow.go b/pipeline/frontend/yaml/types/workflow.go index 1c5f98a07c..4f291dec3f 100644 --- a/pipeline/frontend/yaml/types/workflow.go +++ b/pipeline/frontend/yaml/types/workflow.go @@ -16,7 +16,6 @@ package types import ( "go.woodpecker-ci.org/woodpecker/v2/pipeline/frontend/yaml/constraint" - "go.woodpecker-ci.org/woodpecker/v2/pipeline/frontend/yaml/types/base" ) type ( @@ -33,9 +32,8 @@ type ( SkipClone bool `yaml:"skip_clone"` // Undocumented - Cache base.StringOrSlice `yaml:"cache,omitempty"` - Networks WorkflowNetworks `yaml:"networks,omitempty"` - Volumes WorkflowVolumes `yaml:"volumes,omitempty"` + Networks WorkflowNetworks `yaml:"networks,omitempty"` + Volumes WorkflowVolumes `yaml:"volumes,omitempty"` // Deprecated PlatformDoNotUseIt string `yaml:"platform,omitempty"` // TODO: remove in next major version