From 683db1230d6a3442eb12a371eaacfe65c1c6b957 Mon Sep 17 00:00:00 2001 From: Abhishek Date: Thu, 15 Jun 2017 11:30:09 +0530 Subject: [PATCH] move git and related functions from openshift. go into a separate file --- pkg/transformer/openshift/openshift.go | 92 ++------------------- pkg/transformer/openshift/openshift_test.go | 8 +- pkg/transformer/openshift/utils.go | 77 +++++++++++++++++ 3 files changed, 90 insertions(+), 87 deletions(-) create mode 100644 pkg/transformer/openshift/utils.go diff --git a/pkg/transformer/openshift/openshift.go b/pkg/transformer/openshift/openshift.go index 1003bfd4..63be2732 100644 --- a/pkg/transformer/openshift/openshift.go +++ b/pkg/transformer/openshift/openshift.go @@ -19,8 +19,6 @@ package openshift import ( "fmt" "os" - "os/exec" - "strings" "github.com/kubernetes/kompose/pkg/kobject" "github.com/kubernetes/kompose/pkg/transformer/kubernetes" @@ -39,6 +37,8 @@ import ( "reflect" + "sort" + "github.com/kubernetes/kompose/pkg/transformer" buildapi "github.com/openshift/origin/pkg/build/api" buildconfigreaper "github.com/openshift/origin/pkg/build/cmd" @@ -51,7 +51,6 @@ import ( "k8s.io/kubernetes/pkg/kubectl" "k8s.io/kubernetes/pkg/labels" "k8s.io/kubernetes/pkg/util/intstr" - "sort" ) // OpenShift implements Transformer interface and represents OpenShift transformer @@ -73,84 +72,11 @@ const TIMEOUT = 300 // by keeping record if already saw this key in another service var unsupportedKey = map[string]bool{} -// getImageTag get tag name from image name -// if no tag is specified return 'latest' -func getImageTag(image string) string { - // format: registry_host:registry_port/repo_name/image_name:image_tag - // example: - // 1) myregistryhost:5000/fedora/httpd:version1.0 - // 2) myregistryhost:5000/fedora/httpd - // 3) myregistryhost/fedora/httpd:version1.0 - // 4) myregistryhost/fedora/httpd - // 5) fedora/httpd - // 6) httpd - imageAndTag := image - - i := strings.Split(image, "/") - if len(i) >= 2 { - imageAndTag = i[len(i)-1] - } - - p := strings.Split(imageAndTag, ":") - if len(p) == 2 { - return p[1] - } - return "latest" - -} - -// hasGitBinary checks if the 'git' binary is available on the system -func hasGitBinary() bool { - _, err := exec.LookPath("git") - return err == nil -} - -// getGitCurrentRemoteURL gets current git remote URI for the current git repo -func getGitCurrentRemoteURL(composeFileDir string) (string, error) { - cmd := exec.Command("git", "ls-remote", "--get-url") - cmd.Dir = composeFileDir - out, err := cmd.Output() - if err != nil { - return "", err - } - url := strings.TrimRight(string(out), "\n") - - if !strings.HasSuffix(url, ".git") { - url += ".git" - } - - return url, nil -} - -// getGitCurrentBranch gets current git branch name for the current git repo -func getGitCurrentBranch(composeFileDir string) (string, error) { - cmd := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD") - cmd.Dir = composeFileDir - out, err := cmd.Output() - if err != nil { - return "", err - } - return strings.TrimRight(string(out), "\n"), nil -} - -// getAbsBuildContext returns build context relative to project root dir -func getAbsBuildContext(context string) (string, error) { - cmd := exec.Command("git", "rev-parse", "--show-prefix") - cmd.Dir = context - out, err := cmd.Output() - if err != nil { - return "", err - } - //convert output of command to string - contextDir := strings.Trim(string(out), "\n") - return contextDir, nil -} - // initImageStream initialize ImageStream object func (o *OpenShift) initImageStream(name string, service kobject.ServiceConfig, opt kobject.ConvertOptions) *imageapi.ImageStream { // Retrieve tags and image name for mapping - tag := getImageTag(service.Image) + tag := GetImageTag(service.Image) var importPolicy imageapi.TagImportPolicy if opt.InsecureRepository { @@ -188,7 +114,7 @@ func (o *OpenShift) initImageStream(name string, service kobject.ServiceConfig, } func initBuildConfig(name string, service kobject.ServiceConfig, repo string, branch string) (*buildapi.BuildConfig, error) { - contextDir, err := getAbsBuildContext(service.Build) + contextDir, err := GetAbsBuildContext(service.Build) envList := transformer.EnvSort{} for envName, envValue := range service.BuildArgs { if *envValue == "\x00" { @@ -236,7 +162,7 @@ func initBuildConfig(name string, service kobject.ServiceConfig, repo string, br Output: buildapi.BuildOutput{ To: &kapi.ObjectReference{ Kind: "ImageStreamTag", - Name: name + ":" + getImageTag(service.Image), + Name: name + ":" + GetImageTag(service.Image), }, }, }, @@ -250,7 +176,7 @@ func (o *OpenShift) initDeploymentConfig(name string, service kobject.ServiceCon containerName := []string{name} // Properly add tags to the image name - tag := getImageTag(service.Image) + tag := GetImageTag(service.Image) // Use ContainerName if it was set if service.ContainerName != "" { @@ -417,13 +343,13 @@ func (o *OpenShift) Transform(komposeObject kobject.KomposeObject, opt kobject.C } // Check for Git - if !hasGitBinary() && (buildRepo == "" || buildBranch == "") { + if !HasGitBinary() && (buildRepo == "" || buildBranch == "") { return nil, errors.New("Git is not installed! Please install Git to create buildconfig, else supply source repository and branch to use for build using '--build-repo', '--build-branch' options respectively") } // Check the Git branch if buildBranch == "" { - buildBranch, err = getGitCurrentBranch(composeFileDir) + buildBranch, err = GetGitCurrentBranch(composeFileDir) if err != nil { return nil, errors.Wrap(err, "Buildconfig cannot be created because current git branch couldn't be detected.") } @@ -434,7 +360,7 @@ func (o *OpenShift) Transform(komposeObject kobject.KomposeObject, opt kobject.C if err != nil { return nil, errors.Wrap(err, "Buildconfig cannot be created because remote for current git branch couldn't be detected.") } - buildRepo, err = getGitCurrentRemoteURL(composeFileDir) + buildRepo, err = GetGitCurrentRemoteURL(composeFileDir) if err != nil { return nil, errors.Wrap(err, "Buildconfig cannot be created because git remote origin repo couldn't be detected.") } diff --git a/pkg/transformer/openshift/openshift_test.go b/pkg/transformer/openshift/openshift_test.go index 1d32d0b1..302e42fe 100644 --- a/pkg/transformer/openshift/openshift_test.go +++ b/pkg/transformer/openshift/openshift_test.go @@ -150,7 +150,7 @@ func TestGetGitRemote(t *testing.T) { for name, test := range testCases { t.Log("Test case: ", name) - output, err = getGitCurrentRemoteURL(test.dir) + output, err = GetGitCurrentRemoteURL(test.dir) if test.expectError { if err == nil { @@ -190,7 +190,7 @@ func TestGitGetCurrentBranch(t *testing.T) { for name, test := range testCases { t.Log("Test case: ", name) - output, err = getGitCurrentBranch(test.dir) + output, err = GetGitCurrentBranch(test.dir) if test.expectError { if err == nil { @@ -263,7 +263,7 @@ func TestGetAbsBuildContext(t *testing.T) { for name, test := range testCases { t.Log("Test case: ", name) - output, err = getAbsBuildContext(test.context) + output, err = GetAbsBuildContext(test.context) if test.expectError { if err == nil { @@ -331,7 +331,7 @@ func TestInitBuildConfig(t *testing.T) { "Assert buildconfig source git Ref": {bc.Spec.CommonSpec.Source.Git.Ref, branch}, "Assert buildconfig source context dir": {bc.Spec.CommonSpec.Source.ContextDir, testDir + "/"}, // BuildConfig output image is named after service name. If image key is set than tag from that is used. - "Assert buildconfig output name": {bc.Spec.CommonSpec.Output.To.Name, serviceName + ":" + getImageTag(test.ServiceConfig.Image)}, + "Assert buildconfig output name": {bc.Spec.CommonSpec.Output.To.Name, serviceName + ":" + GetImageTag(test.ServiceConfig.Image)}, "Assert buildconfig dockerfilepath": {bc.Spec.CommonSpec.Strategy.DockerStrategy.DockerfilePath, test.ServiceConfig.Dockerfile}, } diff --git a/pkg/transformer/openshift/utils.go b/pkg/transformer/openshift/utils.go new file mode 100644 index 00000000..6ec26928 --- /dev/null +++ b/pkg/transformer/openshift/utils.go @@ -0,0 +1,77 @@ +package openshift + +import ( + "os/exec" + "strings" +) + +// getImageTag get tag name from image name +// if no tag is specified return 'latest' +func GetImageTag(image string) string { + // format: registry_host:registry_port/repo_name/image_name:image_tag + // example: + // 1) myregistryhost:5000/fedora/httpd:version1.0 + // 2) myregistryhost:5000/fedora/httpd + // 3) myregistryhost/fedora/httpd:version1.0 + // 4) myregistryhost/fedora/httpd + // 5) fedora/httpd + // 6) httpd + imageAndTag := image + + i := strings.Split(image, "/") + if len(i) >= 2 { + imageAndTag = i[len(i)-1] + } + + p := strings.Split(imageAndTag, ":") + if len(p) == 2 { + return p[1] + } + return "latest" + +} + +// getAbsBuildContext returns build context relative to project root dir +func GetAbsBuildContext(context string) (string, error) { + cmd := exec.Command("git", "rev-parse", "--show-prefix") + cmd.Dir = context + out, err := cmd.Output() + if err != nil { + return "", err + } + //convert output of command to string + contextDir := strings.Trim(string(out), "\n") + return contextDir, nil +} + +// HasGitBinary checks if the 'git' binary is available on the system +func HasGitBinary() bool { + _, err := exec.LookPath("git") + return err == nil +} + +// GetGitCurrentRemoteURL gets current git remote URI for the current git repo +func GetGitCurrentRemoteURL(composeFileDir string) (string, error) { + cmd := exec.Command("git", "ls-remote", "--get-url") + cmd.Dir = composeFileDir + out, err := cmd.Output() + if err != nil { + return "", err + } + url := strings.TrimRight(string(out), "\n") + if !strings.HasSuffix(url, ".git") { + url += ".git" + } + return url, nil +} + +// GetGitCurrentBranch gets current git branch name for the current git repo +func GetGitCurrentBranch(composeFileDir string) (string, error) { + cmd := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD") + cmd.Dir = composeFileDir + out, err := cmd.Output() + if err != nil { + return "", err + } + return strings.TrimRight(string(out), "\n"), nil +}