Skip to content

Commit

Permalink
move git and related functions from openshift. go into a separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
procrypt authored and surajnarwade committed Aug 11, 2017
1 parent a9bffa6 commit 683db12
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 87 deletions.
92 changes: 9 additions & 83 deletions pkg/transformer/openshift/openshift.go
Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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" {
Expand Down Expand Up @@ -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),
},
},
},
Expand All @@ -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 != "" {
Expand Down Expand Up @@ -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.")
}
Expand All @@ -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.")
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/transformer/openshift/openshift_test.go
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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},
}

Expand Down
77 changes: 77 additions & 0 deletions 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
}

0 comments on commit 683db12

Please sign in to comment.