Skip to content

Commit

Permalink
refactor: use go 1.13 error wrapping
Browse files Browse the repository at this point in the history
This removes the github.com/pkg/errors package in favor of the official
error wrapping in go 1.13.

Signed-off-by: Andrew Rynhard <[email protected]>
  • Loading branch information
andrewrynhard committed Oct 16, 2019
1 parent 94c2865 commit d430a37
Show file tree
Hide file tree
Showing 90 changed files with 342 additions and 377 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ TOOLS ?= autonomy/tools:96cb1b9
# TODO(andrewrynhard): Move this logic to a shell script.
BUILDKIT_VERSION ?= v0.6.0
KUBECTL_VERSION ?= v1.16.0
GO_VERSION ?= 1.12
GO_VERSION ?= 1.13
BUILDKIT_IMAGE ?= moby/buildkit:$(BUILDKIT_VERSION)
BUILDKIT_HOST ?= tcp://0.0.0.0:1234
BUILDKIT_CONTAINER_NAME ?= talos-buildkit
Expand Down
8 changes: 4 additions & 4 deletions cmd/osctl/cmd/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package cmd
import (
"context"
"encoding/base64"
"errors"
"fmt"
"io"
"io/ioutil"
Expand All @@ -20,7 +21,6 @@ import (
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/client"
"github.com/hashicorp/go-multierror"
"github.com/pkg/errors"
"github.com/spf13/cobra"

"github.com/talos-systems/talos/cmd/osctl/cmd/cluster/pkg/node"
Expand Down Expand Up @@ -119,7 +119,7 @@ func create() (err error) {
fmt.Println("creating network", clusterName)

if _, err = createNetwork(cli); err != nil {
return errors.Wrap(err, " A cluster might already exist, run \"osctl cluster destroy\" to permanently delete the existing cluster, and try again.")
return fmt.Errorf("a cluster might already exist, run \"osctl cluster destroy\" to permanently delete the existing cluster, and try again: %w", err)
}

// Create the master nodes.
Expand Down Expand Up @@ -182,7 +182,7 @@ func createNodes(requests []*node.Request) (err error) {
fmt.Println("creating node", req.Name)

if err = node.NewNode(clusterName, req); err != nil {
helpers.Fatalf("failed to create node: %v", err)
helpers.Fatalf("failed to create node: %w", err)
}

wg.Done()
Expand Down Expand Up @@ -355,7 +355,7 @@ func saveConfig(input *generate.Input) (err error) {
func parseCPUShare() (int64, error) {
cpu, ok := new(big.Rat).SetString(clusterCpus)
if !ok {
return 0, errors.Errorf("failed to parsing as a rational number: %s", clusterCpus)
return 0, fmt.Errorf("failed to parsing as a rational number: %s", clusterCpus)
}

nano := cpu.Mul(cpu, big.NewRat(1e9, 1))
Expand Down
6 changes: 3 additions & 3 deletions cmd/osctl/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ var configGenerateCmd = &cobra.Command{
func genV1Alpha1Config(args []string) {
input, err := genv1alpha1.NewInput(args[0], args[1], kubernetesVersion)
if err != nil {
helpers.Fatalf("failed to generate PKI and tokens: %v", err)
helpers.Fatalf("failed to generate PKI and tokens: %w", err)
}

input.AdditionalSubjectAltNames = additionalSANs
Expand All @@ -155,7 +155,7 @@ func genV1Alpha1Config(args []string) {

for _, t := range []genv1alpha1.Type{genv1alpha1.TypeInit, genv1alpha1.TypeControlPlane, genv1alpha1.TypeJoin} {
if err = writeV1Alpha1Config(input, t, t.String()); err != nil {
helpers.Fatalf("failed to generate config for %s: %v", t.String(), err)
helpers.Fatalf("failed to generate config for %s: %w", t.String(), err)
}
}

Expand All @@ -177,7 +177,7 @@ func genV1Alpha1Config(args []string) {
}

if err = ioutil.WriteFile("talosconfig", data, 0644); err != nil {
helpers.Fatalf("%v", err)
helpers.Fatalf("%w", err)
}

fmt.Println("created talosconfig")
Expand Down
6 changes: 3 additions & 3 deletions cmd/osctl/cmd/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ var lsCmd = &cobra.Command{
}
long, err := cmd.Flags().GetBool("long")
if err != nil {
helpers.Fatalf("failed to parse long flag: %v", err)
helpers.Fatalf("failed to parse long flag: %w", err)
}
recurse, err := cmd.Flags().GetBool("recurse")
if err != nil {
helpers.Fatalf("failed to parse recurse flag: %v", err)
helpers.Fatalf("failed to parse recurse flag: %w", err)
}
recursionDepth, err := cmd.Flags().GetInt32("depth")
if err != nil {
helpers.Fatalf("failed to parse depth flag: %v", err)
helpers.Fatalf("failed to parse depth flag: %w", err)
}

stream, err := c.LS(globalCtx, machineapi.LSRequest{
Expand Down
2 changes: 1 addition & 1 deletion cmd/osctl/cmd/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var timeCmd = &cobra.Command{
setupClient(func(c *client.Client) {
server, err := cmd.Flags().GetString("check")
if err != nil {
helpers.Fatalf("failed to parse check flag: %v", err)
helpers.Fatalf("failed to parse check flag: %w", err)
}

var output *timeapi.TimeReply
Expand Down
6 changes: 3 additions & 3 deletions cmd/osctl/pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,17 @@ func NewClientTargetAndCredentialsFromConfig(p string) (target string, creds *Cr

caBytes, err := base64.StdEncoding.DecodeString(context.CA)
if err != nil {
return "", nil, fmt.Errorf("error decoding CA: %v", err)
return "", nil, fmt.Errorf("error decoding CA: %w", err)
}

crtBytes, err := base64.StdEncoding.DecodeString(context.Crt)
if err != nil {
return "", nil, fmt.Errorf("error decoding certificate: %v", err)
return "", nil, fmt.Errorf("error decoding certificate: %w", err)
}

keyBytes, err := base64.StdEncoding.DecodeString(context.Key)
if err != nil {
return "", nil, fmt.Errorf("error decoding key: %v", err)
return "", nil, fmt.Errorf("error decoding key: %w", err)
}

creds = &Credentials{
Expand Down
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ require (
github.com/opencontainers/runc v1.0.0-rc8 // indirect
github.com/opencontainers/runtime-spec v1.0.1
github.com/pborman/uuid v1.2.0 // indirect
github.com/pkg/errors v0.8.1
github.com/prometheus/procfs v0.0.3
github.com/ryanuber/columnize v2.1.0+incompatible
github.com/spf13/cobra v0.0.5
Expand All @@ -62,7 +61,6 @@ require (
golang.org/x/sys v0.0.0-20190825160603-fb81701db80f
golang.org/x/text v0.3.2
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4 // indirect
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7
google.golang.org/grpc v1.23.0
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/freddierice/go-losetup.v1 v1.0.0-20170407175016-fc9adea44124
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -434,8 +434,6 @@ golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190312170243-e65039ee4138/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7 h1:9zdDQZ7Thm29KFXgAX/+yaf3eVbP7djjWp/dXAppNCc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE=
google.golang.org/appengine v1.1.0 h1:igQkv0AAhEIvTEpD5LIpAfav2eeVO9HBTjvKHVJPRSs=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
Expand Down
5 changes: 3 additions & 2 deletions internal/app/init/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
package main

import (
"errors"
"fmt"
"log"
"time"

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

"github.com/talos-systems/talos/internal/pkg/kmsg"
Expand Down Expand Up @@ -79,7 +80,7 @@ func main() {
defer recovery()

if err := run(); err != nil {
panic(errors.Wrap(err, "early boot failed"))
panic(fmt.Errorf("early boot failed: %w", err))
}

// We should never reach this point if things are working as intended.
Expand Down
3 changes: 1 addition & 2 deletions internal/app/machined/internal/api/reg/reg.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (

"github.com/golang/protobuf/ptypes/empty"
"github.com/hashicorp/go-multierror"
"github.com/pkg/errors"
"golang.org/x/sys/unix"
"google.golang.org/grpc"

Expand Down Expand Up @@ -182,7 +181,7 @@ func (r *Registrator) CopyOut(req *machineapi.CopyOutRequest, s machineapi.Machi
path = filepath.Clean(path)

if !filepath.IsAbs(path) {
return errors.Errorf("path is not absolute %v", path)
return fmt.Errorf("path is not absolute %v", path)
}

pr, pw := io.Pipe()
Expand Down
4 changes: 2 additions & 2 deletions internal/app/machined/internal/phase/acpi/acpi.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
package acpi

import (
"fmt"
"log"

"github.com/mdlayher/genetlink"
"github.com/mdlayher/netlink"
"github.com/pkg/errors"

"github.com/talos-systems/talos/internal/app/machined/internal/phase"
"github.com/talos-systems/talos/internal/pkg/event"
Expand Down Expand Up @@ -59,7 +59,7 @@ func listenForPowerButton() (err error) {
if netlink.IsNotExist(err) {
// nolint: errcheck
conn.Close()
return errors.Wrap(err, acpiGenlFamilyName+" not available")
return fmt.Errorf(acpiGenlFamilyName+" not available: %w", err)
}

var id uint32
Expand Down
4 changes: 2 additions & 2 deletions internal/app/machined/internal/phase/disk/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
package disk

import (
"github.com/pkg/errors"
"fmt"

"github.com/talos-systems/talos/internal/app/machined/internal/phase"
"github.com/talos-systems/talos/internal/pkg/runtime"
Expand Down Expand Up @@ -50,7 +50,7 @@ func (task *ResetDisk) standard() (err error) {

for _, p := range pt.Partitions() {
if err = pt.Delete(p); err != nil {
return errors.Wrap(err, "failed to delete partition")
return fmt.Errorf("failed to delete partition: %w", err)
}
}

Expand Down
8 changes: 4 additions & 4 deletions internal/app/machined/internal/phase/kubernetes/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ package kubernetes

import (
"context"
"fmt"
"log"
"syscall"

"github.com/containerd/containerd"
"github.com/containerd/containerd/api/services/tasks/v1"
"github.com/containerd/containerd/namespaces"
"github.com/pkg/errors"
"golang.org/x/sync/errgroup"

"github.com/talos-systems/talos/internal/app/machined/internal/phase"
Expand Down Expand Up @@ -63,14 +63,14 @@ func (task *KillKubernetesTasks) standard() (err error) {
log.Printf("killing task %s", task.ID)
g.Go(func() error {
if _, err = s.Kill(ctx, &tasks.KillRequest{ContainerID: task.ID, Signal: uint32(syscall.SIGTERM), All: true}); err != nil {
return errors.Wrap(err, "error killing task")
return fmt.Errorf("error killing task: %w", err)
}
// TODO(andrewrynhard): Send SIGKILL on a timeout threshold.
if _, err = s.Wait(ctx, &tasks.WaitRequest{ContainerID: task.ID}); err != nil {
return errors.Wrap(err, "error waiting on task")
return fmt.Errorf("error waiting on task: %w", err)
}
if _, err = s.Delete(ctx, &tasks.DeleteTaskRequest{ContainerID: task.ID}); err != nil {
return errors.Wrap(err, "error deleting task")
return fmt.Errorf("error deleting task: %w", err)
}

return nil
Expand Down
8 changes: 4 additions & 4 deletions internal/app/machined/internal/phase/phase.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
package phase

import (
"fmt"
"log"
goruntime "runtime"
"time"

"github.com/hashicorp/go-multierror"
"github.com/pkg/errors"

"github.com/talos-systems/talos/internal/pkg/kmsg"
"github.com/talos-systems/talos/internal/pkg/runtime"
Expand Down Expand Up @@ -57,7 +57,7 @@ func NewRunner(config runtime.Configurator) (*Runner, error) {
case runtime.Cloud:
// Setup logging to /dev/kmsg.
if _, err = kmsg.Setup("[talos]"); err != nil {
return nil, errors.Errorf("failed to setup logging to /dev/kmsg: %v", err)
return nil, fmt.Errorf("failed to setup logging to /dev/kmsg: %w", err)
}
}

Expand All @@ -84,7 +84,7 @@ func (r *RuntimeArgs) Config() runtime.Configurator {
func (r *Runner) Run() error {
for _, phase := range r.phases {
if err := r.runPhase(phase); err != nil {
return errors.Wrapf(err, "error running phase %q", phase.description)
return fmt.Errorf("error running phase %q: %w", phase.description, err)
}
}

Expand Down Expand Up @@ -130,7 +130,7 @@ func (r *Runner) runTask(task Task, errCh chan<- error) {
if r := recover(); r != nil {
buf := make([]byte, 8192)
n := goruntime.Stack(buf, false)
err = errors.Errorf("panic recovered: %v\n%s", r, string(buf[:n]))
err = fmt.Errorf("panic recovered: %v\n%s", r, string(buf[:n]))
}
}()

Expand Down
12 changes: 5 additions & 7 deletions internal/app/machined/internal/phase/rootfs/etc/etc.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
"strings"
"text/template"

"github.com/pkg/errors"

"github.com/talos-systems/talos/pkg/version"

"golang.org/x/sys/unix"
Expand Down Expand Up @@ -74,11 +72,11 @@ func Hosts(hostname string) (err error) {
}

if err = ioutil.WriteFile("/run/system/etc/hosts", writer.Bytes(), 0644); err != nil {
return fmt.Errorf("write /run/hosts: %v", err)
return fmt.Errorf("write /run/hosts: %w", err)
}

if err = unix.Mount("/run/system/etc/hosts", "/etc/hosts", "", unix.MS_BIND, ""); err != nil {
return errors.Wrap(err, "failed to create bind mount for /etc/hosts")
return fmt.Errorf("failed to create bind mount for /etc/hosts: %w", err)
}

return nil
Expand All @@ -99,7 +97,7 @@ func ResolvConf() (err error) {
defer f.Close()

if err = unix.Mount("/run/system/etc/resolv.conf", "/etc/resolv.conf", "", unix.MS_BIND, ""); err != nil {
return errors.Wrap(err, "failed to create bind mount for /etc/resolv.conf")
return fmt.Errorf("failed to create bind mount for /etc/resolv.conf: %w", err)
}

return nil
Expand Down Expand Up @@ -142,11 +140,11 @@ func OSRelease() (err error) {
}

if err = ioutil.WriteFile("/run/system/etc/os-release", writer.Bytes(), 0644); err != nil {
return fmt.Errorf("write /run/system/etc/os-release: %v", err)
return fmt.Errorf("write /run/system/etc/os-release: %w", err)
}

if err = unix.Mount("/run/system/etc/os-release", "/etc/os-release", "", unix.MS_BIND, ""); err != nil {
return errors.Wrap(err, "failed to create bind mount for /etc/os-release")
return fmt.Errorf("failed to create bind mount for /etc/os-release: %w", err)
}

return nil
Expand Down
5 changes: 2 additions & 3 deletions internal/app/machined/internal/phase/rootfs/mount_cgroups.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
package rootfs

import (
"fmt"
"io/ioutil"
"os"
"path"
"strconv"

"github.com/pkg/errors"

"github.com/talos-systems/talos/internal/app/machined/internal/phase"
"github.com/talos-systems/talos/internal/pkg/mount"
"github.com/talos-systems/talos/internal/pkg/mount/manager"
Expand Down Expand Up @@ -61,7 +60,7 @@ func (task *MountCgroups) runtime(r runtime.Runtime) (err error) {
// See https://www.kernel.org/doc/Documentation/cgroup-v1/memory.txt
target := path.Join("/sys/fs/cgroup", memoryCgroup, memoryUseHierarchy)
if err = ioutil.WriteFile(target, memoryUseHierarchyContents, memoryUseHierarchyPermissions); err != nil {
return errors.Wrap(err, "failed to enable memory hierarchy support")
return fmt.Errorf("failed to enable memory hierarchy support: %w", err)
}

return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
package rootfs

import (
// "github.com/pkg/errors"

"github.com/talos-systems/talos/internal/app/machined/internal/phase"
"github.com/talos-systems/talos/internal/pkg/mount"
"github.com/talos-systems/talos/internal/pkg/mount/manager"
Expand Down
Loading

0 comments on commit d430a37

Please sign in to comment.