merge conflict fixes

This commit is contained in:
Dani Raznikov 2020-04-15 16:07:52 +03:00
commit 8d6a75a745
5 changed files with 66 additions and 50 deletions

View File

@ -432,6 +432,12 @@ If `--destination=gcr.io/kaniko-project/test`, then cached layers will be stored
_This flag must be used in conjunction with the `--cache=true` flag._ _This flag must be used in conjunction with the `--cache=true` flag._
#### --context-sub-path
Set a sub path within the given `--context`.
Its particularly useful when your context is, for example, a git repository,
and you want to build one of its subfolders instead of the root folder.
#### --digest-file #### --digest-file

View File

@ -40,6 +40,7 @@ import (
var ( var (
opts = &config.KanikoOptions{} opts = &config.KanikoOptions{}
ctxSubPath string
force bool force bool
logLevel string logLevel string
logFormat string logFormat string
@ -131,6 +132,7 @@ var RootCmd = &cobra.Command{
func addKanikoOptionsFlags() { func addKanikoOptionsFlags() {
RootCmd.PersistentFlags().StringVarP(&opts.DockerfilePath, "dockerfile", "f", "Dockerfile", "Path to the dockerfile to be built.") RootCmd.PersistentFlags().StringVarP(&opts.DockerfilePath, "dockerfile", "f", "Dockerfile", "Path to the dockerfile to be built.")
RootCmd.PersistentFlags().StringVarP(&opts.SrcContext, "context", "c", "/workspace/", "Path to the dockerfile build context.") RootCmd.PersistentFlags().StringVarP(&opts.SrcContext, "context", "c", "/workspace/", "Path to the dockerfile build context.")
RootCmd.PersistentFlags().StringVarP(&ctxSubPath, "context-sub-path", "", "", "Sub path within the given context.")
RootCmd.PersistentFlags().StringVarP(&opts.Bucket, "bucket", "b", "", "Name of the GCS bucket from which to access build context as tarball.") RootCmd.PersistentFlags().StringVarP(&opts.Bucket, "bucket", "b", "", "Name of the GCS bucket from which to access build context as tarball.")
RootCmd.PersistentFlags().VarP(&opts.Destinations, "destination", "d", "Registry the final image should be pushed to. Set it repeatedly for multiple destinations.") RootCmd.PersistentFlags().VarP(&opts.Destinations, "destination", "d", "Registry the final image should be pushed to. Set it repeatedly for multiple destinations.")
RootCmd.PersistentFlags().StringVarP(&opts.SnapshotMode, "snapshotMode", "", "full", "Change the file attributes inspected during snapshotting") RootCmd.PersistentFlags().StringVarP(&opts.SnapshotMode, "snapshotMode", "", "full", "Change the file attributes inspected during snapshotting")
@ -259,6 +261,12 @@ func resolveSourceContext() error {
if err != nil { if err != nil {
return err return err
} }
if ctxSubPath != "" {
opts.SrcContext = filepath.Join(opts.SrcContext, ctxSubPath)
if _, err := os.Stat(opts.SrcContext); os.IsNotExist(err) {
return err
}
}
logrus.Debugf("Build context located at %s", opts.SrcContext) logrus.Debugf("Build context located at %s", opts.SrcContext)
return nil return nil
} }

View File

@ -260,6 +260,50 @@ func TestGitBuildcontext(t *testing.T) {
checkContainerDiffOutput(t, diff, expected) checkContainerDiffOutput(t, diff, expected)
} }
func TestGitBuildcontextSubPath(t *testing.T) {
repo := getGitRepo()
dockerfile := "Dockerfile_test_run_2"
// Build with docker
dockerImage := GetDockerImage(config.imageRepo, "Dockerfile_test_git")
dockerCmd := exec.Command("docker",
append([]string{
"build",
"-t", dockerImage,
"-f", dockerfile,
repo + ":" + filepath.Join(integrationPath, dockerfilesPath),
})...)
out, err := RunCommandWithoutTest(dockerCmd)
if err != nil {
t.Errorf("Failed to build image %s with docker command %q: %s %s", dockerImage, dockerCmd.Args, err, string(out))
}
// Build with kaniko
kanikoImage := GetKanikoImage(config.imageRepo, "Dockerfile_test_git")
dockerRunFlags := []string{"run", "--net=host"}
dockerRunFlags = addServiceAccountFlags(dockerRunFlags, config.serviceAccount)
dockerRunFlags = append(
dockerRunFlags,
ExecutorImage,
"-f", dockerfile,
"-d", kanikoImage,
"-c", fmt.Sprintf("git://%s", repo),
"--context-sub-path", filepath.Join(integrationPath, dockerfilesPath),
)
kanikoCmd := exec.Command("docker", dockerRunFlags...)
out, err = RunCommandWithoutTest(kanikoCmd)
if err != nil {
t.Errorf("Failed to build image %s with kaniko command %q: %v %s", dockerImage, kanikoCmd.Args, err, string(out))
}
diff := containerDiff(t, daemonPrefix+dockerImage, kanikoImage, "--no-cache")
expected := fmt.Sprintf(emptyContainerDiff, dockerImage, kanikoImage, dockerImage, kanikoImage)
checkContainerDiffOutput(t, diff, expected)
}
func TestBuildViaRegistryMirror(t *testing.T) { func TestBuildViaRegistryMirror(t *testing.T) {
repo := getGitRepo() repo := getGitRepo()
dockerfile := "integration/dockerfiles/Dockerfile_registry_mirror" dockerfile := "integration/dockerfiles/Dockerfile_registry_mirror"

View File

@ -259,7 +259,6 @@ func MakeKanikoStages(opts *config.KanikoOptions, stages []instructions.Stage, m
logrus.Infof("Resolved base name %s to %s", stage.BaseName, stage.Name) logrus.Infof("Resolved base name %s to %s", stage.BaseName, stage.Name)
} }
baseImageIndex := baseImageIndex(index, stages) baseImageIndex := baseImageIndex(index, stages)
kanikoStages = append(kanikoStages, config.KanikoStage{ kanikoStages = append(kanikoStages, config.KanikoStage{
Stage: stage, Stage: stage,
BaseImageIndex: baseImageIndex, BaseImageIndex: baseImageIndex,

View File

@ -190,46 +190,6 @@ func Test_stripEnclosingQuotes(t *testing.T) {
} }
} }
func Test_ResolveCrossStageCommands(t *testing.T) {
type testCase struct {
name string
cmd instructions.CopyCommand
stageToIdx map[string]string
expFrom string
}
tests := []testCase{
{
name: "resolve copy command",
cmd: instructions.CopyCommand{From: "builder"},
stageToIdx: map[string]string{"builder": "0"},
expFrom: "0",
},
{
name: "resolve upper case FROM",
cmd: instructions.CopyCommand{From: "BuIlDeR"},
stageToIdx: map[string]string{"builder": "0"},
expFrom: "0",
},
{
name: "nothing to resolve",
cmd: instructions.CopyCommand{From: "downloader"},
stageToIdx: map[string]string{"builder": "0"},
expFrom: "downloader",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
cmds := []instructions.Command{&test.cmd}
ResolveCrossStageCommands(cmds, test.stageToIdx)
if test.cmd.From != test.expFrom {
t.Fatalf("Failed to resolve command: expected from %s, resolved to %s", test.expFrom, test.cmd.From)
}
})
}
}
func Test_GetOnBuildInstructions(t *testing.T) { func Test_GetOnBuildInstructions(t *testing.T) {
type testCase struct { type testCase struct {
name string name string
@ -461,7 +421,6 @@ func Test_ResolveStagesArgs(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
stagesLen := len(stages) stagesLen := len(stages)
args := unifyArgs(metaArgs, buildArgs) args := unifyArgs(metaArgs, buildArgs)
if err := resolveStagesArgs(stages, args); err != nil { if err := resolveStagesArgs(stages, args); err != nil {
t.Fatalf("fail to resolves args %v: %v", buildArgs, err) t.Fatalf("fail to resolves args %v: %v", buildArgs, err)