diff --git a/pkg/buildcontext/git.go b/pkg/buildcontext/git.go index 9908350b17..7c2dddc6db 100644 --- a/pkg/buildcontext/git.go +++ b/pkg/buildcontext/git.go @@ -25,6 +25,16 @@ import ( "gopkg.in/src-d/go-git.v4/plumbing" ) +const ( + gitPullMethodEnvKey = "GIT_PULL_METHOD" + gitPullMethodHTTPS = "https" + gitPullMethodHTTP = "http" +) + +var ( + supportedGitPullMethods = map[string]bool{gitPullMethodHTTPS: true, gitPullMethodHTTP: true} +) + // Git unifies calls to download and unpack the build context. type Git struct { context string @@ -35,7 +45,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 +54,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 = gitPullMethodHTTPS + } + return gitPullMethod +} diff --git a/pkg/buildcontext/git_test.go b/pkg/buildcontext/git_test.go new file mode 100644 index 0000000000..ccc53fcb91 --- /dev/null +++ b/pkg/buildcontext/git_test.go @@ -0,0 +1,82 @@ +/* +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 ( + "os" + "testing" + + "github.com/GoogleContainerTools/kaniko/testutil" +) + +func TestGetGitPullMethod(t *testing.T) { + tests := []struct { + testName string + setEnv func() (expectedValue string) + }{ + { + testName: "noEnv", + setEnv: func() (expectedValue string) { + expectedValue = gitPullMethodHTTPS + return + }, + }, + { + testName: "emptyEnv", + setEnv: func() (expectedValue string) { + _ = os.Setenv(gitPullMethodEnvKey, "") + expectedValue = gitPullMethodHTTPS + return + }, + }, + { + testName: "httpEnv", + setEnv: func() (expectedValue string) { + err := os.Setenv(gitPullMethodEnvKey, gitPullMethodHTTP) + if nil != err { + expectedValue = gitPullMethodHTTPS + } else { + expectedValue = gitPullMethodHTTP + } + return + }, + }, + { + testName: "httpsEnv", + setEnv: func() (expectedValue string) { + _ = os.Setenv(gitPullMethodEnvKey, gitPullMethodHTTPS) + expectedValue = gitPullMethodHTTPS + return + }, + }, + { + testName: "unknownEnv", + setEnv: func() (expectedValue string) { + _ = os.Setenv(gitPullMethodEnvKey, "unknown") + expectedValue = gitPullMethodHTTPS + return + }, + }, + } + + for _, tt := range tests { + t.Run(tt.testName, func(t *testing.T) { + expectedValue := tt.setEnv() + testutil.CheckDeepEqual(t, expectedValue, getGitPullMethod()) + }) + } +}