Skip to content

Commit

Permalink
Merge pull request #206 from rtnpro/buildconfig
Browse files Browse the repository at this point in the history
Generate buildconfig for Openshift
  • Loading branch information
surajssd authored Dec 28, 2016
2 parents c80735c + 0d86f3e commit 9c3fdaa
Show file tree
Hide file tree
Showing 29 changed files with 16,330 additions and 129 deletions.
20 changes: 14 additions & 6 deletions cmd/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ import (
)

var (
ConvertSource, ConvertOut string
ConvertChart, ConvertDeployment, ConvertDaemonSet bool
ConvertReplicationController, ConvertYaml, ConvertStdout bool
ConvertEmptyVols, ConvertDeploymentConfig bool
ConvertReplicas int
ConvertOpt kobject.ConvertOptions
ConvertSource, ConvertOut, ConvertBuildRepo, ConvertBuildBranch string
ConvertChart, ConvertDeployment, ConvertDaemonSet bool
ConvertReplicationController, ConvertYaml, ConvertStdout bool
ConvertEmptyVols, ConvertDeploymentConfig, ConvertBuildConfig bool
ConvertReplicas int
ConvertOpt kobject.ConvertOptions
)

var ConvertProvider string = GlobalProvider
Expand All @@ -53,6 +53,8 @@ var convertCmd = &cobra.Command{
CreateD: ConvertDeployment,
CreateDS: ConvertDaemonSet,
CreateRC: ConvertReplicationController,
BuildRepo: ConvertBuildRepo,
BuildBranch: ConvertBuildBranch,
CreateDeploymentConfig: ConvertDeploymentConfig,
EmptyVols: ConvertEmptyVols,
}
Expand Down Expand Up @@ -84,6 +86,10 @@ func init() {
// OpenShift only
convertCmd.Flags().BoolVar(&ConvertDeploymentConfig, "deployment-config", true, "Generate an OpenShift deploymentconfig object")
convertCmd.Flags().MarkHidden("deployment-config")
convertCmd.Flags().StringVar(&ConvertBuildRepo, "build-repo", "", "Specify source repository for buildconfig (default remote origin)")
convertCmd.Flags().MarkHidden("build-repo")
convertCmd.Flags().StringVar(&ConvertBuildBranch, "build-branch", "", "Specify repository branch to use for buildconfig (default master)")
convertCmd.Flags().MarkHidden("build-branch")

// Standard between the two
convertCmd.Flags().BoolVarP(&ConvertYaml, "yaml", "y", false, "Generate resource files into yaml format")
Expand All @@ -107,6 +113,8 @@ Available Commands:{{range .Commands}}{{if .IsAvailableCommand}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{ if .HasAvailableLocalFlags}}
Resource Flags:
--build-branch Specify repository branch to use for buildconfig (default is current branch name)
--build-repo Specify source repository for buildconfig (default is current branch's remote url
-c, --chart Create a Helm chart for converted objects
--daemon-set Generate a Kubernetes daemonset object
-d, --deployment Generate a Kubernetes deployment object
Expand Down
17 changes: 15 additions & 2 deletions docs/user-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ INFO[0000] file "result-imagestream.json" created
```

In similar way you can convert DAB files to OpenShift.
```console$
```console
$ kompose --bundle docker-compose-bundle.dab --provider openshift convert
WARN[0000]: Unsupported key networks - ignoring
INFO[0000] file "redis-svc.json" created
Expand All @@ -106,6 +106,19 @@ INFO[0000] file "redis-deploymentconfig.json" created
INFO[0000] file "redis-imagestream.json" created
```

It also supports creating buildconfig for build directive in a service. By default, it uses the remote repo for the current git branch as the source repo, and the current branch as the source branch for the build. You can specify a different source repo and branch using ``--build-repo`` and ``--build-branch`` options respectively.

```console
kompose --provider openshift --file buildconfig/docker-compose.yml convert
WARN[0000] [foo] Service cannot be created because of missing port.
INFO[0000] Buildconfig using git@github.com:rtnpro/kompose.git::master as source.
INFO[0000] file "foo-deploymentconfig.json" created
INFO[0000] file "foo-imagestream.json" created
INFO[0000] file "foo-buildconfig.json" created
```

**Note**: If you are manually pushing the Openshift artifacts using ``oc create -f``, you need to ensure that you push the imagestream artifact before the buildconfig artifact, to workaround this Openshift issue: https://github.com/openshift/origin/issues/4518 .

## Kompose up

Kompose supports a straightforward way to deploy your "composed" application to Kubernetes or OpenShift via `kompose up`.
Expand Down Expand Up @@ -354,4 +367,4 @@ services:
mariadb:
image: centos/mariadb
restart: "no"
```
```
5 changes: 5 additions & 0 deletions examples/buildconfig/build/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM busybox


RUN touch /test

7 changes: 7 additions & 0 deletions examples/buildconfig/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version: "2"

services:
foo:
build: "./build"
command: "sleep 3600"

8 changes: 6 additions & 2 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import:
- package: github.com/openshift/origin
version: v1.4.0-rc1
subpackages:
- pkg/build/api/install
- pkg/build/api/v1
- pkg/client
- pkg/cmd/cli/config
- pkg/deploy/api
Expand Down
9 changes: 9 additions & 0 deletions pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
_ "k8s.io/kubernetes/pkg/apis/extensions/install"

// install OpenShift api
_ "github.com/openshift/origin/pkg/build/api/install"
_ "github.com/openshift/origin/pkg/deploy/api/install"
_ "github.com/openshift/origin/pkg/image/api/install"
_ "github.com/openshift/origin/pkg/route/api/install"
Expand Down Expand Up @@ -62,6 +63,8 @@ func ValidateFlags(bundle string, args []string, cmd *cobra.Command, opt *kobjec

// OpenShift specific flags
deploymentConfig := cmd.Flags().Lookup("deployment-config").Changed
buildRepo := cmd.Flags().Lookup("build-repo").Changed
buildBranch := cmd.Flags().Lookup("build-branch").Changed

// Kubernetes specific flags
chart := cmd.Flags().Lookup("chart").Changed
Expand All @@ -88,6 +91,12 @@ func ValidateFlags(bundle string, args []string, cmd *cobra.Command, opt *kobjec
if deploymentConfig {
logrus.Fatalf("--deployment-config is an OpenShift only flag")
}
if buildRepo {
logrus.Fatalf("--build-repo is an Openshift only flag")
}
if buildBranch {
logrus.Fatalf("--build-branch is an Openshift only flag")
}
}

// Standard checks regardless of provider
Expand Down
2 changes: 2 additions & 0 deletions pkg/kobject/kobject.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ type ConvertOptions struct {
CreateRC bool
CreateDS bool
CreateDeploymentConfig bool
BuildRepo string
BuildBranch string
CreateChart bool
GenerateYaml bool
EmptyVols bool
Expand Down
1 change: 1 addition & 0 deletions pkg/loader/compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ func (c *Compose) LoadFile(file string) kobject.KomposeObject {
if composeServiceConfig, ok := composeObject.ServiceConfigs.Get(name); ok {
serviceConfig := kobject.ServiceConfig{}
serviceConfig.Image = composeServiceConfig.Image
serviceConfig.Build = composeServiceConfig.Build.Context
serviceConfig.ContainerName = composeServiceConfig.ContainerName
serviceConfig.Command = composeServiceConfig.Entrypoint
serviceConfig.Args = composeServiceConfig.Command
Expand Down
71 changes: 71 additions & 0 deletions pkg/testutils/git.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package testutils

import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"testing"
)

func NewCommand(cmd string) *exec.Cmd {
return exec.Command("sh", "-c", cmd)
}

func CreateLocalDirectory(t *testing.T) string {
dir, err := ioutil.TempDir(os.TempDir(), "kompose-test-")
if err != nil {
t.Fatal(err)
}
return dir
}

func CreateLocalGitDirectory(t *testing.T) string {
dir := CreateLocalDirectory(t)
cmd := NewCommand(
`git init && touch README &&
git add README &&
git commit -m 'testcommit'`)
cmd.Dir = dir
_, err := cmd.Output()
if err != nil {
t.Logf("create local git dir: %v", err)
t.Fatal(err)
}
return dir
}

func SetGitRemote(t *testing.T, dir string, remote string, remoteUrl string) {
cmd := NewCommand(fmt.Sprintf("git remote add %s %s", remote, remoteUrl))
cmd.Dir = dir
_, err := cmd.Output()
if err != nil {
t.Logf("set git remote: %v", err)
t.Fatal(err)
}
}

func CreateGitRemoteBranch(t *testing.T, dir string, branch string, remote string) {
cmd := NewCommand(
fmt.Sprintf(`git checkout -b %s &&
git config branch.%s.remote %s &&
git config branch.%s.merge refs/heads/%s`,
branch, branch, remote, branch, branch))
cmd.Dir = dir

_, err := cmd.Output()
if err != nil {
t.Logf("create git branch: %v", err)
t.Fatal(err)
}
}

func CreateSubdir(t *testing.T, dir string, subdir string) {
cmd := NewCommand(fmt.Sprintf("mkdir -p %s", subdir))
cmd.Dir = dir

_, err := cmd.Output()
if err != nil {
t.Fatal(err)
}
}
3 changes: 3 additions & 0 deletions pkg/transformer/kubernetes/k8sutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"k8s.io/kubernetes/pkg/apis/extensions"
"k8s.io/kubernetes/pkg/runtime"

buildapi "github.com/openshift/origin/pkg/build/api"
deployapi "github.com/openshift/origin/pkg/deploy/api"
imageapi "github.com/openshift/origin/pkg/image/api"
routeapi "github.com/openshift/origin/pkg/route/api"
Expand Down Expand Up @@ -211,6 +212,8 @@ func PrintList(objects []runtime.Object, opt kobject.ConvertOptions) error {
file = transformer.Print(t.Name, dirName, strings.ToLower(t.Kind), data, opt.ToStdout, opt.GenerateYaml, f)
case *deployapi.DeploymentConfig:
file = transformer.Print(t.Name, dirName, strings.ToLower(t.Kind), data, opt.ToStdout, opt.GenerateYaml, f)
case *buildapi.BuildConfig:
file = transformer.Print(t.Name, dirName, strings.ToLower(t.Kind), data, opt.ToStdout, opt.GenerateYaml, f)
case *imageapi.ImageStream:
file = transformer.Print(t.Name, dirName, strings.ToLower(t.Kind), data, opt.ToStdout, opt.GenerateYaml, f)
case *api.Service:
Expand Down
3 changes: 3 additions & 0 deletions pkg/transformer/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/fatih/structs"
"github.com/kubernetes-incubator/kompose/pkg/kobject"
"github.com/kubernetes-incubator/kompose/pkg/transformer"
buildapi "github.com/openshift/origin/pkg/build/api"
deployapi "github.com/openshift/origin/pkg/deploy/api"

// install kubernetes api
Expand Down Expand Up @@ -512,6 +513,8 @@ func (k *Kubernetes) UpdateController(obj runtime.Object, updateTemplate func(*a
updateTemplate(&p)
t.Spec = p.Spec
t.ObjectMeta = p.ObjectMeta
case *buildapi.BuildConfig:
updateMeta(&t.ObjectMeta)
}
}

Expand Down
Loading

0 comments on commit 9c3fdaa

Please sign in to comment.