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

[Exec] Allow expanding envs at tester runtime #110

Merged
merged 3 commits into from
Mar 18, 2021

Conversation

amwat
Copy link
Contributor

@amwat amwat commented Mar 12, 2021

ref: kubernetes/test-infra#21331

kubetest2 passes a bunch of info through envs to the tester, but typical inline bash prow jobs imply that they will be expanded during deployer invocation and not tester invocation. This especially affects the exec tester.

Might be possible to quote it in some fancy way, but instead opted to support it directly in the tester.

Covered only the most common use cases and is not intended to cover all shell special character shenanigans.

Previously

$ export ARTIFACTS=/tmp
$ kubetest2 noop --test=exec -- echo "$ARTIFACTS"
$ /tmp
$ kubetest2 noop --test=exec -- echo '$ARTIFACTS'
$ $ARTIFACTS

Now

$ export ARTIFACTS=/tmp
$ kubetest2 noop --test=exec -- echo "$ARTIFACTS"
$ /tmp
$ kubetest2 noop --test=exec -- echo '$ARTIFACTS'
$ /tmp/<kubetest2_run_id>
$ kubetest2 noop --test=exec -- echo '\$ARTIFACTS'
$ $ARTIFACTS

Note: while running on the command line users still need to keep in mind the shell expansion itself.

/cc @spiffxp @BenTheElder @MushuEE

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. approved Indicates a PR has been approved by an approver from all required OWNERS files. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Mar 12, 2021
@amwat
Copy link
Contributor Author

amwat commented Mar 16, 2021

ping

Comment on lines +73 to +77
if strings.Contains(arg, `\$`) {
expandedArgs[i] = strings.ReplaceAll(arg, `\$`, `$`)
} else {
expandedArgs[i] = os.ExpandEnv(arg)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if strings.Contains(arg, `\$`) {
expandedArgs[i] = strings.ReplaceAll(arg, `\$`, `$`)
} else {
expandedArgs[i] = os.ExpandEnv(arg)
}
if strings.Contains(arg, `\$`) {
arg = strings.ReplaceAll(arg, `\$`, `$`)
}
expandedArgs[i] = os.ExpandEnv(arg)

flows a little better imo

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we explicitly don't want to expand escaped $ hence the else?

@@ -64,8 +65,23 @@ func (t *Tester) Execute() error {
return t.Test()
}

func expandEnv(args []string) []string {
Copy link
Member

@BenTheElder BenTheElder Mar 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

usually an expandenv function supports shell syntax like --foo=${BAR}.

we should look at how kubernetes implements this for pods for ecosystem consistency

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's using os.ExpandEnv inside so this syntax also works?

@amwat
Copy link
Contributor Author

amwat commented Mar 17, 2021

Also note:

This can currently be worked around by spawning a subshell (and letting it do the evaluation) as the test command instead of the actual test command.

kubetest2 noop --test=exec -- sh -c 'echo $ARTIFACTS'

But I think this (expanding envs at tester runtime) is going to be the more widely required use case, so it's better if the exec tester itself supports this and we can avoid having subshells in all the configs.

Copy link
Member

@spiffxp spiffxp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/approve
/lgtm
The syntax in the description is pretty straightforward to me re: when does my env get expanded

I sorta wonder if this should happen consistently for all testers though? Maybe exec is the only one that needs it for now

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Mar 18, 2021
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: amwat, spiffxp

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot merged commit 4dca5cd into kubernetes-sigs:master Mar 18, 2021
@amwat amwat deleted the environment branch March 18, 2021 21:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants