Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add http support for git repository context #1196

Merged
merged 8 commits into from
May 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
gitPullMethod = gitPullMethodHTTPS
return gitPullMethodHTTPS

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @tejal29 for your suggestion, you may commit that.

}
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())
})
}
}