Skip to content

Commit d2f617d

Browse files
committed
Inherit arguments between stages
Signed-off-by: Gregor Riepl <[email protected]>
1 parent 9983122 commit d2f617d

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

imagebuildah/executor.go

+15-6
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ type Executor struct {
164164
compatVolumes types.OptionalBool
165165
compatScratchConfig types.OptionalBool
166166
noPivotRoot bool
167+
argResult map[string]map[string]string
167168
}
168169

169170
type imageTypeAndHistoryAndDiffIDs struct {
@@ -324,6 +325,7 @@ func newExecutor(logger *logrus.Logger, logPrefix string, store storage.Store, o
324325
compatVolumes: options.CompatVolumes,
325326
compatScratchConfig: options.CompatScratchConfig,
326327
noPivotRoot: options.NoPivotRoot,
328+
argResult: make(map[string]map[string]string),
327329
}
328330
if exec.err == nil {
329331
exec.err = os.Stderr
@@ -413,7 +415,7 @@ func (b *Executor) resolveNameToImageRef(output string) (types.ImageReference, e
413415
// that the specified stage has finished. If there is no stage defined by that
414416
// name, then it will return (false, nil). If there is a stage defined by that
415417
// name, it will return true along with any error it encounters.
416-
func (b *Executor) waitForStage(ctx context.Context, name string, stages imagebuilder.Stages) (bool, error) {
418+
func (b *Executor) waitForStage(ctx context.Context, name string, stages imagebuilder.Stages) (bool, map[string]string, error) {
417419
found := false
418420
for _, otherStage := range stages {
419421
if otherStage.Name == name || strconv.Itoa(otherStage.Position) == name {
@@ -422,32 +424,39 @@ func (b *Executor) waitForStage(ctx context.Context, name string, stages imagebu
422424
}
423425
}
424426
if !found {
425-
return false, nil
427+
return false, nil, nil
426428
}
427429
for {
428430
if b.lastError != nil {
429-
return true, b.lastError
431+
return true, nil, b.lastError
430432
}
431433

432434
b.stagesLock.Lock()
433435
terminationError, terminated := b.terminatedStage[name]
436+
args := b.argResult[name]
434437
b.stagesLock.Unlock()
435438

436439
if terminationError != nil {
437-
return false, terminationError
440+
return false, nil, terminationError
438441
}
439442
if terminated {
440-
return true, nil
443+
return true, nil, nil
441444
}
442445

443446
b.stagesSemaphore.Release(1)
444447
time.Sleep(time.Millisecond * 10)
445448
if err := b.stagesSemaphore.Acquire(ctx, 1); err != nil {
446-
return true, fmt.Errorf("reacquiring job semaphore: %w", err)
449+
return true, args, fmt.Errorf("reacquiring job semaphore: %w", err)
447450
}
448451
}
449452
}
450453

454+
func (b *Executor) freezeArgs(name string, args map[string]string) {
455+
b.stagesLock.Lock()
456+
b.argResult[name] = args
457+
b.stagesLock.Unlock()
458+
}
459+
451460
// getImageTypeAndHistoryAndDiffIDs returns the manifest type, history, and diff IDs list of imageID.
452461
func (b *Executor) getImageTypeAndHistoryAndDiffIDs(ctx context.Context, imageID string) (string, []v1.History, []digest.Digest, error) {
453462
b.imageInfoLock.Lock()

imagebuildah/stage_executor.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"errors"
77
"fmt"
88
"io"
9+
"maps"
910
"os"
1011
"path"
1112
"path/filepath"
@@ -512,7 +513,7 @@ func (s *StageExecutor) performCopy(excludes []string, copies ...imagebuilder.Co
512513
}
513514
}
514515
if additionalBuildContext == nil {
515-
if isStage, err := s.executor.waitForStage(s.ctx, from, s.stages[:s.index]); isStage && err != nil {
516+
if isStage, _, err := s.executor.waitForStage(s.ctx, from, s.stages[:s.index]); isStage && err != nil {
516517
return err
517518
}
518519
if other, ok := s.executor.stages[from]; ok && other.index < s.index {
@@ -677,7 +678,7 @@ func (s *StageExecutor) runStageMountPoints(mountList []string) (map[string]inte
677678
// If the source's name corresponds to the
678679
// result of an earlier stage, wait for that
679680
// stage to finish being built.
680-
if isStage, err := s.executor.waitForStage(s.ctx, from, s.stages[:s.index]); isStage && err != nil {
681+
if isStage, _, err := s.executor.waitForStage(s.ctx, from, s.stages[:s.index]); isStage && err != nil {
681682
return nil, err
682683
}
683684
// If the source's name is a stage, return a
@@ -1149,8 +1150,13 @@ func (s *StageExecutor) Execute(ctx context.Context, base string) (imgID string,
11491150
// If not, then go on assuming that it's just a regular image that's
11501151
// either in local storage, or one that we have to pull from a
11511152
// registry, subject to the passed-in pull policy.
1152-
if isStage, err := s.executor.waitForStage(ctx, base, s.stages[:s.index]); isStage && err != nil {
1153+
// This helper will also return the final resolved argument list from
1154+
// the parent state.
1155+
if isStage, parentArgs, err := s.executor.waitForStage(ctx, base, s.stages[:s.index]); isStage && err != nil {
11531156
return "", nil, false, err
1157+
} else {
1158+
// Update the start args with those from the parent stage
1159+
maps.Copy(ib.Args, parentArgs)
11541160
}
11551161
pullPolicy := s.executor.pullPolicy
11561162
s.executor.stagesLock.Lock()
@@ -1369,7 +1375,7 @@ func (s *StageExecutor) Execute(ctx context.Context, base string) (imgID string,
13691375
// If the source's name corresponds to the
13701376
// result of an earlier stage, wait for that
13711377
// stage to finish being built.
1372-
if isStage, err := s.executor.waitForStage(ctx, from, s.stages[:s.index]); isStage && err != nil {
1378+
if isStage, _, err := s.executor.waitForStage(ctx, from, s.stages[:s.index]); isStage && err != nil {
13731379
return "", nil, false, err
13741380
}
13751381
if otherStage, ok := s.executor.stages[from]; ok && otherStage.index < s.index {
@@ -1742,6 +1748,8 @@ func (s *StageExecutor) Execute(ctx context.Context, base string) (imgID string,
17421748
}
17431749
}
17441750

1751+
s.executor.freezeArgs(s.name, ib.Args)
1752+
17451753
return imgID, ref, onlyBaseImage, nil
17461754
}
17471755

0 commit comments

Comments
 (0)