Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set env vars on oc new-app of template #12048

Merged
merged 1 commit into from
Dec 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/man/man1/oc-new-app.1
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ If you provide source code, a new build will be automatically triggered. You can

.PP
\fB\-e\fP, \fB\-\-env\fP=[]
Specify a key\-value pair for an environment variable to set into each container. This doesn't apply to objects created from a template, use parameters instead.
Specify a key\-value pair for an environment variable to set into each container.

.PP
\fB\-f\fP, \fB\-\-file\fP=[]
Expand Down
2 changes: 1 addition & 1 deletion docs/man/man1/openshift-cli-new-app.1
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ If you provide source code, a new build will be automatically triggered. You can

.PP
\fB\-e\fP, \fB\-\-env\fP=[]
Specify a key\-value pair for an environment variable to set into each container. This doesn't apply to objects created from a template, use parameters instead.
Specify a key\-value pair for an environment variable to set into each container.

.PP
\fB\-f\fP, \fB\-\-file\fP=[]
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/cli/cmd/newapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func NewCmdNewApplication(name, baseName string, f *clientcmd.Factory, out, erro
cmd.MarkFlagFilename("file", "yaml", "yml", "json")
cmd.Flags().StringArrayVarP(&config.TemplateParameters, "param", "p", config.TemplateParameters, "Specify a key-value pair (e.g., -p FOO=BAR) to set/override a parameter value in the template.")
cmd.Flags().StringSliceVar(&config.Groups, "group", config.Groups, "Indicate components that should be grouped together as <comp1>+<comp2>.")
cmd.Flags().StringArrayVarP(&config.Environment, "env", "e", config.Environment, "Specify a key-value pair for an environment variable to set into each container. This doesn't apply to objects created from a template, use parameters instead.")
cmd.Flags().StringArrayVarP(&config.Environment, "env", "e", config.Environment, "Specify a key-value pair for an environment variable to set into each container.")
cmd.Flags().StringVar(&config.Name, "name", "", "Set name to use for generated application artifacts")
cmd.Flags().Var(&config.Strategy, "strategy", "Specify the build strategy to use if you don't want to detect (docker|pipeline|source).")
cmd.Flags().StringP("labels", "l", "", "Label to set in all resources for this application.")
Expand Down
23 changes: 20 additions & 3 deletions pkg/generate/app/cmd/newapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
kutilerrors "k8s.io/kubernetes/pkg/util/errors"

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

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

glog.V(4).Infof("processing template %s/%s", c.OriginNamespace, tpl.Name)
result, err := TransformTemplate(tpl, c.OSClient, c.OriginNamespace, environment)
result, err := TransformTemplate(tpl, c.OSClient, c.OriginNamespace, parameters)
if err != nil {
return name, nil, err
}
if len(name) == 0 {
name = tpl.Name
}
objects = append(objects, result.Objects...)
if len(result.Objects) > 0 {
// if environment variables were passed in, let's apply the environment variables
// to every pod template object
for i := range result.Objects {
podSpec, _, err := ometa.GetPodSpec(result.Objects[i])
if err == nil {
for ii := range podSpec.Containers {
if podSpec.Containers[ii].Env != nil {
podSpec.Containers[ii].Env = app.JoinEnvironment(environment.List(), podSpec.Containers[ii].Env)
} else {
podSpec.Containers[ii].Env = environment.List()
}
}
}
}
}

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

objects = app.AddServices(objects, false)

templateName, templateObjects, err := c.buildTemplates(components.TemplateComponentRefs(), app.Environment(parameters))
templateName, templateObjects, err := c.buildTemplates(components.TemplateComponentRefs(), app.Environment(parameters), app.Environment(environment))
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/generate/app/cmd/newapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func TestBuildTemplates(t *testing.T) {
t.Errorf("%s: Unexpected error: %v", n, err)
continue
}
_, _, err = appCfg.buildTemplates(components, app.Environment(parms))
_, _, err = appCfg.buildTemplates(components, app.Environment(parms), app.Environment(map[string]string{}))
if err != nil {
t.Errorf("%s: Unexpected error: %v", n, err)
}
Expand Down
2 changes: 2 additions & 0 deletions test/cmd/newapp.sh
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ os::cmd::expect_success_and_text 'oc new-app ruby-helloworld-sample -o yaml' 'MY
os::cmd::expect_success_and_text 'oc new-app ruby-helloworld-sample -o yaml' 'ADMIN_USERNAME'
os::cmd::expect_success_and_text 'oc new-app ruby-helloworld-sample -o yaml' 'ADMIN_PASSWORD'
os::cmd::expect_success_and_text 'oc new-app ruby-helloworld-sample --param MYSQL_PASSWORD=hello -o yaml' 'hello'
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$'
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'

# check that values are not split on commas
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'
Expand Down