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

Integrating golangci-lint #1179

Merged
merged 1 commit into from
Oct 27, 2020
Merged

Integrating golangci-lint #1179

merged 1 commit into from
Oct 27, 2020

Conversation

wilsonehusin
Copy link
Contributor

@wilsonehusin wilsonehusin commented Oct 20, 2020

What this PR does / why we need it:
Repository housecleaning.

Which issue(s) this PR fixes
Partially addresses #1176, baby steps towards #1178

Special notes for your reviewer:
Seems like GitHub is not running the actions on this PR as vmware-tanzu, but it's available on my fork

Release note:

NONE

@wilsonehusin wilsonehusin marked this pull request as ready for review October 20, 2020 23:04
Copy link

@vladimirvivien vladimirvivien left a comment

Choose a reason for hiding this comment

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

This is a great first start. Let's discuss further.

@@ -279,7 +278,7 @@ func (g *genFlags) resolveConfig() *config.Config {
// Set these values as if the user had requested the defaults.
if g.genflags != nil {
for _, v := range conf.PluginSelections {
g.genflags.Lookup("plugin").Value.Set(v.Name)
g.genflags.Lookup("plugin").Value.Set(v.Name) //nolint:errcheck

Choose a reason for hiding this comment

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

Is //nolint:errcheck skipping this as an error check ? I see several of these. It may be worth it to actually fix these in another pass instead of having that comment all over the place.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

//nolint:errcheck will tell golangci-lint to not mark this as errcheck lint failure. the only resolutions I can think of are:

// practically silencing error
_ = g.genflags.Lookup("plugin").Value.Set(v.Name)

or

// potentially changing behavior, because previously we ignored this error if they exist
err := g.genflags.Lookup("plugin").Value.Set(v.Name)
if err != nil {
	...
}

is there other alternatives?

Choose a reason for hiding this comment

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

I think there may be one more option. Looking at the doc for errcheck linter, it supports an external file where you can put package, functions, methods to skip.

Take a look here https://github.com/kisielk/errcheck#excluding-functions

fmt.Println(fmt.Sprintf("MinimumKubeVersion: %s", buildinfo.MinimumKubeVersion))
fmt.Println(fmt.Sprintf("MaximumKubeVersion: %s", buildinfo.MaximumKubeVersion))
fmt.Println(fmt.Sprintf("GitSHA: %s", buildinfo.GitSHA))
fmt.Printf("Sonobuoy Version: %s\n", buildinfo.Version)

Choose a reason for hiding this comment

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

Good catch here.

@@ -121,6 +121,12 @@ func (c *SonobuoyClient) RunManifest(cfg *RunConfig, manifest []byte) error {
seenStatus = true
}

// if nil below was added for coverage on staticcheck
// TODO: ensure this is the desired behavior
if status == nil {

Choose a reason for hiding this comment

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

Let's discuss this further. Agreed we need to understand this is not adding new side effects.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

so after taking another look, it seems that in scenario where status is nil, previous behavior would have runtime de-referencing error on switch{}. so prooooobably ok to have a more channeled error delivery?

@@ -55,7 +55,7 @@ func LoadConfig() (*Config, error) {
}
defer jsonFile.Close()

b, err := ioutil.ReadAll(jsonFile)
b, _ := ioutil.ReadAll(jsonFile)

Choose a reason for hiding this comment

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

Error handling: the error should not be silenced here.

Choose a reason for hiding this comment

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

Same thing here, let's add the error handling logic needed. Probably should add something like this

b, err := ioutil.ReadAll(jsonFile)
if err != nil {
   return nil, errors.Wrap(err, "read config")
}

// Only set values if they have values greater than 0 (as in they user specified).
limitBytes := podLogLimits.SizeLimitBytes(0)
sinceSeconds := int64(podLogLimits.TimeLimitDuration(0) / time.Second)
limitBytes := podLogLimits.SizeLimitBytes(0) //nolint:staticcheck

Choose a reason for hiding this comment

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

This is good for now. Lets investigate what would satisfy this check and if it requires package upgrade then we can handle that in a different PR.

Copy link
Contributor Author

@wilsonehusin wilsonehusin left a comment

Choose a reason for hiding this comment

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

some clarifications on changes and responses. I've been defaulting to silencing previously unhandled errors to preserve behavior, but I agree with the general sentiment that we should ensure they are handled appropriately -- whether to ignore (mark as insignificant) or surface it to user.

@@ -279,7 +278,7 @@ func (g *genFlags) resolveConfig() *config.Config {
// Set these values as if the user had requested the defaults.
if g.genflags != nil {
for _, v := range conf.PluginSelections {
g.genflags.Lookup("plugin").Value.Set(v.Name)
g.genflags.Lookup("plugin").Value.Set(v.Name) //nolint:errcheck
Copy link
Contributor Author

Choose a reason for hiding this comment

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

//nolint:errcheck will tell golangci-lint to not mark this as errcheck lint failure. the only resolutions I can think of are:

// practically silencing error
_ = g.genflags.Lookup("plugin").Value.Set(v.Name)

or

// potentially changing behavior, because previously we ignored this error if they exist
err := g.genflags.Lookup("plugin").Value.Set(v.Name)
if err != nil {
	...
}

is there other alternatives?

Copy link

@vladimirvivien vladimirvivien left a comment

Choose a reason for hiding this comment

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

Looks good. Left a link for you to check out for the errcheck linter

@@ -279,7 +278,7 @@ func (g *genFlags) resolveConfig() *config.Config {
// Set these values as if the user had requested the defaults.
if g.genflags != nil {
for _, v := range conf.PluginSelections {
g.genflags.Lookup("plugin").Value.Set(v.Name)
g.genflags.Lookup("plugin").Value.Set(v.Name) //nolint:errcheck

Choose a reason for hiding this comment

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

I think there may be one more option. Looking at the doc for errcheck linter, it supports an external file where you can put package, functions, methods to skip.

Take a look here https://github.com/kisielk/errcheck#excluding-functions

Copy link

@vladimirvivien vladimirvivien left a comment

Choose a reason for hiding this comment

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

Ok, added some comments about error handling in several places.

err := runGather(false)
// staticcheck somehow flags err as unused when it's actually returned
// at the end of this function
err := runGather(false) //nolint:staticcheck

Choose a reason for hiding this comment

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

For this one, the static analysis is correct, one of the switch case sleeps forever. So, add a err handling block right after the rungGather call

if err := runGather(flase); err != nil {
   return err
}
...
// return nil at the end of func
return nil

@@ -30,6 +30,8 @@ import (
)

// ConfigValidator allows the command configurations to be validated.
// TODO: investigate for removal, currently only used in interfaces_test.go

Choose a reason for hiding this comment

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

If this is only used in a test, I would move this in the test file where it is used.

@@ -55,7 +55,7 @@ func LoadConfig() (*Config, error) {
}
defer jsonFile.Close()

b, err := ioutil.ReadAll(jsonFile)
b, _ := ioutil.ReadAll(jsonFile)

Choose a reason for hiding this comment

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

Same thing here, let's add the error handling logic needed. Probably should add something like this

b, err := ioutil.ReadAll(jsonFile)
if err != nil {
   return nil, errors.Wrap(err, "read config")
}

@@ -152,7 +152,7 @@ func Run(restConf *rest.Config, cfg *config.Config) (errCount int) {

// 5. Run the queries
recorder := NewQueryRecorder()
clusterResources, nsResources, err := getAllFilteredResources(apiHelper, cfg.Resources)
clusterResources, nsResources, _ := getAllFilteredResources(apiHelper, cfg.Resources) // TODO: handle error checking

Choose a reason for hiding this comment

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

Same deal here, let's add an error handler

clusterResources, nsResources, err := getAllFilteredResources(apiHelper, cfg.Resources)
if err != nil {
	errlog.LogError(errors.Wrap(err, "getting all filtered resources"))
	return errCount + 1
}

Copy link

@vladimirvivien vladimirvivien left a comment

Choose a reason for hiding this comment

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

LGTM

@vladimirvivien
Copy link

@wilsonehusin Please squash commits and include signoff

- source code lint with golangci-lint
- ignore low-priority errchecks (for now)
- add GitHub actions for linting CI

Signed-off-by: Wilson E. Husin <[email protected]>
@wilsonehusin wilsonehusin merged commit 7419e1d into vmware-tanzu:master Oct 27, 2020
@wilsonehusin wilsonehusin deleted the golangci-lint branch October 28, 2020 00:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants