Skip to content

Commit

Permalink
use revamped source-to-image git url parser
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim Minter authored and deads2k committed Jul 18, 2017
1 parent 247b6de commit 9a95fd6
Show file tree
Hide file tree
Showing 26 changed files with 134 additions and 269 deletions.
6 changes: 3 additions & 3 deletions pkg/bootstrap/docker/dockerhelper/filetransfer.go
Expand Up @@ -11,7 +11,7 @@ import (

"github.com/docker/engine-api/types"
s2itar "github.com/openshift/source-to-image/pkg/tar"
s2iutil "github.com/openshift/source-to-image/pkg/util"
s2ifs "github.com/openshift/source-to-image/pkg/util/fs"
)

// removeLeadingDirectoryAdapter wraps a tar.Reader and strips the first leading
Expand Down Expand Up @@ -106,15 +106,15 @@ func DownloadDirFromContainer(client Interface, container, src, dst string) erro
defer response.Close()
tarReader := &removeLeadingDirectoryAdapter{Reader: tar.NewReader(response)}

t := s2itar.New(s2iutil.NewFileSystem())
t := s2itar.New(s2ifs.NewFileSystem())
return t.ExtractTarStreamFromTarReader(dst, tarReader, nil)
}

// UploadFileToContainer uploads a file to a remote container.
func UploadFileToContainer(client Interface, container, src, dest string) error {
uploader, errch := newContainerUploader(client, container, path.Dir(dest))

t := s2itar.New(s2iutil.NewFileSystem())
t := s2itar.New(s2ifs.NewFileSystem())
tarWriter := s2itar.RenameAdapter{Writer: tar.NewWriter(uploader), Old: filepath.Base(src), New: path.Base(dest)}

err := t.CreateTarStreamToTarWriter(src, true, tarWriter, nil)
Expand Down
3 changes: 2 additions & 1 deletion pkg/build/builder/cmd/builder.go
Expand Up @@ -18,6 +18,7 @@ import (
kapi "k8s.io/kubernetes/pkg/api"

s2iapi "github.com/openshift/source-to-image/pkg/api"
s2igit "github.com/openshift/source-to-image/pkg/scm/git"

buildapi "github.com/openshift/origin/pkg/build/apis/build"
"github.com/openshift/origin/pkg/build/apis/build/validation"
Expand Down Expand Up @@ -123,7 +124,7 @@ func (c *builderConfig) setupGitEnvironment() (string, []string, error) {
if sourceSecret != nil {
// TODO: this should be refactored to let each source type manage which secrets
// it accepts
sourceURL, err := git.ParseRepository(gitSource.URI)
sourceURL, err := s2igit.Parse(gitSource.URI)
if err != nil {
return "", nil, fmt.Errorf("cannot parse build URL: %s", gitSource.URI)
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/build/builder/cmd/scmauth/cacert.go
Expand Up @@ -3,9 +3,9 @@ package scmauth
import (
"fmt"
"io/ioutil"
"net/url"
"path/filepath"
"strings"

s2igit "github.com/openshift/source-to-image/pkg/scm/git"
)

const (
Expand All @@ -18,12 +18,12 @@ const (

// CACert implements SCMAuth interface for using a custom certificate authority
type CACert struct {
SourceURL url.URL
SourceURL s2igit.URL
}

// Setup creates a .gitconfig fragment that points to the given ca.crt
func (s CACert) Setup(baseDir string, context SCMAuthContext) error {
if strings.ToLower(s.SourceURL.Scheme) != "https" {
if !(s.SourceURL.Type == s2igit.URLTypeURL && s.SourceURL.URL.Scheme == "https" && s.SourceURL.URL.Opaque == "") {
return nil
}
gitconfig, err := ioutil.TempFile("", "ca.crt.")
Expand Down
7 changes: 4 additions & 3 deletions pkg/build/builder/cmd/scmauth/cacert_test.go
@@ -1,9 +1,10 @@
package scmauth

import (
"net/url"
"os"
"testing"

"github.com/openshift/source-to-image/pkg/scm/git"
)

func TestCACertHandles(t *testing.T) {
Expand All @@ -19,7 +20,7 @@ func TestCACertHandles(t *testing.T) {
func TestCACertSetup(t *testing.T) {
context := NewDefaultSCMContext()
caCert := &CACert{
SourceURL: url.URL{Scheme: "https", Host: "my.host", Path: "git/repo"},
SourceURL: *git.MustParse("https://my.host/git/repo"),
}
secretDir := secretDir(t, "ca.crt")
defer os.RemoveAll(secretDir)
Expand All @@ -36,7 +37,7 @@ func TestCACertSetup(t *testing.T) {
func TestCACertSetupNoSSL(t *testing.T) {
context := NewDefaultSCMContext()
caCert := &CACert{
SourceURL: url.URL{Scheme: "http", Host: "my.host", Path: "git/repo"},
SourceURL: *git.MustParse("http://my.host/git/repo"),
}
secretDir := secretDir(t, "ca.crt")
defer os.RemoveAll(secretDir)
Expand Down
11 changes: 7 additions & 4 deletions pkg/build/builder/cmd/scmauth/password.go
Expand Up @@ -6,9 +6,10 @@ import (
"net/url"
"os"
"path/filepath"
"strings"

"github.com/openshift/origin/pkg/util/file"

s2igit "github.com/openshift/source-to-image/pkg/scm/git"
)

const (
Expand All @@ -25,14 +26,16 @@ const (

// UsernamePassword implements SCMAuth interface for using Username and Password credentials
type UsernamePassword struct {
SourceURL url.URL
SourceURL s2igit.URL
}

// Setup creates a gitconfig fragment that includes a substitution URL with the username/password
// included in the URL. Returns source URL stripped of username/password credentials.
func (u UsernamePassword) Setup(baseDir string, context SCMAuthContext) error {
// Only apply to https and http URLs
if scheme := strings.ToLower(u.SourceURL.Scheme); scheme != "http" && scheme != "https" {
if !(u.SourceURL.Type == s2igit.URLTypeURL &&
(u.SourceURL.URL.Scheme == "http" || u.SourceURL.URL.Scheme == "https") &&
u.SourceURL.URL.Opaque == "") {
return nil
}

Expand All @@ -51,7 +54,7 @@ func (u UsernamePassword) Setup(baseDir string, context SCMAuthContext) error {
}

// Determine overrides
overrideSourceURL, gitconfigURL, err := doSetup(u.SourceURL, usernameSecret, passwordSecret, tokenSecret)
overrideSourceURL, gitconfigURL, err := doSetup(u.SourceURL.URL, usernameSecret, passwordSecret, tokenSecret)
if err != nil {
return err
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/build/builder/cmd/scmauth/scmauths.go
Expand Up @@ -7,11 +7,13 @@ import (
"os"

"github.com/golang/glog"

s2igit "github.com/openshift/source-to-image/pkg/scm/git"
)

type SCMAuths []SCMAuth

func GitAuths(sourceURL *url.URL) SCMAuths {
func GitAuths(sourceURL *s2igit.URL) SCMAuths {
auths := SCMAuths{
&SSHPrivateKey{},
&UsernamePassword{SourceURL: *sourceURL},
Expand Down
4 changes: 2 additions & 2 deletions pkg/build/builder/docker.go
Expand Up @@ -19,7 +19,7 @@ import (
s2iapi "github.com/openshift/source-to-image/pkg/api"
"github.com/openshift/source-to-image/pkg/tar"
"github.com/openshift/source-to-image/pkg/util"
s2iutil "github.com/openshift/source-to-image/pkg/util"
s2ifs "github.com/openshift/source-to-image/pkg/util/fs"

buildapi "github.com/openshift/origin/pkg/build/apis/build"
"github.com/openshift/origin/pkg/build/builder/cmd/dockercfg"
Expand Down Expand Up @@ -50,7 +50,7 @@ func NewDockerBuilder(dockerClient DockerClient, buildsClient client.BuildInterf
dockerClient: dockerClient,
build: build,
gitClient: gitClient,
tar: tar.New(s2iutil.NewFileSystem()),
tar: tar.New(s2ifs.NewFileSystem()),
client: buildsClient,
cgLimits: cgLimits,
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/build/builder/docker_test.go
Expand Up @@ -14,7 +14,7 @@ import (
kapi "k8s.io/kubernetes/pkg/api"

"github.com/openshift/source-to-image/pkg/tar"
s2iutil "github.com/openshift/source-to-image/pkg/util"
s2ifs "github.com/openshift/source-to-image/pkg/util/fs"

buildapi "github.com/openshift/origin/pkg/build/apis/build"
"github.com/openshift/origin/pkg/build/util/dockerfile"
Expand Down Expand Up @@ -261,7 +261,7 @@ func TestDockerfilePath(t *testing.T) {
dockerClient: dockerClient,
build: build,
gitClient: git.NewRepository(),
tar: tar.New(s2iutil.NewFileSystem()),
tar: tar.New(s2ifs.NewFileSystem()),
}

// this will validate that the Dockerfile is readable
Expand Down Expand Up @@ -385,7 +385,7 @@ USER 1001`
build: build,
dockerClient: dockerClient,
gitClient: git.NewRepository(),
tar: tar.New(s2iutil.NewFileSystem()),
tar: tar.New(s2ifs.NewFileSystem()),
}

if err := dockerBuilder.Build(); err != nil {
Expand Down
9 changes: 3 additions & 6 deletions pkg/build/builder/source.go
Expand Up @@ -14,7 +14,7 @@ import (
docker "github.com/fsouza/go-dockerclient"

s2igit "github.com/openshift/source-to-image/pkg/scm/git"
s2iutil "github.com/openshift/source-to-image/pkg/util"
s2ifs "github.com/openshift/source-to-image/pkg/util/fs"

buildapi "github.com/openshift/origin/pkg/build/apis/build"
"github.com/openshift/origin/pkg/build/builder/cmd/dockercfg"
Expand Down Expand Up @@ -167,13 +167,10 @@ func checkRemoteGit(gitClient GitClient, url string, initialTimeout time.Duratio
// checkSourceURI performs a check on the URI associated with the build
// to make sure that it is valid.
func checkSourceURI(gitClient GitClient, rawurl string, timeout time.Duration) error {
ok, err := s2igit.New(s2iutil.NewFileSystem()).ValidCloneSpec(rawurl)
_, err := s2igit.Parse(rawurl)
if err != nil {
return fmt.Errorf("Invalid git source url %q: %v", rawurl, err)
}
if !ok {
return fmt.Errorf("Invalid git source url: %s", rawurl)
}
return checkRemoteGit(gitClient, rawurl, timeout)
}

Expand Down Expand Up @@ -391,7 +388,7 @@ func extractSourceFromImage(ctx context.Context, dockerClient DockerClient, imag
}
defer dockerClient.RemoveContainer(docker.RemoveContainerOptions{ID: container.ID})

tarHelper := tar.New(s2iutil.NewFileSystem())
tarHelper := tar.New(s2ifs.NewFileSystem())
tarHelper.SetExclusionPattern(nil)

for _, path := range paths {
Expand Down
11 changes: 6 additions & 5 deletions pkg/build/builder/sti.go
Expand Up @@ -18,6 +18,7 @@ import (
s2ibuild "github.com/openshift/source-to-image/pkg/build"
s2i "github.com/openshift/source-to-image/pkg/build/strategies"
"github.com/openshift/source-to-image/pkg/docker"
s2igit "github.com/openshift/source-to-image/pkg/scm/git"

buildapi "github.com/openshift/origin/pkg/build/apis/build"
"github.com/openshift/origin/pkg/build/builder/cmd/dockercfg"
Expand Down Expand Up @@ -169,7 +170,7 @@ func (s *S2IBuilder) Build() error {
}
}

var s2iSourceInfo *s2iapi.SourceInfo
var s2iSourceInfo *s2igit.SourceInfo
if sourceInfo != nil {
s2iSourceInfo = &sourceInfo.SourceInfo
revision := updateBuildRevision(s.build, sourceInfo)
Expand Down Expand Up @@ -220,7 +221,7 @@ func (s *S2IBuilder) Build() error {
Labels: buildLabels(s.build),
DockerNetworkMode: getDockerNetworkMode(),

Source: srcDir,
Source: &s2igit.URL{URL: url.URL{Path: srcDir}, Type: s2igit.URLTypeLocal},
ContextDir: contextDir,
SourceInfo: s2iSourceInfo,
ForceCopy: true,
Expand Down Expand Up @@ -382,13 +383,13 @@ func (s *S2IBuilder) Build() error {
}

type downloader struct {
sourceInfo *s2iapi.SourceInfo
sourceInfo *s2igit.SourceInfo
}

// Download no-ops (because we already downloaded the source to the right location)
// and returns the previously computed sourceInfo for the source.
func (d *downloader) Download(config *s2iapi.Config) (*s2iapi.SourceInfo, error) {
config.WorkingSourceDir = config.Source
func (d *downloader) Download(config *s2iapi.Config) (*s2igit.SourceInfo, error) {
config.WorkingSourceDir = config.Source.LocalPath()

return d.sourceInfo, nil
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/cli/cmd/rsync/copytar.go
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/spf13/cobra"
kerrors "k8s.io/apimachinery/pkg/util/errors"

s2iutil "github.com/openshift/source-to-image/pkg/util"
s2ifs "github.com/openshift/source-to-image/pkg/util/fs"

"github.com/openshift/origin/pkg/cmd/util/clientcmd"
)
Expand All @@ -37,7 +37,7 @@ type tarStrategy struct {

func newTarStrategy(f *clientcmd.Factory, c *cobra.Command, o *RsyncOptions) (copyStrategy, error) {

tarHelper := tar.New(s2iutil.NewFileSystem())
tarHelper := tar.New(s2ifs.NewFileSystem())
tarHelper.SetExclusionPattern(nil)

ignoredFlags := rsyncSpecificFlags(o)
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/cli/cmd/startbuild.go
Expand Up @@ -19,7 +19,7 @@ import (
"github.com/spf13/cobra"

"github.com/openshift/source-to-image/pkg/tar"
s2iutil "github.com/openshift/source-to-image/pkg/util"
s2ifs "github.com/openshift/source-to-image/pkg/util/fs"

kerrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
Expand Down Expand Up @@ -599,7 +599,7 @@ func streamPathToBuild(repo git.Repository, in io.Reader, out io.Writer, client
pr, pw := io.Pipe()
go func() {
w := gzip.NewWriter(pw)
if err := tar.New(s2iutil.NewFileSystem()).CreateTarStream(path, false, w); err != nil {
if err := tar.New(s2ifs.NewFileSystem()).CreateTarStream(path, false, w); err != nil {
pw.CloseWithError(err)
} else {
w.Close()
Expand Down
4 changes: 2 additions & 2 deletions pkg/diagnostics/network/results.go
Expand Up @@ -11,7 +11,7 @@ import (
"strconv"

"github.com/openshift/source-to-image/pkg/tar"
s2iutil "github.com/openshift/source-to-image/pkg/util"
s2ifs "github.com/openshift/source-to-image/pkg/util/fs"

kerrs "k8s.io/apimachinery/pkg/util/errors"
kapi "k8s.io/kubernetes/pkg/api"
Expand Down Expand Up @@ -93,7 +93,7 @@ func (d *NetworkDiagnostic) copyNetworkPodInfo(pod *kapi.Pod) error {
}
defer tmp.Close()

tarHelper := tar.New(s2iutil.NewFileSystem())
tarHelper := tar.New(s2ifs.NewFileSystem())
tarHelper.SetExclusionPattern(nil)
logdir := filepath.Join(d.LogDir, util.NetworkDiagNodeLogDirPrefix, pod.Spec.NodeName)
err = tarHelper.ExtractTarStream(logdir, tmp)
Expand Down

0 comments on commit 9a95fd6

Please sign in to comment.