Skip to content

Commit

Permalink
Merge pull request #1196 from yw-liu/master
Browse files Browse the repository at this point in the history
Add http support for git repository context
  • Loading branch information
tejal29 committed May 4, 2020
2 parents ae11db4 + 73eb47c commit fe0500f
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 1 deletion.
20 changes: 19 additions & 1 deletion pkg/buildcontext/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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
}
82 changes: 82 additions & 0 deletions pkg/buildcontext/git_test.go
Original file line number Diff line number Diff line change
@@ -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())
})
}
}

0 comments on commit fe0500f

Please sign in to comment.