Remove github.com/pkg/errors with appropriate replacements (#3172)

* errors.Wrap* removal

Signed-off-by: Sanket Sudake <sanketsudake@gmail.com>

* remove errors.Errorf

Signed-off-by: Sanket Sudake <sanketsudake@gmail.com>

* Remove remaining calls

Signed-off-by: Sanket Sudake <sanketsudake@gmail.com>

* Few more errors

Signed-off-by: Sanket Sudake <sanketsudake@gmail.com>

* Fix golint errors

Signed-off-by: Sanket Sudake <sanketsudake@gmail.com>

---------

Signed-off-by: Sanket Sudake <sanketsudake@gmail.com>
This commit is contained in:
Sanket Sudake
2025-02-20 08:58:37 +05:30
committed by GitHub
parent 2fd44174ce
commit caffed92f4
113 changed files with 585 additions and 588 deletions
+2 -2
View File
@@ -4,12 +4,12 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"time"
"github.com/pkg/errors"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.uber.org/zap"
"golang.org/x/net/context/ctxhttp"
@@ -104,7 +104,7 @@ func sendRequest(logger *zap.Logger, ctx context.Context, httpClient *http.Clien
// skip retry and return directly due to context deadline exceeded
if err == context.DeadlineExceeded {
msg := "error specializing function pod, either increase the specialization timeout for function or check function pod log would help."
err = errors.Wrap(err, msg)
err = fmt.Errorf("%s: %w", msg, err)
logger.Error(msg, zap.Error(err), zap.String("url", url))
return nil, err
}
+1 -2
View File
@@ -7,7 +7,6 @@ import (
"path/filepath"
"github.com/hashicorp/go-multierror"
"github.com/pkg/errors"
apiv1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -305,7 +304,7 @@ func (cfg *Config) addFetcherToPodSpecWithCommand(podSpec *apiv1.PodSpec, mainCo
for _, existingContainer := range podSpec.Containers {
existingContainerNames = append(existingContainerNames, existingContainer.Name)
}
return errors.Errorf("could not find main container '%s' in given PodSpec. Found: %v",
return fmt.Errorf("could not find main container '%s' in given PodSpec. Found: %v",
mainContainerName,
existingContainerNames)
}
+27 -27
View File
@@ -20,6 +20,7 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
@@ -29,7 +30,6 @@ import (
"time"
"github.com/google/uuid"
"github.com/pkg/errors"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.uber.org/zap"
"golang.org/x/net/context/ctxhttp"
@@ -92,21 +92,21 @@ func MakeFetcher(logger *zap.Logger, clientGen crd.ClientGeneratorInterface, sha
fissionClient, err := clientGen.GetFissionClient()
if err != nil {
return nil, errors.Wrap(err, "error making the fission client")
return nil, fmt.Errorf("error making the fission client: %w", err)
}
kubeClient, err := clientGen.GetKubernetesClient()
if err != nil {
return nil, errors.Wrap(err, "error making the kube client")
return nil, fmt.Errorf("error making the kube client: %w", err)
}
name, err := os.ReadFile(podInfoMountDir + "/name")
if err != nil {
return nil, errors.Wrap(err, "error reading pod name from downward volume")
return nil, fmt.Errorf("error reading pod name from downward volume: %w", err)
}
namespace, err := os.ReadFile(podInfoMountDir + "/namespace")
if err != nil {
return nil, errors.Wrap(err, "error reading pod namespace from downward volume")
return nil, fmt.Errorf("error reading pod namespace from downward volume: %w", err)
}
hc := &http.Client{Transport: otelhttp.NewTransport(http.DefaultTransport)}
@@ -140,7 +140,7 @@ func writeSecretOrConfigMap(dataMap map[string][]byte, dirPath string) error {
writeFilePath := filepath.Join(dirPath, key)
err := os.WriteFile(writeFilePath, val, 0750)
if err != nil {
return errors.Wrapf(err, "Failed to write file %s", writeFilePath)
return fmt.Errorf("failed to write file %s: %w", writeFilePath, err)
}
}
return nil
@@ -255,7 +255,7 @@ func (fetcher *Fetcher) Fetch(ctx context.Context, pkg *fv1.Package, req Functio
storePath, err := utils.SanitizeFilePath(filepath.Join(fetcher.sharedVolumePath, req.Filename), fetcher.sharedVolumePath)
if err != nil {
logger.Error(err.Error(), zap.String("filename", req.Filename))
return http.StatusBadRequest, errors.New(fmt.Sprintf("%s, request: %v", err, req))
return http.StatusBadRequest, fmt.Errorf("%s, request: %v", err, req)
}
// verify first if the file already exists.
@@ -270,7 +270,7 @@ func (fetcher *Fetcher) Fetch(ctx context.Context, pkg *fv1.Package, req Functio
tmpPath, err := utils.SanitizeFilePath(storePath+".tmp", fetcher.sharedVolumePath)
if err != nil {
logger.Error(err.Error(), zap.String("filename", req.Filename))
return http.StatusBadRequest, errors.New(fmt.Sprintf("%s, request: %v", err, req))
return http.StatusBadRequest, fmt.Errorf("%s, request: %v", err, req)
}
if req.FetchType == fv1.FETCH_URL {
@@ -284,7 +284,7 @@ func (fetcher *Fetcher) Fetch(ctx context.Context, pkg *fv1.Package, req Functio
if err != nil {
e := "failed to download url"
logger.Error(e, zap.Error(err), zap.String("url", req.Url))
return http.StatusBadRequest, errors.Wrapf(err, "%s: %s", e, req.Url)
return http.StatusBadRequest, fmt.Errorf("%s: %s: %w", e, req.Url, err)
}
} else {
var archive *fv1.Archive
@@ -301,7 +301,7 @@ func (fetcher *Fetcher) Fetch(ctx context.Context, pkg *fv1.Package, req Functio
zap.String("package_name", pkg.ObjectMeta.Name),
zap.String("package_namespace", pkg.ObjectMeta.Namespace),
zap.Any("package_build_status", pkg.Status.BuildStatus))
return http.StatusInternalServerError, errors.New(fmt.Sprintf("%s: pkg %s.%s has a status of %s", e, pkg.ObjectMeta.Name, pkg.ObjectMeta.Namespace, pkg.Status.BuildStatus))
return http.StatusInternalServerError, fmt.Errorf("%s: pkg %s.%s has a status of %s", e, pkg.ObjectMeta.Name, pkg.ObjectMeta.Namespace, pkg.Status.BuildStatus)
}
archive = &pkg.Spec.Deployment
} else {
@@ -315,7 +315,7 @@ func (fetcher *Fetcher) Fetch(ctx context.Context, pkg *fv1.Package, req Functio
if err != nil {
e := "failed to write file"
logger.Error(e, zap.Error(err), zap.String("location", tmpPath))
return http.StatusInternalServerError, errors.Wrapf(err, "%s %s", e, tmpPath)
return http.StatusInternalServerError, fmt.Errorf("%s %s: %w", e, tmpPath, err)
}
otelUtils.SpanTrackEvent(ctx, "archiveLiteral", otelUtils.GetAttributesForPackage(pkg)...)
} else {
@@ -329,7 +329,7 @@ func (fetcher *Fetcher) Fetch(ctx context.Context, pkg *fv1.Package, req Functio
if err != nil {
e := "failed to download url"
logger.Error(e, zap.Error(err), zap.String("url", req.Url))
return http.StatusBadRequest, errors.Wrapf(err, "%s %s", e, req.Url)
return http.StatusBadRequest, fmt.Errorf("%s %s: %w", e, req.Url, err)
}
// check file integrity only if checksum is not empty.
@@ -338,13 +338,13 @@ func (fetcher *Fetcher) Fetch(ctx context.Context, pkg *fv1.Package, req Functio
if err != nil {
e := "failed to get checksum"
logger.Error(e, zap.Error(err))
return http.StatusBadRequest, errors.Wrap(err, e)
return http.StatusBadRequest, fmt.Errorf("%s: %w", e, err)
}
err = verifyChecksum(checksum, &archive.Checksum)
if err != nil {
e := "failed to verify checksum"
logger.Error(e, zap.Error(err))
return http.StatusBadRequest, errors.Wrap(err, e)
return http.StatusBadRequest, fmt.Errorf("%s: %w", e, err)
}
}
}
@@ -373,7 +373,7 @@ func (fetcher *Fetcher) Fetch(ctx context.Context, pkg *fv1.Package, req Functio
zap.Error(err),
zap.String("original_path", tmpPath),
zap.String("rename_path", storePath))
return http.StatusInternalServerError, err
return http.StatusInternalServerError, fmt.Errorf("error renaming file: %w", err)
}
otelUtils.SpanTrackEvent(ctx, "packageFetched", otelUtils.GetAttributesForPackage(pkg)...)
@@ -409,7 +409,7 @@ func (fetcher *Fetcher) FetchSecretsAndCfgMaps(ctx context.Context, secrets []fv
secretDir, err := utils.SanitizeFilePath(filepath.Join(fetcher.sharedSecretPath, secret.Namespace, secret.Name), fetcher.sharedSecretPath)
if err != nil {
logger.Error(err.Error(), zap.String("directory", secretDir), zap.String("secret_name", secret.Name), zap.String("secret_namespace", secret.Namespace))
return http.StatusBadRequest, errors.New(fmt.Sprintf("%s, request: %v", err, secret))
return http.StatusBadRequest, fmt.Errorf("%s, request: %v", err, secret)
}
err = os.MkdirAll(secretDir, os.ModeDir|0750)
@@ -420,7 +420,7 @@ func (fetcher *Fetcher) FetchSecretsAndCfgMaps(ctx context.Context, secrets []fv
zap.String("directory", secretDir),
zap.String("secret_name", secret.Name),
zap.String("secret_namespace", secret.Namespace))
return http.StatusInternalServerError, errors.Wrapf(err, "%s: %s", e, secretDir)
return http.StatusInternalServerError, fmt.Errorf("%s: %s: %w", e, secretDir, err)
}
err = writeSecretOrConfigMap(data.Data, secretDir)
if err != nil {
@@ -461,8 +461,8 @@ func (fetcher *Fetcher) FetchSecretsAndCfgMaps(ctx context.Context, secrets []fv
configDir, err := utils.SanitizeFilePath(filepath.Join(fetcher.sharedConfigPath, config.Namespace, config.Name), fetcher.sharedConfigPath)
if err != nil {
logger.Error(err.Error(), zap.String("directory", configDir), zap.String("config_map_name", config.Name), zap.String("config_map_namespace", config.Namespace))
return http.StatusBadRequest, errors.New(fmt.Sprintf("%s, request: %v", err,
config))
return http.StatusBadRequest, fmt.Errorf("%s, request: %v", err,
config)
}
err = os.MkdirAll(configDir, os.ModeDir|0750)
@@ -473,7 +473,7 @@ func (fetcher *Fetcher) FetchSecretsAndCfgMaps(ctx context.Context, secrets []fv
zap.String("directory", configDir),
zap.String("config_map_name", config.Name),
zap.String("config_map_namespace", config.Namespace))
return http.StatusInternalServerError, errors.Wrapf(err, "%s: %s", e, configDir)
return http.StatusInternalServerError, fmt.Errorf("%s: %s: %w", e, configDir, err)
}
configMap := make(map[string][]byte)
for key, val := range data.Data {
@@ -617,7 +617,7 @@ func (fetcher *Fetcher) UploadHandler(w http.ResponseWriter, r *http.Request) {
func (fetcher *Fetcher) rename(src string, dst string) error {
err := os.Rename(src, dst)
if err != nil {
return errors.Wrap(err, "failed to move file")
return fmt.Errorf("failed to move file: %w", err)
}
return nil
}
@@ -673,17 +673,17 @@ func (fetcher *Fetcher) SpecializePod(ctx context.Context, fetchReq FunctionFetc
pkg, err := fetcher.getPkgInformation(ctx, fetchReq)
if err != nil {
return errors.Wrap(err, "error getting package information")
return fmt.Errorf("error getting package information: %w", err)
}
_, err = fetcher.Fetch(ctx, pkg, fetchReq)
if err != nil {
return errors.Wrap(err, "error fetching deploy package")
return fmt.Errorf("error fetching deploy package: %w", err)
}
_, err = fetcher.FetchSecretsAndCfgMaps(ctx, fetchReq.Secrets, fetchReq.ConfigMaps)
if err != nil {
return errors.Wrap(err, "error fetching secrets/configs")
return fmt.Errorf("error fetching secrets/configs: %w", err)
}
// Specialize the pod
@@ -695,7 +695,7 @@ func (fetcher *Fetcher) SpecializePod(ctx context.Context, fetchReq FunctionFetc
loadPayload, err := json.Marshal(loadReq)
if err != nil {
return errors.Wrap(err, "error encoding load request")
return fmt.Errorf("error encoding load request: %w", err)
}
// Instead of using "localhost", here we use "127.0.0.1" for
@@ -739,10 +739,10 @@ func (fetcher *Fetcher) SpecializePod(ctx context.Context, fetchReq FunctionFetc
err = ferror.MakeErrorFromHTTP(resp)
}
return errors.Wrap(err, "error specializing function pod")
return fmt.Errorf("error specializing function pod: %w", err)
}
return errors.Wrapf(err, "error specializing function pod after %v times", maxRetries)
return fmt.Errorf("error specializing function pod after %v times: %w", maxRetries, err)
}
// WsStartHandler is used to generate websocket events in Kubernetes