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

Fixed set env did not support keys with dot in it #98846

Merged
merged 1 commit into from
Mar 10, 2021

Conversation

lauchokyip
Copy link
Member

@lauchokyip lauchokyip commented Feb 7, 2021

What type of PR is this?

/kind bug

What this PR does / why we need it:
Fixed the bug that cannot set an environment variable name with a dot.
To reproduce:
kubectl set env deployment/app test.on=true.
An error will be thrown error: environment variables must be of the form key=value and can only contain letters, numbers, and underscores

Which issue(s) this PR fixes:

Fixes kubernetes/kubectl#1014

Special notes for your reviewer:
/cc @rikatz
/cc @soltysh
Very similar PR #48986 <- was referring to this

Does this PR introduce a user-facing change?:

NONE

Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:


@k8s-ci-robot k8s-ci-robot added release-note-none Denotes a PR that doesn't merit a release note. kind/bug Categorizes issue or PR as related to a bug. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Feb 7, 2021
@k8s-ci-robot
Copy link
Contributor

Hi @lauchokyip. Thanks for your PR.

I'm waiting for a kubernetes member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@k8s-ci-robot k8s-ci-robot added needs-priority Indicates a PR lacks a `priority/foo` label and requires one. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. area/kubectl sig/cli Categorizes an issue or PR as relevant to SIG CLI. and removed do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Feb 7, 2021
@lauchokyip
Copy link
Member Author

/cc @soltysh

@rikatz
Copy link
Contributor

rikatz commented Feb 7, 2021

/ok-to-test
/assign

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Feb 7, 2021
@lauchokyip
Copy link
Member Author

/retest

@rikatz
Copy link
Contributor

rikatz commented Feb 10, 2021

TBH I'm a bit confused of what should be the right behavior: accept or not the env var with ".".

If I try to export a variable with . in the name (like export LALA.BLABLA=1) shell will not allow me, because this is not a valid identifier.

If we allow this to be done with kubectl, can this lead any other component (kubelet, the container runtime, etc) into some wrong behavior?

I'll make some tests here

@rikatz
Copy link
Contributor

rikatz commented Feb 10, 2021

OK now I saw the referenced PR of the past that this is strict only in shells. Will make some further tests here and let you know

@lauchokyip
Copy link
Member Author

Yup @rikatz , I think that PR mentions K8s environment is different from shell environment. I tested in a deployment pod and added some test cases.

@rikatz
Copy link
Contributor

rikatz commented Feb 10, 2021

@lauchokyip thank you for the PR.

I'm taking a look into the 'parseIntoEnvVar' and I guess this could be a bit improved with your PR.

As an example, what I guess could work:

  1. Remove the case !IsValidEnvironmentArgument and let the case envSpec == "-" as the first case
  2. On the second case (strings.Contains(envSpec, "=") there's already a split there, separating the key from the Value. There, you can instead of use the IsValidEnvironmentArgument, use something like:
if !validation.IsEnvVarName(parts[0]) [....]

This validation is being used in other parts of the code (including the PR you've referenced before), like here: https://github.com/kubernetes/kubernetes/blob/master/staging/src/k8s.io/kubectl/pkg/cmd/util/env_file.go#L56-L60

This way, you can:

  • Remove all the hasChdirPrefix function (as this is already covered in validation.IsEnvVarName, I guess
  • Remove the first case, and move this validation to inside the code that deals with env vars with name=var

WDYT?

@lauchokyip
Copy link
Member Author

do we still want to check for strings.HasSuffix(envSpenc, "-")?

@lauchokyip
Copy link
Member Author

and should I include the dependency from https://github.com/kubernetes/kubernetes/blob/master/staging/src/k8s.io/kubectl/pkg/cmd/util/env_file.go#L56-L60, or copy the code inside?

@rikatz
Copy link
Contributor

rikatz commented Feb 16, 2021

and should I include the dependency from https://github.com/kubernetes/kubernetes/blob/master/staging/src/k8s.io/kubectl/pkg/cmd/util/env_file.go#L56-L60, or copy the code inside?

Only including the util is enough IMO.

About the suffix, I guess it would be good to keep the last case statement.

@rikatz
Copy link
Contributor

rikatz commented Feb 16, 2021

/triage accepted

@k8s-ci-robot k8s-ci-robot added the triage/accepted Indicates an issue or PR is ready to be actively worked on. label Feb 16, 2021
@lauchokyip
Copy link
Member Author

/retest

@rikatz
Copy link
Contributor

rikatz commented Feb 20, 2021

/lgtm

Thank you!

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

rikatz commented Feb 20, 2021

/priority important-longterm

@k8s-ci-robot k8s-ci-robot added priority/important-longterm Important over the long term, but may not be staffed and/or may need multiple releases to complete. and removed needs-priority Indicates a PR lacks a `priority/foo` label and requires one. labels Feb 20, 2021
@dougsland
Copy link
Member

/lgtm


// IsEnvironmentArgument checks whether a string is an environment argument, that is, whether it matches the "anycharacters=anycharacters" pattern.
func IsEnvironmentArgument(s string) bool {
return argumentEnvironment.MatchString(s)
}

// IsValidEnvironmentArgument checks whether a string is a valid environment argument, that is, whether it matches the "wordcharacters=anycharacters" pattern. Word characters can be letters, numbers, and underscores.
func IsValidEnvironmentArgument(s string) bool {
Copy link
Member

Choose a reason for hiding this comment

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

This is an exported function but it doesn't look like it's being used elsewhere? Probably not a breaking change?

https://cs.k8s.io/?q=IsValidEnvironmentArgument&i=nope&files=&excludeFiles=&repos=

@soltysh thoughts?

Copy link
Member

Choose a reason for hiding this comment

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

This shouldn't be an issue.

Copy link
Contributor

Choose a reason for hiding this comment

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

this is being removed anyway :D

Moved to call validation.IsEnvVarName which is already used by other parts of the code

Copy link
Contributor

@seans3 seans3 left a comment

Choose a reason for hiding this comment

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

Except for a small nit about import aliases, this looks good to me. I will approve this once import is updated.

@@ -23,23 +23,18 @@ import (
"regexp"
"strings"

"k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
Copy link
Contributor

Choose a reason for hiding this comment

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

This import alias is redundant and unnecessary. In the future, most code is aliasing this import as "corev1".

Copy link
Member

Choose a reason for hiding this comment

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

vscode does this trying to "help". Probably something we could get in the developer docs if it's not already there.

Copy link
Contributor

Choose a reason for hiding this comment

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

I get beat by vscode ALWAYS with the autoimport doing this :/

Copy link
Member Author

Choose a reason for hiding this comment

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

Oops

@seans3
Copy link
Contributor

seans3 commented Mar 9, 2021

/retest

@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Mar 9, 2021
@lauchokyip
Copy link
Member Author

@seans3 Fixed

@eddiezane
Copy link
Member

/lgtm

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

@seans3 seans3 left a comment

Choose a reason for hiding this comment

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

/lgtm

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: lauchokyip, rikatz, seans3

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 added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Mar 9, 2021
@seans3
Copy link
Contributor

seans3 commented Mar 9, 2021

/milestone v1.21

@k8s-ci-robot k8s-ci-robot added this to the v1.21 milestone Mar 9, 2021
@seans3
Copy link
Contributor

seans3 commented Mar 9, 2021

/retest

@k8s-ci-robot k8s-ci-robot merged commit 114c656 into kubernetes:master Mar 10, 2021
@lauchokyip lauchokyip deleted the fixsetenv branch March 14, 2021 14:25
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. area/kubectl cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/bug Categorizes issue or PR as related to a bug. lgtm "Looks good to me", indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. priority/important-longterm Important over the long term, but may not be staffed and/or may need multiple releases to complete. release-note-none Denotes a PR that doesn't merit a release note. sig/cli Categorizes an issue or PR as relevant to SIG CLI. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

"kubectl set env resource/name key=value" did not support keys with dot in it.
6 participants