Skip to content

Commit 703436d

Browse files
authored
Merge pull request #337 from osscontainertools/mz334-cache-lookahead-refactoring-part-2
mz334: move resolveCrossStageCommands forwards
2 parents dbebd7e + 94dfc0e commit 703436d

3 files changed

Lines changed: 9 additions & 35 deletions

File tree

pkg/dockerfile/dockerfile.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ func saveStage(index int, stages []instructions.Stage) bool {
253253
// ResolveCrossStageCommands resolves any calls to previous stages with names to indices
254254
// Ex. --from=secondStage should be --from=1 for easier processing later on
255255
// As third party library lowers stage name in FROM instruction, this function resolves stage case insensitively.
256-
func ResolveCrossStageCommands(cmds []instructions.Command, stageNameToIdx map[string]int) {
256+
func resolveCrossStageCommands(cmds []instructions.Command, stageNameToIdx map[string]int) {
257257
for _, cmd := range cmds {
258258
switch c := cmd.(type) {
259259
case *instructions.CopyCommand:
@@ -331,7 +331,7 @@ func MakeKanikoStages(opts *config.KanikoOptions, stages []instructions.Stage, m
331331
return nil, fmt.Errorf("failed to parse ONBUILD instructions: %w", err)
332332
}
333333
stage.Commands = append(cmds, stage.Commands...)
334-
334+
resolveCrossStageCommands(stage.Commands, stageByName)
335335
if opts.SkipUnusedStages {
336336
if baseImageStoredLocally {
337337
stagesDependencies[baseImageIndex]++
@@ -340,16 +340,7 @@ func MakeKanikoStages(opts *config.KanikoOptions, stages []instructions.Stage, m
340340
switch cmd := c.(type) {
341341
case *instructions.CopyCommand:
342342
if copyFromIndex, err := strconv.Atoi(cmd.From); err == nil {
343-
// numeric reference `COPY --from=0`
344343
copyDependencies[copyFromIndex]++
345-
} else {
346-
// named reference `COPY --from=base`
347-
if copyFromIndex, ok := stageByName[strings.ToLower(cmd.From)]; ok {
348-
// There can be references that appear as non-existing stages
349-
// ie. `COPY --from=debian` would try refer to `debian` as stage
350-
// before falling back to `debian` as a docker image.
351-
copyDependencies[copyFromIndex]++
352-
}
353344
}
354345
}
355346
}

pkg/dockerfile/dockerfile_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ func Test_GetOnBuildInstructions(t *testing.T) {
235235
if len(cmds) != len(test.expCommands) {
236236
t.Fatalf("Expected %d commands, got %d", len(test.expCommands), len(cmds))
237237
}
238-
ResolveCrossStageCommands(cmds, test.stageToIdx)
238+
resolveCrossStageCommands(cmds, test.stageToIdx)
239239

240240
for i, cmd := range cmds {
241241
if reflect.TypeOf(cmd) != reflect.TypeOf(test.expCommands[i]) {

pkg/executor/build.go

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,8 +1084,6 @@ func fetchExtraStages(stages []config.KanikoStage, opts *config.KanikoOptions) e
10841084
t := timing.Start("Fetching Extra Stages")
10851085
defer timing.DefaultRun.Stop(t)
10861086

1087-
var names []string
1088-
10891087
for _, s := range stages {
10901088
for _, cmd := range s.Commands {
10911089
c, ok := cmd.(*instructions.CopyCommand)
@@ -1094,14 +1092,13 @@ func fetchExtraStages(stages []config.KanikoStage, opts *config.KanikoOptions) e
10941092
}
10951093

10961094
// FROMs at this point are guaranteed to be either an integer referring to a previous stage,
1097-
// the name of a previous stage, or a name of a remote image.
1095+
// or a name of a remote image.
10981096

1099-
// If it is an integer stage index, validate that it is actually a previous index
1100-
if fromIndex, err := strconv.Atoi(c.From); err == nil && s.Index > fromIndex && fromIndex >= 0 {
1101-
continue
1102-
}
1103-
// Check if the name is the alias of a previous stage
1104-
if fromPreviousStage(c, names) {
1097+
if fromIndex, err := strconv.Atoi(c.From); err == nil {
1098+
// If it is an integer stage index, validate that it is actually a previous index
1099+
if s.Index <= fromIndex || fromIndex < 0 {
1100+
return fmt.Errorf("%s refers to invalid stage: %d", c.String(), fromIndex)
1101+
}
11051102
continue
11061103
}
11071104

@@ -1118,23 +1115,10 @@ func fetchExtraStages(stages []config.KanikoStage, opts *config.KanikoOptions) e
11181115
return err
11191116
}
11201117
}
1121-
// Store the name of the current stage in the list with names, if applicable.
1122-
if s.Name != "" {
1123-
names = append(names, s.Name)
1124-
}
11251118
}
11261119
return nil
11271120
}
11281121

1129-
func fromPreviousStage(copyCommand *instructions.CopyCommand, previousStageNames []string) bool {
1130-
for _, previousStageName := range previousStageNames {
1131-
if previousStageName == copyCommand.From {
1132-
return true
1133-
}
1134-
}
1135-
return false
1136-
}
1137-
11381122
func extractImageToDependencyDir(name string, image v1.Image) error {
11391123
t := timing.Start("Extracting Image to Dependency Dir")
11401124
defer timing.DefaultRun.Stop(t)
@@ -1214,7 +1198,6 @@ func ResolveCrossStageInstructions(stages []config.KanikoStage) map[string]int {
12141198
if stage.Name != "" {
12151199
nameToIndex[stage.Name] = stage.Index
12161200
}
1217-
dockerfile.ResolveCrossStageCommands(stage.Commands, nameToIndex)
12181201
}
12191202

12201203
logrus.Debugf("Built stage name to index map: %v", nameToIndex)

0 commit comments

Comments
 (0)