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 {
for _, config := range service.Secrets {
secret := project.Secrets[config.Source]
if secret.Environment == "" {
file := project.Secrets[config.Source]
if file.Environment == "" {
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 {
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))
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 {
for _, config := range service.Configs {
secret := project.Configs[config.Source]
content := secret.Content
if secret.Environment != "" {
env, ok := project.Environment[secret.Environment]
file := project.Configs[config.Source]
content := file.Content
if file.Environment != "" {
env, ok := project.Environment[file.Environment]
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
}
@ -69,6 +75,10 @@ func (s *composeService) injectConfigs(ctx context.Context, project *types.Proje
continue
}
if config.Target == "" {
config.Target = "/" + config.Source
}
b, err := createTar(content, types.FileReferenceConfig(config))
if err != nil {
return err
@ -93,13 +103,6 @@ func createTar(env string, config types.FileReferenceConfig) (bytes.Buffer, erro
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
if 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{
Name: target,
Name: config.Target,
Size: int64(len(value)),
Mode: int64(mode),
ModTime: time.Now(),

View File

@ -45,4 +45,12 @@ func TestConfigFromEnv(t *testing.T) {
})
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
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
target:
image: alpine
configs:
- source: inlined
target: /target
command: cat /target
configs:
from_env:
environment: CONFIG