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

Add basic validation of configs #763

Merged
merged 1 commit into from
Jun 21, 2019

Conversation

zubron
Copy link
Contributor

@zubron zubron commented Jun 19, 2019

What this PR does / why we need it:
This adds basic checking and validation of configs passed to some of the
interface function implementations in the Sonobuoy client. It is primarily
being added to prevent panics caused by passing nil configs to these
functions. It could be expanded in future to provide more comprehensive
validation of configuration objects.

Fixes #394

Signed-off-by: Bridget McErlean bmcerlean@vmware.com

Which issue(s) this PR fixes

Special notes for your reviewer:
This does not include comprehensive validation for some of the more
complex configuration objects (e.g. GenConfig). It primarily focuses on
the nil checks. A lot of the added tests in the client don't test the
"successful" case, where the execution of the function would proceed
past the validation. This is due to the complexity of the test set up that
would be required which seemed out of scope for this change.

Release note:

Fixed a bug that caused a panic if a nil configuration was provided to Sonobuoy client functions.

@zubron zubron requested a review from johnSchnake June 19, 2019 20:32
@codecov-io
Copy link

codecov-io commented Jun 19, 2019

Codecov Report

Merging #763 into master will increase coverage by 0.84%.
The diff coverage is 76.25%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #763      +/-   ##
==========================================
+ Coverage   42.59%   43.43%   +0.84%     
==========================================
  Files          70       70              
  Lines        4029     4089      +60     
==========================================
+ Hits         1716     1776      +60     
  Misses       2208     2208              
  Partials      105      105
Impacted Files Coverage Δ
pkg/client/defaults.go 0% <0%> (ø) ⬆️
cmd/sonobuoy/app/retrieve.go 24.32% <0%> (ø) ⬆️
pkg/client/logs.go 32.4% <100%> (+3.56%) ⬆️
pkg/client/interfaces.go 82.5% <100%> (+36.34%) ⬆️
pkg/client/delete.go 4.8% <100%> (+4.8%) ⬆️
pkg/client/status.go 55.55% <100%> (+55.55%) ⬆️
pkg/client/preflight.go 57.35% <100%> (+4.22%) ⬆️
pkg/client/run.go 7.24% <100%> (+7.24%) ⬆️
pkg/client/gen.go 83.63% <100%> (+0.3%) ⬆️
cmd/sonobuoy/app/delete.go 36.36% <20%> (-3.64%) ⬇️
... and 2 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f1fbf8a...0fb7192. Read the comment docs.

Copy link
Contributor

@johnSchnake johnSchnake 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 cleaning this stuff up!

I made a bunch of comments but most of them are near-trivial to fixup and involve minor test organization and other small issues.

pkg/client/delete_test.go Show resolved Hide resolved
pkg/client/delete_test.go Show resolved Hide resolved
@@ -29,6 +29,11 @@ import (
"k8s.io/client-go/rest"
)

// ConfigValidator allows the command configurations to be validated.
type ConfigValidator interface {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: I'd name this just Validator since it is just a one function interface with that function. Nothing about the interface is config-specific.

Copy link
Contributor

Choose a reason for hiding this comment

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

In addition, I would actually just call this validator. Users of the library:

  • probably dont need the validator type at all, that is just for our own testing
  • if they do need it, they can define their own validator interface in 2-3 lines.

Keeping this unexported helps limit dependencies we feel obligated to support. We can always export it later if we come up with some important reason to do so.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, I didn't think about the impact of exporting it. I will change that.

pkg/client/interfaces.go Outdated Show resolved Hide resolved
return errors.Wrap(err, "GenConfig validation failed")
}

return nil
Copy link
Contributor

Choose a reason for hiding this comment

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

On all of these Validate methods you can actually make them one line and just do:

return errors.Wrap(err, "GenConfig validation failed")

The reason be that errors.Wrap will return nil if the error is nil so that you don't always have to do this if err := ... block.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice! I didn't realise that. Will update these.

pkg/client/retrieve.go Show resolved Hide resolved
ec := make(chan error, 1)
client, err := c.Client()
if err != nil {
ec <- err
return nil, ec
return nil, nil, err
Copy link
Contributor

Choose a reason for hiding this comment

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

In this case there isn't a need to put the err on the channel (line above this) since we aren't even returning it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good spot. I missed that!

@@ -51,7 +52,7 @@ func NewDeleteConfig() *DeleteConfig {
// NewLogConfig is a LogConfig with follow disabled and default images.
func NewLogConfig() *LogConfig {
return &LogConfig{
Follow: false,
Namespace: config.DefaultNamespace,
Copy link
Contributor

Choose a reason for hiding this comment

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

Since we are leaving in the namespace defaulting in the other methods, we should do the same here, right? Was there some other reason to specifically pull out namespace from this and the delete config?

pkg/client/defaults.go Show resolved Hide resolved
// Validate checks the config to determine if it is valid.
func (lc *LogConfig) Validate() error {
if lc.Namespace == "" {
return errors.New("Namespace cannot be empty")
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: errors should be lower cased rather than using sentence capitalization. This applies throughout.
See: https://github.com/golang/go/wiki/CodeReviewComments#error-strings

@zubron zubron force-pushed the default-configs-394 branch 5 times, most recently from 84d9500 to 07d599b Compare June 21, 2019 14:12
This adds basic checking and validation of configs passed to some of the
interface function implementations in the Sonobuoy client. It is primarily
being added to prevent panics caused by passing nil configs to these
functions. It could be expanded in future to provide more comprehensive
validation of configuration objects.

Fixes vmware-tanzu#394

Signed-off-by: Bridget McErlean <bmcerlean@vmware.com>
@johnSchnake johnSchnake merged commit ee916a1 into vmware-tanzu:master Jun 21, 2019
@zubron zubron deleted the default-configs-394 branch August 11, 2019 11:01
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.

Default configurations
3 participants