Skip to content

Commit

Permalink
use go standard errors (#2382)
Browse files Browse the repository at this point in the history
Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
  • Loading branch information
mmorel-35 authored Jan 2, 2024
1 parent 6268983 commit b935d77
Show file tree
Hide file tree
Showing 25 changed files with 99 additions and 100 deletions.
4 changes: 2 additions & 2 deletions api/v1alpha1/validation/securitypolicy_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ func ValidateJWTProvider(providers []egv1a1.JWTProvider) error {
if _, err := url.ParseRequestURI(provider.Issuer); err != nil {
_, err := mail.ParseAddress(provider.Issuer)
if err != nil {
errs = append(errs, fmt.Errorf("invalid issuer; must be a URL or email address: %v", err))
errs = append(errs, fmt.Errorf("invalid issuer; must be a URL or email address: %w", err))
}
}
case len(provider.RemoteJWKS.URI) == 0:
errs = append(errs, fmt.Errorf("uri must be set for remote JWKS provider: %s", provider.Name))
}
if _, err := url.ParseRequestURI(provider.RemoteJWKS.URI); err != nil {
errs = append(errs, fmt.Errorf("invalid remote JWKS URI: %v", err))
errs = append(errs, fmt.Errorf("invalid remote JWKS URI: %w", err))
}

if len(errs) == 0 {
Expand Down
31 changes: 14 additions & 17 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require (
github.com/google/go-cmp v0.6.0
github.com/grafana/tempo v1.5.0
github.com/miekg/dns v1.1.57
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.18.0
github.com/prometheus/common v0.45.0
github.com/spf13/cobra v1.8.0
github.com/spf13/pflag v1.0.5
Expand Down Expand Up @@ -45,27 +45,15 @@ require (
k8s.io/utils v0.0.0-20230726121419-3b25d923346b
sigs.k8s.io/controller-runtime v0.16.3
sigs.k8s.io/gateway-api v1.0.0
sigs.k8s.io/mcs-api v0.1.0
sigs.k8s.io/yaml v1.4.0
)

require (
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0 // indirect
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f // indirect
go.opentelemetry.io/otel/sdk v1.21.0 // indirect
go.opentelemetry.io/otel/trace v1.21.0 // indirect
golang.org/x/mod v0.13.0 // indirect
golang.org/x/sync v0.4.0 // indirect
golang.org/x/tools v0.14.0 // indirect
)

require (
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/MakeNowJust/heredoc v1.0.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/census-instrumentation/opencensus-proto v0.4.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/chai2010/gettext-go v1.0.2 // indirect
Expand All @@ -76,6 +64,7 @@ require (
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-asn1-ber/asn1-ber v1.5.4 // indirect
github.com/go-errors/errors v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-openapi/jsonpointer v0.20.0 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.4 // indirect
Expand All @@ -85,7 +74,9 @@ require (
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/google/uuid v1.3.1 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
Expand All @@ -95,30 +86,37 @@ require (
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de // indirect
github.com/lyft/gostats v0.4.1 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/moby/spdystream v0.2.0 // indirect
github.com/moby/term v0.0.0-20221205130635-1aeaba878587 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f // 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/prometheus/client_golang v1.18.0
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/tsaarni/x500dn v1.0.0 // indirect
github.com/xlab/treeprint v1.2.0 // indirect
go.opentelemetry.io/otel/sdk v1.21.0 // indirect
go.opentelemetry.io/otel/trace v1.21.0 // indirect
go.starlark.net v0.0.0-20230525235612-a134d8f9ddca // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/mod v0.13.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/oauth2 v0.13.0 // indirect
golang.org/x/sync v0.4.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/term v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.14.0 // indirect
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto v0.0.0-20231002182017-d307bd883b97 // indirect
Expand All @@ -132,6 +130,5 @@ require (
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/kustomize/api v0.13.5-0.20230601165947-6ce0bf390ce3 // indirect
sigs.k8s.io/kustomize/kyaml v0.14.3-0.20230601165947-6ce0bf390ce3 // indirect
sigs.k8s.io/mcs-api v0.1.0
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
)
8 changes: 4 additions & 4 deletions internal/cmd/certgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,17 @@ func certGen() error {

certs, err := crypto.GenerateCerts(cfg)
if err != nil {
return fmt.Errorf("failed to generate certificates: %v", err)
return fmt.Errorf("failed to generate certificates: %w", err)
}
log.Info("generated certificates")

cli, err := client.New(clicfg.GetConfigOrDie(), client.Options{Scheme: envoygateway.GetScheme()})
if err != nil {
return fmt.Errorf("failed to create controller-runtime client: %v", err)
return fmt.Errorf("failed to create controller-runtime client: %w", err)
}

if err := outputCerts(ctrl.SetupSignalHandler(), cli, cfg, certs); err != nil {
return fmt.Errorf("failed to output certificates: %v", err)
return fmt.Errorf("failed to output certificates: %w", err)
}

return nil
Expand All @@ -79,7 +79,7 @@ func outputCerts(ctx context.Context, cli client.Client, cfg *config.Server, cer
return nil
}

return fmt.Errorf("failed to create or update secrets: %v", err)
return fmt.Errorf("failed to create or update secrets: %w", err)
}

for i := range secrets {
Expand Down
12 changes: 6 additions & 6 deletions internal/cmd/egctl/envoy_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,17 +148,17 @@ func setupEnvoyServerStatsConfig(kubeClient kubernetes.CLIClient, podName, podNa

fw, err := portForwarder(kubeClient, types.NamespacedName{Namespace: podNamespace, Name: podName})
if err != nil {
return "", fmt.Errorf("failed to initialize pod-forwarding for %s/%s: %v", podNamespace, podName, err)
return "", fmt.Errorf("failed to initialize pod-forwarding for %s/%s: %w", podNamespace, podName, err)
}
err = fw.Start()
if err != nil {
return "", fmt.Errorf("failed to start port forwarding for pod %s/%s: %v", podNamespace, podName, err)
return "", fmt.Errorf("failed to start port forwarding for pod %s/%s: %w", podNamespace, podName, err)
}
defer fw.Stop()

result, err := statsRequest(fw.Address(), path)
if err != nil {
return "", fmt.Errorf("failed to get stats on envoy for pod %s/%s: %v", podNamespace, podName, err)
return "", fmt.Errorf("failed to get stats on envoy for pod %s/%s: %w", podNamespace, podName, err)
}
return string(result), nil
}
Expand All @@ -171,17 +171,17 @@ func setupEnvoyClusterStatsConfig(kubeClient kubernetes.CLIClient, podName, podN
}
fw, err := portForwarder(kubeClient, types.NamespacedName{Namespace: podNamespace, Name: podName})
if err != nil {
return "", fmt.Errorf("failed to initialize pod-forwarding for %s/%s: %v", podNamespace, podName, err)
return "", fmt.Errorf("failed to initialize pod-forwarding for %s/%s: %w", podNamespace, podName, err)
}
err = fw.Start()
if err != nil {
return "", fmt.Errorf("failed to start port forwarding for pod %s/%s: %v", podNamespace, podName, err)
return "", fmt.Errorf("failed to start port forwarding for pod %s/%s: %w", podNamespace, podName, err)
}
defer fw.Stop()

result, err := statsRequest(fw.Address(), path)
if err != nil {
return "", fmt.Errorf("failed to get stats on envoy for pod %s/%s: %v", podNamespace, podName, err)
return "", fmt.Errorf("failed to get stats on envoy for pod %s/%s: %w", podNamespace, podName, err)
}
return string(result), nil
}
Expand Down
3 changes: 1 addition & 2 deletions internal/cmd/egctl/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"io"
"sort"

"github.com/pkg/errors"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -95,7 +94,7 @@ func versions(w io.Writer, containerName, output string, remote bool) error {

pods, err := c.PodsForSelector(metav1.NamespaceAll, "control-plane=envoy-gateway")
if err != nil {
return errors.Wrap(err, "list EG pods failed")
return fmt.Errorf("list EG pods failed: %w", err)
}

for _, pod := range pods.Items {
Expand Down
2 changes: 1 addition & 1 deletion internal/crypto/certgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func newCert(request *certificateRequest) ([]byte, []byte, error) {

newKey, err := rsa.GenerateKey(rand.Reader, keySize)
if err != nil {
return nil, nil, fmt.Errorf("cannot generate key: %v", err)
return nil, nil, fmt.Errorf("cannot generate key: %w", err)
}

now := time.Now()
Expand Down
2 changes: 1 addition & 1 deletion internal/crypto/certgen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func verifyCert(certPEM []byte, roots *x509.CertPool, dnsname string, currentTim
CurrentTime: currentTime,
}
if _, err = cert.Verify(opts); err != nil {
return fmt.Errorf("certificate verification failed: %s", err)
return fmt.Errorf("certificate verification failed: %w", err)
}

return nil
Expand Down
22 changes: 11 additions & 11 deletions internal/infrastructure/kubernetes/infra.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ package kubernetes

import (
"context"
"fmt"

"github.com/pkg/errors"
appsv1 "k8s.io/api/apps/v1"
autoscalingv2 "k8s.io/api/autoscaling/v2"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -55,23 +55,23 @@ func NewInfra(cli client.Client, cfg *config.Server) *Infra {
// provided ResourceRender, if it doesn't exist and updates it if it does.
func (i *Infra) createOrUpdate(ctx context.Context, r ResourceRender) error {
if err := i.createOrUpdateServiceAccount(ctx, r); err != nil {
return errors.Wrapf(err, "failed to create or update serviceaccount %s/%s", i.Namespace, r.Name())
return fmt.Errorf("failed to create or update serviceaccount %s/%s: %w", i.Namespace, r.Name(), err)
}

if err := i.createOrUpdateConfigMap(ctx, r); err != nil {
return errors.Wrapf(err, "failed to create or update configmap %s/%s", i.Namespace, r.Name())
return fmt.Errorf("failed to create or update configmap %s/%s: %w", i.Namespace, r.Name(), err)
}

if err := i.createOrUpdateDeployment(ctx, r); err != nil {
return errors.Wrapf(err, "failed to create or update deployment %s/%s", i.Namespace, r.Name())
return fmt.Errorf("failed to create or update deployment %s/%s: %w", i.Namespace, r.Name(), err)
}

if err := i.createOrUpdateService(ctx, r); err != nil {
return errors.Wrapf(err, "failed to create or update service %s/%s", i.Namespace, r.Name())
return fmt.Errorf("failed to create or update service %s/%s: %w", i.Namespace, r.Name(), err)
}

if err := i.createOrUpdateHPA(ctx, r); err != nil {
return errors.Wrapf(err, "failed to create or update hpa %s/%s", i.Namespace, r.Name())
return fmt.Errorf("failed to create or update hpa %s/%s: %w", i.Namespace, r.Name(), err)
}

return nil
Expand All @@ -80,23 +80,23 @@ func (i *Infra) createOrUpdate(ctx context.Context, r ResourceRender) error {
// delete deletes the ServiceAccount/ConfigMap/Deployment/Service in the kube api server, if it exists.
func (i *Infra) delete(ctx context.Context, r ResourceRender) error {
if err := i.deleteServiceAccount(ctx, r); err != nil {
return errors.Wrapf(err, "failed to delete serviceaccount %s/%s", i.Namespace, r.Name())
return fmt.Errorf("failed to delete serviceaccount %s/%s: %w", i.Namespace, r.Name(), err)
}

if err := i.deleteConfigMap(ctx, r); err != nil {
return errors.Wrapf(err, "failed to delete configmap %s/%s", i.Namespace, r.Name())
return fmt.Errorf("failed to delete configmap %s/%s: %w", i.Namespace, r.Name(), err)
}

if err := i.deleteDeployment(ctx, r); err != nil {
return errors.Wrapf(err, "failed to delete deployment %s/%s", i.Namespace, r.Name())
return fmt.Errorf("failed to delete deployment %s/%s: %w", i.Namespace, r.Name(), err)
}

if err := i.deleteService(ctx, r); err != nil {
return errors.Wrapf(err, "failed to delete service %s/%s", i.Namespace, r.Name())
return fmt.Errorf("failed to delete service %s/%s: %w", i.Namespace, r.Name(), err)
}

if err := i.deleteHPA(ctx, r); err != nil {
return errors.Wrapf(err, "failed to delete hpa %s/%s", i.Namespace, r.Name())
return fmt.Errorf("failed to delete hpa %s/%s: %w", i.Namespace, r.Name(), err)
}

return nil
Expand Down
6 changes: 3 additions & 3 deletions internal/infrastructure/kubernetes/infra_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ package kubernetes

import (
"context"
"fmt"

"github.com/pkg/errors"
kerrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/util/retry"
Expand All @@ -31,7 +31,7 @@ func (cli *InfraClient) CreateOrUpdate(ctx context.Context, key client.ObjectKey
if kerrors.IsNotFound(err) {
// Create if it does not exist.
if err := cli.Client.Create(ctx, specific); err != nil {
return errors.Wrap(err, "for Create")
return fmt.Errorf("for Create: %w", err)
}
}
} else {
Expand All @@ -40,7 +40,7 @@ func (cli *InfraClient) CreateOrUpdate(ctx context.Context, key client.ObjectKey
if updateChecker() {
specific.SetUID(current.GetUID())
if err := cli.Client.Update(ctx, specific); err != nil {
return errors.Wrap(err, "for Update")
return fmt.Errorf("for Update: %w", err)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions internal/kubernetes/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,10 @@ func (c *client) PodExec(namespacedName types.NamespacedName, container string,
defer func() {
if err != nil {
if len(stderr) > 0 {
err = fmt.Errorf("error exec into %s/%s container %s: %v\n%s",
err = fmt.Errorf("error exec into %s/%s container %s: %w\n%s",
namespacedName.Namespace, namespacedName.Name, container, err, stderr)
} else {
err = fmt.Errorf("error exec into %s/%s container %s: %v",
err = fmt.Errorf("error exec into %s/%s container %s: %w",
namespacedName.Namespace, namespacedName.Name, container, err)
}
}
Expand Down
9 changes: 4 additions & 5 deletions internal/kubernetes/port-forwarder.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"net/http"
"os"

"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/portforward"
Expand Down Expand Up @@ -53,7 +52,7 @@ func NewLocalPortForwarder(client CLIClient, namespacedName types.NamespacedName
// get a random port
p, err := netutil.LocalAvailablePort()
if err != nil {
return nil, errors.Wrapf(err, "failed to get a local available port for Pod %q", namespacedName)
return nil, fmt.Errorf("failed to get a local available port for Pod %q: %w", namespacedName, err)
}
f.localPort = p
}
Expand Down Expand Up @@ -90,7 +89,7 @@ func (f *localForwarder) Start() error {

select {
case err := <-errCh:
return errors.Wrap(err, "failed to start port forwarder")
return fmt.Errorf("failed to start port forwarder: %w", err)
case <-readyCh:
return nil
}
Expand All @@ -107,7 +106,7 @@ func (f *localForwarder) buildKubernetesPortForwarder(readyCh chan struct{}) (*p

roundTripper, upgrader, err := spdy.RoundTripperFor(f.RESTConfig())
if err != nil {
return nil, fmt.Errorf("failure creating roundtripper: %v", err)
return nil, fmt.Errorf("failure creating roundtripper: %w", err)
}

dialer := spdy.NewDialer(upgrader, &http.Client{Transport: roundTripper}, http.MethodPost, serverURL)
Expand All @@ -119,7 +118,7 @@ func (f *localForwarder) buildKubernetesPortForwarder(readyCh chan struct{}) (*p
io.Discard,
os.Stderr)
if err != nil {
return nil, fmt.Errorf("failed establishing portforward: %v", err)
return nil, fmt.Errorf("failed establishing portforward: %w", err)
}

return fw, nil
Expand Down
2 changes: 1 addition & 1 deletion internal/kubernetes/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ package kubernetes

import (
"context"
"errors"
"fmt"

"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
k8smachinery "k8s.io/apimachinery/pkg/types"
k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
Expand Down
Loading

0 comments on commit b935d77

Please sign in to comment.