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

Changed log.info to tidy up using fmt.sprintf #2246

Merged
merged 19 commits into from
Dec 9, 2019
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
### Removed

### Bug Fixes
- Changed watches.go to fix log.Info to modify message,and maxWorkers logic ([#2246](https://github.com/operator-framework/operator-sdk/pull/2246/files))
- Fix issue faced in the Ansible based operators when `jmespath` queries are used because it was not installed. ([#2252](https://github.com/operator-framework/operator-sdk/pull/2252))

## v0.12.0
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ require (
github.com/ziutek/mymysql v1.5.4 // indirect
go.uber.org/zap v1.10.0
golang.org/x/tools v0.0.0-20191018212557-ed542cd5b28a
google.golang.org/genproto v0.0.0-20181016170114-94acd270e44e // indirect
gopkg.in/gorp.v1 v1.7.2 // indirect
gopkg.in/yaml.v2 v2.2.4
k8s.io/api v0.0.0
Expand Down
19 changes: 8 additions & 11 deletions pkg/ansible/watches/watches.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,16 +272,13 @@ func getMaxWorkers(gvk schema.GroupVersionKind, defValue int) int {
"_",
-1,
))
maxWorkers, err := strconv.Atoi(os.Getenv(envVar))
if err != nil {
// we don't care why we couldn't parse it just use default
log.Info("Failed to parse %v from environment. Using default %v", envVar, defValue)
return defValue
}

if maxWorkers <= 0 {
log.Info("Value %v not valid. Using default %v", maxWorkers, defValue)
return defValue
maxWorkers := defValue
if val, ok := os.LookupEnv(envVar); ok {
if i, err := strconv.Atoi(val); err != nil {
log.Info(fmt.Sprintf("Unable to find a value for %v. Using default value for maxWorkers %d", envVar, defValue))
Copy link
Member

Choose a reason for hiding this comment

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

I could be wrong, but I think @camilamacedo86 was suggesting that we should log a separate message if we use the default when the environment variable is not set. I would also suggest we follow the structured logging conventions of the logr.Logger.

So here, we can use the following since we have encountered an error trying to parse the string as an integer.

log.Info("could not parse environment variable as an integer; using default value", "envVar", envVar, "default", defValue)

For @camilamacedo86's suggestion, if we do the os.LookupEnv() and ok == false, that means the environment variable was not set. In think in that case, we could log this message:

log.Info("environment variable not set; using default value", "envVar", envVar, "default", defValue)

@camilamacedo86 @jmrodri @bharathi-tenneti WDYT?

} else {
maxWorkers = i
}
}
return maxWorkers
}
Expand All @@ -297,7 +294,7 @@ func getAnsibleVerbosity(gvk schema.GroupVersionKind, defValue int) int {
))
ansibleVerbosity, err := strconv.Atoi(os.Getenv(envVar))
Copy link
Member

Choose a reason for hiding this comment

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

I think we should use the same logic for the ansible verbosity operator.

Since the logic is identical, I think it would make sense to define a new function with this signature:

func getIntegerEnvWithDefault(envVar string, defValue int) int

The call it for both maxWorkers and ansibleVerbosity.

if err != nil {
log.Info("Failed to parse %v from environment. Using default %v", envVar, defValue)
log.Info(fmt.Sprintf("Unable to find a value for %v. Using default value for the ansible verbosity %d", envVar, defValue))
return defValue
}

Expand Down