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

Improve error reporting, add errorlint, fix its warnings; drop pkg/errors #3011

Merged
merged 24 commits into from
Jun 24, 2021

Conversation

kolyshkin
Copy link
Contributor

@kolyshkin kolyshkin commented Jun 9, 2021

This commit does a few things related to error reporting and handling.

  1. Fix and improve a bunch of error messages and associated error handling,
    using features that appear in Go 1.13 (fmt.Errorf with %w to wrap errors,
    errors.Is and errors.As for unwrapping) and some other methods
    (such as using os.PathError, introducing and using ParseError etc.)

  2. Fix all errorlint linter warnings, enable errorlint it in CI.

  3. Remove all uses of pkg/errors, switching to native Go error (un)wrapping.

TODO (not in this PR, as it's already too big):

@kolyshkin
Copy link
Contributor Author

Hmm, weird failure in update devices [minimal transition rules] test; filed #3014; CI restarted.

@kolyshkin
Copy link
Contributor Author

Hmm, weird failure in update devices [minimal transition rules] test; filed #3014; CI restarted.

OK it seems that this error is repeatable. Kicked CI again, created #3015 to investigate if this is indeed caused by this PR.

@kolyshkin kolyshkin changed the title [WIP] improve error reporting, add errorlint, fix its warnings Improve error reporting, add errorlint, fix its warnings Jun 10, 2021
@kolyshkin kolyshkin marked this pull request as ready for review June 10, 2021 14:57
@kolyshkin kolyshkin added this to the post-1.0 milestone Jun 10, 2021
@kolyshkin kolyshkin modified the milestones: post-1.0, 1.1.0 Jun 11, 2021
@kolyshkin
Copy link
Contributor Author

Rebased; set milestone to 1.1

@kolyshkin kolyshkin changed the title Improve error reporting, add errorlint, fix its warnings Improve error reporting, add errorlint, fix its warnings; drop pkg/errors Jun 11, 2021
@kolyshkin
Copy link
Contributor Author

Added commits that switch from pkg/error to Go native error (un)wrapping.

@kolyshkin kolyshkin added the kind/refactor refactoring label Jun 12, 2021
@kolyshkin kolyshkin force-pushed the errlinter branch 3 times, most recently from 59d43b0 to d8de1a7 Compare June 14, 2021 19:49
@kolyshkin
Copy link
Contributor Author

Rebased on top of merged #3024

An errror from ioutil.WriteFile already contains file name, so there is
no need to duplicate that information.

Signed-off-by: Kir Kolyshkin <[email protected]>
@kolyshkin kolyshkin changed the title Improve error reporting, add errorlint, fix its warnings; drop pkg/errors [WIP] Improve error reporting, add errorlint, fix its warnings; drop pkg/errors Jun 22, 2021
@kolyshkin
Copy link
Contributor Author

Working on addressing my own review comments :)

kolyshkin added 13 commits June 22, 2021 16:09
Errors returned by unix are bare. In some cases it's impossible to find
out what went wrong because there's is not enough context.

Add a mountError type (mostly copy-pasted from github.com/moby/sys/mount),
and mount/unmount helpers. Use these where appropriate, and convert error
checks to use errors.Is.

Signed-off-by: Kir Kolyshkin <[email protected]>
Errors from os.Open, os.Symlink etc do not need to be wrapped, as they
are already wrapped into os.PathError.

Error from unix are bare errnos and need to be wrapped. Same
os.PathError is a good candidate.

Signed-off-by: Kir Kolyshkin <[email protected]>
This should result in no change when the error is printed, but make the
errors returned unwrappable, meaning errors.As and errors.Is will work.

Signed-off-by: Kir Kolyshkin <[email protected]>
This one is tough as errorlint insists on using errors.Is, and the
latter is known to not work for Go 1.13 which we still support.

So, add a nolint annotation to suppress the warning, and a TODO to
address it later.

For intelrdt, we can do the same, but it is easier to reuse the very
same function from fscommon (note we can't use fscommon for other stuff
as it expects cgroupfs).

Signed-off-by: Kir Kolyshkin <[email protected]>
Use fmt.Errorf with %w instead.

Convert the users to the new wrapping.

This fixes an errorlint warning.

Signed-off-by: Kir Kolyshkin <[email protected]>
Instead of using errors.Wrap, use fmt.Errorf with %w for error wrapping.

Also, use errors.Is instead of direct error comparison.

Signed-off-by: Kir Kolyshkin <[email protected]>
Do this for all errors except one from unix.*.

This fixes a bunch of errorlint warnings, like these

libcontainer/generic_error.go:25:15: type assertion on error will fail on wrapped errors. Use errors.As to check for specific errors (errorlint)
	if le, ok := err.(Error); ok {
	             ^
libcontainer/factory_linux_test.go:145:14: type assertion on error will fail on wrapped errors. Use errors.As to check for specific errors (errorlint)
	lerr, ok := err.(Error)
	            ^
libcontainer/state_linux_test.go:28:11: type assertion on error will fail on wrapped errors. Use errors.As to check for specific errors (errorlint)
	_, ok := err.(*stateTransitionError)
	         ^
libcontainer/seccomp/patchbpf/enosys_linux.go:88:4: switch on an error will fail on wrapped errors. Use errors.Is to check for specific errors (errorlint)
			switch err {
			^

Signed-off-by: Kir Kolyshkin <[email protected]>
Errors from unix.* are always bare and thus can be used directly.

Add //nolint:errorlint annotation to ignore errors such as these:

libcontainer/system/xattrs_linux.go:18:7: comparing with == will fail on wrapped errors. Use errors.Is to check for a specific error (errorlint)
	case errno == unix.ERANGE:
	     ^
libcontainer/container_linux.go:1259:9: comparing with != will fail on wrapped errors. Use errors.Is to check for a specific error (errorlint)
					if e != unix.EINVAL {
					   ^
libcontainer/rootfs_linux.go:919:7: comparing with != will fail on wrapped errors. Use errors.Is to check for a specific error (errorlint)
			if err != unix.EINVAL && err != unix.EPERM {
			   ^
libcontainer/rootfs_linux.go:1002:4: switch on an error will fail on wrapped errors. Use errors.Is to check for specific errors (errorlint)
			switch err {
			^

Signed-off-by: Kir Kolyshkin <[email protected]>
Signed-off-by: Kir Kolyshkin <[email protected]>
Use Go native errors wrapping.

Introduce wrapErr helper to minimise the patch size.

Signed-off-by: Kir Kolyshkin <[email protected]>
Signed-off-by: Kir Kolyshkin <[email protected]>
Signed-off-by: Kir Kolyshkin <[email protected]>
@kolyshkin kolyshkin changed the title [WIP] Improve error reporting, add errorlint, fix its warnings; drop pkg/errors Improve error reporting, add errorlint, fix its warnings; drop pkg/errors Jun 22, 2021
@kolyshkin
Copy link
Contributor Author

(my own) review comments addressed; no longer WIP.

Copy link
Member

@cyphar cyphar left a comment

Choose a reason for hiding this comment

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

One nit, two things need changing, and one question.

libcontainer/cgroups/fs/blkio_test.go Show resolved Hide resolved
libcontainer/cgroups/fs/pids.go Show resolved Hide resolved
libcontainer/cgroups/fs2/pids.go Show resolved Hide resolved
libcontainer/container_linux.go Show resolved Hide resolved
@kolyshkin
Copy link
Contributor Author

One nit, two things need changing, and one question.

Sorry, what was the question? I can't find it.

@cyphar
Copy link
Member

cyphar commented Jun 24, 2021

@kolyshkin It was about removing genericError but after posting it I noticed that there was a separate PR for that. #3011 (comment)

Copy link
Member

@cyphar cyphar left a comment

Choose a reason for hiding this comment

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

LGTM, my suggestions missed that the pids.max handling already existed in the fscommon utilities.

@cyphar cyphar closed this in 51beb5c Jun 24, 2021
@cyphar cyphar merged commit 51beb5c into opencontainers:master Jun 24, 2021
@kolyshkin kolyshkin mentioned this pull request Jun 24, 2021
10 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/refactor refactoring
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants