Fix build context (#1411)

This commit is contained in:
Lex Cao 2021-08-11 09:43:42 +08:00 committed by GitHub
parent b605fd986c
commit ef474809e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 41 additions and 6 deletions

View File

@ -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 {

View File

@ -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)
}

View File

@ -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"

View File

@ -0,0 +1,7 @@
version: "3"
services:
foo:
build:
context: ./build
dockerfile: Dockerfile