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

Added support for multiple --from-env flags #104232

Merged
merged 1 commit into from Aug 24, 2021

Conversation

lauchokyip
Copy link
Member

What type of PR is this?

/kind feature

What this PR does / why we need it:

Added support for multiple --from-env flags

Which issue(s) this PR fixes:

Fixes kubernetes/kubectl#1094

Special notes for your reviewer:

/cc @KnVerey
/cc @eddiezane

Does this PR introduce a user-facing change?

Adding support for multiple --from-env-file flags

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

None

@k8s-ci-robot k8s-ci-robot added release-note Denotes a PR that will be considered when it comes time to generate release notes. kind/feature Categorizes issue or PR as related to a new feature. size/L Denotes a PR that changes 100-499 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. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. 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 Aug 9, 2021
t.Errorf("test %s was expecting an error but no error occurred", name)
if test.expectErr != "" && err == nil {
t.Errorf("was expecting an error but no error occurred")
return
Copy link
Member Author

Choose a reason for hiding this comment

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

Added return because without it if the test fails it will throw a panic for the next line when we want to dereference the err err.Error() but the err will be nil. Logically, it makes sense to have return there because we want to move on to the next test cases if the current fail.

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree that only one of the four conditionals here is intended to apply to any given case. The same would be true for the configmap test changes too, right?

FWIW I like using testify's require for this, e.g.:

if test.expectErr != "" {
  require.EqualError(t, test.expectErr, err.Error())
} 
require.NoError(t, err)
if !apiequality.Semantic.DeepEqual ... {
}

It looks like testify is already used in other create tests, so that could be an option, if you want (up to you).

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, didn't know that exists 👍🏼

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually I think I didn't explain it well enough,
For example if I have a test case

		"foo": {
			secretName:  "foo",
			fromLiteral:    []string{"key1=value1"},
			expectErr:   `foo`,
		},

when it will first reach this line, which is fine because we are expecting an error and the err is nil

if test.expectErr != "" && err == nil

But when it reaches the next line, err.Error() will cause a panic because err is nil.

if test.expectErr != "" && test.expectErr != err.Error() {

If I do,

if test.expectErr != "" {
  require.EqualError(t, test.expectErr, err.Error())
} 
require.NoError(t, err)
if !apiequality.Semantic.DeepEqual ... {
}

woudln't require.EqualError(t, test.expectErr, err.Error()) cause panic too if err is nil?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah yes, you're right. You'd need an additional require.Error(t, err) as the second line.

Copy link
Member Author

Choose a reason for hiding this comment

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

If that's the case, do you think require is better or return? Personally, I think return would make the code cleaner.

Copy link
Contributor

Choose a reason for hiding this comment

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

I find the require version more succinct/readable, but it's a personal preference. I'll LGTM either way once the error wording fix is pushed.

Copy link
Member Author

Choose a reason for hiding this comment

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

sure! let's go with require then, I will play around with it and make an update. Thank you

@lauchokyip
Copy link
Member Author

/triage accepted
/priority backlog

@k8s-ci-robot k8s-ci-robot added triage/accepted Indicates an issue or PR is ready to be actively worked on. priority/backlog Higher priority than priority/awaiting-more-evidence. and removed needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. labels Aug 9, 2021
@knight42
Copy link
Member

/cc

t.Errorf("test %s was expecting an error but no error occurred", name)
if test.expectErr != "" && err == nil {
t.Errorf("was expecting an error but no error occurred")
return
Copy link
Contributor

Choose a reason for hiding this comment

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

I agree that only one of the four conditionals here is intended to apply to any given case. The same would be true for the configmap test changes too, right?

FWIW I like using testify's require for this, e.g.:

if test.expectErr != "" {
  require.EqualError(t, test.expectErr, err.Error())
} 
require.NoError(t, err)
if !apiequality.Semantic.DeepEqual ... {
}

It looks like testify is already used in other create tests, so that could be an option, if you want (up to you).

}
}
if info.IsDir() {
return fmt.Errorf("env config file cannot be a directory")
Copy link
Contributor

Choose a reason for hiding this comment

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

This error message changed slightly. Was it intentional or ported over from the configmap code?

Suggested change
return fmt.Errorf("env config file cannot be a directory")
return fmt.Errorf("env secret file cannot be a directory")

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for the quick review! I ported the code from configmap and made the careless mistake 😅 , will make changes now

@lauchokyip
Copy link
Member Author

/assign @eddiezane

Copy link
Contributor

@KnVerey KnVerey left a comment

Choose a reason for hiding this comment

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

/lgtm

if !test.expectErr && err != nil {
t.Errorf("test %s, unexpected error: %v", name, err)
if test.expectErr == "" && err != nil {
t.Errorf("unexpected error: %v", err)
Copy link
Contributor

Choose a reason for hiding this comment

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

If you want you can simplify this one slightly with require as well, and have it return instead of attempt to run the DeepEqual if the assertion fails. My full suggestion is below but this is also fine as-is.

if test.expectErr == "" {
  require.NoError(err)
  if !apiequality.Semantic.DeepEqual(secret, test.expected) {
    t.Errorf("\nexpected:\n%#v\ngot:\n%#v", test.expected, secret)
  }
} else {
  require.Error(t, err)
  require.EqualError(t, err, test.expectErr)
}

Copy link
Member Author

Choose a reason for hiding this comment

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

Let's simplify it then, I think this change is better ! will make a change now

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

Thank you @KnVerey for your thoughtful reviews!

@KnVerey
Copy link
Contributor

KnVerey commented Aug 17, 2021

/lgtm

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

@apelisse apelisse left a comment

Choose a reason for hiding this comment

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

That seems to only be changing secrets, does that already work for ConfigMaps?

Comment on lines +389 to +391
err = cmdutil.AddFromEnvFile(envFileSource, func(key, value string) error {
return addKeyFromLiteralToSecret(secret, key, []byte(value))
})
Copy link
Member

Choose a reason for hiding this comment

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

What do you do if you have duplicate keys? Is this properly handled by this function? Specifically if they are coming from different files with the same name in different directories?

Copy link
Member Author

Choose a reason for hiding this comment

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

@eddiezane actually asked the same questions 😄 and @KnVerey answered it over here (#101646 (comment))

Second one is rejected. Regardless of original source, the entries are added with addKeyFromLiteralToConfigMap, which as documented "return[s] an error if the key is not valid or if the key already exists." This behavior didn't need to change from the original single-file implementation, since it was already a possibility there.

@lauchokyip
Copy link
Member Author

That seems to only be changing secrets, does that already work for ConfigMaps?

Yea there was a PR merge for ConfigMap #101646

Copy link
Member

@eddiezane eddiezane left a comment

Choose a reason for hiding this comment

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

Thanks for this! One very small nitpick.

/approve
/hold

@@ -21,6 +21,7 @@ import (
"os"
"testing"

"github.com/stretchr/testify/require"
Copy link
Member

Choose a reason for hiding this comment

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

nit: Add a line here please. Convention is:

built in

3rd party

k8s

@k8s-ci-robot k8s-ci-robot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Aug 23, 2021
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: eddiezane, lauchokyip

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 Aug 23, 2021
@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Aug 23, 2021
@pacoxu pacoxu added this to In progress in SIG CLI Aug 24, 2021
@eddiezane
Copy link
Member

/lgtm
/hold cancel

@k8s-ci-robot k8s-ci-robot added lgtm "Looks good to me", indicates that a PR is ready to be merged. and removed do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. labels Aug 24, 2021
@k8s-ci-robot k8s-ci-robot merged commit 5f0180e into kubernetes:master Aug 24, 2021
SIG CLI automation moved this from In progress to Done Aug 24, 2021
@k8s-ci-robot k8s-ci-robot added this to the v1.23 milestone Aug 24, 2021
@lauchokyip lauchokyip deleted the fixSecret branch September 24, 2021 04:12
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/feature Categorizes issue or PR as related to a new feature. lgtm "Looks good to me", indicates that a PR is ready to be merged. priority/backlog Higher priority than priority/awaiting-more-evidence. release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/cli Categorizes an issue or PR as relevant to SIG CLI. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
Development

Successfully merging this pull request may close these issues.

kubectl create secret: Support multiple --from-env-file flags
6 participants