Skip to content

Commit

Permalink
Merge pull request kubernetes#1808 from mrueg/drop-pkg-errors
Browse files Browse the repository at this point in the history
Replace pkg/errors with stdlib errors
  • Loading branch information
k8s-ci-robot authored Aug 16, 2022
2 parents 767af98 + 2f20203 commit 9063fa6
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 25 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions internal/store/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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(), ","))
}
}

Expand Down
5 changes: 3 additions & 2 deletions internal/store/cronjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions internal/store/testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"strings"

"github.com/google/go-cmp/cmp"
"github.com/pkg/errors"

"k8s.io/kube-state-metrics/v2/pkg/metric"
)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
3 changes: 1 addition & 2 deletions pkg/allowdenylist/allowdenylist.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 1 addition & 2 deletions pkg/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
15 changes: 8 additions & 7 deletions pkg/metricshandler/metrics_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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()
Expand All @@ -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)
}
4 changes: 2 additions & 2 deletions tests/e2e/framework/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ package framework

import (
"bytes"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"path"
"strings"

"github.com/pkg/errors"
dto "github.com/prometheus/client_model/go"
"github.com/prometheus/common/expfmt"
)
Expand Down Expand Up @@ -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{}
Expand Down
7 changes: 3 additions & 4 deletions tests/e2e/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
Expand All @@ -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() {
Expand All @@ -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], "_.*")
}
Expand Down

0 comments on commit 9063fa6

Please sign in to comment.