Skip to content

Commit

Permalink
Merge pull request #34 from pacoxu/pkg-errors-replacing
Browse files Browse the repository at this point in the history
Remove dependency on github.com/pkg/errors
  • Loading branch information
k8s-ci-robot authored Jan 12, 2023
2 parents 737f948 + c2b6b14 commit a0cb0d1
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 49 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ go 1.16

require (
github.com/blang/semver/v4 v4.0.0
github.com/pkg/errors v0.8.0
github.com/stretchr/testify v1.3.0
golang.org/x/sys v0.0.0-20191022100944-742c48ecaeb7
)
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ github.com/blang/semver/v4 v4.0.0 h1:1PFHFE6yCCTv8C1TeyNNarDzntLi7wMI5i/pzqYIsAM
github.com/blang/semver/v4 v4.0.0/go.mod h1:IbckMUScFkM3pff0VJDNKRiT6TG/YpiHIM2yvyW5YoQ=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
2 changes: 1 addition & 1 deletion hack/verify-boilerplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func verifyBoilerplate(contents string) error {
return fmt.Errorf("copyright line should contain exactly %d words", expectedLen)
}
if !yearRegexp.MatchString(yearWords[1]) {
return fmt.Errorf("cannot parse the year in the copyright line")
return errors.New("cannot parse the year in the copyright line")
}
bpLine = strings.ReplaceAll(bpLine, yearPlaceholder, yearWords[1])
}
Expand Down
12 changes: 6 additions & 6 deletions validators/cgroup_validator_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ package system

import (
"bufio"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"

"github.com/pkg/errors"
"golang.org/x/sys/unix"
)

Expand Down Expand Up @@ -55,32 +55,32 @@ func (c *CgroupsValidator) Validate(spec SysSpec) (warns, errs []error) {
var st unix.Statfs_t
var err error
if err := unix.Statfs(unifiedMountpoint, &st); err != nil {
return nil, []error{errors.Wrap(err, "cannot statfs the cgroupv2 root")}
return nil, []error{fmt.Errorf("cannot statfs the cgroupv2 root: %w", err)}
}
var requiredCgroupSpec []string
var optionalCgroupSpec []string
var subsystems []string
if st.Type == unix.CGROUP2_SUPER_MAGIC {
subsystems, err = c.getCgroupV2Subsystems()
if err != nil {
return nil, []error{errors.Wrap(err, "failed to get cgroup v2 subsystems")}
return nil, []error{fmt.Errorf("failed to get cgroup v2 subsystems: %w", err)}
}
requiredCgroupSpec = spec.CgroupsV2
optionalCgroupSpec = spec.CgroupsV2Optional
} else {
subsystems, err = c.getCgroupV1Subsystems()
if err != nil {
return nil, []error{errors.Wrap(err, "failed to get cgroup v1 subsystems")}
return nil, []error{fmt.Errorf("failed to get cgroup v1 subsystems: %w", err)}
}
requiredCgroupSpec = spec.Cgroups
optionalCgroupSpec = spec.CgroupsOptional
}

if missingRequired := c.validateCgroupSubsystems(requiredCgroupSpec, subsystems, true); len(missingRequired) != 0 {
errs = []error{errors.Errorf("missing required cgroups: %s", strings.Join(missingRequired, " "))}
errs = []error{fmt.Errorf("missing required cgroups: %s", strings.Join(missingRequired, " "))}
}
if missingOptional := c.validateCgroupSubsystems(optionalCgroupSpec, subsystems, false); len(missingOptional) != 0 {
warns = []error{errors.Errorf("missing optional cgroups: %s", strings.Join(missingOptional, " "))}
warns = []error{fmt.Errorf("missing optional cgroups: %s", strings.Join(missingOptional, " "))}
}
return
}
Expand Down
17 changes: 8 additions & 9 deletions validators/docker_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ package system
import (
"bytes"
"encoding/json"
"fmt"
"os/exec"
"regexp"
"strings"

"github.com/pkg/errors"
)

var _ Validator = &DockerValidator{}
Expand Down Expand Up @@ -72,7 +71,7 @@ func (d *DockerValidator) Validate(spec SysSpec) ([]error, []error) {
cmd.Stderr = &errb
err := cmd.Run()
if err != nil {
return nil, []error{errors.Errorf(`failed executing "docker info --format '{{json .}}'"\noutput: %s\nstderr: %s\nerror: %v`, outb.String(), errb.String(), err)}
return nil, []error{fmt.Errorf(`failed executing "docker info --format '{{json .}}'"\noutput: %s\nstderr: %s\nerror: %v`, outb.String(), errb.String(), err)}
}
if err := d.unmarshalDockerInfo(outb.Bytes(), &info); err != nil {
return nil, []error{err}
Expand All @@ -82,22 +81,22 @@ func (d *DockerValidator) Validate(spec SysSpec) ([]error, []error) {
warnings, errs := d.validateDockerInfo(spec.RuntimeSpec.DockerSpec, info)

if len(errb.String()) > 0 {
warnings = append(warnings, errors.Errorf(`the command "docker info --format '{{json.}}'" succeeded with potential warnings\noutput: %s`, errb.String()))
warnings = append(warnings, fmt.Errorf(`the command "docker info --format '{{json.}}'" succeeded with potential warnings\noutput: %s`, errb.String()))
}
return warnings, errs
}

func (d *DockerValidator) unmarshalDockerInfo(b []byte, info *dockerInfo) error {
if err := json.Unmarshal(b, &info); err != nil {
return errors.Wrapf(err, "could not unmarshal the JSON output of 'docker info':\n%s\n", b)
return fmt.Errorf("could not unmarshal the JSON output of 'docker info':\n%s\n err: %w", b, err)
}
return nil
}

func (d *DockerValidator) validateDockerInfo(spec *DockerSpec, info dockerInfo) ([]error, []error) {
// Validate docker version.
if info.ServerErrors != nil {
return nil, []error{errors.Errorf("error verifying Docker info: %q", strings.Join(info.ServerErrors, `", "`))}
return nil, []error{fmt.Errorf("error verifying Docker info: %q", strings.Join(info.ServerErrors, `", "`))}
}

matched := false
Expand All @@ -115,15 +114,15 @@ func (d *DockerValidator) validateDockerInfo(spec *DockerSpec, info dockerInfo)
r := regexp.MustCompile(ver)
if r.MatchString(info.ServerVersion) {
d.Reporter.Report(dockerConfigPrefix+"VERSION", info.ServerVersion, good)
w := errors.Errorf(
w := fmt.Errorf(
"this Docker version is not on the list of validated versions: %s. Latest validated version: %s",
info.ServerVersion,
latestValidatedDockerVersion,
)
return []error{w}, nil
}
d.Reporter.Report(dockerConfigPrefix+"VERSION", info.ServerVersion, bad)
return nil, []error{errors.Errorf("unsupported docker version: %s", info.ServerVersion)}
return nil, []error{fmt.Errorf("unsupported docker version: %s", info.ServerVersion)}
}
// Validate graph driver.
item := dockerConfigPrefix + "GRAPH_DRIVER"
Expand All @@ -134,5 +133,5 @@ func (d *DockerValidator) validateDockerInfo(spec *DockerSpec, info dockerInfo)
}
}
d.Reporter.Report(item, info.Driver, bad)
return nil, []error{errors.Errorf("unsupported graph driver: %s", info.Driver)}
return nil, []error{fmt.Errorf("unsupported graph driver: %s", info.Driver)}
}
16 changes: 7 additions & 9 deletions validators/kernel_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ import (
"path/filepath"
"regexp"
"strings"

"github.com/pkg/errors"
)

var _ Validator = &KernelValidator{}
Expand Down Expand Up @@ -65,7 +63,7 @@ func (k *KernelValidator) Validate(spec SysSpec) ([]error, []error) {
helper := KernelValidatorHelperImpl{}
release, err := helper.GetKernelReleaseVersion()
if err != nil {
return nil, []error{errors.Wrap(err, "failed to get kernel release")}
return nil, []error{fmt.Errorf("failed to get kernel release: %w", err)}
}
k.kernelRelease = release
var errs []error
Expand All @@ -92,14 +90,14 @@ func (k *KernelValidator) validateKernelVersion(kSpec KernelSpec) error {
}
}
k.Reporter.Report("KERNEL_VERSION", k.kernelRelease, bad)
return errors.Errorf("unsupported kernel release: %s", k.kernelRelease)
return fmt.Errorf("unsupported kernel release: %s", k.kernelRelease)
}

// validateKernelConfig validates the kernel configurations.
func (k *KernelValidator) validateKernelConfig(kSpec KernelSpec) error {
allConfig, err := k.getKernelConfig()
if err != nil {
return errors.Wrap(err, "failed to parse kernel config")
return fmt.Errorf("failed to parse kernel config: %w", err)
}
return k.validateCachedKernelConfig(allConfig, kSpec)
}
Expand Down Expand Up @@ -168,7 +166,7 @@ func (k *KernelValidator) validateCachedKernelConfig(allConfig map[string]kConfi
validateOpt(config, forbidden)
}
if len(badConfigs) > 0 {
return errors.Errorf("unexpected kernel config: %s", strings.Join(badConfigs, " "))
return fmt.Errorf("unexpected kernel config: %s", strings.Join(badConfigs, " "))
}
return nil
}
Expand Down Expand Up @@ -223,14 +221,14 @@ func (k *KernelValidator) getKernelConfigReader() (io.Reader, error) {
// config module and check again.
output, err := exec.Command(modprobeCmd, configsModule).CombinedOutput()
if err != nil {
return nil, errors.Wrapf(err, "unable to load kernel module: %q, output: %q, err",
configsModule, output)
return nil, fmt.Errorf("unable to load kernel module: %q, output: %q, err: %w",
configsModule, output, err)
}
// Unload the kernel config module to make sure the validation have no side effect.
defer exec.Command(modprobeCmd, "-r", configsModule).Run()
loadModule = true
}
return nil, errors.Errorf("no config path in %v is available", possibePaths)
return nil, fmt.Errorf("no config path in %v is available", possibePaths)
}

// getKernelConfig gets kernel config from kernel config file and convert kernel config to internal type.
Expand Down
7 changes: 3 additions & 4 deletions validators/os_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ limitations under the License.
package system

import (
"fmt"
"os/exec"
"strings"

"github.com/pkg/errors"
)

var _ Validator = &OSValidator{}
Expand All @@ -39,7 +38,7 @@ func (o *OSValidator) Name() string {
func (o *OSValidator) Validate(spec SysSpec) ([]error, []error) {
os, err := exec.Command("uname").CombinedOutput()
if err != nil {
return nil, []error{errors.Wrap(err, "failed to get os name")}
return nil, []error{fmt.Errorf("failed to get os name: %w", err)}
}
if err = o.validateOS(strings.TrimSpace(string(os)), spec.OS); err != nil {
return nil, []error{err}
Expand All @@ -50,7 +49,7 @@ func (o *OSValidator) Validate(spec SysSpec) ([]error, []error) {
func (o *OSValidator) validateOS(os, specOS string) error {
if os != specOS {
o.Reporter.Report("OS", os, bad)
return errors.Errorf("unsupported operating system: %s", os)
return fmt.Errorf("unsupported operating system: %s", os)
}
o.Reporter.Report("OS", os, good)
return nil
Expand Down
21 changes: 11 additions & 10 deletions validators/package_validator_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ limitations under the License.
package system

import (
"errors"
"fmt"
"io/ioutil"
"os/exec"
"strings"

"github.com/blang/semver/v4"
"github.com/pkg/errors"
)

// semVerDotsCount is the number of dots in a valid semantic version.
Expand Down Expand Up @@ -68,7 +68,7 @@ func newDPKG() (packageManager, bool) {
func (dpkg) getPackageVersion(packageName string) (string, error) {
output, err := exec.Command("dpkg-query", "--show", "--showformat='${Version}'", packageName).Output()
if err != nil {
return "", errors.Wrap(err, "dpkg-query failed")
return "", fmt.Errorf("dpkg-query failed: %w", err)
}
version := extractUpstreamVersion(string(output))
if version == "" {
Expand Down Expand Up @@ -152,7 +152,7 @@ func (validator *packageValidator) validate(packageSpecs []PackageSpec, manager
if versionRange(sv) {
validator.reporter.Report(nameWithVerRange, version, good)
} else {
errs = append(errs, errors.Errorf("package \"%s %s\" does not meet the spec \"%s (%s)\"", packageName, sv, packageName, spec.VersionRange))
errs = append(errs, fmt.Errorf("package \"%s %s\" does not meet the spec \"%s (%s)\"", packageName, sv, packageName, spec.VersionRange))
validator.reporter.Report(nameWithVerRange, version, bad)
}
}
Expand All @@ -163,7 +163,7 @@ func (validator *packageValidator) validate(packageSpecs []PackageSpec, manager
func getKernelRelease() (string, error) {
output, err := exec.Command("uname", "-r").Output()
if err != nil {
return "", errors.Wrap(err, "failed to get kernel release")
return "", fmt.Errorf("failed to get kernel release: %w", err)
}
return strings.TrimSpace(string(output)), nil
}
Expand All @@ -173,7 +173,7 @@ func getOSDistro() (string, error) {
f := "/etc/lsb-release"
b, err := ioutil.ReadFile(f)
if err != nil {
return "", errors.Wrapf(err, "failed to read %q", f)
return "", fmt.Errorf("failed to read %q: %w", f, err)
}
content := string(b)
switch {
Expand All @@ -184,7 +184,7 @@ func getOSDistro() (string, error) {
case strings.Contains(content, "CoreOS"):
return "coreos", nil
default:
return "", errors.Errorf("failed to get OS distro: %s", content)
return "", fmt.Errorf("failed to get OS distro: %s", content)
}
}

Expand Down Expand Up @@ -244,10 +244,11 @@ func extractUpstreamVersion(version string) string {
}

// toSemVerRange converts the input to a semantic version range.
// E.g., ">=1.0" -> ">=1.0.x"
// ">=1" -> ">=1.x"
// ">=1 <=2.3" -> ">=1.x <=2.3.x"
// ">1 || >3.1.0 !4.2" -> ">1.x || >3.1.0 !4.2.x"
// E.g.,
// - ">=1.0" -> ">=1.0.x"
// - ">=1" -> ">=1.x"
// - ">=1 <=2.3" -> ">=1.x <=2.3.x"
// - ">1 || >3.1.0 !4.2" -> ">1.x || >3.1.0 !4.2.x"
func toSemVerRange(input string) string {
var output []string
fields := strings.Fields(input)
Expand Down
5 changes: 2 additions & 3 deletions validators/package_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ limitations under the License.
package system

import (
"errors"
"fmt"
"reflect"
"testing"

"github.com/pkg/errors"
)

func TestExtractUpstreamVersion(t *testing.T) {
Expand Down Expand Up @@ -169,7 +168,7 @@ func (m testPackageManager) getPackageVersion(packageName string) (string, error
if v, ok := m.packageVersions[packageName]; ok {
return v, nil
}
return "", errors.Errorf("package %q does not exist", packageName)
return "", fmt.Errorf("package %q does not exist", packageName)
}

func TestValidatePackageVersion(t *testing.T) {
Expand Down
3 changes: 1 addition & 2 deletions validators/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ limitations under the License.
package system

import (
"errors"
"fmt"
"io"
"os"

"github.com/pkg/errors"
)

// ValidationResultType is type of the validation result. Different validation results
Expand Down
4 changes: 2 additions & 2 deletions validators/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ type KernelConfig struct {

// KernelSpec defines the specification for the kernel. Currently, it contains
// specification for:
// * Kernel Version
// * Kernel Configuration
// - Kernel Version
// - Kernel Configuration
type KernelSpec struct {
// Versions define supported kernel version. It is a group of regexps.
Versions []string `json:"versions,omitempty"`
Expand Down

0 comments on commit a0cb0d1

Please sign in to comment.