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-05-16 12:19:00 +05:30
parent b877014380
commit 8fc262bd99
10 changed files with 385 additions and 11 deletions
+1
View File
@@ -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:""`
+1
View File
@@ -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
+8
View File
@@ -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{
+10 -5
View File
@@ -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.