diff --git a/pkg/commands/add.go b/pkg/commands/add.go index 3c3efeb57c..7581b56cbc 100644 --- a/pkg/commands/add.go +++ b/pkg/commands/add.go @@ -98,7 +98,7 @@ func (a *AddCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.Bui copyCmd := CopyCommand{ cmd: &instructions.CopyCommand{ - SourcesAndDest: instructions.SourcesAndDest{SourcePaths: append(unresolvedSrcs, dest)}, + SourcesAndDest: instructions.SourcesAndDest{SourcePaths: unresolvedSrcs, DestPath: dest}, Chown: a.cmd.Chown, }, fileContext: a.fileContext, diff --git a/pkg/commands/copy_test.go b/pkg/commands/copy_test.go index d5064b1d59..d647a727c1 100755 --- a/pkg/commands/copy_test.go +++ b/pkg/commands/copy_test.go @@ -142,10 +142,7 @@ func Test_CachingCopyCommand_ExecuteCommand(t *testing.T) { }, fileContext: util.FileContext{Root: tempDir}, cmd: &instructions.CopyCommand{ - SourcesAndDest: []string{ - "foo.txt", "foo.txt", - }, - }, + SourcesAndDest: instructions.SourcesAndDest{SourcePaths: []string{"foo.txt"}, DestPath: "foo.txt"}}, } count := 0 tc := testCase{ @@ -274,7 +271,8 @@ func TestCopyExecuteCmd(t *testing.T) { cmd := CopyCommand{ cmd: &instructions.CopyCommand{ - SourcesAndDest: test.sourcesAndDest, + SourcesAndDest: instructions.SourcesAndDest{SourcePaths: test.sourcesAndDest[0 : len(test.sourcesAndDest)-1], + DestPath: test.sourcesAndDest[len(test.sourcesAndDest)-1]}, }, fileContext: fileContext, } @@ -421,7 +419,7 @@ func TestCopyCommand_ExecuteCommand_Extended(t *testing.T) { cmd := CopyCommand{ cmd: &instructions.CopyCommand{ - SourcesAndDest: []string{srcDir, "dest"}, + SourcesAndDest: instructions.SourcesAndDest{SourcePaths: []string{srcDir}, DestPath: "dest"}, }, fileContext: util.FileContext{Root: testDir}, } @@ -453,7 +451,7 @@ func TestCopyCommand_ExecuteCommand_Extended(t *testing.T) { defer os.RemoveAll(testDir) cmd := CopyCommand{ cmd: &instructions.CopyCommand{ - SourcesAndDest: []string{filepath.Join(srcDir, "bam.txt"), "dest/"}, + SourcesAndDest: instructions.SourcesAndDest{SourcePaths: []string{filepath.Join(srcDir, "bam.txt")}, DestPath: "dest/"}, }, fileContext: util.FileContext{Root: testDir}, } @@ -480,7 +478,7 @@ func TestCopyCommand_ExecuteCommand_Extended(t *testing.T) { defer os.RemoveAll(testDir) cmd := CopyCommand{ cmd: &instructions.CopyCommand{ - SourcesAndDest: []string{filepath.Join(srcDir, "bam.txt"), "dest"}, + SourcesAndDest: instructions.SourcesAndDest{SourcePaths: []string{filepath.Join(srcDir, "bam.txt")}, DestPath: "dest"}, }, fileContext: util.FileContext{Root: testDir}, } @@ -509,7 +507,7 @@ func TestCopyCommand_ExecuteCommand_Extended(t *testing.T) { cmd := CopyCommand{ cmd: &instructions.CopyCommand{ - SourcesAndDest: []string{filepath.Join(srcDir, "bam.txt"), "dest"}, + SourcesAndDest: instructions.SourcesAndDest{SourcePaths: []string{filepath.Join(srcDir, "bam.txt")}, DestPath: "dest"}, }, fileContext: util.FileContext{Root: testDir}, } @@ -538,7 +536,7 @@ func TestCopyCommand_ExecuteCommand_Extended(t *testing.T) { cmd := CopyCommand{ cmd: &instructions.CopyCommand{ - SourcesAndDest: []string{filepath.Join(srcDir, "sym.link"), "dest/"}, + SourcesAndDest: instructions.SourcesAndDest{SourcePaths: []string{filepath.Join(srcDir, "sym.link")}, DestPath: "dest/"}, }, fileContext: util.FileContext{Root: testDir}, } @@ -583,7 +581,7 @@ func TestCopyCommand_ExecuteCommand_Extended(t *testing.T) { cmd := CopyCommand{ cmd: &instructions.CopyCommand{ - SourcesAndDest: []string{filepath.Join(srcDir, "dead.link"), "dest/"}, + SourcesAndDest: instructions.SourcesAndDest{SourcePaths: []string{filepath.Join(srcDir, "dead.link")}, DestPath: "dest/"}, }, fileContext: util.FileContext{Root: testDir}, } @@ -614,7 +612,7 @@ func TestCopyCommand_ExecuteCommand_Extended(t *testing.T) { t.Run("copy src symlink dir to a dir", func(t *testing.T) { testDir, srcDir := setupDirs(t) defer os.RemoveAll(testDir) - expected, err := ioutil.ReadDir(filepath.Join(testDir, srcDir)) + expected, err := os.ReadDir(filepath.Join(testDir, srcDir)) if err != nil { t.Fatal(err) } @@ -624,7 +622,7 @@ func TestCopyCommand_ExecuteCommand_Extended(t *testing.T) { cmd := CopyCommand{ cmd: &instructions.CopyCommand{ - SourcesAndDest: []string{"another", "dest"}, + SourcesAndDest: instructions.SourcesAndDest{SourcePaths: []string{"another"}, DestPath: "dest"}, }, fileContext: util.FileContext{Root: testDir}, } @@ -638,13 +636,13 @@ func TestCopyCommand_ExecuteCommand_Extended(t *testing.T) { err = cmd.ExecuteCommand(cfg, dockerfile.NewBuildArgs([]string{})) testutil.CheckNoError(t, err) // Check if "dest" dir exists with contents of srcDir - actual, err := ioutil.ReadDir(filepath.Join(testDir, "dest")) + actual, err := os.ReadDir(filepath.Join(testDir, "dest")) if err != nil { t.Fatal(err) } for i, f := range actual { testutil.CheckDeepEqual(t, expected[i].Name(), f.Name()) - testutil.CheckDeepEqual(t, expected[i].Mode(), f.Mode()) + testutil.CheckDeepEqual(t, expected[i].Type(), f.Type()) } }) @@ -670,7 +668,7 @@ func TestCopyCommand_ExecuteCommand_Extended(t *testing.T) { cmd := CopyCommand{ cmd: &instructions.CopyCommand{ - SourcesAndDest: []string{srcDir, "dest"}, + SourcesAndDest: instructions.SourcesAndDest{SourcePaths: []string{srcDir}, DestPath: "dest"}, }, fileContext: util.FileContext{Root: testDir}, } @@ -704,7 +702,7 @@ func TestCopyCommand_ExecuteCommand_Extended(t *testing.T) { t.Run("copy src symlink dir to a dir", func(t *testing.T) { testDir, srcDir := setupDirs(t) defer os.RemoveAll(testDir) - expected, err := ioutil.ReadDir(filepath.Join(testDir, srcDir)) + expected, err := os.ReadDir(filepath.Join(testDir, srcDir)) if err != nil { t.Fatal(err) } @@ -714,7 +712,7 @@ func TestCopyCommand_ExecuteCommand_Extended(t *testing.T) { cmd := CopyCommand{ cmd: &instructions.CopyCommand{ - SourcesAndDest: []string{"another", "dest"}, + SourcesAndDest: instructions.SourcesAndDest{SourcePaths: []string{"another"}, DestPath: "dest"}, }, fileContext: util.FileContext{Root: testDir}, } @@ -728,13 +726,13 @@ func TestCopyCommand_ExecuteCommand_Extended(t *testing.T) { err = cmd.ExecuteCommand(cfg, dockerfile.NewBuildArgs([]string{})) testutil.CheckNoError(t, err) // Check if "dest" dir exists with bam.txt and "dest" dir is a symlink - actual, err := ioutil.ReadDir(filepath.Join(testDir, "dest")) + actual, err := os.ReadDir(filepath.Join(testDir, "dest")) if err != nil { t.Fatal(err) } for i, f := range actual { testutil.CheckDeepEqual(t, expected[i].Name(), f.Name()) - testutil.CheckDeepEqual(t, expected[i].Mode(), f.Mode()) + testutil.CheckDeepEqual(t, expected[i].Type(), f.Type()) } }) @@ -757,7 +755,7 @@ func TestCopyCommand_ExecuteCommand_Extended(t *testing.T) { cmd := CopyCommand{ cmd: &instructions.CopyCommand{ - SourcesAndDest: []string{srcDir, linkedDest}, + SourcesAndDest: instructions.SourcesAndDest{SourcePaths: []string{srcDir}, DestPath: linkedDest}, }, fileContext: util.FileContext{Root: testDir}, } @@ -802,7 +800,7 @@ func TestCopyCommand_ExecuteCommand_Extended(t *testing.T) { cmd := CopyCommand{ cmd: &instructions.CopyCommand{ - SourcesAndDest: []string{fmt.Sprintf("%s/bam.txt", srcDir), linkedDest}, + SourcesAndDest: instructions.SourcesAndDest{SourcePaths: []string{fmt.Sprintf("%s/bam.txt", srcDir)}, DestPath: linkedDest}, }, fileContext: util.FileContext{Root: testDir}, } @@ -850,7 +848,7 @@ func TestCopyCommand_ExecuteCommand_Extended(t *testing.T) { cmd := CopyCommand{ cmd: &instructions.CopyCommand{ - SourcesAndDest: []string{fmt.Sprintf("%s/bam.txt", srcDir), testDir}, + SourcesAndDest: instructions.SourcesAndDest{SourcePaths: []string{fmt.Sprintf("%s/bam.txt", srcDir)}, DestPath: testDir}, Chown: "alice:group", }, fileContext: util.FileContext{Root: testDir}, @@ -895,7 +893,7 @@ func TestCopyCommand_ExecuteCommand_Extended(t *testing.T) { cmd := CopyCommand{ cmd: &instructions.CopyCommand{ - SourcesAndDest: []string{fmt.Sprintf("%s/bam.txt", srcDir), testDir}, + SourcesAndDest: instructions.SourcesAndDest{SourcePaths: []string{fmt.Sprintf("%s/bam.txt", srcDir)}, DestPath: testDir}, Chown: "missing:missing", }, fileContext: util.FileContext{Root: testDir}, @@ -927,7 +925,7 @@ func TestCopyCommand_ExecuteCommand_Extended(t *testing.T) { dest := filepath.Join(testDir, "copy") cmd := CopyCommand{ cmd: &instructions.CopyCommand{ - SourcesAndDest: []string{srcDir, dest}, + SourcesAndDest: instructions.SourcesAndDest{SourcePaths: []string{srcDir}, DestPath: dest}, }, fileContext: util.FileContext{Root: testDir}, } @@ -939,7 +937,7 @@ func TestCopyCommand_ExecuteCommand_Extended(t *testing.T) { } err := cmd.ExecuteCommand(cfg, dockerfile.NewBuildArgs([]string{})) testutil.CheckNoError(t, err) - actual, err := ioutil.ReadDir(filepath.Join(dest, "another")) + actual, err := os.ReadDir(filepath.Join(dest, "another")) if err != nil { t.Fatal(err) } diff --git a/pkg/dockerfile/dockerfile_test.go b/pkg/dockerfile/dockerfile_test.go index 4e251f319c..e287a292bd 100644 --- a/pkg/dockerfile/dockerfile_test.go +++ b/pkg/dockerfile/dockerfile_test.go @@ -250,11 +250,11 @@ func Test_GetOnBuildInstructions(t *testing.T) { stageToIdx: map[string]string{"builder": "0", "temp": "1"}, expCommands: []instructions.Command{ &instructions.CopyCommand{ - SourcesAndDest: []string{"a.txt b.txt"}, + SourcesAndDest: instructions.SourcesAndDest{SourcePaths: []string{"a.txt"}, DestPath: "b.txt"}, From: "0", }, &instructions.CopyCommand{ - SourcesAndDest: []string{"/app /app"}, + SourcesAndDest: instructions.SourcesAndDest{SourcePaths: []string{"/app"}, DestPath: "/app"}, From: "1", }, }}, diff --git a/pkg/executor/build.go b/pkg/executor/build.go index 7871e89e76..9de9a0ba97 100644 --- a/pkg/executor/build.go +++ b/pkg/executor/build.go @@ -579,7 +579,7 @@ func CalculateDependencies(stages []config.KanikoStage, opts *config.KanikoOptio if err != nil { return nil, err } - depGraph[i] = append(depGraph[i], resolved[0:len(resolved)-1]...) + depGraph[i] = append(depGraph[i], resolved[0:len(resolved)]...) } case *instructions.EnvCommand: if err := util.UpdateConfigEnv(cmd.Env, &cfg.Config, ba.ReplacementEnvs(cfg.Config.Env)); err != nil { diff --git a/pkg/executor/build_test.go b/pkg/executor/build_test.go index 23baf944f1..d58d2484eb 100644 --- a/pkg/executor/build_test.go +++ b/pkg/executor/build_test.go @@ -369,8 +369,8 @@ COPY --from=second /bar /bat initializeConfig = tt.args.mockInitConfig } - f, _ := ioutil.TempFile("", "") - ioutil.WriteFile(f.Name(), []byte(tt.args.dockerfile), 0755) + f, _ := os.CreateTemp("", "") + os.WriteFile(f.Name(), []byte(tt.args.dockerfile), 0755) opts := &config.KanikoOptions{ DockerfilePath: f.Name(), CustomPlatform: platforms.Format(platforms.Normalize(platforms.DefaultSpec())), diff --git a/pkg/util/command_util.go b/pkg/util/command_util.go index ce23825eb0..29ba27cdf7 100644 --- a/pkg/util/command_util.go +++ b/pkg/util/command_util.go @@ -95,9 +95,9 @@ func ResolveEnvAndWildcards(sd instructions.SourcesAndDest, fileContext FileCont if len(resolvedEnvs) == 0 { return nil, "", errors.New("resolved envs is empty") } - dest := resolvedEnvs[len(resolvedEnvs)-1] + dest := sd.DestPath // Resolve wildcards and get a list of resolved sources - srcs, err := ResolveSources(resolvedEnvs[0:len(resolvedEnvs)-1], fileContext.Root) + srcs, err := ResolveSources(resolvedEnvs[0:len(resolvedEnvs)], fileContext.Root) if err != nil { return nil, "", errors.Wrap(err, "failed to resolve sources") } @@ -226,8 +226,8 @@ func URLDestinationFilepath(rawurl, dest, cwd string, envs []string) (string, er } func IsSrcsValid(srcsAndDest instructions.SourcesAndDest, resolvedSources []string, fileContext FileContext) error { - srcs := srcsAndDest.SourcePaths[:len(srcsAndDest.SourcePaths)-1] - dest := srcsAndDest.SourcePaths[len(srcsAndDest.SourcePaths)-1] + srcs := srcsAndDest.SourcePaths + dest := srcsAndDest.DestPath if !ContainsWildcards(srcs) { totalSrcs := 0 diff --git a/pkg/util/command_util_test.go b/pkg/util/command_util_test.go index a4fde651e8..47fed6f744 100644 --- a/pkg/util/command_util_test.go +++ b/pkg/util/command_util_test.go @@ -490,7 +490,7 @@ func Test_IsSrcsValid(t *testing.T) { if err != nil { t.Fatalf("error creating file context: %v", err) } - err = IsSrcsValid(instructions.SourcesAndDest{SourcePaths: test.srcsAndDest}, test.resolvedSources, fileContext) + err = IsSrcsValid(instructions.SourcesAndDest{SourcePaths: test.srcsAndDest[0 : len(test.srcsAndDest)-1], DestPath: test.srcsAndDest[len(test.srcsAndDest)-1]}, test.resolvedSources, fileContext) testutil.CheckError(t, test.shouldErr, err) }) } diff --git a/pkg/util/fs_util_test.go b/pkg/util/fs_util_test.go index 226658bd42..108a420927 100644 --- a/pkg/util/fs_util_test.go +++ b/pkg/util/fs_util_test.go @@ -1018,7 +1018,7 @@ func Test_GetFSFromLayers_with_whiteouts_include_whiteout_enabled(t *testing.T) root := t.TempDir() // Write a whiteout path d1 := []byte("Hello World\n") - if err := ioutil.WriteFile(filepath.Join(root, "foobar"), d1, 0644); err != nil { + if err := os.WriteFile(filepath.Join(root, "foobar"), d1, 0644); err != nil { t.Fatal(err) } @@ -1067,7 +1067,7 @@ func Test_GetFSFromLayers_with_whiteouts_include_whiteout_enabled(t *testing.T) mockLayer := mockv1.NewMockLayer(ctrl) mockLayer.EXPECT().MediaType().Return(types.OCILayer, nil) - rc := ioutil.NopCloser(buf) + rc := io.NopCloser(buf) mockLayer.EXPECT().Uncompressed().Return(rc, nil) secondLayerFiles := []string{ @@ -1082,7 +1082,7 @@ func Test_GetFSFromLayers_with_whiteouts_include_whiteout_enabled(t *testing.T) mockLayer2 := mockv1.NewMockLayer(ctrl) mockLayer2.EXPECT().MediaType().Return(types.OCILayer, nil) - rc = ioutil.NopCloser(buf) + rc = io.NopCloser(buf) mockLayer2.EXPECT().Uncompressed().Return(rc, nil) layers := []v1.Layer{