From 54b2fad0330032ae1bbac990f93a3644aa8a12af Mon Sep 17 00:00:00 2001 From: Stephen Kitt Date: Mon, 26 May 2025 10:44:46 +0200 Subject: [PATCH] kubectl: drop dependency on github.com/pkg/errors The package is unmaintained, and kubectl doesn't rely on the functionality it provides on top of Golang errors (stack traces). Signed-off-by: Stephen Kitt --- hack/unwanted-dependencies.json | 1 - staging/src/k8s.io/kubectl/go.mod | 2 +- .../src/k8s.io/kubectl/pkg/cmd/apply/patcher.go | 16 +++++++--------- .../src/k8s.io/kubectl/pkg/cmd/patch/patch.go | 3 +-- 4 files changed, 9 insertions(+), 13 deletions(-) diff --git a/hack/unwanted-dependencies.json b/hack/unwanted-dependencies.json index 27ca1f485c2..12b5ff4d1b1 100644 --- a/hack/unwanted-dependencies.json +++ b/hack/unwanted-dependencies.json @@ -227,7 +227,6 @@ "github.com/pkg/errors": [ "github.com/Microsoft/hnslib", "github.com/google/cadvisor", - "k8s.io/kubectl", "k8s.io/kubernetes", "sigs.k8s.io/kustomize/api", "sigs.k8s.io/kustomize/kustomize/v5" diff --git a/staging/src/k8s.io/kubectl/go.mod b/staging/src/k8s.io/kubectl/go.mod index 1461e83a267..185c1534516 100644 --- a/staging/src/k8s.io/kubectl/go.mod +++ b/staging/src/k8s.io/kubectl/go.mod @@ -22,7 +22,6 @@ require ( github.com/moby/term v0.5.0 github.com/onsi/ginkgo/v2 v2.21.0 github.com/onsi/gomega v1.35.1 - github.com/pkg/errors v0.9.1 github.com/russross/blackfriday/v2 v2.1.0 github.com/spf13/cobra v1.8.1 github.com/spf13/pflag v1.0.6 @@ -76,6 +75,7 @@ require ( github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f // indirect github.com/opencontainers/go-digest v1.0.0 // indirect github.com/peterbourgon/diskv v2.0.1+incompatible // indirect + github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/x448/float16 v0.8.4 // indirect github.com/xlab/treeprint v1.2.0 // indirect diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/apply/patcher.go b/staging/src/k8s.io/kubectl/pkg/cmd/apply/patcher.go index 5f2f1423d3f..c5b99ce6644 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/apply/patcher.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/apply/patcher.go @@ -23,8 +23,6 @@ import ( "io" "time" - "github.com/pkg/errors" - "github.com/jonboulle/clockwork" apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/meta" @@ -61,7 +59,7 @@ const ( // patchRetryBackOffPeriod is the period to back off when apply patch results in error. var patchRetryBackOffPeriod = 1 * time.Second -var createPatchErrFormat = "creating patch with:\noriginal:\n%s\nmodified:\n%s\ncurrent:\n%s\nfor:" +var createPatchErrFormat = "creating patch with:\noriginal:\n%s\nmodified:\n%s\ncurrent:\n%s\nfor: %w" // Patcher defines options to patch OpenAPI objects. type Patcher struct { @@ -120,13 +118,13 @@ func (p *Patcher) patchSimple(obj runtime.Object, modified []byte, namespace, na // Serialize the current configuration of the object from the server. current, err := runtime.Encode(unstructured.UnstructuredJSONScheme, obj) if err != nil { - return nil, nil, errors.Wrapf(err, "serializing current configuration from:\n%v\nfor:", obj) + return nil, nil, fmt.Errorf("serializing current configuration from:\n%v\nfor: %w", obj, err) } // Retrieve the original configuration of the object from the annotation. original, err := util.GetOriginalConfiguration(obj) if err != nil { - return nil, nil, errors.Wrapf(err, "retrieving original configuration from:\n%v\nfor:", obj) + return nil, nil, fmt.Errorf("retrieving original configuration from:\n%v\nfor: %w", obj, err) } var patchType types.PatchType @@ -178,17 +176,17 @@ func (p *Patcher) patchSimple(obj runtime.Object, modified []byte, namespace, na patchType = types.StrategicMergePatchType patch, err = p.buildStrategicMergeFromBuiltins(versionedObj, original, modified, current) if err != nil { - return nil, nil, errors.Wrapf(err, createPatchErrFormat, original, modified, current) + return nil, nil, fmt.Errorf(createPatchErrFormat, original, modified, current, err) } } else { if !runtime.IsNotRegisteredError(err) { - return nil, nil, errors.Wrapf(err, "getting instance of versioned object for %v:", p.Mapping.GroupVersionKind) + return nil, nil, fmt.Errorf("getting instance of versioned object for %v: %w", p.Mapping.GroupVersionKind, err) } patchType = types.MergePatchType patch, err = p.buildMergePatch(original, modified, current) if err != nil { - return nil, nil, errors.Wrapf(err, createPatchErrFormat, original, modified, current) + return nil, nil, fmt.Errorf(createPatchErrFormat, original, modified, current, err) } } } @@ -200,7 +198,7 @@ func (p *Patcher) patchSimple(obj runtime.Object, modified []byte, namespace, na if p.ResourceVersion != nil { patch, err = addResourceVersion(patch, *p.ResourceVersion) if err != nil { - return nil, nil, errors.Wrap(err, "Failed to insert resourceVersion in patch") + return nil, nil, fmt.Errorf("failed to insert resourceVersion in patch: %w", err) } } diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/patch/patch.go b/staging/src/k8s.io/kubectl/pkg/cmd/patch/patch.go index d67b978e278..8f60ba4f7fd 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/patch/patch.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/patch/patch.go @@ -22,7 +22,6 @@ import ( "reflect" "strings" - "github.com/pkg/errors" "github.com/spf13/cobra" jsonpatch "gopkg.in/evanphx/json-patch.v4" "k8s.io/klog/v2" @@ -260,7 +259,7 @@ func (o *PatchOptions) RunPatch() error { patchedObj, err := helper.Patch(namespace, name, patchType, patchBytes, nil) if err != nil { if apierrors.IsUnsupportedMediaType(err) { - return errors.Wrap(err, fmt.Sprintf("%s is not supported by %s", patchType, mapping.GroupVersionKind)) + return fmt.Errorf("%s is not supported by %s: %w", patchType, mapping.GroupVersionKind, err) } return err }