Add build_args support in buildconfig

now args provided under build in docker-compose file can be available in buildconfig.
it solves #406
Added unit test and functional test
solves #445
Separated key:"value" pairs by spaces
This commit is contained in:
Suraj Narwade 2017-02-13 12:15:21 +05:30
parent b877014380
commit 8fc262bd99
10 changed files with 385 additions and 11 deletions

View File

@ -82,6 +82,7 @@ type ServiceConfig struct {
VolumesFrom []string `compose:"volumes_from" bundle:""` VolumesFrom []string `compose:"volumes_from" bundle:""`
ServiceType string `compose:"kompose.service.type" bundle:""` ServiceType string `compose:"kompose.service.type" bundle:""`
Build string `compose:"build" bundle:""` Build string `compose:"build" bundle:""`
BuildArgs map[string]*string `compose:"build-args" bundle:""`
ExposeService string `compose:"kompose.service.expose" bundle:""` ExposeService string `compose:"kompose.service.expose" bundle:""`
Stdin bool `compose:"stdin_open" bundle:""` Stdin bool `compose:"stdin_open" bundle:""`
Tty bool `compose:"tty" bundle:""` Tty bool `compose:"tty" bundle:""`

View File

@ -321,6 +321,7 @@ func (c *Compose) LoadFile(files []string) (kobject.KomposeObject, error) {
serviceConfig.Command = composeServiceConfig.Entrypoint serviceConfig.Command = composeServiceConfig.Entrypoint
serviceConfig.Args = composeServiceConfig.Command serviceConfig.Args = composeServiceConfig.Command
serviceConfig.Dockerfile = composeServiceConfig.Build.Dockerfile serviceConfig.Dockerfile = composeServiceConfig.Build.Dockerfile
serviceConfig.BuildArgs = composeServiceConfig.Build.Args
envs := loadEnvVars(composeServiceConfig.Environment) envs := loadEnvVars(composeServiceConfig.Environment)
serviceConfig.Environment = envs serviceConfig.Environment = envs

View File

@ -200,6 +200,13 @@ func (o *OpenShift) initImageStream(name string, service kobject.ServiceConfig,
// initBuildConfig initialize Openshifts BuildConfig Object // initBuildConfig initialize Openshifts BuildConfig Object
func initBuildConfig(name string, service kobject.ServiceConfig, repo string, branch string) (*buildapi.BuildConfig, error) { func initBuildConfig(name string, service kobject.ServiceConfig, repo string, branch string) (*buildapi.BuildConfig, error) {
contextDir, err := getAbsBuildContext(service.Build) 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 { if err != nil {
return nil, errors.Wrap(err, name+"buildconfig cannot be created due to error in creating build context, getAbsBuildContext failed") 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{ Strategy: buildapi.BuildStrategy{
DockerStrategy: &buildapi.DockerBuildStrategy{ DockerStrategy: &buildapi.DockerBuildStrategy{
DockerfilePath: service.Dockerfile, DockerfilePath: service.Dockerfile,
Env: envList,
}, },
}, },
Output: buildapi.BuildOutput{ Output: buildapi.BuildOutput{

View File

@ -17,13 +17,13 @@ limitations under the License.
package openshift package openshift
import ( import (
kapi "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/runtime"
"os" "os"
"path/filepath" "path/filepath"
"reflect"
"testing" "testing"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/runtime"
deployapi "github.com/openshift/origin/pkg/deploy/api" deployapi "github.com/openshift/origin/pkg/deploy/api"
"github.com/kubernetes-incubator/kompose/pkg/kobject" "github.com/kubernetes-incubator/kompose/pkg/kobject"
@ -37,7 +37,7 @@ func newServiceConfig() kobject.ServiceConfig {
ContainerName: "myfoobarname", ContainerName: "myfoobarname",
Image: "image", Image: "image",
Environment: []kobject.EnvVar{kobject.EnvVar{Name: "env", Value: "value"}}, 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"}, Command: []string{"cmd"},
WorkingDir: "dir", WorkingDir: "dir",
Args: []string{"arg1", "arg2"}, Args: []string{"arg1", "arg2"},
@ -288,9 +288,12 @@ func TestInitBuildConfig(t *testing.T) {
serviceName := "serviceA" serviceName := "serviceA"
repo := "https://git.test.com/org/repo" repo := "https://git.test.com/org/repo"
branch := "somebranch" branch := "somebranch"
buildArgs := []kapi.EnvVar{{Name: "name", Value: "value"}}
value := "value"
sc := kobject.ServiceConfig{ sc := kobject.ServiceConfig{
Build: filepath.Join(dir, "a/build"), Build: filepath.Join(dir, "a/build"),
Dockerfile: "Dockerfile-alternate", Dockerfile: "Dockerfile-alternate",
BuildArgs: map[string]*string{"name": &value},
} }
bc, err := initBuildConfig(serviceName, sc, repo, branch) bc, err := initBuildConfig(serviceName, sc, repo, branch)
if err != nil { 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 output name": {bc.Spec.CommonSpec.Output.To.Name, serviceName + ":latest"},
"Assert buildconfig dockerfilepath": {bc.Spec.CommonSpec.Strategy.DockerStrategy.DockerfilePath, "Dockerfile-alternate"}, "Assert buildconfig dockerfilepath": {bc.Spec.CommonSpec.Strategy.DockerStrategy.DockerfilePath, "Dockerfile-alternate"},
} }
for name, test := range testCases { for name, test := range testCases {
t.Log("Test case: ", name) t.Log("Test case: ", name)
if test.field != test.value { if test.field != test.value {
t.Errorf("Expected: %#v, got: %#v", test.value, test.field) 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. // TestServiceWithoutPort this tests if Headless Service is created for services without Port.

View File

@ -27,8 +27,6 @@ fi
# Warning Template # Warning Template
warning="Buildconfig using $uri::$branch as source." 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 # Tests related to docker-compose file in /script/test/fixtures/etherpad
@ -58,7 +56,11 @@ unset $(cat $KOMPOSE_ROOT/script/test/fixtures/gitlab/envs | cut -d'=' -f1)
# kubernetes test # 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" 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 # 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" 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 # Tests related to docker-compose file in /script/test/fixtures/entrypoint-command
# kubernetes test # kubernetes test
@ -209,6 +211,8 @@ convert::check_artifacts_generated "kompose -f $KOMPOSE_ROOT/script/test/fixture
#### ####
# Test regarding build context (running kompose from various directories) # 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) CURRENT_DIR=$(pwd)
cd "$KOMPOSE_ROOT/script/test/fixtures/nginx-node-redis/" 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" 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" 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" convert::expect_success_and_warning "kompose convert --provider openshift --stdout -j -f ../docker-compose.yml" "/tmp/output-os.json" "$warning"
cd $CURRENT_DIR 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 # Test related to support docker-compose.yaml beside docker-compose.yml
# Store the original path # Store the original path
@ -234,7 +247,4 @@ convert::expect_success "kompose --provider=openshift convert --stdout -j" "$KOM
# Return back to the original path # Return back to the original path
cd $CURRENT_DIR cd $CURRENT_DIR
# Removes generated output
rm -rf /tmp/output-os.json
exit $EXIT_STATUS exit $EXIT_STATUS

View File

@ -0,0 +1,11 @@
## Docker Compose Buildargs
### Usage
The simplest thing to do:
```bash
export $(cat envs)
```
To customize the values edit `envs` file.

View File

@ -0,0 +1,2 @@
FROM busybox
RUN touch /test

View File

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

1
script/test/fixtures/buildargs/envs vendored Normal file
View File

@ -0,0 +1 @@
foo=bar

View File

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