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 #101646

Merged
merged 1 commit into from Aug 5, 2021

Conversation

lauchokyip
Copy link
Member

@lauchokyip lauchokyip commented Apr 30, 2021

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#1033

Special notes for your reviewer:

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.:


@k8s-ci-robot k8s-ci-robot added release-note-none Denotes a PR that doesn't merit a release note. 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. labels Apr 30, 2021
@k8s-ci-robot
Copy link
Contributor

@lauchokyip: This issue is currently awaiting triage.

If a SIG or subproject determines this is a relevant issue, they will accept it by applying the triage/accepted label and provide further guidance.

The triage/accepted label can be added by org members by writing /triage accepted in a comment.

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-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 Apr 30, 2021
@k8s-ci-robot k8s-ci-robot added 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 Apr 30, 2021
@lauchokyip
Copy link
Member Author

/cc @KnVerey

@@ -429,20 +476,28 @@ func TestCreateConfigMap(t *testing.T) {
}
}

func setupEnvFile(lines ...string) func(*testing.T, *ConfigMapOptions) func() {
func setupEnvFile(filenames []string, lines [][]string) func(*testing.T, *ConfigMapOptions) func() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do the tests actually need to control the filenames? If not, I'd suggest simply including the index in the filename below, to simplify setup. If they do, I'd suggest making the argument filemap map[string][]string (e.g. "filename": {"l1", "l2"} so that it's easier to understand what's going where.

Copy link
Member Author

Choose a reason for hiding this comment

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

no, it's no needed 😕

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.

Great work! I have a few small questions/quibbles, but I tried it out locally and it is working nicely.

@@ -87,7 +87,7 @@ type ConfigMapOptions struct {
// LiteralSources to derive the configMap from (optional)
LiteralSources []string
// EnvFileSource to derive the configMap from (optional)
EnvFileSource string
EnvFileSource []string
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's change the variable/method naming to be plural all around, like it is for the other sources.

Suggested change
EnvFileSource []string
EnvFileSources []string

return fmt.Errorf("error reading %s: %v", envFileSource, err.Err)
default:
return fmt.Errorf("error reading %s: %v", envFileSource, err)
func handleConfigMapFromEnvFileSource(configMap *corev1.ConfigMap, envFileSources []string) error {
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
func handleConfigMapFromEnvFileSource(configMap *corev1.ConfigMap, envFileSources []string) error {
func handleConfigMapFromEnvFileSources(configMap *corev1.ConfigMap, envFileSources []string) error {

@@ -414,8 +458,11 @@ func TestCreateConfigMap(t *testing.T) {
defer teardown()
}
}
err := configMapOptions.Validate()
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this added?

Copy link
Member Author

Choose a reason for hiding this comment

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

I was the one that remove the generator of the ConfigMap (https://github.com/pulls?q=is%3Apr+author%3Alauchokyip+archived%3Afalse+is%3Aclosed) and when I wrote the test cases for example this test cases

"create_invalid_configmap_too_many_args": {
I forgot that Validate() would actually catch the error here
if len(o.EnvFileSource) > 0 && (len(o.FileSources) > 0 || len(o.LiteralSources) > 0) {
so I think we have to add that now. Same as what I did for create_secret_test.go

Copy link
Contributor

Choose a reason for hiding this comment

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

What I still don't understand is why this PR makes this change necessary. If the errors the cases you point to are meant to hit are in Validate and only createConfigMap is being called on master today, then how are the tests passing on master? Are they hitting errors other than the ones they're meant to? Incidentally, that possibility is one reason why I personally prefer to make error expectations strings to match on rather than booleans, to provide better assurances that the error that happens is the one I expect.

To be clear, I'm not opposed to this change--just want to make sure I understand it.

Copy link
Member Author

Choose a reason for hiding this comment

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

Will take a look into it

Copy link
Member Author

Choose a reason for hiding this comment

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

Turn out the error happened during createConfigMap
For instance the test case create_invalid_configmap_too_many_args, instead of throwing the error (from-env-file cannot be combined with from-file or from-literal) it actually tries to create a config map from the file and it cannot read the file therefore throws an error

I agree comparing string error is way better so I will make a change from that

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for digging into this, and great job with the improvements to this file!

Copy link
Contributor

Choose a reason for hiding this comment

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

@lauchokyip your latest rebase seems to have lost the changes from this discussion, i.e. the ones visible in https://github.com/kubernetes/kubernetes/compare/cc37d5df54caaaa50291d37c93042d73ea506778..977ca6feb4fe433e667df051dfa4b4c21e4fce02. Can you please restore them? Note that one of the error strings that shows up in that diff answers @eddiezane question. 😄

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes I am restoring them! Thanks for the reminder 😃

@lauchokyip lauchokyip force-pushed the fixConfigMap branch 2 times, most recently from e92e84b to e00ee11 Compare May 19, 2021 14:42
@lauchokyip
Copy link
Member Author

/retest

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

Choose a reason for hiding this comment

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

removed the name because if the unit test fails it would already output the name so in my opinion it's repetitive and confusing

@lauchokyip
Copy link
Member Author

/assign @eddiezane

@@ -86,8 +86,8 @@ type ConfigMapOptions struct {
FileSources []string
// LiteralSources to derive the configMap from (optional)
LiteralSources []string
// EnvFileSource to derive the configMap from (optional)
EnvFileSource string
Copy link
Member

Choose a reason for hiding this comment

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

This struct and field are exported. This will be a breaking change to any external consumers. Not sure if there are any 🤔.

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

Copy link
Member

Choose a reason for hiding this comment

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

I guess with our refactoring of the commands anyways it doesn't matter.

Copy link
Contributor

Choose a reason for hiding this comment

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

My instinct is that the flags used to build a CLI are particular to that CLI itself, and not intended for library use. That search isn't really showing any consumers in any case; Kustomize's implementation matches but is independent, and driver-registrar is just showing up as vendored, not used. The rest is this code itself and its mirror. So IMO there's no problem with this change, but we can check with other approvers if you're worried about it. :)

@k8s-ci-robot k8s-ci-robot added release-note Denotes a PR that will be considered when it comes time to generate release notes. and removed release-note-none Denotes a PR that doesn't merit a release note. labels May 27, 2021
expectErr bool
expected *corev1.ConfigMap
expectErr bool
expectErrStr string
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd suggest consolidating into only the string field. We should never expect a blank error, so the boolean is kinda redundant and needs to be synchronized (e.g. right now you could set an error string but forget to set expectErr to true and it wouldn't get checked).

Copy link
Member Author

Choose a reason for hiding this comment

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

makes sense

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

@@ -86,8 +86,8 @@ type ConfigMapOptions struct {
FileSources []string
// LiteralSources to derive the configMap from (optional)
LiteralSources []string
// EnvFileSource to derive the configMap from (optional)
EnvFileSource string
Copy link
Contributor

Choose a reason for hiding this comment

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

My instinct is that the flags used to build a CLI are particular to that CLI itself, and not intended for library use. That search isn't really showing any consumers in any case; Kustomize's implementation matches but is independent, and driver-registrar is just showing up as vendored, not used. The rest is this code itself and its mirror. So IMO there's no problem with this change, but we can check with other approvers if you're worried about it. :)

@@ -414,8 +458,11 @@ func TestCreateConfigMap(t *testing.T) {
defer teardown()
}
}
err := configMapOptions.Validate()
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for digging into this, and great job with the improvements to this file!

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jul 9, 2021
@eddiezane
Copy link
Member

@lauchokyip sorry I missed this. Please rebase when you get a chance and we can get merged after code thaw.

@k8s-ci-robot k8s-ci-robot removed lgtm "Looks good to me", indicates that a PR is ready to be merged. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. labels Jul 18, 2021
@lauchokyip
Copy link
Member Author

@eddiezane Rebased. PTAL 😄

@lauchokyip lauchokyip requested a review from KnVerey July 18, 2021 15:16
@eddiezane
Copy link
Member

Thanks!

What's the intended behavior for duplicate entries? Second one overrides the first?

@KnVerey
Copy link
Contributor

KnVerey commented Jul 19, 2021

What's the intended behavior for duplicate entries? Second one overrides the first?

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.

@KnVerey
Copy link
Contributor

KnVerey commented Jul 21, 2021

In case my comment is hard to find, I noticed yesterday that the latest force push seems to have lost some of the previous requested changes: #101646 (comment)

@lauchokyip lauchokyip force-pushed the fixConfigMap branch 2 times, most recently from 15dbb42 to 8b91b8c Compare July 25, 2021 19:37
@lauchokyip
Copy link
Member Author

lauchokyip commented Jul 25, 2021

@KnVerey and @eddiezane , I restored the previous changes, PTAL, thank you 👍🏼

@finnickkim
Copy link

  • [ ]

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.

Just a tiny request to revert an additional change that slipped in, otherwise looks good!

@@ -68,8 +68,8 @@ var (
# Create a new config map named my-config from the key=value pairs in the file
kubectl create configmap my-config --from-file=path/to/bar

# Create a new config map named my-config from an env file
kubectl create configmap my-config --from-env-file=path/to/bar.env`))
# Create a new configmap named my-config from an env file
Copy link
Contributor

Choose a reason for hiding this comment

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

Please revert this change--it's spelled as two words in all the other help text above

Copy link
Member Author

Choose a reason for hiding this comment

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

Will do! Thanks for the fast review!

@lauchokyip
Copy link
Member Author

/retest

@lauchokyip
Copy link
Member Author

/retest-required

@KnVerey
Copy link
Contributor

KnVerey commented Jul 28, 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 Jul 28, 2021
@eddiezane
Copy link
Member

Great job team! Awesome work on this one.

/approve

@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 Jul 28, 2021
@k8s-ci-robot k8s-ci-robot merged commit 0d99701 into kubernetes:master Aug 5, 2021
SIG CLI automation moved this from Needs Approver to Done Aug 5, 2021
@k8s-ci-robot k8s-ci-robot added this to the v1.23 milestone Aug 5, 2021
@lauchokyip lauchokyip deleted the fixConfigMap branch August 17, 2021 23:19
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. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. 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.
Projects
Development

Successfully merging this pull request may close these issues.

kubectl create configmap: Support multiple --from-env-file flags
5 participants