From 34a6ec250ff898280658245821aa056b4b9aa872 Mon Sep 17 00:00:00 2001 From: yw-liu <43599588+yw-liu@users.noreply.github.com> Date: Tue, 14 Apr 2020 20:25:20 +0800 Subject: [PATCH 1/8] add http support for git pull usage: set the GIT_PULL_METHOD env var to http or https for starting the container --- pkg/buildcontext/git.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/pkg/buildcontext/git.go b/pkg/buildcontext/git.go index 9908350b17..fe4eae1acb 100644 --- a/pkg/buildcontext/git.go +++ b/pkg/buildcontext/git.go @@ -25,6 +25,14 @@ import ( "gopkg.in/src-d/go-git.v4/plumbing" ) +const ( + gitPullMethodEnvKey = "GIT_PULL_METHOD" +) + +var ( + supportedGitPullMethods = map[string]bool{"https":true, "http":true} +) + // Git unifies calls to download and unpack the build context. type Git struct { context string @@ -35,7 +43,7 @@ func (g *Git) UnpackTarFromBuildContext() (string, error) { directory := constants.BuildContextDir parts := strings.Split(g.context, "#") options := git.CloneOptions{ - URL: "https://" + parts[0], + URL: getGitPullMethod() + "://" + parts[0], Progress: os.Stdout, } if len(parts) > 1 { @@ -44,3 +52,11 @@ func (g *Git) UnpackTarFromBuildContext() (string, error) { _, err := git.PlainClone(directory, false, &options) return directory, err } + +func getGitPullMethod() string { + gitPullMethod := os.Getenv(gitPullMethodEnvKey) + if ok := supportedGitPullMethods[gitPullMethod]; !ok { + gitPullMethod = "https" + } + return gitPullMethod +} From 4f8d074e00aac7105cc8d8d9cf3b1c7a5d6edbad Mon Sep 17 00:00:00 2001 From: yw-liu <43599588+yw-liu@users.noreply.github.com> Date: Thu, 16 Apr 2020 22:21:09 +0800 Subject: [PATCH 2/8] add unit-test --- pkg/buildcontext/git_test.go | 42 ++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 pkg/buildcontext/git_test.go diff --git a/pkg/buildcontext/git_test.go b/pkg/buildcontext/git_test.go new file mode 100644 index 0000000000..c56159b95c --- /dev/null +++ b/pkg/buildcontext/git_test.go @@ -0,0 +1,42 @@ +package buildcontext + +import ( + "github.com/GoogleContainerTools/kaniko/testutil" + "os" + "testing" +) + +func TestGetGitPullMethod(t *testing.T) { + tests := []struct { + setEnv func() + expectedValue string + }{ + { + setEnv: func() {}, + expectedValue: "https", + }, + { + setEnv: func() { + _ = os.Setenv(gitPullMethodEnvKey, "http") + }, + expectedValue: "http", + }, + { + setEnv: func() { + _ = os.Setenv(gitPullMethodEnvKey, "https") + }, + expectedValue: "https", + }, + { + setEnv: func() { + _ = os.Setenv(gitPullMethodEnvKey, "unknown") + }, + expectedValue: "https", + }, + } + + for _, tt := range tests { + tt.setEnv() + testutil.CheckDeepEqual(t, getGitPullMethod(), tt.expectedValue) + } +} From d8b8e811ddbb0bbc28bf88b379f37a30cd64d9b1 Mon Sep 17 00:00:00 2001 From: yw-liu <43599588+yw-liu@users.noreply.github.com> Date: Thu, 16 Apr 2020 23:44:37 +0800 Subject: [PATCH 3/8] modify code format and unit-test --- pkg/buildcontext/git.go | 2 +- pkg/buildcontext/git_test.go | 52 ++++++++++++++++++++++++++---------- 2 files changed, 39 insertions(+), 15 deletions(-) diff --git a/pkg/buildcontext/git.go b/pkg/buildcontext/git.go index fe4eae1acb..11daea4c83 100644 --- a/pkg/buildcontext/git.go +++ b/pkg/buildcontext/git.go @@ -30,7 +30,7 @@ const ( ) var ( - supportedGitPullMethods = map[string]bool{"https":true, "http":true} + supportedGitPullMethods = map[string]bool{"https": true, "http": true} ) // Git unifies calls to download and unpack the build context. diff --git a/pkg/buildcontext/git_test.go b/pkg/buildcontext/git_test.go index c56159b95c..c9ba88b21f 100644 --- a/pkg/buildcontext/git_test.go +++ b/pkg/buildcontext/git_test.go @@ -1,42 +1,66 @@ package buildcontext import ( - "github.com/GoogleContainerTools/kaniko/testutil" "os" "testing" + + "github.com/GoogleContainerTools/kaniko/testutil" ) func TestGetGitPullMethod(t *testing.T) { tests := []struct { - setEnv func() - expectedValue string + testName string + setEnv func() (expectedValue string) }{ { - setEnv: func() {}, - expectedValue: "https", + testName: "noEnv", + setEnv: func() (expectedValue string) { + expectedValue = "https" + return + }, + }, + { + testName: "emptyEnv", + setEnv: func() (expectedValue string) { + _ = os.Setenv(gitPullMethodEnvKey, "") + expectedValue = "https" + return + }, }, { - setEnv: func() { - _ = os.Setenv(gitPullMethodEnvKey, "http") + testName: "httpEnv", + setEnv: func() (expectedValue string) { + err := os.Setenv(gitPullMethodEnvKey, "http") + if nil != err { + expectedValue = "https" + } else { + expectedValue = "http" + } + return }, - expectedValue: "http", }, { - setEnv: func() { + testName: "httpsEnv", + setEnv: func() (expectedValue string) { _ = os.Setenv(gitPullMethodEnvKey, "https") + expectedValue = "https" + return }, - expectedValue: "https", }, { - setEnv: func() { + testName: "unknownEnv", + setEnv: func() (expectedValue string) { _ = os.Setenv(gitPullMethodEnvKey, "unknown") + expectedValue = "https" + return }, - expectedValue: "https", }, } for _, tt := range tests { - tt.setEnv() - testutil.CheckDeepEqual(t, getGitPullMethod(), tt.expectedValue) + t.Run(tt.testName, func(t *testing.T) { + expectedValue := tt.setEnv() + testutil.CheckDeepEqual(t, getGitPullMethod(), expectedValue) + }) } } From 7912e4c87bce4446992ba3377dae7264aad63c85 Mon Sep 17 00:00:00 2001 From: yw-liu <43599588+yw-liu@users.noreply.github.com> Date: Thu, 16 Apr 2020 23:48:43 +0800 Subject: [PATCH 4/8] modify unit-test --- pkg/buildcontext/git_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/buildcontext/git_test.go b/pkg/buildcontext/git_test.go index c9ba88b21f..8505897f06 100644 --- a/pkg/buildcontext/git_test.go +++ b/pkg/buildcontext/git_test.go @@ -60,7 +60,7 @@ func TestGetGitPullMethod(t *testing.T) { for _, tt := range tests { t.Run(tt.testName, func(t *testing.T) { expectedValue := tt.setEnv() - testutil.CheckDeepEqual(t, getGitPullMethod(), expectedValue) + testutil.CheckDeepEqual(t, expectedValue, getGitPullMethod()) }) } } From 0fc311a8b73594fb7dac0e61235b64337a612779 Mon Sep 17 00:00:00 2001 From: yw-liu <43599588+yw-liu@users.noreply.github.com> Date: Fri, 17 Apr 2020 00:00:24 +0800 Subject: [PATCH 5/8] make string var as constant --- pkg/buildcontext/git.go | 4 +++- pkg/buildcontext/git_test.go | 16 ++++++++-------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/pkg/buildcontext/git.go b/pkg/buildcontext/git.go index 11daea4c83..291f834f68 100644 --- a/pkg/buildcontext/git.go +++ b/pkg/buildcontext/git.go @@ -27,10 +27,12 @@ import ( const ( gitPullMethodEnvKey = "GIT_PULL_METHOD" + gitPullMethodHttps = "https" + gitPullMethodHttp = "http" ) var ( - supportedGitPullMethods = map[string]bool{"https": true, "http": true} + supportedGitPullMethods = map[string]bool{gitPullMethodHttps: true, gitPullMethodHttp: true} ) // Git unifies calls to download and unpack the build context. diff --git a/pkg/buildcontext/git_test.go b/pkg/buildcontext/git_test.go index 8505897f06..6f293a6ef5 100644 --- a/pkg/buildcontext/git_test.go +++ b/pkg/buildcontext/git_test.go @@ -15,7 +15,7 @@ func TestGetGitPullMethod(t *testing.T) { { testName: "noEnv", setEnv: func() (expectedValue string) { - expectedValue = "https" + expectedValue = gitPullMethodHttps return }, }, @@ -23,18 +23,18 @@ func TestGetGitPullMethod(t *testing.T) { testName: "emptyEnv", setEnv: func() (expectedValue string) { _ = os.Setenv(gitPullMethodEnvKey, "") - expectedValue = "https" + expectedValue = gitPullMethodHttps return }, }, { testName: "httpEnv", setEnv: func() (expectedValue string) { - err := os.Setenv(gitPullMethodEnvKey, "http") + err := os.Setenv(gitPullMethodEnvKey, gitPullMethodHttp) if nil != err { - expectedValue = "https" + expectedValue = gitPullMethodHttps } else { - expectedValue = "http" + expectedValue = gitPullMethodHttp } return }, @@ -42,8 +42,8 @@ func TestGetGitPullMethod(t *testing.T) { { testName: "httpsEnv", setEnv: func() (expectedValue string) { - _ = os.Setenv(gitPullMethodEnvKey, "https") - expectedValue = "https" + _ = os.Setenv(gitPullMethodEnvKey, gitPullMethodHttps) + expectedValue = gitPullMethodHttps return }, }, @@ -51,7 +51,7 @@ func TestGetGitPullMethod(t *testing.T) { testName: "unknownEnv", setEnv: func() (expectedValue string) { _ = os.Setenv(gitPullMethodEnvKey, "unknown") - expectedValue = "https" + expectedValue = gitPullMethodHttps return }, }, From c9fc6b5bcfa9db57ed6656cb01030425cde05c19 Mon Sep 17 00:00:00 2001 From: yw-liu <43599588+yw-liu@users.noreply.github.com> Date: Fri, 17 Apr 2020 00:21:48 +0800 Subject: [PATCH 6/8] fix golint problem --- pkg/buildcontext/git.go | 6 +++--- pkg/buildcontext/git_test.go | 16 ++++++++-------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/pkg/buildcontext/git.go b/pkg/buildcontext/git.go index 291f834f68..931a07089a 100644 --- a/pkg/buildcontext/git.go +++ b/pkg/buildcontext/git.go @@ -27,12 +27,12 @@ import ( const ( gitPullMethodEnvKey = "GIT_PULL_METHOD" - gitPullMethodHttps = "https" - gitPullMethodHttp = "http" + gitPullMethodHTTPS = "https" + gitPullMethodHTTP = "http" ) var ( - supportedGitPullMethods = map[string]bool{gitPullMethodHttps: true, gitPullMethodHttp: true} + supportedGitPullMethods = map[string]bool{gitPullMethodHTTPS: true, gitPullMethodHTTP: true} ) // Git unifies calls to download and unpack the build context. diff --git a/pkg/buildcontext/git_test.go b/pkg/buildcontext/git_test.go index 6f293a6ef5..e165bd508c 100644 --- a/pkg/buildcontext/git_test.go +++ b/pkg/buildcontext/git_test.go @@ -15,7 +15,7 @@ func TestGetGitPullMethod(t *testing.T) { { testName: "noEnv", setEnv: func() (expectedValue string) { - expectedValue = gitPullMethodHttps + expectedValue = gitPullMethodHTTPS return }, }, @@ -23,18 +23,18 @@ func TestGetGitPullMethod(t *testing.T) { testName: "emptyEnv", setEnv: func() (expectedValue string) { _ = os.Setenv(gitPullMethodEnvKey, "") - expectedValue = gitPullMethodHttps + expectedValue = gitPullMethodHTTPS return }, }, { testName: "httpEnv", setEnv: func() (expectedValue string) { - err := os.Setenv(gitPullMethodEnvKey, gitPullMethodHttp) + err := os.Setenv(gitPullMethodEnvKey, gitPullMethodHTTP) if nil != err { - expectedValue = gitPullMethodHttps + expectedValue = gitPullMethodHTTPS } else { - expectedValue = gitPullMethodHttp + expectedValue = gitPullMethodHTTP } return }, @@ -42,8 +42,8 @@ func TestGetGitPullMethod(t *testing.T) { { testName: "httpsEnv", setEnv: func() (expectedValue string) { - _ = os.Setenv(gitPullMethodEnvKey, gitPullMethodHttps) - expectedValue = gitPullMethodHttps + _ = os.Setenv(gitPullMethodEnvKey, gitPullMethodHTTPS) + expectedValue = gitPullMethodHTTPS return }, }, @@ -51,7 +51,7 @@ func TestGetGitPullMethod(t *testing.T) { testName: "unknownEnv", setEnv: func() (expectedValue string) { _ = os.Setenv(gitPullMethodEnvKey, "unknown") - expectedValue = gitPullMethodHttps + expectedValue = gitPullMethodHTTPS return }, }, From 2e901732384ea2142c1bf79094bdad54e7727437 Mon Sep 17 00:00:00 2001 From: yw-liu <43599588+yw-liu@users.noreply.github.com> Date: Fri, 17 Apr 2020 00:37:12 +0800 Subject: [PATCH 7/8] fix boilerplate --- pkg/buildcontext/git_test.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/pkg/buildcontext/git_test.go b/pkg/buildcontext/git_test.go index e165bd508c..ccc53fcb91 100644 --- a/pkg/buildcontext/git_test.go +++ b/pkg/buildcontext/git_test.go @@ -1,3 +1,19 @@ +/* +Copyright 2020 Google LLC + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + package buildcontext import ( From 73eb47c7520f8a1cd2f46ea46f6ac4615a6c3e39 Mon Sep 17 00:00:00 2001 From: yw-liu <43599588+yw-liu@users.noreply.github.com> Date: Fri, 17 Apr 2020 09:07:38 +0800 Subject: [PATCH 8/8] replace string literal with constant --- pkg/buildcontext/git.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/buildcontext/git.go b/pkg/buildcontext/git.go index 931a07089a..7c2dddc6db 100644 --- a/pkg/buildcontext/git.go +++ b/pkg/buildcontext/git.go @@ -58,7 +58,7 @@ func (g *Git) UnpackTarFromBuildContext() (string, error) { func getGitPullMethod() string { gitPullMethod := os.Getenv(gitPullMethodEnvKey) if ok := supportedGitPullMethods[gitPullMethod]; !ok { - gitPullMethod = "https" + gitPullMethod = gitPullMethodHTTPS } return gitPullMethod }