Fix configs are mounted under /<id>

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2023-11-29 14:21:39 +01:00 committed by Nicolas De loof
parent 750553c866
commit 59f11ecbeb
3 changed files with 35 additions and 20 deletions

View File

@ -30,14 +30,20 @@ import (
func (s *composeService) injectSecrets(ctx context.Context, project *types.Project, service types.ServiceConfig, id string) error { func (s *composeService) injectSecrets(ctx context.Context, project *types.Project, service types.ServiceConfig, id string) error {
for _, config := range service.Secrets { for _, config := range service.Secrets {
secret := project.Secrets[config.Source] file := project.Secrets[config.Source]
if secret.Environment == "" { if file.Environment == "" {
continue continue
} }
env, ok := project.Environment[secret.Environment] if config.Target == "" {
config.Target = "/run/secrets/" + config.Source
} else if !isAbsTarget(config.Target) {
config.Target = "/run/secrets/" + config.Target
}
env, ok := project.Environment[file.Environment]
if !ok { if !ok {
return fmt.Errorf("environment variable %q required by secret %q is not set", secret.Environment, secret.Name) return fmt.Errorf("environment variable %q required by file %q is not set", file.Environment, file.Name)
} }
b, err := createTar(env, types.FileReferenceConfig(config)) b, err := createTar(env, types.FileReferenceConfig(config))
if err != nil { if err != nil {
@ -56,12 +62,12 @@ func (s *composeService) injectSecrets(ctx context.Context, project *types.Proje
func (s *composeService) injectConfigs(ctx context.Context, project *types.Project, service types.ServiceConfig, id string) error { func (s *composeService) injectConfigs(ctx context.Context, project *types.Project, service types.ServiceConfig, id string) error {
for _, config := range service.Configs { for _, config := range service.Configs {
secret := project.Configs[config.Source] file := project.Configs[config.Source]
content := secret.Content content := file.Content
if secret.Environment != "" { if file.Environment != "" {
env, ok := project.Environment[secret.Environment] env, ok := project.Environment[file.Environment]
if !ok { if !ok {
return fmt.Errorf("environment variable %q required by secret %q is not set", secret.Environment, secret.Name) return fmt.Errorf("environment variable %q required by file %q is not set", file.Environment, file.Name)
} }
content = env content = env
} }
@ -69,6 +75,10 @@ func (s *composeService) injectConfigs(ctx context.Context, project *types.Proje
continue continue
} }
if config.Target == "" {
config.Target = "/" + config.Source
}
b, err := createTar(content, types.FileReferenceConfig(config)) b, err := createTar(content, types.FileReferenceConfig(config))
if err != nil { if err != nil {
return err return err
@ -93,13 +103,6 @@ func createTar(env string, config types.FileReferenceConfig) (bytes.Buffer, erro
mode = *config.Mode mode = *config.Mode
} }
target := config.Target
if config.Target == "" {
target = "/run/secrets/" + config.Source
} else if !isAbsTarget(config.Target) {
target = "/run/secrets/" + config.Target
}
var uid, gid int var uid, gid int
if config.UID != "" { if config.UID != "" {
v, err := strconv.Atoi(config.UID) v, err := strconv.Atoi(config.UID)
@ -117,7 +120,7 @@ func createTar(env string, config types.FileReferenceConfig) (bytes.Buffer, erro
} }
header := &tar.Header{ header := &tar.Header{
Name: target, Name: config.Target,
Size: int64(len(value)), Size: int64(len(value)),
Mode: int64(mode), Mode: int64(mode),
ModTime: time.Now(), ModTime: time.Now(),

View File

@ -45,4 +45,12 @@ func TestConfigFromEnv(t *testing.T) {
}) })
res.Assert(t, icmd.Expected{Out: "This is my config"}) res.Assert(t, icmd.Expected{Out: "This is my config"})
}) })
t.Run("custom target", func(t *testing.T) {
res := icmd.RunCmd(c.NewDockerComposeCmd(t, "-f", "./fixtures/configs/compose.yaml", "run", "target"),
func(cmd *icmd.Cmd) {
cmd.Env = append(cmd.Env, "CONFIG=config")
})
res.Assert(t, icmd.Expected{Out: "This is my config"})
})
} }

View File

@ -3,23 +3,27 @@ services:
image: alpine image: alpine
configs: configs:
- source: from_env - source: from_env
target: /from_env
command: cat /from_env command: cat /from_env
from_file: from_file:
image: alpine image: alpine
configs: configs:
- source: from_file - source: from_file
target: /from_file
command: cat /from_file command: cat /from_file
inlined: inlined:
image: alpine image: alpine
configs: configs:
- source: inlined - source: inlined
target: /inlined
command: cat /inlined command: cat /inlined
target:
image: alpine
configs:
- source: inlined
target: /target
command: cat /target
configs: configs:
from_env: from_env:
environment: CONFIG environment: CONFIG