From f0b9ad3a57f5e0ad16bb0b26097d6864a1e52006 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sat, 22 Jun 2019 11:45:07 -0300 Subject: [PATCH 1/5] feat: support specifying branch for cloning Signed-off-by: Carlos Alexandro Becker --- integration/integration_test.go | 43 +++++++++++++++++++++++++++++++++ pkg/buildcontext/git.go | 16 ++++++++++-- 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/integration/integration_test.go b/integration/integration_test.go index c11dd1378..b1bb4fd0d 100644 --- a/integration/integration_test.go +++ b/integration/integration_test.go @@ -278,6 +278,49 @@ func TestGitBuildcontext(t *testing.T) { checkContainerDiffOutput(t, diff, expected) } +func TestGitBuildContextWithBranch(t *testing.T) { + repo := "github.com/GoogleContainerTools/kaniko#v0.10.0" + dockerfile := "integration/dockerfiles/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})...) + out, err := RunCommandWithoutTest(dockerCmd) + if err != nil { + t.Errorf("Failed to build image %s with docker command \"%s\": %s %s", dockerImage, dockerCmd.Args, err, string(out)) + } + + // Build with kaniko + kanikoImage := GetKanikoImage(config.imageRepo, "Dockerfile_test_git") + kanikoCmd := exec.Command("docker", + append([]string{"run", + "-v", os.Getenv("HOME") + "/.config/gcloud:/root/.config/gcloud", + ExecutorImage, + "-f", dockerfile, + "-d", kanikoImage, + "-c", fmt.Sprintf("git://%s", repo)})...) + + out, err = RunCommandWithoutTest(kanikoCmd) + if err != nil { + t.Errorf("Failed to build image %s with kaniko command \"%s\": %v %s", dockerImage, kanikoCmd.Args, err, string(out)) + } + + // container-diff + daemonDockerImage := daemonPrefix + dockerImage + containerdiffCmd := exec.Command("container-diff", "diff", "--no-cache", + daemonDockerImage, kanikoImage, + "-q", "--type=file", "--type=metadata", "--json") + diff := RunCommand(containerdiffCmd, t) + t.Logf("diff = %s", string(diff)) + + expected := fmt.Sprintf(emptyContainerDiff, dockerImage, kanikoImage, dockerImage, kanikoImage) + checkContainerDiffOutput(t, diff, expected) +} + func TestLayers(t *testing.T) { offset := map[string]int{ "Dockerfile_test_add": 11, diff --git a/pkg/buildcontext/git.go b/pkg/buildcontext/git.go index 1aa8691be..7d61c6298 100644 --- a/pkg/buildcontext/git.go +++ b/pkg/buildcontext/git.go @@ -17,10 +17,13 @@ limitations under the License. package buildcontext import ( + "fmt" "os" + "strings" "github.com/GoogleContainerTools/kaniko/pkg/constants" git "gopkg.in/src-d/go-git.v4" + "gopkg.in/src-d/go-git.v4/plumbing" ) // Git unifies calls to download and unpack the build context. @@ -31,9 +34,18 @@ type Git struct { // UnpackTarFromBuildContext will provide the directory where Git Repository is Cloned func (g *Git) UnpackTarFromBuildContext() (string, error) { directory := constants.BuildContextDir + parts := strings.Split(g.context, "#") + url := "https://" + parts[0] + branch := "master" + if len(parts) > 1 { + branch = parts[1] + } + fmt.Println("will clone branch", branch) _, err := git.PlainClone(directory, false, &git.CloneOptions{ - URL: "https://" + g.context, - Progress: os.Stdout, + URL: url, + Progress: os.Stdout, + ReferenceName: plumbing.ReferenceName(branch), + SingleBranch: true, }) return directory, err } From c45e05f668f0077f266bf9e31657e2c344b4ad86 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sat, 22 Jun 2019 11:45:42 -0300 Subject: [PATCH 2/5] clean: remove debug msg Signed-off-by: Carlos Alexandro Becker --- pkg/buildcontext/git.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/pkg/buildcontext/git.go b/pkg/buildcontext/git.go index 7d61c6298..1909e8377 100644 --- a/pkg/buildcontext/git.go +++ b/pkg/buildcontext/git.go @@ -17,7 +17,6 @@ limitations under the License. package buildcontext import ( - "fmt" "os" "strings" @@ -40,7 +39,6 @@ func (g *Git) UnpackTarFromBuildContext() (string, error) { if len(parts) > 1 { branch = parts[1] } - fmt.Println("will clone branch", branch) _, err := git.PlainClone(directory, false, &git.CloneOptions{ URL: url, Progress: os.Stdout, From f578b09846bd5f26da89037c36c427bd2267caab Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sat, 22 Jun 2019 12:17:46 -0300 Subject: [PATCH 3/5] fix: remove single branch option Signed-off-by: Carlos Alexandro Becker --- pkg/buildcontext/git.go | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/pkg/buildcontext/git.go b/pkg/buildcontext/git.go index 1909e8377..9908350b1 100644 --- a/pkg/buildcontext/git.go +++ b/pkg/buildcontext/git.go @@ -34,16 +34,13 @@ type Git struct { func (g *Git) UnpackTarFromBuildContext() (string, error) { directory := constants.BuildContextDir parts := strings.Split(g.context, "#") - url := "https://" + parts[0] - branch := "master" - if len(parts) > 1 { - branch = parts[1] + options := git.CloneOptions{ + URL: "https://" + parts[0], + Progress: os.Stdout, } - _, err := git.PlainClone(directory, false, &git.CloneOptions{ - URL: url, - Progress: os.Stdout, - ReferenceName: plumbing.ReferenceName(branch), - SingleBranch: true, - }) + if len(parts) > 1 { + options.ReferenceName = plumbing.ReferenceName(parts[1]) + } + _, err := git.PlainClone(directory, false, &options) return directory, err } From 46a738f2b2661675588f804a55e051014cbd4455 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sat, 22 Jun 2019 12:18:42 -0300 Subject: [PATCH 4/5] fix: integration tests 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 b1bb4fd0d..fcd6246f9 100644 --- a/integration/integration_test.go +++ b/integration/integration_test.go @@ -279,7 +279,7 @@ func TestGitBuildcontext(t *testing.T) { } func TestGitBuildContextWithBranch(t *testing.T) { - repo := "github.com/GoogleContainerTools/kaniko#v0.10.0" + repo := "github.com/GoogleContainerTools/kaniko#refs/tags/v0.10.0" dockerfile := "integration/dockerfiles/Dockerfile_test_run_2" // Build with docker