From cf608fa954ab7a4de1910d8b3f6245894c0df7a5 Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Mon, 13 Nov 2023 19:10:29 +0100 Subject: [PATCH] bump compose-go to v1.20.1 Signed-off-by: Nicolas De Loof --- go.mod | 2 +- go.sum | 4 +-- pkg/compose/build.go | 24 +++++++++++--- pkg/compose/convergence.go | 5 +++ pkg/compose/create.go | 17 ++++++++-- pkg/compose/secrets.go | 34 +++++++++++++++++-- pkg/e2e/configs_test.go | 48 +++++++++++++++++++++++++++ pkg/e2e/fixtures/configs/compose.yaml | 29 ++++++++++++++++ pkg/e2e/fixtures/configs/config.txt | 1 + 9 files changed, 152 insertions(+), 12 deletions(-) create mode 100644 pkg/e2e/configs_test.go create mode 100644 pkg/e2e/fixtures/configs/compose.yaml create mode 100644 pkg/e2e/fixtures/configs/config.txt diff --git a/go.mod b/go.mod index a07d56ab..f06ef26a 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( github.com/AlecAivazis/survey/v2 v2.3.7 github.com/Microsoft/go-winio v0.6.1 github.com/buger/goterm v1.0.4 - github.com/compose-spec/compose-go v1.20.0 + github.com/compose-spec/compose-go v1.20.1 github.com/containerd/console v1.0.3 github.com/containerd/containerd v1.7.7 github.com/davecgh/go-spew v1.1.1 diff --git a/go.sum b/go.sum index 5052371d..0c7c004a 100644 --- a/go.sum +++ b/go.sum @@ -137,8 +137,8 @@ github.com/cncf/xds/go v0.0.0-20230607035331-e9ce68804cb4 h1:/inchEIKaYC1Akx+H+g github.com/cncf/xds/go v0.0.0-20230607035331-e9ce68804cb4/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= github.com/codahale/rfc6979 v0.0.0-20141003034818-6a90f24967eb h1:EDmT6Q9Zs+SbUoc7Ik9EfrFqcylYqgPZ9ANSbTAntnE= github.com/codahale/rfc6979 v0.0.0-20141003034818-6a90f24967eb/go.mod h1:ZjrT6AXHbDs86ZSdt/osfBi5qfexBrKUdONk989Wnk4= -github.com/compose-spec/compose-go v1.20.0 h1:h4ZKOst1EF/DwZp7dWkb+wbTVE4nEyT9Lc89to84Ol4= -github.com/compose-spec/compose-go v1.20.0/go.mod h1:+MdqXV4RA7wdFsahh/Kb8U0pAJqkg7mr4PM9tFKU8RM= +github.com/compose-spec/compose-go v1.20.1 h1:I6gCMGLl96kEf8XZwaozeTwnNfxA2eVsO46W+5ciTEg= +github.com/compose-spec/compose-go v1.20.1/go.mod h1:+MdqXV4RA7wdFsahh/Kb8U0pAJqkg7mr4PM9tFKU8RM= github.com/containerd/cgroups v1.1.0 h1:v8rEWFl6EoqHB+swVNjVoCJE8o3jX7e8nqBGPLaDFBM= github.com/containerd/cgroups v1.1.0/go.mod h1:6ppBcbh/NOOUU+dMKrykgaBnK9lCIBxHqJDGwsa1mIw= github.com/containerd/console v1.0.3 h1:lIr7SlA5PxZyMV30bDW0MGbiOPXwc63yRuCP0ARubLw= diff --git a/pkg/compose/build.go b/pkg/compose/build.go index 7ad944f6..8cb9950c 100644 --- a/pkg/compose/build.go +++ b/pkg/compose/build.go @@ -32,11 +32,9 @@ import ( "github.com/docker/buildx/util/buildflags" xprogress "github.com/docker/buildx/util/progress" "github.com/docker/cli/cli/command" - "github.com/docker/compose/v2/internal/tracing" - "github.com/docker/compose/v2/pkg/api" - "github.com/docker/compose/v2/pkg/progress" - "github.com/docker/compose/v2/pkg/utils" + cliopts "github.com/docker/cli/opts" "github.com/docker/docker/builder/remotecontext/urlutil" + "github.com/docker/go-units" bclient "github.com/moby/buildkit/client" "github.com/moby/buildkit/session" "github.com/moby/buildkit/session/auth/authprovider" @@ -46,6 +44,11 @@ import ( specs "github.com/opencontainers/image-spec/specs-go/v1" "github.com/sirupsen/logrus" + "github.com/docker/compose/v2/internal/tracing" + "github.com/docker/compose/v2/pkg/api" + "github.com/docker/compose/v2/pkg/progress" + "github.com/docker/compose/v2/pkg/utils" + // required to get default driver registered _ "github.com/docker/buildx/driver/docker" ) @@ -407,11 +410,24 @@ func (s *composeService) toBuildOptions(project *types.Project, service types.Se Labels: imageLabels, NetworkMode: service.Build.Network, ExtraHosts: service.Build.ExtraHosts.AsList(), + Ulimits: toUlimitOpt(service.Build.Ulimits), Session: sessionConfig, Allow: allow, }, nil } +func toUlimitOpt(ulimits map[string]*types.UlimitsConfig) *cliopts.UlimitOpt { + ref := map[string]*units.Ulimit{} + for _, limit := range toUlimits(ulimits) { + ref[limit.Name] = &units.Ulimit{ + Name: limit.Name, + Hard: limit.Hard, + Soft: limit.Soft, + } + } + return cliopts.NewUlimitOpt(&ref) +} + func flatten(in types.MappingWithEquals) types.Mapping { out := types.Mapping{} if len(in) == 0 { diff --git a/pkg/compose/convergence.go b/pkg/compose/convergence.go index dabfb491..a7303b15 100644 --- a/pkg/compose/convergence.go +++ b/pkg/compose/convergence.go @@ -623,6 +623,11 @@ func (s *composeService) createMobyContainer(ctx context.Context, } err = s.injectSecrets(ctx, project, service, created.ID) + if err != nil { + return created, err + } + + err = s.injectConfigs(ctx, project, service, created.ID) return created, err } diff --git a/pkg/compose/create.go b/pkg/compose/create.go index 2932f0a4..2d88d66b 100644 --- a/pkg/compose/create.go +++ b/pkg/compose/create.go @@ -521,7 +521,14 @@ func getDeployResources(s types.ServiceConfig) container.Resources { }) } - for name, u := range s.Ulimits { + ulimits := toUlimits(s.Ulimits) + resources.Ulimits = ulimits + return resources +} + +func toUlimits(m map[string]*types.UlimitsConfig) []*units.Ulimit { + var ulimits []*units.Ulimit + for name, u := range m { soft := u.Single if u.Soft != 0 { soft = u.Soft @@ -530,13 +537,13 @@ func getDeployResources(s types.ServiceConfig) container.Resources { if u.Hard != 0 { hard = u.Hard } - resources.Ulimits = append(resources.Ulimits, &units.Ulimit{ + ulimits = append(ulimits, &units.Ulimit{ Name: name, Hard: int64(hard), Soft: int64(soft), }) } - return resources + return ulimits } func setReservations(reservations *types.Resource, resources *container.Resources) { @@ -812,6 +819,10 @@ func buildContainerConfigMounts(p types.Project, s types.ServiceConfig) ([]mount return nil, errors.New("Docker Compose does not support configs.*.template_driver") } + if definedConfig.Environment != "" || definedConfig.Content != "" { + continue + } + bindMount, err := buildMount(p, types.ServiceVolumeConfig{ Type: types.VolumeTypeBind, Source: definedConfig.File, diff --git a/pkg/compose/secrets.go b/pkg/compose/secrets.go index 26573fbd..96eb1b95 100644 --- a/pkg/compose/secrets.go +++ b/pkg/compose/secrets.go @@ -39,7 +39,7 @@ func (s *composeService) injectSecrets(ctx context.Context, project *types.Proje if !ok { return fmt.Errorf("environment variable %q required by secret %q is not set", secret.Environment, secret.Name) } - b, err := createTar(env, config) + b, err := createTar(env, types.FileReferenceConfig(config)) if err != nil { return err } @@ -54,7 +54,37 @@ func (s *composeService) injectSecrets(ctx context.Context, project *types.Proje return nil } -func createTar(env string, config types.ServiceSecretConfig) (bytes.Buffer, error) { +func (s *composeService) injectConfigs(ctx context.Context, project *types.Project, service types.ServiceConfig, id string) error { + for _, config := range service.Configs { + secret := project.Configs[config.Source] + content := secret.Content + if secret.Environment != "" { + env, ok := project.Environment[secret.Environment] + if !ok { + return fmt.Errorf("environment variable %q required by secret %q is not set", secret.Environment, secret.Name) + } + content = env + } + if content == "" { + continue + } + + b, err := createTar(content, types.FileReferenceConfig(config)) + if err != nil { + return err + } + + err = s.apiClient().CopyToContainer(ctx, id, "/", &b, moby.CopyToContainerOptions{ + CopyUIDGID: config.UID != "" || config.GID != "", + }) + if err != nil { + return err + } + } + return nil +} + +func createTar(env string, config types.FileReferenceConfig) (bytes.Buffer, error) { value := []byte(env) b := bytes.Buffer{} tarWriter := tar.NewWriter(&b) diff --git a/pkg/e2e/configs_test.go b/pkg/e2e/configs_test.go new file mode 100644 index 00000000..a3c42962 --- /dev/null +++ b/pkg/e2e/configs_test.go @@ -0,0 +1,48 @@ +/* + Copyright 2020 Docker Compose CLI 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 e2e + +import ( + "testing" + + "gotest.tools/v3/icmd" +) + +func TestConfigFromEnv(t *testing.T) { + c := NewParallelCLI(t) + + t.Run("config from file", func(t *testing.T) { + res := icmd.RunCmd(c.NewDockerComposeCmd(t, "-f", "./fixtures/configs/compose.yaml", "run", "from_file")) + res.Assert(t, icmd.Expected{Out: "This is my config file"}) + }) + + t.Run("config from env", func(t *testing.T) { + res := icmd.RunCmd(c.NewDockerComposeCmd(t, "-f", "./fixtures/configs/compose.yaml", "run", "from_env"), + func(cmd *icmd.Cmd) { + cmd.Env = append(cmd.Env, "CONFIG=config") + }) + res.Assert(t, icmd.Expected{Out: "config"}) + }) + + t.Run("config inlined", func(t *testing.T) { + res := icmd.RunCmd(c.NewDockerComposeCmd(t, "-f", "./fixtures/configs/compose.yaml", "run", "inlined"), + func(cmd *icmd.Cmd) { + cmd.Env = append(cmd.Env, "CONFIG=config") + }) + res.Assert(t, icmd.Expected{Out: "This is my config"}) + }) +} diff --git a/pkg/e2e/fixtures/configs/compose.yaml b/pkg/e2e/fixtures/configs/compose.yaml new file mode 100644 index 00000000..9286d6f4 --- /dev/null +++ b/pkg/e2e/fixtures/configs/compose.yaml @@ -0,0 +1,29 @@ +services: + from_env: + image: alpine + configs: + - source: from_env + target: /from_env + command: cat /from_env + + from_file: + image: alpine + configs: + - source: from_file + target: /from_file + command: cat /from_file + + inlined: + image: alpine + configs: + - source: inlined + target: /inlined + command: cat /inlined + +configs: + from_env: + environment: CONFIG + from_file: + file: config.txt + inlined: + content: This is my $CONFIG diff --git a/pkg/e2e/fixtures/configs/config.txt b/pkg/e2e/fixtures/configs/config.txt new file mode 100644 index 00000000..58b9a406 --- /dev/null +++ b/pkg/e2e/fixtures/configs/config.txt @@ -0,0 +1 @@ +This is my config file