Use github.com/pkg/errors
Signed-off-by: Lantao Liu <lantaol@google.com>
This commit is contained in:
@@ -18,8 +18,8 @@ package sandbox
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
runtime "k8s.io/kubernetes/pkg/kubelet/apis/cri/runtime/v1alpha2"
|
||||
)
|
||||
|
||||
@@ -76,5 +76,5 @@ func (c *Metadata) UnmarshalJSON(data []byte) error {
|
||||
*c = Metadata(versioned.Metadata)
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("unsupported version: %q", versioned.Version)
|
||||
return errors.Errorf("unsupported version: %q", versioned.Version)
|
||||
}
|
||||
|
||||
@@ -17,14 +17,13 @@ limitations under the License.
|
||||
package sandbox
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"sync"
|
||||
|
||||
cnins "github.com/containernetworking/plugins/pkg/ns"
|
||||
"github.com/docker/docker/pkg/mount"
|
||||
"github.com/docker/docker/pkg/symlink"
|
||||
"github.com/pkg/errors"
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
@@ -43,7 +42,7 @@ type NetNS struct {
|
||||
func NewNetNS() (*NetNS, error) {
|
||||
netns, err := cnins.NewNS()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to setup network namespace %v", err)
|
||||
return nil, errors.Wrap(err, "failed to setup network namespace")
|
||||
}
|
||||
n := new(NetNS)
|
||||
n.ns = netns
|
||||
@@ -63,7 +62,7 @@ func LoadNetNS(path string) (*NetNS, error) {
|
||||
os.RemoveAll(path) // nolint: errcheck
|
||||
return nil, ErrClosedNetNS
|
||||
}
|
||||
return nil, fmt.Errorf("failed to load network namespace %v", err)
|
||||
return nil, errors.Wrap(err, "failed to load network namespace")
|
||||
}
|
||||
return &NetNS{ns: ns, restored: true}, nil
|
||||
}
|
||||
@@ -76,7 +75,7 @@ func (n *NetNS) Remove() error {
|
||||
if !n.closed {
|
||||
err := n.ns.Close()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to close network namespace: %v", err)
|
||||
return errors.Wrap(err, "failed to close network namespace")
|
||||
}
|
||||
n.closed = true
|
||||
}
|
||||
@@ -88,24 +87,24 @@ func (n *NetNS) Remove() error {
|
||||
if os.IsNotExist(err) {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("failed to stat netns: %v", err)
|
||||
return errors.Wrap(err, "failed to stat netns")
|
||||
}
|
||||
path, err := symlink.FollowSymlinkInScope(path, "/")
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to follow symlink: %v", err)
|
||||
return errors.Wrap(err, "failed to follow symlink")
|
||||
}
|
||||
mounted, err := mount.Mounted(path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to check netns mounted: %v", err)
|
||||
return errors.Wrap(err, "failed to check netns mounted")
|
||||
}
|
||||
if mounted {
|
||||
err := unix.Unmount(path, unix.MNT_DETACH)
|
||||
if err != nil && !os.IsNotExist(err) {
|
||||
return fmt.Errorf("failed to umount netns: %v", err)
|
||||
return errors.Wrap(err, "failed to umount netns")
|
||||
}
|
||||
}
|
||||
if err := os.RemoveAll(path); err != nil {
|
||||
return fmt.Errorf("failed to remove netns: %v", err)
|
||||
return errors.Wrap(err, "failed to remove netns")
|
||||
}
|
||||
n.restored = false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user