diff --git a/pkg/loader/compose/v3.go b/pkg/loader/compose/v3.go index 962cd569..a7f7907c 100644 --- a/pkg/loader/compose/v3.go +++ b/pkg/loader/compose/v3.go @@ -17,6 +17,9 @@ limitations under the License. package compose import ( + "fmt" + "os" + "path" "strconv" "strings" "time" @@ -30,10 +33,6 @@ import ( "github.com/docker/cli/cli/compose/loader" "github.com/docker/cli/cli/compose/types" - "os" - - "fmt" - shlex "github.com/google/shlex" "github.com/kubernetes/kompose/pkg/kobject" "github.com/pkg/errors" @@ -444,7 +443,7 @@ func dockerComposeToKomposeMapping(composeObject *types.Config) (kobject.Kompose // TODO: Build is not yet supported, see: // https://github.com/docker/cli/blob/master/cli/compose/types/types.go#L9 // We will have to *manually* add this / parse. - serviceConfig.Build = composeServiceConfig.Build.Context + serviceConfig.Build = resolveV3Context(composeObject.Filename, composeServiceConfig.Build.Context) serviceConfig.Dockerfile = composeServiceConfig.Build.Dockerfile serviceConfig.BuildArgs = composeServiceConfig.Build.Args serviceConfig.BuildLabels = composeServiceConfig.Build.Labels @@ -490,6 +489,21 @@ func dockerComposeToKomposeMapping(composeObject *types.Config) (kobject.Kompose return komposeObject, nil } +// resolveV3Context resolves build context like v2 does, see: +// https://github.com/docker/libcompose/blob/master/config/merge_v2.go#L155 +func resolveV3Context(inFile string, context string) string { + if context == "" { + return "" + } + + current := path.Dir(inFile) + if context != "." { + current = path.Join(current, context) + } + + return current +} + func parseV3Network(composeServiceConfig *types.ServiceConfig, serviceConfig *kobject.ServiceConfig, composeObject *types.Config) { if len(composeServiceConfig.Networks) == 0 { if defaultNetwork, ok := composeObject.Networks["default"]; ok { diff --git a/pkg/transformer/utils.go b/pkg/transformer/utils.go index 65e43309..6627c480 100644 --- a/pkg/transformer/utils.go +++ b/pkg/transformer/utils.go @@ -262,8 +262,9 @@ func BuildDockerImage(service kobject.ServiceConfig, name string) error { log.Debug("Build image working dir is: ", wd) + log.Debug("Build image service build is: ", service.Build) // Get the appropriate image source and name - imagePath := path.Join(wd, path.Base(service.Build)) + imagePath := service.Build if !path.IsAbs(service.Build) { imagePath = path.Join(wd, service.Build) } diff --git a/script/test/cmd/tests_new.sh b/script/test/cmd/tests_new.sh index a96a032f..53eb2fc3 100755 --- a/script/test/cmd/tests_new.sh +++ b/script/test/cmd/tests_new.sh @@ -82,3 +82,16 @@ convert::check_artifacts_generated "kompose -f $KOMPOSE_ROOT/script/test/fixture #TEST the pvc-request-size command parameter convert::check_artifacts_generated "kompose -f $KOMPOSE_ROOT/script/test/fixtures/pvc-request-size/docker-compose.yml convert -o $TEMP_DIR/output_dir2/output-k8s.json -j --pvc-request-size=300Mi" "$TEMP_DIR/output_dir2/output-k8s.json" convert::check_artifacts_generated "kompose --provider=openshift -f $KOMPOSE_ROOT/script/test/fixtures/pvc-request-size/docker-compose.yml convert -o $TEMP_DIR/output_dir2/output-os.json -j --pvc-request-size=300Mi" "$TEMP_DIR/output_dir2/output-os.json" + +###### +# Test the path of build image +# Test build v2 absolute compose file +convert::check_artifacts_generated "kompose --build local -f $KOMPOSE_ROOT/script/test/fixtures/buildconfig/docker-compose.yml convert -o $TEMP_DIR/output_file" "$TEMP_DIR/output_file" +# Test build v2 relative compose file +relative_path=$(realpath --relative-to="$PWD" "$KOMPOSE_ROOT/script/test/fixtures/buildconfig/docker-compose.yml") +convert::check_artifacts_generated "kompose --build local -f $relative_path convert -o $TEMP_DIR/output_file" "$TEMP_DIR/output_file" +# Test build v3 absolute compose file with context +convert::check_artifacts_generated "kompose --build local -f $KOMPOSE_ROOT/script/test/fixtures/buildconfig/docker-compose-v3.yml convert -o $TEMP_DIR/output_file" "$TEMP_DIR/output_file" +# Test build v3 relative compose file with context +relative_path=$(realpath --relative-to="$PWD" "$KOMPOSE_ROOT/script/test/fixtures/buildconfig/docker-compose-v3.yml") +convert::check_artifacts_generated "kompose --build local -f $relative_path convert -o $TEMP_DIR/output_file" "$TEMP_DIR/output_file" diff --git a/script/test/fixtures/buildconfig/docker-compose-v3.yml b/script/test/fixtures/buildconfig/docker-compose-v3.yml new file mode 100644 index 00000000..b19c2eee --- /dev/null +++ b/script/test/fixtures/buildconfig/docker-compose-v3.yml @@ -0,0 +1,7 @@ +version: "3" + +services: + foo: + build: + context: ./build + dockerfile: Dockerfile