Skip to content

Commit

Permalink
Merge pull request #312 from procrypt/multiple_files
Browse files Browse the repository at this point in the history
added support for multiple-compose files
  • Loading branch information
surajssd authored Jan 3, 2017
2 parents 9c3fdaa + a5a3805 commit b059c44
Show file tree
Hide file tree
Showing 18 changed files with 739 additions and 34 deletions.
2 changes: 1 addition & 1 deletion cmd/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ var convertCmd = &cobra.Command{
CreateChart: ConvertChart,
GenerateYaml: ConvertYaml,
Replicas: ConvertReplicas,
InputFile: GlobalFile,
InputFiles: GlobalFiles,
OutFile: ConvertOut,
Provider: strings.ToLower(GlobalProvider),
CreateD: ConvertDeployment,
Expand Down
8 changes: 4 additions & 4 deletions cmd/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ var downCmd = &cobra.Command{

// Create the Convert options.
DownOpt = kobject.ConvertOptions{
Replicas: DownReplicas,
InputFile: GlobalFile,
Provider: strings.ToLower(GlobalProvider),
EmptyVols: DownEmptyVols,
Replicas: DownReplicas,
InputFiles: GlobalFiles,
Provider: strings.ToLower(GlobalProvider),
EmptyVols: DownEmptyVols,
}
},
Run: func(cmd *cobra.Command, args []string) {
Expand Down
5 changes: 3 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ func (errorOnWarningHook) Fire(entry *logrus.Entry) error {
}

var (
GlobalBundle, GlobalFile, GlobalProvider string
GlobalBundle, GlobalProvider string
GlobalVerbose, GlobalSuppressWarnings, GlobalErrorOnWarning bool
GlobalFiles []string
)

var RootCmd = &cobra.Command{
Expand Down Expand Up @@ -86,7 +87,7 @@ func init() {
RootCmd.PersistentFlags().BoolVarP(&GlobalVerbose, "verbose", "v", false, "verbose output")
RootCmd.PersistentFlags().BoolVar(&GlobalSuppressWarnings, "suppress-warnings", false, "Suppress all warnings")
RootCmd.PersistentFlags().BoolVar(&GlobalErrorOnWarning, "error-on-warning", false, "Treat any warning as an error")
RootCmd.PersistentFlags().StringVarP(&GlobalFile, "file", "f", "docker-compose.yml", "Specify an alternative compose file")
RootCmd.PersistentFlags().StringArrayVarP(&GlobalFiles, "file", "f", []string{}, "Specify an alternative compose file")
RootCmd.PersistentFlags().StringVarP(&GlobalBundle, "bundle", "b", "", "Specify a Distributed Application GlobalBundle (DAB) file")
RootCmd.PersistentFlags().StringVar(&GlobalProvider, "provider", "kubernetes", "Specify a provider. Kubernetes or OpenShift.")
}
8 changes: 4 additions & 4 deletions cmd/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ var upCmd = &cobra.Command{

// Create the Convert options.
UpOpt = kobject.ConvertOptions{
Replicas: UpReplicas,
InputFile: GlobalFile,
Provider: strings.ToLower(GlobalProvider),
EmptyVols: UpEmptyVols,
Replicas: UpReplicas,
InputFiles: GlobalFiles,
Provider: strings.ToLower(GlobalProvider),
EmptyVols: UpEmptyVols,
}
},
Run: func(cmd *cobra.Command, args []string) {
Expand Down
25 changes: 25 additions & 0 deletions docs/user-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,31 @@ db-deployment.json docker-compose.yml docker-gitlab.yml redis-deployme
db-svc.json docker-compose-bundle.dab docker-voting.yml redis-svc.json result-svc.json vote-svc.json worker-svc.json
```

You can also provide multiple docker-compose files at the same time:

```console
$ kompose -f docker-compose.yml -f docker-guestbook.yml convert
file "frontend-service.json" created
file "mlbparks-service.json" created
file "mongodb-service.json" created
file "redis-master-service.json" created
file "redis-slave-service.json" created
file "frontend-deployment.json" created
file "mlbparks-deployment.json" created
file "mongodb-deployment.json" created
file "mongodb-claim0-persistentvolumeclaim.json" created
file "redis-master-deployment.json" created
file "redis-slave-deployment.json" created

$ ls
mlbparks-deployment.json mongodb-service.json redis-slave-service.jsonmlbparks-service.json
frontend-deployment.json mongodb-claim0-persistentvolumeclaim.json redis-master-service.json
frontend-service.json mongodb-deployment.json redis-slave-deployment.json
redis-master-deployment.json
```

When multiple docker-compose files are provided the configuration is merged. Any configuration that is common will be over ridden by subsequent file.

Using `--bundle, --dab` to specify a DAB file as below:

```console
Expand Down
8 changes: 4 additions & 4 deletions pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func ValidateFlags(bundle string, args []string, cmd *cobra.Command, opt *kobjec

if len(bundle) > 0 {
inputFormat = "bundle"
opt.InputFile = bundle
opt.InputFiles = []string{bundle}
}

if len(bundle) > 0 && isFileSet {
Expand Down Expand Up @@ -185,7 +185,7 @@ func Convert(opt kobject.ConvertOptions) {
komposeObject := kobject.KomposeObject{
ServiceConfigs: make(map[string]kobject.ServiceConfig),
}
komposeObject = l.LoadFile(opt.InputFile)
komposeObject = l.LoadFile(opt.InputFiles)

// Get a transformer that maps komposeObject to provider's primitives
t := getTransformer(opt)
Expand All @@ -211,7 +211,7 @@ func Up(opt kobject.ConvertOptions) {
komposeObject := kobject.KomposeObject{
ServiceConfigs: make(map[string]kobject.ServiceConfig),
}
komposeObject = l.LoadFile(opt.InputFile)
komposeObject = l.LoadFile(opt.InputFiles)

// Get the transformer
t := getTransformer(opt)
Expand All @@ -237,7 +237,7 @@ func Down(opt kobject.ConvertOptions) {
komposeObject := kobject.KomposeObject{
ServiceConfigs: make(map[string]kobject.ServiceConfig),
}
komposeObject = l.LoadFile(opt.InputFile)
komposeObject = l.LoadFile(opt.InputFiles)

// Get the transformer
t := getTransformer(opt)
Expand Down
2 changes: 1 addition & 1 deletion pkg/kobject/kobject.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type ConvertOptions struct {
GenerateYaml bool
EmptyVols bool
Replicas int
InputFile string
InputFiles []string
OutFile string
Provider string
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/loader/bundle/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,12 @@ func loadPorts(service Service) ([]kobject.Ports, string) {
}

// load dab file into KomposeObject
func (b *Bundle) LoadFile(file string) kobject.KomposeObject {
func (b *Bundle) LoadFile(files []string) kobject.KomposeObject {
komposeObject := kobject.KomposeObject{
ServiceConfigs: make(map[string]kobject.ServiceConfig),
LoadedFrom: "bundle",
}

file := files[0]
buf, err := ioutil.ReadFile(file)
if err != nil {
logrus.Fatalf("Failed to read bundles file: %s ", err)
Expand Down
8 changes: 4 additions & 4 deletions pkg/loader/compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,16 +221,16 @@ func loadPorts(composePorts []string) ([]kobject.Ports, error) {
}

// load compose file into KomposeObject
func (c *Compose) LoadFile(file string) kobject.KomposeObject {
func (c *Compose) LoadFile(files []string) kobject.KomposeObject {
komposeObject := kobject.KomposeObject{
ServiceConfigs: make(map[string]kobject.ServiceConfig),
LoadedFrom: "compose",
}
context := &project.Context{}
if file == "" {
file = "docker-compose.yml"
if len(files) == 0 {
files = append(files, "docker-compose.yml")
}
context.ComposeFiles = []string{file}
context.ComposeFiles = files

if context.ResourceLookup == nil {
context.ResourceLookup = &lookup.FileResourceLookup{}
Expand Down
2 changes: 1 addition & 1 deletion pkg/loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
)

type Loader interface {
LoadFile(file string) kobject.KomposeObject
LoadFile(files []string) kobject.KomposeObject
///Name() string
}

Expand Down
7 changes: 4 additions & 3 deletions pkg/transformer/kubernetes/k8sutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ import (
/**
* Generate Helm Chart configuration
*/
func generateHelm(filename string, outFiles []string) error {
func generateHelm(filenames []string, outFiles []string) error {
type ChartDetails struct {
Name string
}

// Let assume all the docker-compose files are in the same directory
filename := filenames[0]
extension := filepath.Ext(filename)
dirName := filename[0 : len(filename)-len(extension)]
details := ChartDetails{dirName}
Expand Down Expand Up @@ -231,7 +232,7 @@ func PrintList(objects []runtime.Object, opt kobject.ConvertOptions) error {
}
}
if opt.CreateChart {
generateHelm(opt.InputFile, files)
generateHelm(opt.InputFiles, files)
}
return nil
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/transformer/openshift/openshift.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ func getGitCurrentBranch(composeFileDir string) (string, error) {
}

// getComposeFileDir returns compose file directory
func getComposeFileDir(inputFile string) (string, error) {
func getComposeFileDir(inputFiles []string) (string, error) {
// Lets assume all the docker-compose files are in the same directory
inputFile := inputFiles[0]
if strings.Index(inputFile, "/") != 0 {
workDir, err := os.Getwd()
if err != nil {
Expand Down Expand Up @@ -332,7 +334,7 @@ func (o *OpenShift) Transform(komposeObject kobject.KomposeObject, opt kobject.C
// buildconfig needs to be added to objects after imagestream because of this Openshift bug: https://github.com/openshift/origin/issues/4518
if service.Build != "" {
if !hasBuild {
composeFileDir, err = getComposeFileDir(opt.InputFile)
composeFileDir, err = getComposeFileDir(opt.InputFiles)
if err != nil {
logrus.Warningf("Error in detecting compose file's directory.")
continue
Expand Down
10 changes: 5 additions & 5 deletions pkg/transformer/openshift/openshift_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,17 +214,17 @@ func TestGetComposeFileDir(t *testing.T) {
wd, _ := os.Getwd()

testCases := map[string]struct {
inputFile string
output string
inputFiles []string
output string
}{
"Get compose file dir for relative input file path": {"foo/bar.yaml", filepath.Join(wd, "foo")},
"Get compose file dir for abs input file path": {"/abs/path/to/compose.yaml", "/abs/path/to"},
"Get compose file dir for relative input file path": {[]string{"foo/bar.yaml"}, filepath.Join(wd, "foo")},
"Get compose file dir for abs input file path": {[]string{"/abs/path/to/compose.yaml"}, "/abs/path/to"},
}

for name, test := range testCases {
t.Log("Test case: ", name)

output, err = getComposeFileDir(test.inputFile)
output, err = getComposeFileDir(test.inputFiles)

if err != nil {
t.Errorf("Expected success, got error: %#v", err)
Expand Down
6 changes: 5 additions & 1 deletion script/test/cmd/tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ convert::expect_failure "kompose -f $KOMPOSE_ROOT/script/test/fixtures/bundles/f
# Test related to kompose --bundle convert to ensure that docker bundles are converted properly
convert::expect_success "kompose --bundle $KOMPOSE_ROOT/script/test/fixtures/bundles/dab/docker-compose-bundle.dab convert --stdout" "$KOMPOSE_ROOT/script/test/fixtures/bundles/dab/output-k8s.json"

# Test related to multiple-compose files
# Kubernets test
convert::expect_success_and_warning "kompose -f $KOMPOSE_ROOT/script/test/fixtures/multiple-compose-files/docker-k8s.yml -f $KOMPOSE_ROOT/script/test/fixtures/multiple-compose-files/docker-os.yml convert --stdout" "$KOMPOSE_ROOT/script/test/fixtures/multiple-compose-files/output-k8s.json" "Unsupported depends_on key - ignoring"
convert::expect_success_and_warning "kompose --provider=openshift -f $KOMPOSE_ROOT/script/test/fixtures/multiple-compose-files/docker-k8s.yml -f $KOMPOSE_ROOT/script/test/fixtures/multiple-compose-files/docker-os.yml convert --stdout" "$KOMPOSE_ROOT/script/test/fixtures/multiple-compose-files/output-openshift.json" "Unsupported depends_on key - ignoring"

######
# Test related to kompose --bundle convert to ensure that DSB bundles are converted properly
convert::expect_success_and_warning "kompose --bundle $KOMPOSE_ROOT/script/test/fixtures/bundles/dsb/docker-voting-bundle.dsb convert --stdout" "$KOMPOSE_ROOT/script/test/fixtures/bundles/dsb/output-k8s.json" "Service cannot be created because of missing port."
Expand Down Expand Up @@ -170,5 +175,4 @@ convert::files_exist "kompose -f $KOMPOSE_ROOT/examples/docker-compose.yml conve
# Behavior with -o <dirname>/<dirname>/<filename>
convert::files_exist "kompose -f $KOMPOSE_ROOT/examples/docker-compose.yml convert -o output_dir/output_dir_nested/output_file" "$TEMP_DIR/output_dir/output_dir_nested" "$TEMP_DIR/output_dir/output_dir_nested/output_file"


exit $EXIT_STATUS
27 changes: 27 additions & 0 deletions script/test/fixtures/multiple-compose-files/docker-k8s.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
version: "2"

services:
mariadb:
image: centos/mariadb
ports:
- "3306"
environment:
MYSQL_ROOT_PASSWORD: kubernetes
MYSQL_DATABASE: kubernetes
MYSQL_PASSWORD: kubernetes
MYSQL_USER: kubernetes
volumes:
- /var/lib/mysql

etherpad:
image: centos/etherpad
ports:
- "80:9001"
depends_on:
- mariadb
environment:
DB_HOST: kubernetes
DB_DBID: kubernetes
DB_PASS: kubernetes
DB_PORT: kubernetes
DB_USER: kubernetes
27 changes: 27 additions & 0 deletions script/test/fixtures/multiple-compose-files/docker-os.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
version: "2"

services:
mariadb:
image: centos/mariadb
ports:
- "3307"
environment:
MYSQL_ROOT_PASSWORD: openshift
MYSQL_DATABASE: openshift
MYSQL_PASSWORD: openshift
MYSQL_USER: openshift
volumes:
- /var/lib/mysql

etherpad:
image: centos/etherpad
ports:
- "80:9001"
depends_on:
- mariadb
environment:
DB_HOST: openshift
DB_DBID: openshift
DB_PASS: openshift
DB_PORT: openshift
DB_USER: openshift
Loading

0 comments on commit b059c44

Please sign in to comment.