diff --git a/pkg/kobject/kobject.go b/pkg/kobject/kobject.go index 8c95ba2f..594ee085 100644 --- a/pkg/kobject/kobject.go +++ b/pkg/kobject/kobject.go @@ -82,6 +82,7 @@ type ServiceConfig struct { VolumesFrom []string `compose:"volumes_from" bundle:""` ServiceType string `compose:"kompose.service.type" bundle:""` Build string `compose:"build" bundle:""` + BuildArgs map[string]*string `compose:"build-args" bundle:""` ExposeService string `compose:"kompose.service.expose" bundle:""` Stdin bool `compose:"stdin_open" bundle:""` Tty bool `compose:"tty" bundle:""` diff --git a/pkg/loader/compose/compose.go b/pkg/loader/compose/compose.go index 542f5c31..e8c093c6 100644 --- a/pkg/loader/compose/compose.go +++ b/pkg/loader/compose/compose.go @@ -321,6 +321,7 @@ func (c *Compose) LoadFile(files []string) (kobject.KomposeObject, error) { serviceConfig.Command = composeServiceConfig.Entrypoint serviceConfig.Args = composeServiceConfig.Command serviceConfig.Dockerfile = composeServiceConfig.Build.Dockerfile + serviceConfig.BuildArgs = composeServiceConfig.Build.Args envs := loadEnvVars(composeServiceConfig.Environment) serviceConfig.Environment = envs diff --git a/pkg/transformer/openshift/openshift.go b/pkg/transformer/openshift/openshift.go index 25436b48..975fe8c4 100644 --- a/pkg/transformer/openshift/openshift.go +++ b/pkg/transformer/openshift/openshift.go @@ -200,6 +200,13 @@ func (o *OpenShift) initImageStream(name string, service kobject.ServiceConfig, // initBuildConfig initialize Openshifts BuildConfig Object func initBuildConfig(name string, service kobject.ServiceConfig, repo string, branch string) (*buildapi.BuildConfig, error) { contextDir, err := getAbsBuildContext(service.Build) + envList := []kapi.EnvVar{} + for envName, envValue := range service.BuildArgs { + if *envValue == "\x00" { + *envValue = os.Getenv(envName) + } + envList = append(envList, kapi.EnvVar{Name: envName, Value: *envValue}) + } if err != nil { return nil, errors.Wrap(err, name+"buildconfig cannot be created due to error in creating build context, getAbsBuildContext failed") } @@ -229,6 +236,7 @@ func initBuildConfig(name string, service kobject.ServiceConfig, repo string, br Strategy: buildapi.BuildStrategy{ DockerStrategy: &buildapi.DockerBuildStrategy{ DockerfilePath: service.Dockerfile, + Env: envList, }, }, Output: buildapi.BuildOutput{ diff --git a/pkg/transformer/openshift/openshift_test.go b/pkg/transformer/openshift/openshift_test.go index c1d74fe5..f88f3844 100644 --- a/pkg/transformer/openshift/openshift_test.go +++ b/pkg/transformer/openshift/openshift_test.go @@ -17,13 +17,13 @@ limitations under the License. package openshift import ( + kapi "k8s.io/kubernetes/pkg/api" + "k8s.io/kubernetes/pkg/runtime" "os" "path/filepath" + "reflect" "testing" - "k8s.io/kubernetes/pkg/api" - "k8s.io/kubernetes/pkg/runtime" - deployapi "github.com/openshift/origin/pkg/deploy/api" "github.com/kubernetes-incubator/kompose/pkg/kobject" @@ -37,7 +37,7 @@ func newServiceConfig() kobject.ServiceConfig { ContainerName: "myfoobarname", Image: "image", Environment: []kobject.EnvVar{kobject.EnvVar{Name: "env", Value: "value"}}, - Port: []kobject.Ports{kobject.Ports{HostPort: 123, ContainerPort: 456, Protocol: api.ProtocolTCP}}, + Port: []kobject.Ports{kobject.Ports{HostPort: 123, ContainerPort: 456, Protocol: kapi.ProtocolTCP}}, Command: []string{"cmd"}, WorkingDir: "dir", Args: []string{"arg1", "arg2"}, @@ -288,9 +288,12 @@ func TestInitBuildConfig(t *testing.T) { serviceName := "serviceA" repo := "https://git.test.com/org/repo" branch := "somebranch" + buildArgs := []kapi.EnvVar{{Name: "name", Value: "value"}} + value := "value" sc := kobject.ServiceConfig{ Build: filepath.Join(dir, "a/build"), Dockerfile: "Dockerfile-alternate", + BuildArgs: map[string]*string{"name": &value}, } bc, err := initBuildConfig(serviceName, sc, repo, branch) if err != nil { @@ -307,13 +310,15 @@ func TestInitBuildConfig(t *testing.T) { "Assert buildconfig output name": {bc.Spec.CommonSpec.Output.To.Name, serviceName + ":latest"}, "Assert buildconfig dockerfilepath": {bc.Spec.CommonSpec.Strategy.DockerStrategy.DockerfilePath, "Dockerfile-alternate"}, } - for name, test := range testCases { t.Log("Test case: ", name) if test.field != test.value { t.Errorf("Expected: %#v, got: %#v", test.value, test.field) } } + if !reflect.DeepEqual(bc.Spec.CommonSpec.Strategy.DockerStrategy.Env, buildArgs) { + t.Errorf("Expected: %#v, got: %#v", bc.Spec.CommonSpec.Strategy.DockerStrategy.Env, buildArgs) + } } // TestServiceWithoutPort this tests if Headless Service is created for services without Port. diff --git a/script/test/cmd/tests.sh b/script/test/cmd/tests.sh index 07aed44c..618ffda3 100755 --- a/script/test/cmd/tests.sh +++ b/script/test/cmd/tests.sh @@ -27,8 +27,6 @@ fi # Warning Template warning="Buildconfig using $uri::$branch as source." -# Replacing variables with current branch and uri -sed -e "s;%URI%;$uri;g" -e "s;%REF%;$branch;g" $KOMPOSE_ROOT/script/test/fixtures/nginx-node-redis/output-os-template.json > /tmp/output-os.json ####### # Tests related to docker-compose file in /script/test/fixtures/etherpad @@ -58,11 +56,15 @@ unset $(cat $KOMPOSE_ROOT/script/test/fixtures/gitlab/envs | cut -d'=' -f1) # kubernetes test convert::expect_success_and_warning "kompose -f $KOMPOSE_ROOT/script/test/fixtures/nginx-node-redis/docker-compose.yml convert --stdout -j" "$KOMPOSE_ROOT/script/test/fixtures/nginx-node-redis/output-k8s.json" "Kubernetes provider doesn't support build key - ignoring" # openshift test +# Replacing variables with current branch and uri +sed -e "s;%URI%;$uri;g" -e "s;%REF%;$branch;g" $KOMPOSE_ROOT/script/test/fixtures/nginx-node-redis/output-os-template.json > /tmp/output-os.json convert::expect_success_and_warning "kompose --provider=openshift -f $KOMPOSE_ROOT/script/test/fixtures/nginx-node-redis/docker-compose.yml convert --stdout -j" "/tmp/output-os.json" "$warning" +rm /tmp/output-os.json + ###### # Tests related to docker-compose file in /script/test/fixtures/entrypoint-command # kubernetes test -convert::expect_success "kompose -f $KOMPOSE_ROOT/script/test/fixtures/entrypoint-command/docker-compose.yml convert --stdout -j" "$KOMPOSE_ROOT/script/test/fixtures/entrypoint-command/output-k8s.json" +convert::expect_success "kompose -f $KOMPOSE_ROOT/script/test/fixtures/entrypoint-command/docker-compose.yml convert --stdout -j" "$KOMPOSE_ROOT/script/test/fixtures/entrypoint-command/output-k8s.json" # openshift test convert::expect_success "kompose --provider=openshift -f $KOMPOSE_ROOT/script/test/fixtures/entrypoint-command/docker-compose.yml convert --stdout -j" "$KOMPOSE_ROOT/script/test/fixtures/entrypoint-command/output-os.json" @@ -209,6 +211,8 @@ convert::check_artifacts_generated "kompose -f $KOMPOSE_ROOT/script/test/fixture #### # Test regarding build context (running kompose from various directories) +# Replacing variables with current branch and uri +sed -e "s;%URI%;$uri;g" -e "s;%REF%;$branch;g" $KOMPOSE_ROOT/script/test/fixtures/nginx-node-redis/output-os-template.json > /tmp/output-os.json CURRENT_DIR=$(pwd) cd "$KOMPOSE_ROOT/script/test/fixtures/nginx-node-redis/" convert::expect_success_and_warning "kompose convert --provider openshift --stdout -j" "/tmp/output-os.json" "$warning" @@ -217,6 +221,15 @@ convert::expect_success_and_warning "kompose convert --provider openshift --stdo cd "$KOMPOSE_ROOT/script/test/fixtures/nginx-node-redis/node" convert::expect_success_and_warning "kompose convert --provider openshift --stdout -j -f ../docker-compose.yml" "/tmp/output-os.json" "$warning" cd $CURRENT_DIR +rm /tmp/output-os.json + + +# Test the presence of build args in buildconfig +# Replacing variables with current branch and uri +sed -e "s;%URI%;$uri;g" -e "s;%REF%;$branch;g" $KOMPOSE_ROOT/script/test/fixtures/buildargs/output-os-template.json > /tmp/output-buildarg-os.json +export $(cat $KOMPOSE_ROOT/script/test/fixtures/buildargs/envs) +convert::expect_success_and_warning "kompose --provider openshift -f $KOMPOSE_ROOT/script/test/fixtures/buildargs/docker-compose.yml convert --stdout -j" "/tmp/output-buildarg-os.json" "$warning" +rm /tmp/output-buildarg-os.json # Test related to support docker-compose.yaml beside docker-compose.yml # Store the original path @@ -234,7 +247,4 @@ convert::expect_success "kompose --provider=openshift convert --stdout -j" "$KOM # Return back to the original path cd $CURRENT_DIR -# Removes generated output -rm -rf /tmp/output-os.json - exit $EXIT_STATUS diff --git a/script/test/fixtures/buildargs/README.md b/script/test/fixtures/buildargs/README.md new file mode 100644 index 00000000..bf97166a --- /dev/null +++ b/script/test/fixtures/buildargs/README.md @@ -0,0 +1,11 @@ +## Docker Compose Buildargs + +### Usage + +The simplest thing to do: + +```bash +export $(cat envs) +``` + +To customize the values edit `envs` file. diff --git a/script/test/fixtures/buildargs/build/Dockerfile b/script/test/fixtures/buildargs/build/Dockerfile new file mode 100644 index 00000000..f094e685 --- /dev/null +++ b/script/test/fixtures/buildargs/build/Dockerfile @@ -0,0 +1,2 @@ +FROM busybox +RUN touch /test \ No newline at end of file diff --git a/script/test/fixtures/buildargs/docker-compose.yml b/script/test/fixtures/buildargs/docker-compose.yml new file mode 100644 index 00000000..ff319374 --- /dev/null +++ b/script/test/fixtures/buildargs/docker-compose.yml @@ -0,0 +1,16 @@ +version: "2" + +services: + foo: + build: + context: "./build" + args: + NAME: web + command: "sleep 3600" + foo1: + build: + context: "./build" + args: + - NAME=web + - foo + command: "sleep 3600" diff --git a/script/test/fixtures/buildargs/envs b/script/test/fixtures/buildargs/envs new file mode 100644 index 00000000..74d0a43f --- /dev/null +++ b/script/test/fixtures/buildargs/envs @@ -0,0 +1 @@ +foo=bar diff --git a/script/test/fixtures/buildargs/output-os-template.json b/script/test/fixtures/buildargs/output-os-template.json new file mode 100644 index 00000000..04ce1e9c --- /dev/null +++ b/script/test/fixtures/buildargs/output-os-template.json @@ -0,0 +1,319 @@ +{ + "kind": "List", + "apiVersion": "v1", + "metadata": {}, + "items": [ + { + "kind": "Service", + "apiVersion": "v1", + "metadata": { + "name": "foo1", + "creationTimestamp": null, + "labels": { + "io.kompose.service": "foo1" + } + }, + "spec": { + "ports": [ + { + "name": "headless", + "port": 55555, + "targetPort": 0 + } + ], + "selector": { + "io.kompose.service": "foo1" + }, + "clusterIP": "None" + }, + "status": { + "loadBalancer": {} + } + }, + { + "kind": "Service", + "apiVersion": "v1", + "metadata": { + "name": "foo", + "creationTimestamp": null, + "labels": { + "io.kompose.service": "foo" + } + }, + "spec": { + "ports": [ + { + "name": "headless", + "port": 55555, + "targetPort": 0 + } + ], + "selector": { + "io.kompose.service": "foo" + }, + "clusterIP": "None" + }, + "status": { + "loadBalancer": {} + } + }, + { + "kind": "DeploymentConfig", + "apiVersion": "v1", + "metadata": { + "name": "foo1", + "creationTimestamp": null, + "labels": { + "io.kompose.service": "foo1" + } + }, + "spec": { + "strategy": { + "resources": {} + }, + "triggers": [ + { + "type": "ConfigChange" + }, + { + "type": "ImageChange", + "imageChangeParams": { + "automatic": true, + "containerNames": [ + "foo1" + ], + "from": { + "kind": "ImageStreamTag", + "name": "foo1:latest" + } + } + } + ], + "replicas": 1, + "test": false, + "selector": { + "io.kompose.service": "foo1" + }, + "template": { + "metadata": { + "creationTimestamp": null, + "labels": { + "io.kompose.service": "foo1" + } + }, + "spec": { + "containers": [ + { + "name": "foo1", + "image": " ", + "args": [ + "sleep", + "3600" + ], + "resources": {} + } + ], + "restartPolicy": "Always" + } + } + }, + "status": {} + }, + { + "kind": "ImageStream", + "apiVersion": "v1", + "metadata": { + "name": "foo1", + "creationTimestamp": null, + "labels": { + "io.kompose.service": "foo1" + } + }, + "spec": {}, + "status": { + "dockerImageRepository": "" + } + }, + { + "kind": "BuildConfig", + "apiVersion": "v1", + "metadata": { + "name": "foo1", + "creationTimestamp": null + }, + "spec": { + "triggers": [ + { + "type": "ConfigChange" + }, + { + "type": "ImageChange" + } + ], + "runPolicy": "Serial", + "source": { + "type": "Git", + "git": { + "uri": "%URI%", + "ref": "%REF%" + }, + "contextDir": "script/test/fixtures/buildargs/build/" + }, + "strategy": { + "type": "Docker", + "dockerStrategy": { + "env": [ + { + "name": "NAME", + "value": "web" + }, + { + "name": "foo", + "value": "bar" + } + ] + } + }, + "output": { + "to": { + "kind": "ImageStreamTag", + "name": "foo1:latest" + } + }, + "resources": {}, + "postCommit": {}, + "nodeSelector": null + }, + "status": { + "lastVersion": 0 + } + }, + { + "kind": "DeploymentConfig", + "apiVersion": "v1", + "metadata": { + "name": "foo", + "creationTimestamp": null, + "labels": { + "io.kompose.service": "foo" + } + }, + "spec": { + "strategy": { + "resources": {} + }, + "triggers": [ + { + "type": "ConfigChange" + }, + { + "type": "ImageChange", + "imageChangeParams": { + "automatic": true, + "containerNames": [ + "foo" + ], + "from": { + "kind": "ImageStreamTag", + "name": "foo:latest" + } + } + } + ], + "replicas": 1, + "test": false, + "selector": { + "io.kompose.service": "foo" + }, + "template": { + "metadata": { + "creationTimestamp": null, + "labels": { + "io.kompose.service": "foo" + } + }, + "spec": { + "containers": [ + { + "name": "foo", + "image": " ", + "args": [ + "sleep", + "3600" + ], + "resources": {} + } + ], + "restartPolicy": "Always" + } + } + }, + "status": {} + }, + { + "kind": "ImageStream", + "apiVersion": "v1", + "metadata": { + "name": "foo", + "creationTimestamp": null, + "labels": { + "io.kompose.service": "foo" + } + }, + "spec": {}, + "status": { + "dockerImageRepository": "" + } + }, + { + "kind": "BuildConfig", + "apiVersion": "v1", + "metadata": { + "name": "foo", + "creationTimestamp": null + }, + "spec": { + "triggers": [ + { + "type": "ConfigChange" + }, + { + "type": "ImageChange" + } + ], + "runPolicy": "Serial", + "source": { + "type": "Git", + "git": { + "uri": "%URI%", + "ref": "%REF%" + }, + "contextDir": "script/test/fixtures/buildargs/build/" + }, + "strategy": { + "type": "Docker", + "dockerStrategy": { + "env": [ + { + "name": "NAME", + "value": "web" + } + ] + } + }, + "output": { + "to": { + "kind": "ImageStreamTag", + "name": "foo:latest" + } + }, + "resources": {}, + "postCommit": {}, + "nodeSelector": null + }, + "status": { + "lastVersion": 0 + } + } + ] +}