Skip to content

Commit

Permalink
Allow build args in target image config
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Engelbert <pmengelbert@gmail.com>
Co-authored-by: Brian Goff <cpuguy83@gmail.com>
  • Loading branch information
pmengelbert and cpuguy83 committed Feb 13, 2025
1 parent 334eb99 commit ba0d98b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
25 changes: 24 additions & 1 deletion load.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,10 @@ func (s *Spec) SubstituteArgs(env map[string]string, opts ...SubstituteOpt) erro
}
}

if err := s.Image.processBuildArgs(lex, args, cfg.AllowArg); err != nil {
appendErr(errors.Wrap(err, "package config"))
}

if err := s.Dependencies.processBuildArgs(args, cfg.AllowArg); err != nil {
appendErr(errors.Wrap(err, "dependencies"))
}
Expand Down Expand Up @@ -473,7 +477,7 @@ func validateSymlinks(symlinks map[string]SymlinkTarget) error {
errs = append(errs, err)
}

if (cfg.Path != "" && len(cfg.Paths) > 0) || (cfg.Path == "" && len(cfg.Paths) == 0 ) {
if (cfg.Path != "" && len(cfg.Paths) > 0) || (cfg.Path == "" && len(cfg.Paths) == 0) {
err = fmt.Errorf("'path' and 'paths' fields are mutually exclusive, and at least one is required: "+
"symlink to %s", oldpath)

Expand Down Expand Up @@ -536,3 +540,22 @@ func validateSymlinks(symlinks map[string]SymlinkTarget) error {

return goerrors.Join(errs...)
}

func (img *ImageConfig) processBuildArgs(lex *shell.Lex, args map[string]string, allowArg func(string) bool) error {
if img == nil {
return nil
}

var errs error

for k, v := range img.Labels {
updated, err := expandArgs(lex, v, args, allowArg)
if err != nil {
errs = goerrors.Join(errs, errors.Wrapf(err, "env %s=%s", k, v))
continue
}
img.Labels[k] = updated
}

return errs
}
10 changes: 9 additions & 1 deletion load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,14 @@ func TestSpec_SubstituteBuildArgs(t *testing.T) {
Args: maps.Clone(pairs),
},
},
Image: &ImageConfig{
Labels: map[string]string{
"foo": "$FOO",
},
Volumes: map[string]struct{}{
"": {},
},
},
},
}

Expand All @@ -611,7 +619,7 @@ func TestSpec_SubstituteBuildArgs(t *testing.T) {
assert.Check(t, cmp.Equal(spec.Targets["t2"].PackageConfig.Signer.Args["BAR"], bar))
assert.Check(t, cmp.Equal(spec.Targets["t2"].PackageConfig.Signer.Args["WHATEVER"], argWithDefault))
assert.Check(t, cmp.Equal(spec.Targets["t2"].PackageConfig.Signer.Args["REGULAR"], plainOleValue))

assert.Check(t, cmp.Equal(spec.Targets["t2"].Image.Labels["foo"], foo))
}

func TestCustomRepoFillDefaults(t *testing.T) {
Expand Down
4 changes: 4 additions & 0 deletions target.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ func (t *Target) processBuildArgs(lex *shell.Lex, args map[string]string, allowA
}
}

if err := t.Image.processBuildArgs(lex, args, allowArg); err != nil {
errs = append(errs, errors.Wrap(err, "package config"))
}

if err := t.Dependencies.processBuildArgs(args, allowArg); err != nil {
errs = append(errs, errors.Wrap(err, "dependencies"))
}
Expand Down

0 comments on commit ba0d98b

Please sign in to comment.