replace hashicorp multierror with core errors

Signed-off-by: Joe Julian <me@joejulian.name>
pull/12298/head
Joe Julian 2 years ago
parent a3cbe35b5f
commit 08f232b147
No known key found for this signature in database
GPG Key ID: FAB12BE0575D999B

@ -18,7 +18,6 @@ require (
github.com/gobwas/glob v0.2.3 github.com/gobwas/glob v0.2.3
github.com/gofrs/flock v0.8.1 github.com/gofrs/flock v0.8.1
github.com/gosuri/uitable v0.0.4 github.com/gosuri/uitable v0.0.4
github.com/hashicorp/go-multierror v1.1.1
github.com/jmoiron/sqlx v1.3.5 github.com/jmoiron/sqlx v1.3.5
github.com/lib/pq v1.10.9 github.com/lib/pq v1.10.9
github.com/mattn/go-shellwords v1.0.12 github.com/mattn/go-shellwords v1.0.12
@ -96,7 +95,6 @@ require (
github.com/gorilla/handlers v1.5.1 // indirect github.com/gorilla/handlers v1.5.1 // indirect
github.com/gorilla/mux v1.8.0 // indirect github.com/gorilla/mux v1.8.0 // indirect
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7 // indirect github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/huandu/xstrings v1.4.0 // indirect github.com/huandu/xstrings v1.4.0 // indirect
github.com/imdario/mergo v0.3.13 // indirect github.com/imdario/mergo v0.3.13 // indirect

@ -265,11 +265,6 @@ github.com/gosuri/uitable v0.0.4 h1:IG2xLKRvErL3uhY6e1BylFzG+aJiwQviDDTfOKeKTpY=
github.com/gosuri/uitable v0.0.4/go.mod h1:tKR86bXuXPZazfOTG1FIzvjIdXzd0mo4Vtn16vt0PJo= github.com/gosuri/uitable v0.0.4/go.mod h1:tKR86bXuXPZazfOTG1FIzvjIdXzd0mo4Vtn16vt0PJo=
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7 h1:pdN6V1QBWetyv/0+wjACpqVH+eVULgEjkurDLq3goeM= github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7 h1:pdN6V1QBWetyv/0+wjACpqVH+eVULgEjkurDLq3goeM=
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA= github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc= github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc=

@ -20,6 +20,7 @@ import (
"bytes" "bytes"
"context" "context"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"io" "io"
"os" "os"
@ -30,14 +31,13 @@ import (
"time" "time"
jsonpatch "github.com/evanphx/json-patch" jsonpatch "github.com/evanphx/json-patch"
"github.com/pkg/errors" errorsp "github.com/pkg/errors"
batch "k8s.io/api/batch/v1" batch "k8s.io/api/batch/v1"
v1 "k8s.io/api/core/v1" v1 "k8s.io/api/core/v1"
apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apiextv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1" apiextv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
apierrors "k8s.io/apimachinery/pkg/api/errors" apierrors "k8s.io/apimachinery/pkg/api/errors"
multierror "github.com/hashicorp/go-multierror"
"k8s.io/apimachinery/pkg/api/meta" "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@ -59,7 +59,7 @@ import (
) )
// ErrNoObjectsVisited indicates that during a visit operation, no matching objects were found. // ErrNoObjectsVisited indicates that during a visit operation, no matching objects were found.
var ErrNoObjectsVisited = errors.New("no objects visited") var ErrNoObjectsVisited = errorsp.New("no objects visited")
var metadataAccessor = meta.NewAccessor() var metadataAccessor = meta.NewAccessor()
@ -118,13 +118,13 @@ func (c *Client) IsReachable() error {
if err == genericclioptions.ErrEmptyConfig { if err == genericclioptions.ErrEmptyConfig {
// re-replace kubernetes ErrEmptyConfig error with a friendy error // re-replace kubernetes ErrEmptyConfig error with a friendy error
// moar workarounds for Kubernetes API breaking. // moar workarounds for Kubernetes API breaking.
return errors.New("Kubernetes cluster unreachable") return errorsp.New("Kubernetes cluster unreachable")
} }
if err != nil { if err != nil {
return errors.Wrap(err, "Kubernetes cluster unreachable") return errorsp.Wrap(err, "Kubernetes cluster unreachable")
} }
if _, err := client.ServerVersion(); err != nil { if _, err := client.ServerVersion(); err != nil {
return errors.Wrap(err, "Kubernetes cluster unreachable") return errorsp.Wrap(err, "Kubernetes cluster unreachable")
} }
return nil return nil
} }
@ -391,7 +391,7 @@ func (c *Client) Update(original, target ResourceList, force bool) (*Result, err
helper := resource.NewHelper(info.Client, info.Mapping).WithFieldManager(getManagedFieldsManager()) helper := resource.NewHelper(info.Client, info.Mapping).WithFieldManager(getManagedFieldsManager())
if _, err := helper.Get(info.Namespace, info.Name); err != nil { if _, err := helper.Get(info.Namespace, info.Name); err != nil {
if !apierrors.IsNotFound(err) { if !apierrors.IsNotFound(err) {
return errors.Wrap(err, "could not get information about the resource") return errorsp.Wrap(err, "could not get information about the resource")
} }
// Append the created resource to the results, even if something fails // Append the created resource to the results, even if something fails
@ -399,7 +399,7 @@ func (c *Client) Update(original, target ResourceList, force bool) (*Result, err
// Since the resource does not exist, create it. // Since the resource does not exist, create it.
if err := createResource(info); err != nil { if err := createResource(info); err != nil {
return errors.Wrap(err, "failed to create resource") return errorsp.Wrap(err, "failed to create resource")
} }
kind := info.Mapping.GroupVersionKind.Kind kind := info.Mapping.GroupVersionKind.Kind
@ -410,7 +410,7 @@ func (c *Client) Update(original, target ResourceList, force bool) (*Result, err
originalInfo := original.Get(info) originalInfo := original.Get(info)
if originalInfo == nil { if originalInfo == nil {
kind := info.Mapping.GroupVersionKind.Kind kind := info.Mapping.GroupVersionKind.Kind
return errors.Errorf("no %s with the name %q found", kind, info.Name) return errorsp.Errorf("no %s with the name %q found", kind, info.Name)
} }
if err := updateResource(c, info, originalInfo.Object, force); err != nil { if err := updateResource(c, info, originalInfo.Object, force); err != nil {
@ -427,7 +427,7 @@ func (c *Client) Update(original, target ResourceList, force bool) (*Result, err
case err != nil: case err != nil:
return res, err return res, err
case len(updateErrors) != 0: case len(updateErrors) != 0:
return res, errors.Errorf(strings.Join(updateErrors, " && ")) return res, errorsp.Errorf(strings.Join(updateErrors, " && "))
} }
for _, info := range original.Difference(target) { for _, info := range original.Difference(target) {
@ -493,7 +493,7 @@ func delete(c *Client, resources ResourceList, propagation metav1.DeletionPropag
return nil return nil
}) })
if err != nil { if err != nil {
if errors.Is(err, ErrNoObjectsVisited) { if errorsp.Is(err, ErrNoObjectsVisited) {
err = fmt.Errorf("object not found, skipping delete: %w", err) err = fmt.Errorf("object not found, skipping delete: %w", err)
} }
errs = append(errs, err) errs = append(errs, err)
@ -543,7 +543,7 @@ func perform(infos ResourceList, fn func(*resource.Info) error) error {
for range infos { for range infos {
err := <-errs err := <-errs
if err != nil { if err != nil {
result = multierror.Append(result, err) result = errors.Join(result, err)
} }
} }
@ -604,24 +604,24 @@ func deleteResource(info *resource.Info, policy metav1.DeletionPropagation) erro
func createPatch(target *resource.Info, current runtime.Object) ([]byte, types.PatchType, error) { func createPatch(target *resource.Info, current runtime.Object) ([]byte, types.PatchType, error) {
oldData, err := json.Marshal(current) oldData, err := json.Marshal(current)
if err != nil { if err != nil {
return nil, types.StrategicMergePatchType, errors.Wrap(err, "serializing current configuration") return nil, types.StrategicMergePatchType, errorsp.Wrap(err, "serializing current configuration")
} }
newData, err := json.Marshal(target.Object) newData, err := json.Marshal(target.Object)
if err != nil { if err != nil {
return nil, types.StrategicMergePatchType, errors.Wrap(err, "serializing target configuration") return nil, types.StrategicMergePatchType, errorsp.Wrap(err, "serializing target configuration")
} }
// Fetch the current object for the three way merge // Fetch the current object for the three way merge
helper := resource.NewHelper(target.Client, target.Mapping).WithFieldManager(getManagedFieldsManager()) helper := resource.NewHelper(target.Client, target.Mapping).WithFieldManager(getManagedFieldsManager())
currentObj, err := helper.Get(target.Namespace, target.Name) currentObj, err := helper.Get(target.Namespace, target.Name)
if err != nil && !apierrors.IsNotFound(err) { if err != nil && !apierrors.IsNotFound(err) {
return nil, types.StrategicMergePatchType, errors.Wrapf(err, "unable to get data for current object %s/%s", target.Namespace, target.Name) return nil, types.StrategicMergePatchType, errorsp.Wrapf(err, "unable to get data for current object %s/%s", target.Namespace, target.Name)
} }
// Even if currentObj is nil (because it was not found), it will marshal just fine // Even if currentObj is nil (because it was not found), it will marshal just fine
currentData, err := json.Marshal(currentObj) currentData, err := json.Marshal(currentObj)
if err != nil { if err != nil {
return nil, types.StrategicMergePatchType, errors.Wrap(err, "serializing live configuration") return nil, types.StrategicMergePatchType, errorsp.Wrap(err, "serializing live configuration")
} }
// Get a versioned object // Get a versioned object
@ -644,7 +644,7 @@ func createPatch(target *resource.Info, current runtime.Object) ([]byte, types.P
patchMeta, err := strategicpatch.NewPatchMetaFromStruct(versionedObject) patchMeta, err := strategicpatch.NewPatchMetaFromStruct(versionedObject)
if err != nil { if err != nil {
return nil, types.StrategicMergePatchType, errors.Wrap(err, "unable to create patch metadata from object") return nil, types.StrategicMergePatchType, errorsp.Wrap(err, "unable to create patch metadata from object")
} }
patch, err := strategicpatch.CreateThreeWayMergePatch(oldData, newData, currentData, patchMeta, true) patch, err := strategicpatch.CreateThreeWayMergePatch(oldData, newData, currentData, patchMeta, true)
@ -663,13 +663,13 @@ func updateResource(c *Client, target *resource.Info, currentObj runtime.Object,
var err error var err error
obj, err = helper.Replace(target.Namespace, target.Name, true, target.Object) obj, err = helper.Replace(target.Namespace, target.Name, true, target.Object)
if err != nil { if err != nil {
return errors.Wrap(err, "failed to replace object") return errorsp.Wrap(err, "failed to replace object")
} }
c.Log("Replaced %q with kind %s for kind %s", target.Name, currentObj.GetObjectKind().GroupVersionKind().Kind, kind) c.Log("Replaced %q with kind %s for kind %s", target.Name, currentObj.GetObjectKind().GroupVersionKind().Kind, kind)
} else { } else {
patch, patchType, err := createPatch(target, currentObj) patch, patchType, err := createPatch(target, currentObj)
if err != nil { if err != nil {
return errors.Wrap(err, "failed to create patch") return errorsp.Wrap(err, "failed to create patch")
} }
if patch == nil || string(patch) == "{}" { if patch == nil || string(patch) == "{}" {
@ -677,7 +677,7 @@ func updateResource(c *Client, target *resource.Info, currentObj runtime.Object,
// This needs to happen to make sure that Helm has the latest info from the API // This needs to happen to make sure that Helm has the latest info from the API
// Otherwise there will be no labels and other functions that use labels will panic // Otherwise there will be no labels and other functions that use labels will panic
if err := target.Get(); err != nil { if err := target.Get(); err != nil {
return errors.Wrap(err, "failed to refresh resource information") return errorsp.Wrap(err, "failed to refresh resource information")
} }
return nil return nil
} }
@ -685,7 +685,7 @@ func updateResource(c *Client, target *resource.Info, currentObj runtime.Object,
c.Log("Patch %s %q in namespace %s", kind, target.Name, target.Namespace) c.Log("Patch %s %q in namespace %s", kind, target.Name, target.Namespace)
obj, err = helper.Patch(target.Namespace, target.Name, patchType, patch, nil) obj, err = helper.Patch(target.Namespace, target.Name, patchType, patch, nil)
if err != nil { if err != nil {
return errors.Wrapf(err, "cannot patch %q with kind %s", target.Name, kind) return errorsp.Wrapf(err, "cannot patch %q with kind %s", target.Name, kind)
} }
} }
@ -743,7 +743,7 @@ func (c *Client) watchUntilReady(timeout time.Duration, info *resource.Info) err
case watch.Error: case watch.Error:
// Handle error and return with an error. // Handle error and return with an error.
c.Log("Error event for %s", info.Name) c.Log("Error event for %s", info.Name)
return true, errors.Errorf("failed to deploy %s", info.Name) return true, errorsp.Errorf("failed to deploy %s", info.Name)
default: default:
return false, nil return false, nil
} }
@ -757,14 +757,14 @@ func (c *Client) watchUntilReady(timeout time.Duration, info *resource.Info) err
func (c *Client) waitForJob(obj runtime.Object, name string) (bool, error) { func (c *Client) waitForJob(obj runtime.Object, name string) (bool, error) {
o, ok := obj.(*batch.Job) o, ok := obj.(*batch.Job)
if !ok { if !ok {
return true, errors.Errorf("expected %s to be a *batch.Job, got %T", name, obj) return true, errorsp.Errorf("expected %s to be a *batch.Job, got %T", name, obj)
} }
for _, c := range o.Status.Conditions { for _, c := range o.Status.Conditions {
if c.Type == batch.JobComplete && c.Status == "True" { if c.Type == batch.JobComplete && c.Status == "True" {
return true, nil return true, nil
} else if c.Type == batch.JobFailed && c.Status == "True" { } else if c.Type == batch.JobFailed && c.Status == "True" {
return true, errors.Errorf("job failed: %s", c.Reason) return true, errorsp.Errorf("job failed: %s", c.Reason)
} }
} }
@ -778,7 +778,7 @@ func (c *Client) waitForJob(obj runtime.Object, name string) (bool, error) {
func (c *Client) waitForPodSuccess(obj runtime.Object, name string) (bool, error) { func (c *Client) waitForPodSuccess(obj runtime.Object, name string) (bool, error) {
o, ok := obj.(*v1.Pod) o, ok := obj.(*v1.Pod)
if !ok { if !ok {
return true, errors.Errorf("expected %s to be a *v1.Pod, got %T", name, obj) return true, errorsp.Errorf("expected %s to be a *v1.Pod, got %T", name, obj)
} }
switch o.Status.Phase { switch o.Status.Phase {
@ -786,7 +786,7 @@ func (c *Client) waitForPodSuccess(obj runtime.Object, name string) (bool, error
c.Log("Pod %s succeeded", o.Name) c.Log("Pod %s succeeded", o.Name)
return true, nil return true, nil
case v1.PodFailed: case v1.PodFailed:
return true, errors.Errorf("pod %s failed", o.Name) return true, errorsp.Errorf("pod %s failed", o.Name)
case v1.PodPending: case v1.PodPending:
c.Log("Pod %s pending", o.Name) c.Log("Pod %s pending", o.Name)
case v1.PodRunning: case v1.PodRunning:
@ -804,7 +804,7 @@ func scrubValidationError(err error) error {
const stopValidateMessage = "if you choose to ignore these errors, turn validation off with --validate=false" const stopValidateMessage = "if you choose to ignore these errors, turn validation off with --validate=false"
if strings.Contains(err.Error(), stopValidateMessage) { if strings.Contains(err.Error(), stopValidateMessage) {
return errors.New(strings.ReplaceAll(err.Error(), "; "+stopValidateMessage, "")) return errorsp.New(strings.ReplaceAll(err.Error(), "; "+stopValidateMessage, ""))
} }
return err return err
} }

Loading…
Cancel
Save