diff --git a/go.mod b/go.mod index 89ea385d80..6731a3ca9f 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,6 @@ require ( github.com/google/go-jsonnet v0.18.0 github.com/jsonnet-bundler/jsonnet-bundler v0.4.1-0.20200708074244-ada055a225fa github.com/oklog/run v1.1.0 - github.com/pkg/errors v0.9.1 github.com/prometheus/client_golang v1.12.2 github.com/prometheus/client_model v0.2.0 github.com/prometheus/common v0.34.0 @@ -73,6 +72,7 @@ require ( github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f // indirect github.com/onsi/ginkgo v1.16.5 // indirect github.com/onsi/gomega v1.17.0 // indirect + github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/prometheus/procfs v0.7.3 // indirect golang.org/x/crypto v0.0.0-20220315160706-3147a52a75dd // indirect diff --git a/internal/store/builder.go b/internal/store/builder.go index 783cfeea87..70f658dfdd 100644 --- a/internal/store/builder.go +++ b/internal/store/builder.go @@ -18,12 +18,12 @@ package store import ( "context" + "fmt" "reflect" "sort" "strconv" "strings" - "github.com/pkg/errors" "github.com/prometheus/client_golang/prometheus" admissionregistrationv1 "k8s.io/api/admissionregistration/v1" appsv1 "k8s.io/api/apps/v1" @@ -93,7 +93,7 @@ func (b *Builder) WithMetrics(r prometheus.Registerer) { func (b *Builder) WithEnabledResources(r []string) error { for _, col := range r { if !resourceExists(col) { - return errors.Errorf("resource %s does not exist. Available resources: %s", col, strings.Join(availableResources(), ",")) + return fmt.Errorf("resource %s does not exist. Available resources: %s", col, strings.Join(availableResources(), ",")) } } diff --git a/internal/store/cronjob.go b/internal/store/cronjob.go index cf43eadf91..f7b82deb86 100644 --- a/internal/store/cronjob.go +++ b/internal/store/cronjob.go @@ -18,9 +18,10 @@ package store import ( "context" + "errors" + "fmt" "time" - "github.com/pkg/errors" "github.com/robfig/cron/v3" batchv1 "k8s.io/api/batch/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -329,7 +330,7 @@ func createCronJobListWatch(kubeClient clientset.Interface, ns string, fieldSele func getNextScheduledTime(schedule string, lastScheduleTime *metav1.Time, createdTime metav1.Time) (time.Time, error) { sched, err := cron.ParseStandard(schedule) if err != nil { - return time.Time{}, errors.Wrapf(err, "Failed to parse cron job schedule '%s'", schedule) + return time.Time{}, fmt.Errorf("Failed to parse cron job schedule '%s': %w", schedule, err) } if !lastScheduleTime.IsZero() { return sched.Next(lastScheduleTime.Time), nil diff --git a/internal/store/testutils.go b/internal/store/testutils.go index 4b8e569ca1..8de6c52ae2 100644 --- a/internal/store/testutils.go +++ b/internal/store/testutils.go @@ -25,7 +25,6 @@ import ( "strings" "github.com/google/go-cmp/cmp" - "github.com/pkg/errors" "k8s.io/kube-state-metrics/v2/pkg/metric" ) @@ -54,7 +53,7 @@ func (testCase *generateMetricsTestCase) run() error { out := headers + "\n" + metrics if err := compareOutput(testCase.Want, out); err != nil { - return errors.Wrap(err, "expected wanted output to equal output") + return fmt.Errorf("expected wanted output to equal output: %w", err) } return nil @@ -70,7 +69,7 @@ func compareOutput(expected, actual string) error { } if diff := cmp.Diff(entities[0], entities[1]); diff != "" { - return errors.Errorf("(-want, +got):\n%s", diff) + return fmt.Errorf("(-want, +got):\n%s", diff) } return nil diff --git a/pkg/allowdenylist/allowdenylist.go b/pkg/allowdenylist/allowdenylist.go index 27188018ef..bf6a427d53 100644 --- a/pkg/allowdenylist/allowdenylist.go +++ b/pkg/allowdenylist/allowdenylist.go @@ -17,12 +17,11 @@ limitations under the License. package allowdenylist import ( + "errors" "regexp" "strings" generator "k8s.io/kube-state-metrics/v2/pkg/metric_generator" - - "github.com/pkg/errors" ) // AllowDenyList encapsulates the logic needed to filter based on a string. diff --git a/pkg/app/server.go b/pkg/app/server.go index d8205de28f..2f97c42cdb 100644 --- a/pkg/app/server.go +++ b/pkg/app/server.go @@ -26,7 +26,6 @@ import ( "time" "github.com/oklog/run" - "github.com/pkg/errors" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/collectors" "github.com/prometheus/client_golang/prometheus/promauto" @@ -250,7 +249,7 @@ func createKubeClient(apiserver string, kubeconfig string, factories ...customre klog.Infof("Testing communication with server") v, err := kubeClient.Discovery().ServerVersion() if err != nil { - return nil, nil, nil, errors.Wrap(err, "error while trying to communicate with apiserver") + return nil, nil, nil, fmt.Errorf("error while trying to communicate with apiserver: %w", err) } klog.Infof("Running with Kubernetes cluster version: v%s.%s. git version: %s. git tree state: %s. commit: %s. platform: %s", v.Major, v.Minor, v.GitVersion, v.GitTreeState, v.GitCommit, v.Platform) diff --git a/pkg/metricshandler/metrics_handler.go b/pkg/metricshandler/metrics_handler.go index e08d1f21fc..c1f8705c74 100644 --- a/pkg/metricshandler/metrics_handler.go +++ b/pkg/metricshandler/metrics_handler.go @@ -19,13 +19,14 @@ package metricshandler import ( "compress/gzip" "context" + "errors" + "fmt" "io" "net/http" "strconv" "strings" "sync" - "github.com/pkg/errors" appsv1 "k8s.io/api/apps/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/fields" @@ -103,7 +104,7 @@ func (m *MetricsHandler) Run(ctx context.Context) error { klog.Infof("Auto detecting sharding settings.") ss, err := detectStatefulSet(m.kubeClient, m.opts.Pod, m.opts.Namespace) if err != nil { - return errors.Wrap(err, "detect StatefulSet") + return fmt.Errorf("detect StatefulSet: %w", err) } statefulSetName := ss.Name @@ -211,7 +212,7 @@ func (m *MetricsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func shardingSettingsFromStatefulSet(ss *appsv1.StatefulSet, podName string) (nominal int32, totalReplicas int, err error) { nominal, err = detectNominalFromPod(ss.Name, podName) if err != nil { - return 0, 0, errors.Wrap(err, "detecting Pod nominal") + return 0, 0, fmt.Errorf("detecting Pod nominal: %w", err) } totalReplicas = 1 @@ -227,7 +228,7 @@ func detectNominalFromPod(statefulSetName, podName string) (int32, error) { nominalString := strings.TrimPrefix(podName, statefulSetName+"-") nominal, err := strconv.Atoi(nominalString) if err != nil { - return 0, errors.Wrapf(err, "failed to detect shard index for Pod %s of StatefulSet %s, parsed %s", podName, statefulSetName, nominalString) + return 0, fmt.Errorf("failed to detect shard index for Pod %s of StatefulSet %s, parsed %s: %w", podName, statefulSetName, nominalString, err) } return int32(nominal), nil @@ -236,7 +237,7 @@ func detectNominalFromPod(statefulSetName, podName string) (int32, error) { func detectStatefulSet(kubeClient kubernetes.Interface, podName, namespaceName string) (*appsv1.StatefulSet, error) { p, err := kubeClient.CoreV1().Pods(namespaceName).Get(context.TODO(), podName, metav1.GetOptions{}) if err != nil { - return nil, errors.Wrapf(err, "retrieve pod %s for sharding", podName) + return nil, fmt.Errorf("retrieve pod %s for sharding: %w", podName, err) } owners := p.GetOwnerReferences() @@ -247,11 +248,11 @@ func detectStatefulSet(kubeClient kubernetes.Interface, podName, namespaceName s ss, err := kubeClient.AppsV1().StatefulSets(namespaceName).Get(context.TODO(), o.Name, metav1.GetOptions{}) if err != nil { - return nil, errors.Wrapf(err, "retrieve shard's StatefulSet: %s/%s", namespaceName, o.Name) + return nil, fmt.Errorf("retrieve shard's StatefulSet: %s/%s: %w", namespaceName, o.Name, err) } return ss, nil } - return nil, errors.Errorf("no suitable statefulset found for auto detecting sharding for Pod %s/%s", namespaceName, podName) + return nil, fmt.Errorf("no suitable statefulset found for auto detecting sharding for Pod %s/%s", namespaceName, podName) } diff --git a/tests/e2e/framework/framework.go b/tests/e2e/framework/framework.go index c8fd75ffd4..65e37463fa 100644 --- a/tests/e2e/framework/framework.go +++ b/tests/e2e/framework/framework.go @@ -18,6 +18,7 @@ package framework import ( "bytes" + "errors" "fmt" "io" "net/http" @@ -25,7 +26,6 @@ import ( "path" "strings" - "github.com/pkg/errors" dto "github.com/prometheus/client_model/go" "github.com/prometheus/common/expfmt" ) @@ -165,7 +165,7 @@ func (f *Framework) ParseMetrics(metrics func(io.Writer) error) (map[string]*dto buf := &bytes.Buffer{} err := metrics(buf) if err != nil { - return nil, errors.Wrap(err, "Failed to get metrics") + return nil, fmt.Errorf("Failed to get metrics: %w", err) } parser := &expfmt.TextParser{} diff --git a/tests/e2e/main_test.go b/tests/e2e/main_test.go index 363997ca49..5594207750 100644 --- a/tests/e2e/main_test.go +++ b/tests/e2e/main_test.go @@ -29,7 +29,6 @@ import ( "strings" "testing" - "github.com/pkg/errors" "github.com/prometheus/client_golang/prometheus/testutil/promlint" dto "github.com/prometheus/client_model/go" @@ -148,7 +147,7 @@ func getLabelsDocumentation() (map[string][]string, error) { docPath := "../../docs/" docFiles, err := os.ReadDir(docPath) if err != nil { - return nil, errors.Wrap(err, "failed to read documentation directory") + return nil, fmt.Errorf("failed to read documentation directory: %w", err) } // Match file names such as daemonset-metrics.md @@ -168,7 +167,7 @@ func getLabelsDocumentation() (map[string][]string, error) { filePath := path.Join(docPath, file.Name()) f, err := os.Open(filePath) if err != nil { - return nil, errors.Wrapf(err, "cannot read file %s", filePath) + return nil, fmt.Errorf("cannot read file %s: %w", filePath, err) } scanner := bufio.NewScanner(f) for scanner.Scan() { @@ -183,7 +182,7 @@ func getLabelsDocumentation() (map[string][]string, error) { labelPatterns := make([]string, len(labels)) for i, l := range labels { if len(l) <= 1 { - return nil, errors.Errorf("Label documentation %s did not match regex", labelsDoc) + return nil, fmt.Errorf("Label documentation %s did not match regex", labelsDoc) } labelPatterns[i] = patternRe.ReplaceAllString(l[1], "_.*") }