diff --git a/cmd/allocator/main.go b/cmd/allocator/main.go index 93065d3621..eec46e6027 100644 --- a/cmd/allocator/main.go +++ b/cmd/allocator/main.go @@ -17,6 +17,7 @@ import ( "context" "crypto/tls" "crypto/x509" + stderrors "errors" "fmt" "net" "net/http" @@ -36,9 +37,9 @@ import ( "agones.dev/agones/pkg/gameserverallocations/processor" "agones.dev/agones/pkg/gameservers" "agones.dev/agones/pkg/metrics" + "agones.dev/agones/pkg/util/errors" "agones.dev/agones/pkg/util/fswatch" "github.com/heptiolabs/healthcheck" - "github.com/pkg/errors" "github.com/sirupsen/logrus" "github.com/spf13/pflag" "github.com/spf13/viper" @@ -65,6 +66,7 @@ import ( var ( podReady bool logger = runtime.NewLoggerWithSource("main") + errs = errors.FromPackage() ) const ( @@ -270,7 +272,7 @@ func main() { grpcHealth := grpchealth.NewServer() // only used for gRPC, ignored o/w health.AddReadinessCheck("allocator-agones-client", func() error { if !podReady { - return errors.New("asked to shut down, failed readiness check") + return errs.New("asked to shut down, failed readiness check") } _, err := agonesClient.ServerVersion() if err != nil { @@ -443,7 +445,7 @@ func runHTTP(listenCtx context.Context, workerCtx context.Context, h *serviceHan err = server.ListenAndServe() } - if err == http.ErrServerClosed { + if stderrors.Is(err, http.ErrServerClosed) { logger.WithError(err).Info("HTTP/HTTPS server closed") os.Exit(0) } @@ -488,6 +490,7 @@ func newProcessorServiceHandler(processorClient processor.Client, mTLSDisabled, tlsDisabled: tlsDisabled, processorClient: processorClient, } + h.errs = errors.FromStruct(&h) if !h.tlsDisabled { tlsCert, err := readTLSCert() @@ -536,6 +539,7 @@ func newServiceHandler(ctx context.Context, kubeClient kubernetes.Interface, ago tlsDisabled: tlsDisabled, grpcUnallocatedStatusCode: grpcUnallocatedStatusCode, } + h.errs = errors.FromStruct(&h) kubeInformerFactory.Start(ctx.Done()) agonesInformerFactory.Start(ctx.Done()) @@ -645,7 +649,7 @@ func (h *serviceHandler) getTLSCert(_ *tls.ClientHelloInfo) (*tls.Certificate, e // VerifyConnection runs on resumption as well, which closes that gap. func (h *serviceHandler) verifyClientConnection(cs tls.ConnectionState) error { if len(cs.PeerCertificates) == 0 { - return errors.New("no client certificate presented") + return h.errs.New("no client certificate presented") } rawCerts := make([][]byte, 0, len(cs.PeerCertificates)) @@ -670,7 +674,7 @@ func (h *serviceHandler) verifyClientCertificate(rawCerts [][]byte, _ [][]*x509. cert, err := x509.ParseCertificate(rawCert) if err != nil { logger.WithError(err).Warning("cannot parse intermediate certificate") - return errors.New("bad intermediate certificate: " + err.Error()) + return h.errs.Wrap(err, "bad intermediate certificate") } opts.Intermediates.AddCert(cert) } @@ -678,7 +682,7 @@ func (h *serviceHandler) verifyClientCertificate(rawCerts [][]byte, _ [][]*x509. c, err := x509.ParseCertificate(rawCerts[0]) if err != nil { logger.WithError(err).Warning("cannot parse client certificate") - return errors.New("bad client certificate: " + err.Error()) + return h.errs.Wrap(err, "bad client certificate") } h.certMutex.RLock() @@ -686,7 +690,7 @@ func (h *serviceHandler) verifyClientCertificate(rawCerts [][]byte, _ [][]*x509. _, err = c.Verify(opts) if err != nil { logger.WithError(err).Warning("failed to verify client certificate") - return errors.New("failed to verify client certificate: " + err.Error()) + return h.errs.Wrap(err, "failed to verify client certificate") } return nil } @@ -696,7 +700,7 @@ func getClients(ctlConfig config) (*kubernetes.Clientset, *versioned.Clientset, // Create the in-cluster config config, err := rest.InClusterConfig() if err != nil { - return nil, nil, errors.New("Could not create in cluster config") + return nil, nil, errs.Wrap(err, "Could not create in cluster config") } config.QPS = float32(ctlConfig.APIServerSustainedQPS) @@ -705,13 +709,13 @@ func getClients(ctlConfig config) (*kubernetes.Clientset, *versioned.Clientset, // Access to the Agones resources through the Agones Clientset kubeClient, err := kubernetes.NewForConfig(config) if err != nil { - return nil, nil, errors.New("Could not create the kubernetes api clientset") + return nil, nil, errs.Wrap(err, "Could not create the kubernetes api clientset") } // Access to the Agones resources through the Agones Clientset agonesClient, err := versioned.NewForConfig(config) if err != nil { - return nil, nil, errors.New("Could not create the agones api clientset") + return nil, nil, errs.Wrap(err, "Could not create the agones api clientset") } return kubeClient, agonesClient, nil } @@ -762,6 +766,8 @@ type serviceHandler struct { grpcUnallocatedStatusCode codes.Code processorClient processor.Client + + errs *errors.Errors } // Allocate implements the Allocate gRPC method definition diff --git a/cmd/allocator/main_test.go b/cmd/allocator/main_test.go index f9ac6551e4..4822e81d24 100644 --- a/cmd/allocator/main_test.go +++ b/cmd/allocator/main_test.go @@ -24,6 +24,7 @@ import ( pb "agones.dev/agones/pkg/allocation/go" allocationv1 "agones.dev/agones/pkg/apis/allocation/v1" + "agones.dev/agones/pkg/util/errors" "github.com/stretchr/testify/assert" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" @@ -186,6 +187,7 @@ func TestVerifyClientCertificateFails(t *testing.T) { h := serviceHandler{ caCertPool: certPool, } + h.errs = errors.FromStruct(&h) block, _ := pem.Decode(crt) input := [][]byte{block.Bytes} diff --git a/cmd/controller/main.go b/cmd/controller/main.go index c02b3475aa..9573f409d8 100644 --- a/cmd/controller/main.go +++ b/cmd/controller/main.go @@ -36,12 +36,12 @@ import ( "agones.dev/agones/pkg/gameserversets" "agones.dev/agones/pkg/metrics" "agones.dev/agones/pkg/portallocator" + "agones.dev/agones/pkg/util/errors" "agones.dev/agones/pkg/util/httpserver" "agones.dev/agones/pkg/util/runtime" "agones.dev/agones/pkg/util/signals" "github.com/google/uuid" "github.com/heptiolabs/healthcheck" - "github.com/pkg/errors" "github.com/sirupsen/logrus" "github.com/spf13/pflag" "github.com/spf13/viper" @@ -96,6 +96,7 @@ const ( var ( logger = runtime.NewLoggerWithSource("main") + errs = errors.FromPackage() ) func setupLogging(logDir string, logSizeLimitMB int) { @@ -529,13 +530,13 @@ func (c *config) validate() []error { func validateResource(request resource.Quantity, limit resource.Quantity, resourceName corev1.ResourceName) []error { validationErrors := make([]error, 0) if !limit.IsZero() && request.Cmp(limit) > 0 { - validationErrors = append(validationErrors, errors.Errorf("Request must be less than or equal to %s limit", resourceName)) + validationErrors = append(validationErrors, errs.Errorf("Request must be less than or equal to %s limit", resourceName)) } if request.Cmp(resource.Quantity{}) < 0 { - validationErrors = append(validationErrors, errors.Errorf("Resource %s request value must be non negative", resourceName)) + validationErrors = append(validationErrors, errs.Errorf("Resource %s request value must be non negative", resourceName)) } if limit.Cmp(resource.Quantity{}) < 0 { - validationErrors = append(validationErrors, errors.Errorf("Resource %s limit value must be non negative", resourceName)) + validationErrors = append(validationErrors, errs.Errorf("Resource %s limit value must be non negative", resourceName)) } return validationErrors @@ -565,11 +566,11 @@ func validatePorts(portRanges map[string]portallocator.PortRange) []error { if overlaps(values[j].MinPort, values[j].MaxPort, pr.MinPort, pr.MaxPort) { switch { case keys[j] == agonesv1.DefaultPortRange: - validationErrors = append(validationErrors, errors.Errorf("port range %s overlaps with min/max port", keys[i])) + validationErrors = append(validationErrors, errs.Errorf("port range %s overlaps with min/max port", keys[i])) case keys[i] == agonesv1.DefaultPortRange: - validationErrors = append(validationErrors, errors.Errorf("port range %s overlaps with min/max port", keys[j])) + validationErrors = append(validationErrors, errs.Errorf("port range %s overlaps with min/max port", keys[j])) default: - validationErrors = append(validationErrors, errors.Errorf("port range %s overlaps with min/max port of range %s", keys[i], keys[j])) + validationErrors = append(validationErrors, errs.Errorf("port range %s overlaps with min/max port of range %s", keys[i], keys[j])) } } } @@ -584,10 +585,10 @@ func validatePortRange(minPort, maxPort int32, rangeName string) []error { rangeCtx = " for port range " + rangeName } if minPort <= 0 || maxPort <= 0 { - validationErrors = append(validationErrors, errors.New("min Port and Max Port values are required"+rangeCtx)) + validationErrors = append(validationErrors, errs.New("min Port and Max Port values are required"+rangeCtx)) } if maxPort < minPort { - validationErrors = append(validationErrors, errors.New("max Port cannot be set less that the Min Port"+rangeCtx)) + validationErrors = append(validationErrors, errs.New("max Port cannot be set less that the Min Port"+rangeCtx)) } return validationErrors } diff --git a/cmd/controller/main_test.go b/cmd/controller/main_test.go index e8a2ea426c..14ad410c3b 100644 --- a/cmd/controller/main_test.go +++ b/cmd/controller/main_test.go @@ -15,6 +15,7 @@ package main import ( + "strings" "testing" agonesv1 "agones.dev/agones/pkg/apis/agones/v1" @@ -116,7 +117,7 @@ func TestControllerConfigValidation_PortRangeOverlap(t *testing.T) { func errorsContainString(t *testing.T, errs []error, expected string) { found := false for _, v := range errs { - if expected == v.Error() { + if strings.Contains(v.Error(), expected) { found = true break } diff --git a/cmd/extensions/main.go b/cmd/extensions/main.go index 4154b857b8..65871bae27 100644 --- a/cmd/extensions/main.go +++ b/cmd/extensions/main.go @@ -36,13 +36,13 @@ import ( "agones.dev/agones/pkg/gameserversets" "agones.dev/agones/pkg/metrics" "agones.dev/agones/pkg/util/apiserver" + "agones.dev/agones/pkg/util/errors" "agones.dev/agones/pkg/util/https" "agones.dev/agones/pkg/util/httpserver" "agones.dev/agones/pkg/util/runtime" "agones.dev/agones/pkg/util/signals" "agones.dev/agones/pkg/util/webhooks" "github.com/heptiolabs/healthcheck" - "github.com/pkg/errors" "github.com/sirupsen/logrus" "github.com/spf13/pflag" "github.com/spf13/viper" @@ -78,6 +78,7 @@ const ( var ( podReady bool logger = runtime.NewLoggerWithSource("main") + errs = errors.FromPackage() ) func setupLogging(logDir string, logSizeLimitMB int) { @@ -174,7 +175,7 @@ func main() { podReady = true health.AddReadinessCheck("agones-extensions", func() error { if !podReady { - return errors.New("asked to shut down, failed readiness check") + return errs.New("asked to shut down, failed readiness check") } return nil }) diff --git a/cmd/ping/main.go b/cmd/ping/main.go index 63cb605177..2b1ebbaa36 100644 --- a/cmd/ping/main.go +++ b/cmd/ping/main.go @@ -22,10 +22,10 @@ import ( "time" "agones.dev/agones/pkg" + "agones.dev/agones/pkg/util/errors" "agones.dev/agones/pkg/util/runtime" "agones.dev/agones/pkg/util/signals" "github.com/heptiolabs/healthcheck" - "github.com/pkg/errors" "github.com/spf13/pflag" "github.com/spf13/viper" "golang.org/x/time/rate" @@ -42,6 +42,7 @@ const ( var ( logger = runtime.NewLoggerWithSource("main") + errs = errors.FromPackage() ) func main() { @@ -121,7 +122,7 @@ type config struct { // validate returns an error if there is a validation problem func (c *config) validate() error { if c.UDPRateLimit < 0 { - return errors.New("UDP Rate limit must be greater that or equal to zero") + return errs.New("UDP Rate limit must be greater that or equal to zero") } return nil diff --git a/cmd/ping/udp.go b/cmd/ping/udp.go index fdbdc0d4ae..e0c029e89c 100644 --- a/cmd/ping/udp.go +++ b/cmd/ping/udp.go @@ -17,16 +17,16 @@ package main import ( "bytes" "context" + stderrors "errors" "math" "net" "os" "sync" "time" + "agones.dev/agones/pkg/util/errors" "agones.dev/agones/pkg/util/runtime" - "github.com/pkg/errors" - "github.com/sirupsen/logrus" "golang.org/x/time/rate" "k8s.io/apimachinery/pkg/util/wait" @@ -45,6 +45,7 @@ type udpServer struct { limits map[string]*visitor healthMutex sync.RWMutex health bool + errs *errors.Errors } // visitor tracks when a visitor last sent @@ -65,6 +66,7 @@ func newUDPServer(rateLimit rate.Limit) *udpServer { limits: map[string]*visitor{}, } udpSrv.logger = runtime.NewLoggerWithType(udpSrv) + udpSrv.errs = errors.FromStruct(udpSrv) return udpSrv } @@ -112,7 +114,7 @@ func (u *udpServer) readWriteLoop(ctx context.Context) { b := make([]byte, 1024) _, sender, err := u.conn.ReadFrom(b) if err != nil { - if ctx.Err() != nil && errors.Is(err, os.ErrClosed) { + if ctx.Err() != nil && stderrors.Is(err, os.ErrClosed) { return } u.logger.WithError(err).Error("Error reading udp packet") @@ -175,7 +177,7 @@ func (u *udpServer) Health() error { u.healthMutex.RLock() defer u.healthMutex.RUnlock() if !u.health { - return errors.New("UDP Server is unhealthy") + return u.errs.New("UDP Server is unhealthy") } return nil } diff --git a/cmd/processor/main.go b/cmd/processor/main.go index 64393400dd..b5475d633f 100644 --- a/cmd/processor/main.go +++ b/cmd/processor/main.go @@ -32,13 +32,13 @@ import ( "agones.dev/agones/pkg/gameserverallocations/processor" "agones.dev/agones/pkg/gameservers" "agones.dev/agones/pkg/metrics" + "agones.dev/agones/pkg/util/errors" "agones.dev/agones/pkg/util/httpserver" "agones.dev/agones/pkg/util/runtime" "agones.dev/agones/pkg/util/signals" "github.com/google/uuid" "github.com/heptiolabs/healthcheck" - "github.com/pkg/errors" "github.com/sirupsen/logrus" "github.com/spf13/pflag" "github.com/spf13/viper" @@ -77,6 +77,7 @@ const ( var ( logger = runtime.NewLoggerWithSource("main") + errs = errors.FromPackage() ) type processorConfig struct { @@ -356,7 +357,7 @@ func getClients(ctlConfig processorConfig) (*kubernetes.Clientset, *versioned.Cl // Create the in-cluster config config, err := rest.InClusterConfig() if err != nil { - return nil, nil, errors.Wrap(err, "Could not create in cluster config") + return nil, nil, errs.Wrap(err, "Could not create in cluster config") } config.QPS = float32(ctlConfig.APIServerSustainedQPS) @@ -365,13 +366,13 @@ func getClients(ctlConfig processorConfig) (*kubernetes.Clientset, *versioned.Cl // Access to the Agones resources through the Agones Clientset kubeClient, err := kubernetes.NewForConfig(config) if err != nil { - return nil, nil, errors.Wrap(err, "Could not create the kubernetes api clientset") + return nil, nil, errs.Wrap(err, "Could not create the kubernetes api clientset") } // Access to the Agones resources through the Agones Clientset agonesClient, err := versioned.NewForConfig(config) if err != nil { - return nil, nil, errors.Wrap(err, "Could not create the agones api clientset") + return nil, nil, errs.Wrap(err, "Could not create the agones api clientset") } return kubeClient, agonesClient, nil } diff --git a/cmd/sdk-server/main.go b/cmd/sdk-server/main.go index ed078c317c..3112fd0ca3 100644 --- a/cmd/sdk-server/main.go +++ b/cmd/sdk-server/main.go @@ -26,7 +26,6 @@ import ( "time" gwruntime "github.com/grpc-ecosystem/grpc-gateway/v2/runtime" - "github.com/pkg/errors" "github.com/sirupsen/logrus" "github.com/spf13/pflag" "github.com/spf13/viper" @@ -42,6 +41,7 @@ import ( sdkalpha "agones.dev/agones/pkg/sdk/alpha" sdkbeta "agones.dev/agones/pkg/sdk/beta" "agones.dev/agones/pkg/sdkserver" + "agones.dev/agones/pkg/util/errors" "agones.dev/agones/pkg/util/runtime" "agones.dev/agones/pkg/util/signals" ) @@ -76,6 +76,7 @@ const ( var ( logger = runtime.NewLoggerWithSource("main") + errs = errors.FromPackage() ) func main() { @@ -201,7 +202,7 @@ func registerLocal(grpcServer *grpc.Server, ctlConf config) (func(), error) { } if _, err = os.Stat(filePath); os.IsNotExist(err) { - return nil, errors.Errorf("Could not find file: %s", filePath) + return nil, errs.Errorf("Could not find file: %s", filePath) } }