-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
oc new-app/new-build/process: add --env-file/--param-file parameters #12164
Conversation
[test] |
@@ -82,6 +82,22 @@ os::cmd::expect_success_and_not_text 'oc new-app ruby-helloworld-sample --param | |||
os::cmd::expect_success_and_text 'oc new-app php PASS=one,two=three -o yaml' 'value: one,two=three' | |||
os::cmd::expect_success_and_not_text 'oc new-app php PASS=one,two=three -o yaml' 'no longer accepts comma-separated list' | |||
|
|||
# check that we can populate template parameters from file | |||
param_file="${OS_ROOT}/test/testdata/test-cmd-newapp-params.env" | |||
os::cmd::expect_success_and_text "oc new-app ruby-helloworld-sample --param-file ${param_file} -o yaml" 'value: thisisapassword' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use jsonpath
to query for the specific value you want to check instead of checking that some small string exists in the broader YAML output. See https://github.com/openshift/origin/blob/master/test/cmd/newapp.sh#L65
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea!
@bparees PTAL |
flake #8571 |
@@ -168,8 +165,12 @@ func NewCmdNewApplication(name, baseName string, f *clientcmd.Factory, out, erro | |||
cmd.Flags().StringSliceVarP(&config.TemplateFiles, "file", "f", config.TemplateFiles, "Path to a template file to use for the app.") | |||
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().StringVar(&config.TemplateParametersFile, "param-file", "", "File containing environment parameter values to set/override in the template.") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/environment//
env, duplicateEnv, envErrs := cmdutil.ParseEnvironmentArguments(c.Environment) | ||
for _, s := range duplicateEnv { | ||
fmt.Fprintf(c.ErrOut, "warning: The environment variable %q was overwritten", s) | ||
func parseAndCombine(what string, cmdline []string, file string, stdin io.Reader, errout io.Writer) (app.Environment, []error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
godoc
func ParseEnvironment(vals ...string) Environment { | ||
// ParseEnvironmentAllowEmpty converts the provided strings in key=value form | ||
// into environment entries. If case there's no equals sign in a string, it's | ||
// considered as a key with empty |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"considered as a key with empty value"
@@ -44,6 +70,22 @@ func (e Environment) Add(envs ...map[string]string) { | |||
} | |||
} | |||
|
|||
// Join adds the environment variables to the current environment. In case of | |||
// key confilct the old value is kept. Conflicting keys are returned as a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
conflict
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will edit go files with :set spell
from now.
@mmilata what was the ultimate resolution to the "how to handle multiline values in files" question? i see you're using a library, what does it support as far as multiline values? did @smarterclayton agree to it? |
@openshift/api-review needs review for new command line args (support providing a file for list of env/parameter key/values) and also agreement on the multiline value syntax supported within the files. |
@bparees Clayton suggested this one. It supports |
cool. (and thanks @smarterclayton) |
@@ -6,7 +6,7 @@ trap os::test::junit::reconcile_output EXIT | |||
( | |||
set +e | |||
oc delete all,templates --all | |||
oc delete user someval | |||
oc delete user someval someval=moreval someval=moreval2 someval=moreval3 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A test in that file creates these users but doesn't delete them thus you can't run that file twice in a row. Not related to the envfile feature at all.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i'm a little surprised "=" is even a valid character in a username, but ok cool.
@@ -88,6 +88,9 @@ os::cmd::expect_success_and_not_text "oc process -f '${required_params}' require | |||
os::cmd::expect_success_and_text "oc process -f '${required_params}' --value=required_param='`cat ${OS_ROOT}/test/testdata/multiline.txt`'" 'also,with=commas' | |||
os::cmd::expect_success_and_text "oc process -f '${required_params}' --param=required_param='`cat ${OS_ROOT}/test/testdata/multiline.txt`'" 'also,with=commas' | |||
|
|||
# set template parameters from parameter file with multiline values | |||
param_file="${OS_ROOT}/test/testdata/template_required_params.env" | |||
os::cmd::expect_success_and_text "oc process -f '${required_params}' --param-file=${param_file} -o yaml" 'first$' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
test from stdin?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i see it's covered in test/cmd/templates.sh
i wonder if we shouldn't combine test/cmd/templates.sh and test/cmd/process.sh, seems like we probably have overlap and it's not like process can do anything except work with templates.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, makes sense. Should I do that as part of this PR or open a new one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's just do it here.
@@ -168,8 +165,12 @@ func NewCmdNewApplication(name, baseName string, f *clientcmd.Factory, out, erro | |||
cmd.Flags().StringSliceVarP(&config.TemplateFiles, "file", "f", config.TemplateFiles, "Path to a template file to use for the app.") | |||
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().StringVar(&config.TemplateParametersFile, "param-file", "", "File containing parameter values to set/override in the template.") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Accept multiple files, i.e. StringArrayVar
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For all of these
@@ -168,8 +165,12 @@ func NewCmdNewApplication(name, baseName string, f *clientcmd.Factory, out, erro | |||
cmd.Flags().StringSliceVarP(&config.TemplateFiles, "file", "f", config.TemplateFiles, "Path to a template file to use for the app.") | |||
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().StringVar(&config.TemplateParametersFile, "param-file", "", "File containing parameter values to set/override in the template.") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For all of these
// | ||
//If "file" is "-" the file contents will be read from argument "stdin" (unless | ||
//it's nil). | ||
func parseAndCombine(what string, cmdline []string, filename string, stdin io.Reader, errout io.Writer) (app.Environment, []error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extract this into common library code (app or something even higher)
// Join adds the environment variables to the current environment. In case of | ||
// key conflict the old value is kept. Conflicting keys are returned as a | ||
// slice. | ||
func (e Environment) Join(more Environment) []string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably more consistent to call this AddIfNotPresent(...)
Ldap.
…On Wed, Dec 7, 2016 at 3:22 PM, Ben Parees ***@***.***> wrote:
***@***.**** commented on this pull request.
------------------------------
In test/cmd/process.sh <#12164>:
> @@ -6,7 +6,7 @@ trap os::test::junit::reconcile_output EXIT
(
set +e
oc delete all,templates --all
- oc delete user someval
+ oc delete user someval someval=moreval someval=moreval2 someval=moreval3
i'm a little surprised "=" is even a valid character in a username, but ok
cool.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#12164>, or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABG_p8autM8m4q_uEBNdArRCJIH7VQLzks5rFxWDgaJpZM4LF-vU>
.
|
Pushed version that addresses problems found in review so far. |
@openshift/cli-review ptal |
Flake #8571 & outdated man pages which are regenerated now. [test] |
Documentation: openshift/openshift-docs#3347 |
Still flake #8571. |
// ParseEnvironmentAllowEmpty converts the provided strings in key=value form | ||
// into environment entries. In case there's no equals sign in a string, it's | ||
// considered as a key with empty value. | ||
func ParseEnvironmentAllowEmpty(vals ...string) Environment { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In case there's no equals sign in a string, it's considered as a key with empty value
Is this only for docker compose?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes.
@@ -24,6 +30,26 @@ func ParseEnvironment(vals ...string) Environment { | |||
return env | |||
} | |||
|
|||
func ParseEnvironment(vals ...string) (Environment, []string, []error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like it would accepts invalid things for env keys. What happens if you do for example
--env="S P A C E S=test"
or have that pair in the file? Env vars are very strict about what's accepted for names, since you still have validArgumentEnvironment
, make use of it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also godoc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added IsValidArgumentEnvironment
check to ParseEnvironment
as well as LoadEnvironmentFile
because godotenv library allows such keys.
return env, nil | ||
} | ||
|
||
// ParseAndCombineEnvironment parses key-value records from the command line |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"... parses key=value records ..." for clarity.
errs = append(errs, fileErr) | ||
} | ||
|
||
duplicates = vars.AddIfNotPresent(fileVars) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make it clear in the godoc for this function that env pairs coming in the first argument takes precedence over the ones in files.
// | ||
// If a file is "-" the file contents will be read from argument stdin (unless | ||
// it's nil). | ||
func ParseAndCombineEnvironment(cmdline []string, filenames []string, stdin io.Reader, dupfn func(string, string) error) (Environment, []error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just call it env []string
or envs []string
instead of cmdline []string
, for proper separation of concerns. This function doesn't understand anything about cmd line stuff, and it's exported.
// If a file is "-" the file contents will be read from argument stdin (unless | ||
// it's nil). | ||
func ParseAndCombineEnvironment(cmdline []string, filenames []string, stdin io.Reader, dupfn func(string, string) error) (Environment, []error) { | ||
vars, duplicates, errs := ParseEnvironment(cmdline...) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
godoc should also mention that this function doesn't return parsing errors right away, but accumulates it with the ones returned by the provided duplicates function.
} | ||
|
||
for _, fname := range filenames { | ||
fileVars, fileErr := LoadEnvironmentFile(fname, stdin) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
godoc should also mention that it doesn't fail right away for errors loading the provided file. In fact, it feels like it should, if I do something completely wrong like --env-file=/nonexistent
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair enough, I'll change the function to return after encountering first error.
@@ -82,6 +82,20 @@ os::cmd::expect_success_and_not_text 'oc new-app ruby-helloworld-sample --param | |||
os::cmd::expect_success_and_text 'oc new-app php PASS=one,two=three -o yaml' 'value: one,two=three' | |||
os::cmd::expect_success_and_not_text 'oc new-app php PASS=one,two=three -o yaml' 'no longer accepts comma-separated list' | |||
|
|||
# check that we can populate template parameters from file | |||
param_file="${OS_ROOT}/test/testdata/test-cmd-newapp-params.env" | |||
os::cmd::expect_success_and_text "oc new-app ruby-helloworld-sample --param-file ${param_file} -o jsonpath='{.items[?(@.kind==\"DeploymentConfig\")].spec.template.spec.containers[0].env[?(@.name==\"MYSQL_PASSWORD\")].value}'" 'thisisapassword' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
--param-file
needs failure test cases. Things like
--param-file invalid
--param-file /path/to/empty-params-file
--param-file /path/to/malformed-params-file (keys with invalid chars like *&*$%&=test, lines without a KEY=VALUE pattern, etc)
--param-file /path/to/access-denied-file
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will add failure test cases. How can I cause access denied if the tests are run as root? Should empty file really cause an error?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How can I cause access denied if the tests are run as root?
Ok, I'm ok with skipping this one. :)
Should empty file really cause an error?
No, it's ok to just interpret that as "no env vars to set", but still should have a test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also
--param-file directory
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, passing a directory does nothing, silently. Opened joho/godotenv#22 and updated the branch with a workaround that Stats the file first.
|
||
# check that we can set environment variables from env file | ||
env_file="${OS_ROOT}/test/testdata/test-cmd-newapp-env.env" | ||
os::cmd::expect_success_and_text "oc new-app php --env-file ${env_file} -o jsonpath='{.items[?(@.kind==\"DeploymentConfig\")].spec.template.spec.containers[0].env[?(@.name==\"SOME_VAR\")].value}'" 'envvarfromfile' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same for --env-file
failure test cases.
Updated, PTAL @fabianofranz |
flake #8502 [test] |
still #8502 [test] |
flake #12235 [test] |
lgtm but needs a rebase. |
test/cmd/process.sh has been merged into test/cmd/templates.sh since they test the same functionality.
Evaluated for origin test up to 53e4af7 |
@bparees rebased. |
continuous-integration/openshift-jenkins/test SUCCESS (https://ci.openshift.redhat.com/jenkins/job/test_pr_origin/12372/) (Base Commit: 41702ed) |
[merge] |
Evaluated for origin merge up to 53e4af7 |
continuous-integration/openshift-jenkins/merge SUCCESS (https://ci.openshift.redhat.com/jenkins/job/test_pr_origin/12373/) (Base Commit: ec9ff39) (Image: devenv-rhel7_5544) |
Trello card: https://trello.com/c/BMuLvOny/968-5-supply-oc-new-app-and-oc-process-parameter-list-env-vars-via-a-file-app-creation
Follow-up of #10766