Skip to content

Commit 75ac180

Browse files
committed
set env vars on oc new-app of template
1 parent 44fc84b commit 75ac180

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

pkg/generate/app/cmd/newapp.go

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
kutilerrors "k8s.io/kubernetes/pkg/util/errors"
2323

2424
dockerfileparser "github.com/docker/docker/builder/dockerfile/parser"
25+
ometa "github.com/openshift/origin/pkg/api/meta"
2526
authapi "github.com/openshift/origin/pkg/authorization/api"
2627
buildapi "github.com/openshift/origin/pkg/build/api"
2728
buildutil "github.com/openshift/origin/pkg/build/util"
@@ -361,21 +362,37 @@ func (c *AppConfig) buildPipelines(components app.ComponentReferences, environme
361362
}
362363

363364
// buildTemplates converts a set of resolved, valid references into references to template objects.
364-
func (c *AppConfig) buildTemplates(components app.ComponentReferences, environment app.Environment) (string, []runtime.Object, error) {
365+
func (c *AppConfig) buildTemplates(components app.ComponentReferences, parameters app.Environment, environment app.Environment) (string, []runtime.Object, error) {
365366
objects := []runtime.Object{}
366367
name := ""
367368
for _, ref := range components {
368369
tpl := ref.Input().ResolvedMatch.Template
369370

370371
glog.V(4).Infof("processing template %s/%s", c.OriginNamespace, tpl.Name)
371-
result, err := TransformTemplate(tpl, c.OSClient, c.OriginNamespace, environment)
372+
result, err := TransformTemplate(tpl, c.OSClient, c.OriginNamespace, parameters)
372373
if err != nil {
373374
return name, nil, err
374375
}
375376
if len(name) == 0 {
376377
name = tpl.Name
377378
}
378379
objects = append(objects, result.Objects...)
380+
if len(result.Objects) > 0 {
381+
// if environment variables were passed in, let's apply the environment variables
382+
// to every pod template object
383+
for i := range result.Objects {
384+
podSpec, _, err := ometa.GetPodSpec(result.Objects[i])
385+
if err == nil {
386+
for ii := range podSpec.Containers {
387+
if podSpec.Containers[ii].Env != nil {
388+
podSpec.Containers[ii].Env = app.JoinEnvironment(environment.List(), podSpec.Containers[ii].Env)
389+
} else {
390+
podSpec.Containers[ii].Env = environment.List()
391+
}
392+
}
393+
}
394+
}
395+
}
379396

380397
DescribeGeneratedTemplate(c.Out, ref.Input().String(), result, c.OriginNamespace)
381398
}
@@ -666,7 +683,7 @@ func (c *AppConfig) Run() (*AppResult, error) {
666683

667684
objects = app.AddServices(objects, false)
668685

669-
templateName, templateObjects, err := c.buildTemplates(components.TemplateComponentRefs(), app.Environment(parameters))
686+
templateName, templateObjects, err := c.buildTemplates(components.TemplateComponentRefs(), app.Environment(parameters), app.Environment(environment))
670687
if err != nil {
671688
return nil, err
672689
}
@@ -710,6 +727,22 @@ func (c *AppConfig) Run() (*AppResult, error) {
710727
}
711728
}
712729

730+
/*
731+
if len(templateObjects) > 0 {
732+
for i := range objects {
733+
podSpec, _, err := ometa.GetPodSpec(objects[i])
734+
if err == nil {
735+
for ii := range podSpec.Containers {
736+
if podSpec.Containers[ii].Env != nil {
737+
podSpec.Containers[ii].Env = app.JoinEnvironment(podSpec.Containers[ii].Env, app.Environment(environment).List())
738+
} else {
739+
podSpec.Containers[ii].Env = app.Environment(environment).List()
740+
}
741+
}
742+
}
743+
}
744+
}
745+
*/
713746
return &AppResult{
714747
List: &kapi.List{Items: objects},
715748
Name: name,

pkg/generate/app/cmd/newapp_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ func TestBuildTemplates(t *testing.T) {
183183
t.Errorf("%s: Unexpected error: %v", n, err)
184184
continue
185185
}
186-
_, _, err = appCfg.buildTemplates(components, app.Environment(parms))
186+
_, _, err = appCfg.buildTemplates(components, app.Environment(parms), app.Environment(map[string]string{}))
187187
if err != nil {
188188
t.Errorf("%s: Unexpected error: %v", n, err)
189189
}

test/cmd/newapp.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ os::cmd::expect_success_and_text 'oc new-app ruby-helloworld-sample -o yaml' 'MY
6262
os::cmd::expect_success_and_text 'oc new-app ruby-helloworld-sample -o yaml' 'ADMIN_USERNAME'
6363
os::cmd::expect_success_and_text 'oc new-app ruby-helloworld-sample -o yaml' 'ADMIN_PASSWORD'
6464
os::cmd::expect_success_and_text 'oc new-app ruby-helloworld-sample --param MYSQL_PASSWORD=hello -o yaml' 'hello'
65+
os::cmd::expect_success_and_text 'oc new-app -e FOO=BAR -f examples/jenkins/jenkins-ephemeral-template.json -o jsonpath="{.items[?(@.kind==\"DeploymentConfig\")].spec.template.spec.containers[0].env[?(@.name==\"FOO\")].value}" ' 'BAR'
66+
os::cmd::expect_success_and_text 'oc new-app -e OPENSHIFT_ENABLE_OAUTH=false -f examples/jenkins/jenkins-ephemeral-template.json -o jsonpath="{.items[?(@.kind==\"DeploymentConfig\")].spec.template.spec.containers[0].env[?(@.name==\"OPENSHIFT_ENABLE_OAUTH\")].value}" ' 'false'
6567

6668
# check that values are not split on commas
6769
os::cmd::expect_success_and_text 'oc new-app ruby-helloworld-sample --param MYSQL_PASSWORD=hello,MYSQL_USER=fail -o yaml' 'value: hello,MYSQL_USER=fail'

0 commit comments

Comments
 (0)