bump compose-go to v1.20.1

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2023-11-13 19:10:29 +01:00 committed by Nicolas De loof
parent 426377a4c9
commit cf608fa954
9 changed files with 152 additions and 12 deletions

2
go.mod
View File

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

4
go.sum
View File

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

View File

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

View File

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

View File

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

View File

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

48
pkg/e2e/configs_test.go Normal file
View File

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

View File

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

View File

@ -0,0 +1 @@
This is my config file