From 3aaec5015bc9c39e922333c29cbaab9a945f99dd Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Fri, 3 Apr 2020 15:34:59 -0300 Subject: [PATCH 1/5] feat: allow a subdir within a context Signed-off-by: Carlos Alexandro Becker --- cmd/executor/cmd/root.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/cmd/executor/cmd/root.go b/cmd/executor/cmd/root.go index ff198060d..a044a305b 100644 --- a/cmd/executor/cmd/root.go +++ b/cmd/executor/cmd/root.go @@ -39,10 +39,11 @@ import ( ) var ( - opts = &config.KanikoOptions{} - force bool - logLevel string - logFormat string + opts = &config.KanikoOptions{} + ctxSubPath string + force bool + logLevel string + logFormat string ) func init() { @@ -131,6 +132,7 @@ var RootCmd = &cobra.Command{ func addKanikoOptionsFlags() { 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(&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().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") @@ -259,6 +261,9 @@ func resolveSourceContext() error { if err != nil { return err } + if ctxSubPath != "" { + opts.SrcContext = filepath.Join(opts.SrcContext, ctxSubPath) + } logrus.Debugf("Build context located at %s", opts.SrcContext) return nil } From 92b9582ff9bb487942981a451836d31ae3db747b Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Fri, 3 Apr 2020 16:21:29 -0300 Subject: [PATCH 2/5] fix: better error if not exists Signed-off-by: Carlos Alexandro Becker --- cmd/executor/cmd/root.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cmd/executor/cmd/root.go b/cmd/executor/cmd/root.go index a044a305b..db27d75f3 100644 --- a/cmd/executor/cmd/root.go +++ b/cmd/executor/cmd/root.go @@ -263,6 +263,9 @@ func resolveSourceContext() error { } 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) return nil From 0fc7b8a4f4b7f5452102dcb6aea9241638e798c8 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Fri, 3 Apr 2020 16:34:22 -0300 Subject: [PATCH 3/5] test: add it Signed-off-by: Carlos Alexandro Becker --- integration/integration_test.go | 44 +++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/integration/integration_test.go b/integration/integration_test.go index 0ae62e4b4..53d4dd364 100644 --- a/integration/integration_test.go +++ b/integration/integration_test.go @@ -260,6 +260,50 @@ func TestGitBuildcontext(t *testing.T) { 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, + filepath.Join(repo, 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) { repo := getGitRepo() dockerfile := "integration/dockerfiles/Dockerfile_registry_mirror" From 8b50908e48c103b848fd7c78113840ba206b5b15 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Fri, 3 Apr 2020 17:15:40 -0300 Subject: [PATCH 4/5] test: fixed Signed-off-by: Carlos Alexandro Becker --- integration/integration_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/integration/integration_test.go b/integration/integration_test.go index 53d4dd364..5cb854782 100644 --- a/integration/integration_test.go +++ b/integration/integration_test.go @@ -271,7 +271,7 @@ func TestGitBuildcontextSubPath(t *testing.T) { "build", "-t", dockerImage, "-f", dockerfile, - filepath.Join(repo, integrationPath, dockerfilesPath), + repo + ":" + filepath.Join(integrationPath, dockerfilesPath), })...) out, err := RunCommandWithoutTest(dockerCmd) if err != nil { From 4bd1444be03302bd1d5ab0161dec6e9158606cb0 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Wed, 8 Apr 2020 14:08:57 -0300 Subject: [PATCH 5/5] docs: --sub-path Signed-off-by: Carlos Alexandro Becker --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 2e0de1929..726dc2708 100644 --- a/README.md +++ b/README.md @@ -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._ +#### --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