Skip to content

Commit ba0d98b

Browse files
pmengelbertcpuguy83
andcommitted
Allow build args in target image config
Signed-off-by: Peter Engelbert <[email protected]> Co-authored-by: Brian Goff <[email protected]>
1 parent 334eb99 commit ba0d98b

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

load.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,10 @@ func (s *Spec) SubstituteArgs(env map[string]string, opts ...SubstituteOpt) erro
199199
}
200200
}
201201

202+
if err := s.Image.processBuildArgs(lex, args, cfg.AllowArg); err != nil {
203+
appendErr(errors.Wrap(err, "package config"))
204+
}
205+
202206
if err := s.Dependencies.processBuildArgs(args, cfg.AllowArg); err != nil {
203207
appendErr(errors.Wrap(err, "dependencies"))
204208
}
@@ -473,7 +477,7 @@ func validateSymlinks(symlinks map[string]SymlinkTarget) error {
473477
errs = append(errs, err)
474478
}
475479

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

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

537541
return goerrors.Join(errs...)
538542
}
543+
544+
func (img *ImageConfig) processBuildArgs(lex *shell.Lex, args map[string]string, allowArg func(string) bool) error {
545+
if img == nil {
546+
return nil
547+
}
548+
549+
var errs error
550+
551+
for k, v := range img.Labels {
552+
updated, err := expandArgs(lex, v, args, allowArg)
553+
if err != nil {
554+
errs = goerrors.Join(errs, errors.Wrapf(err, "env %s=%s", k, v))
555+
continue
556+
}
557+
img.Labels[k] = updated
558+
}
559+
560+
return errs
561+
}

load_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,14 @@ func TestSpec_SubstituteBuildArgs(t *testing.T) {
590590
Args: maps.Clone(pairs),
591591
},
592592
},
593+
Image: &ImageConfig{
594+
Labels: map[string]string{
595+
"foo": "$FOO",
596+
},
597+
Volumes: map[string]struct{}{
598+
"": {},
599+
},
600+
},
593601
},
594602
}
595603

@@ -611,7 +619,7 @@ func TestSpec_SubstituteBuildArgs(t *testing.T) {
611619
assert.Check(t, cmp.Equal(spec.Targets["t2"].PackageConfig.Signer.Args["BAR"], bar))
612620
assert.Check(t, cmp.Equal(spec.Targets["t2"].PackageConfig.Signer.Args["WHATEVER"], argWithDefault))
613621
assert.Check(t, cmp.Equal(spec.Targets["t2"].PackageConfig.Signer.Args["REGULAR"], plainOleValue))
614-
622+
assert.Check(t, cmp.Equal(spec.Targets["t2"].Image.Labels["foo"], foo))
615623
}
616624

617625
func TestCustomRepoFillDefaults(t *testing.T) {

target.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ func (t *Target) processBuildArgs(lex *shell.Lex, args map[string]string, allowA
7070
}
7171
}
7272

73+
if err := t.Image.processBuildArgs(lex, args, allowArg); err != nil {
74+
errs = append(errs, errors.Wrap(err, "package config"))
75+
}
76+
7377
if err := t.Dependencies.processBuildArgs(args, allowArg); err != nil {
7478
errs = append(errs, errors.Wrap(err, "dependencies"))
7579
}

0 commit comments

Comments
 (0)