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

✨clusterctl: add CLUSTERCTL_LOG_LEVEL environment variable #2996

Merged
merged 1 commit into from
May 9, 2020

Conversation

cpanato
Copy link
Member

@cpanato cpanato commented May 3, 2020

What this PR does / why we need it:
add a CLUSTERCTL_LOG_LEVEL environment variable to be used instead of the -v.
if CLUSTERCTL_LOG_LEVEL is set it will override the flag -v

example, I did not have any running cluster, then I added some log output to make the example.
UPDATED with the new behavior

$ export CLUSTERCTL_LOG_LEVEL=5
$  ./bin/clusterctl config cluster my -v3
Info
info level 0
info level 1
info level 2
info level 3
Error: failed to identify the default infrastructure provider. Please specify an infrastructure provider: unsupported management cluster server version: v1.14.6 - minimum required version is v1.16.0

$ export CLUSTERCTL_LOG_LEVEL=5
$  ./bin/clusterctl config cluster my
Info
info level 0
info level 1
info level 2
info level 3
info level 4
info level 5
Error: failed to identify the default infrastructure provider. Please specify an infrastructure provider: unsupported management cluster server version: v1.14.6 - minimum required version is v1.16.0

and

$ unset CLUSTERCTL_LOG_LEVEL
$  ./bin/clusterctl config cluster my -v3
Info
info level 0
info level 1
info level 2
info level 3
Error: failed to identify the default infrastructure provider. Please specify an infrastructure provider: unsupported management cluster server version: v1.14.6 - minimum required version is v1.16.0

Which issue(s) this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close the issue(s) when PR gets merged):

/kind feature
cc @wfernandes

@k8s-ci-robot k8s-ci-robot added kind/feature Categorizes issue or PR as related to a new feature. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels May 3, 2020
@cpanato
Copy link
Member Author

cpanato commented May 3, 2020

/area clusterctl

@k8s-ci-robot k8s-ci-robot added the area/clusterctl Issues or PRs related to clusterctl label May 3, 2020
Copy link
Member

@fabriziopandini fabriziopandini left a comment

Choose a reason for hiding this comment

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

@cpanato thanks for this PR
In clusterctl flags takes precedence on env variables.
Is it possible to change this PR to comply with the above rule?

Also, in clusterctl we are allowing to use env variables or config variables by using a pluggable config component, while the management of this variable is totally detached from the rest of the config variables (e.g. this variable can't be set using the config file).
@wfernandes is this what you are expecting?

@cpanato
Copy link
Member Author

cpanato commented May 4, 2020

@fabriziopandini yes i can change that, also can you point to me this part of the config variables?

and i will wait as well from @wfernandes for some feedback before apply any changes because it might change everything :)

@fabriziopandini
Copy link
Member

the config is managed in this package https://github.com/kubernetes-sigs/cluster-api/tree/master/cmd/clusterctl/client/config
you might look for client (the package entry point) and variable_client

@cpanato
Copy link
Member Author

cpanato commented May 4, 2020

@fabriziopandini did one change for feedback and keep the old change in the previous commit for feedback

one thing that is weird and maybe because I'm doing something wrong is i tried to print the verbosity and it shows to me always 0 even when i set the -v3but the logs get the correct levels, i might doing something very wrong

verbosity := flag.CommandLine.Int("v", 0, "Set the log level verbosity.")
fmt.Println(*verbosity)

@wfernandes
Copy link
Contributor

@cpanato Thanks for this PR. I'll take a look at it by EOD today. Also in the above comment, I think you intended it for @fabriziopandini instead of fabianofranz.

@cpanato
Copy link
Member Author

cpanato commented May 4, 2020

Ok, figured out, the value is not available in the init function, only outside that.
added an initConfig fund to check the values and set from the config file or env var the log level

@cpanato
Copy link
Member Author

cpanato commented May 4, 2020

@wfernandes correct 😬 fixed

@wfernandes
Copy link
Contributor

Reviewing

Copy link
Contributor

@wfernandes wfernandes left a comment

Choose a reason for hiding this comment

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

This looks good to me. I agree with @fabriziopandini suggestions regarding log level priority. That is, flag value should take precedence.

The only other thing I'd like to add is documentation for this config variable. It could either be a new section in https://cluster-api.sigs.k8s.io/clusterctl/developers.html or https://cluster-api.sigs.k8s.io/clusterctl/configuration.html since it still could be useful to others and it pertains to clusterctl configuration.
I prefer the latter since we could just add a section about debugging/logging in clusterctl and mention the -v flag and then also mention the env var CLUSTERCTL_LOG_LEVEL. But I leave it up to you 🙂

cmd/clusterctl/cmd/root.go Outdated Show resolved Hide resolved
cmd/clusterctl/cmd/root.go Outdated Show resolved Hide resolved
cmd/clusterctl/cmd/root.go Outdated Show resolved Hide resolved
@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels May 6, 2020
@cpanato
Copy link
Member Author

cpanato commented May 6, 2020

@wfernandes applied the feedback changes you requested. PTAL when you have time

@cpanato
Copy link
Member Author

cpanato commented May 6, 2020

/test pull-cluster-api-capd-e2e

Copy link
Member

@fabriziopandini fabriziopandini left a comment

Choose a reason for hiding this comment

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

@cpanato thanks for this new version
Looks good to me, I will test it locally ASAP and report back

docs/book/src/clusterctl/configuration.md Outdated Show resolved Hide resolved
docs/book/src/clusterctl/configuration.md Outdated Show resolved Hide resolved
@cpanato cpanato force-pushed the GH-2957 branch 2 times, most recently from 55657de to e76f123 Compare May 6, 2020 14:50
@wfernandes
Copy link
Contributor

I ran this locally and it looks good to me. Thanks so much for doing this PR. I'm going to use the heck out of this feature 😄

I like the fact that I can set the config variable to a high number but then I can do something like clusterctl config cluster foo -v1 | kubectl apply -f - if I need to create workload clusters (for now).

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label May 6, 2020
@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label May 6, 2020
@wfernandes
Copy link
Contributor

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label May 6, 2020
@wfernandes
Copy link
Contributor

I'll leave approval for @fabriziopandini since he mentioned that he'd like to test it out.

Copy link

@jodavis-suse jodavis-suse left a comment

Choose a reason for hiding this comment

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

Looks really good. I had to get nitpicky to find anything to comment on. I wouldn't hold this commit back for any of my nits, just thought I would suggest them in case you wish to do another revision.

cmd/clusterctl/cmd/root.go Outdated Show resolved Hide resolved
cmd/clusterctl/cmd/root.go Outdated Show resolved Hide resolved
To have more verbose logs you can use the `-v` flag when running the `clusterctl` and set the level of the logging verbose from 0 to 5.

If you do not want to use the flag every time you issue a command you can set the environment variable `CLUSTERCTL_LOG_LEVEL` or set the variable in the `clusterctl` config file which is located by default at `$HOME/.cluster-api/clusterctl.yaml`.

Choose a reason for hiding this comment

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

It may be helpful to have a link to the book at https://cluster-api.sigs.k8s.io/clusterctl/configuration.html for more details.

Copy link
Member Author

Choose a reason for hiding this comment

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

not clear to me your comment, this piece of the documentation will be in the https://cluster-api.sigs.k8s.io/clusterctl/configuration.html page

Choose a reason for hiding this comment

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

Fair point, I should have paid attention to the path of the file. 😅

docs/book/src/clusterctl/configuration.md Outdated Show resolved Hide resolved
@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label May 7, 2020
Copy link
Member

@fabriziopandini fabriziopandini left a comment

Choose a reason for hiding this comment

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

Two small nits and then lgtm for me as well

cmd/clusterctl/cmd/root.go Outdated Show resolved Hide resolved
cmd/clusterctl/cmd/root.go Outdated Show resolved Hide resolved
if err == nil && v != "" {
verbosityFromEnv, err := strconv.Atoi(v)
if err != nil {
fmt.Printf("Failed to convert CLUSTERCTL_LOG_LEVEL string to an int. err=%s", err.Error())
Copy link
Contributor

Choose a reason for hiding this comment

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

Add a new line at the end and redirect to os.Stderr.

Suggested change
fmt.Printf("Failed to convert CLUSTERCTL_LOG_LEVEL string to an int. err=%s", err.Error())
fmt.Fprintf(os.Stderr, "Failed to convert CLUSTERCTL_LOG_LEVEL string to an int. err=%s\n", err.Error())

Copy link
Member Author

Choose a reason for hiding this comment

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

done

if -v is set it will override the CLUSTERCTL_LOG_LEVEL variable.
@cpanato
Copy link
Member Author

cpanato commented May 9, 2020

/test pull-cluster-api-e2e

1 similar comment
@cpanato
Copy link
Member Author

cpanato commented May 9, 2020

/test pull-cluster-api-e2e

@k8s-ci-robot
Copy link
Contributor

@cpanato: The following test failed, say /retest to rerun all failed tests:

Test name Commit Details Rerun command
pull-cluster-api-e2e 0ca8bdf link /test pull-cluster-api-e2e

Full PR test history. Your PR dashboard. Please help us cut down on flakes by linking to an open issue when you hit one in your PR.

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. I understand the commands that are listed here.

Copy link
Member

@fabriziopandini fabriziopandini 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

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

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: cpanato, fabriziopandini

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 May 9, 2020
@k8s-ci-robot k8s-ci-robot merged commit 98d4619 into kubernetes-sigs:master May 9, 2020
@cpanato cpanato deleted the GH-2957 branch May 9, 2020 20:41
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/clusterctl Issues or PRs related to clusterctl 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. 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.

Define config var for clusterctl log level
5 participants