Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: move github.com/pkg/errors -> errors #339

Merged
merged 1 commit into from
Oct 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions apis/v1alpha1/clusterresourceplacement_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package v1alpha1
import (
"fmt"

"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand Down Expand Up @@ -53,7 +52,7 @@ func ValidateClusterResourcePlacement(clusterResourcePlacement *ClusterResourceP
allErr = append(allErr, fmt.Errorf("the labelSelector and name fields are mutually exclusive in selector %+v", selector))
}
if _, err := metav1.LabelSelectorAsSelector(selector.LabelSelector); err != nil {
allErr = append(allErr, errors.Wrap(err, fmt.Sprintf("the labelSelector in resource selector %+v is invalid", selector)))
allErr = append(allErr, fmt.Errorf("the labelSelector in resource selector %+v is invalid: %w", selector, err))
}
}
}
Expand All @@ -62,7 +61,7 @@ func ValidateClusterResourcePlacement(clusterResourcePlacement *ClusterResourceP
clusterResourcePlacement.Spec.Policy.Affinity.ClusterAffinity != nil {
for _, selector := range clusterResourcePlacement.Spec.Policy.Affinity.ClusterAffinity.ClusterSelectorTerms {
if _, err := metav1.LabelSelectorAsSelector(&selector.LabelSelector); err != nil {
allErr = append(allErr, errors.Wrap(err, fmt.Sprintf("the labelSelector in cluster selector %+v is invalid", selector)))
allErr = append(allErr, fmt.Errorf("the labelSelector in cluster selector %+v is invalid: %w", selector, err))
}
}
}
Expand Down
14 changes: 7 additions & 7 deletions cmd/memberagent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ package main
import (
"context"
"encoding/base64"
"errors"
"flag"
"fmt"
"os"
"time"

"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/runtime"
utilrand "k8s.io/apimachinery/pkg/util/rand"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
Expand Down Expand Up @@ -79,7 +79,7 @@ func main() {

mcName := os.Getenv("MEMBER_CLUSTER_NAME")
if mcName == "" {
klog.ErrorS(errors.New("Member cluster name cannot be empty"), "error has occurred retrieving MEMBER_CLUSTER_NAME")
klog.ErrorS(errors.New("member cluster name cannot be empty"), "error has occurred retrieving MEMBER_CLUSTER_NAME")
os.Exit(1)
}

Expand Down Expand Up @@ -163,12 +163,12 @@ func main() {
func Start(ctx context.Context, hubCfg, memberConfig *rest.Config, hubOpts, memberOpts ctrl.Options) error {
hubMgr, err := ctrl.NewManager(hubCfg, hubOpts)
if err != nil {
return errors.Wrap(err, "unable to start hub manager")
return fmt.Errorf("unable to start hub manager: %w", err)
}

memberMgr, err := ctrl.NewManager(memberConfig, memberOpts)
if err != nil {
return errors.Wrap(err, "unable to start member manager")
return fmt.Errorf("unable to start member manager: %w", err)
}

if err := hubMgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
Expand Down Expand Up @@ -214,7 +214,7 @@ func Start(ctx context.Context, hubCfg, memberConfig *rest.Config, hubOpts, memb
}

if err = internalmembercluster.NewReconciler(hubMgr.GetClient(), memberMgr.GetClient(), workController).SetupWithManager(hubMgr); err != nil {
return errors.Wrap(err, "unable to create controller hub_member")
return fmt.Errorf("unable to create controller hub_member: %w", err)
}

klog.V(3).InfoS("starting hub manager")
Expand All @@ -223,15 +223,15 @@ func Start(ctx context.Context, hubCfg, memberConfig *rest.Config, hubOpts, memb
defer klog.V(3).InfoS("shutting down hub manager")
err := hubMgr.Start(ctx)
if err != nil {
startErr <- errors.Wrap(err, "problem starting hub manager")
startErr <- fmt.Errorf("problem starting hub manager: %w", err)
return
}
}()

klog.V(3).InfoS("starting member manager")
defer klog.V(3).InfoS("shutting down member manager")
if err := memberMgr.Start(ctx); err != nil {
return errors.Wrap(err, "problem starting member manager")
return fmt.Errorf("problem starting member manager: %w", err)
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ require (
github.com/onsi/ginkgo/v2 v2.1.6
github.com/onsi/gomega v1.20.1
github.com/openkruise/kruise v1.2.0
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.12.1
github.com/spf13/cobra v1.4.0
github.com/spf13/pflag v1.0.5
Expand Down Expand Up @@ -65,6 +64,7 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pkg/browser v0.0.0-20210115035449-ce105d075bb4 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.32.1 // indirect
Expand Down
6 changes: 3 additions & 3 deletions pkg/authtoken/providers/azure/azure_msi.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ package azure

import (
"context"
"fmt"

"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/pkg/errors"
"k8s.io/client-go/util/retry"
"k8s.io/klog/v2"

Expand Down Expand Up @@ -39,7 +39,7 @@ func (a *azureAuthTokenProvider) FetchToken(ctx context.Context) (interfaces.Aut
klog.V(2).InfoS("FetchToken", "client ID", a.clientID)
credential, err := azidentity.NewManagedIdentityCredential(opts)
if err != nil {
return token, errors.Wrap(err, "failed to create managed identity cred")
return token, fmt.Errorf("failed to create managed identity cred: %w", err)
}
var azToken *azcore.AccessToken
err = retry.OnError(retry.DefaultBackoff,
Expand All @@ -56,7 +56,7 @@ func (a *azureAuthTokenProvider) FetchToken(ctx context.Context) (interfaces.Aut
return err
})
if err != nil {
return token, errors.Wrap(err, "failed to get a token")
return token, fmt.Errorf("failed to get a token: %w", err)
}

token.Token = azToken.Token
Expand Down
5 changes: 2 additions & 3 deletions pkg/authtoken/providers/secret/k8s_secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"fmt"
"time"

"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/util/retry"
Expand All @@ -33,7 +32,7 @@ type secretAuthTokenProvider struct {
func New(secretName, namespace string) (interfaces.AuthTokenProvider, error) {
client, err := getClient()
if err != nil {
return nil, errors.Wrapf(err, "an error occurd will creating client")
return nil, fmt.Errorf("an error occurd will creating client: %w", err)
}
return &secretAuthTokenProvider{
client: client,
Expand All @@ -47,7 +46,7 @@ func (s *secretAuthTokenProvider) FetchToken(ctx context.Context) (interfaces.Au
token := interfaces.AuthToken{}
secret, err := s.fetchSecret(ctx)
if err != nil {
return token, errors.Wrapf(err, "cannot get the secret")
return token, fmt.Errorf("cannot get the secret: %w", err)
}

if len(secret.Data[tokenKey]) == 0 {
Expand Down
4 changes: 2 additions & 2 deletions pkg/authtoken/token_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ Licensed under the MIT license.
package authtoken

import (
"fmt"
"io"
"os"

"github.com/pkg/errors"
"k8s.io/klog/v2"

"go.goms.io/fleet/pkg/interfaces"
Expand Down Expand Up @@ -53,7 +53,7 @@ func (w *Writer) WriteToken(token interfaces.AuthToken) error {
}()
_, err = io.WriteString(writer, token.Token)
if err != nil {
return errors.Wrap(err, "cannot write the refresh token")
return fmt.Errorf("cannot write the refresh token: %w", err)
}
klog.V(2).InfoS("token has been saved to the file successfully")
return nil
Expand Down
9 changes: 4 additions & 5 deletions pkg/controllers/clusterresourceplacement/cluster_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package clusterresourceplacement
import (
"fmt"

"github.com/pkg/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -66,11 +65,11 @@ func (r *Reconciler) selectClusters(placement *fleetv1alpha1.ClusterResourcePlac
for _, clusterSelector := range placement.Spec.Policy.Affinity.ClusterAffinity.ClusterSelectorTerms {
selector, err := metav1.LabelSelectorAsSelector(&clusterSelector.LabelSelector)
if err != nil {
return nil, errors.Wrap(err, "cannot convert the label clusterSelector to a clusterSelector")
return nil, fmt.Errorf("cannot convert the label clusterSelector to a clusterSelector: %w", err)
}
matchClusters, err := r.listClusters(selector)
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("selector = %v", clusterSelector.LabelSelector))
return nil, fmt.Errorf("selector = %v: %w", clusterSelector.LabelSelector, err)
}
klog.V(2).InfoS("selector matches some cluster", "clusterNum", len(matchClusters), "placement", placement.Name, "selector", clusterSelector.LabelSelector)
for _, clusterName := range matchClusters {
Expand All @@ -88,7 +87,7 @@ func (r *Reconciler) selectClusters(placement *fleetv1alpha1.ClusterResourcePlac
func (r *Reconciler) listClusters(labelSelector labels.Selector) ([]string, error) {
objs, err := r.InformerManager.Lister(utils.MemberClusterGVR).List(labelSelector)
if err != nil {
return nil, errors.Wrap(err, "failed to list the clusters according to obj label selector")
return nil, fmt.Errorf("failed to list the clusters according to obj label selector: %w", err)
}

clusterNames := make([]string, 0)
Expand Down Expand Up @@ -133,7 +132,7 @@ func convertObjToMemberCluster(obj runtime.Object) (*fleetv1alpha1.MemberCluster
uObj := obj.DeepCopyObject().(*unstructured.Unstructured)
var clusterObj fleetv1alpha1.MemberCluster
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(uObj.Object, &clusterObj); err != nil {
return nil, errors.Wrap(err, "cannot decode the member cluster object")
return nil, fmt.Errorf("cannot decode the member cluster object: %w", err)
}
return &clusterObj, nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ package clusterresourceplacement

import (
"context"
"errors"
"fmt"
"time"

"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down
29 changes: 14 additions & 15 deletions pkg/controllers/clusterresourceplacement/resource_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"sort"
"strings"

"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -79,7 +78,7 @@ func (r *Reconciler) gatherSelectedResource(ctx context.Context, placement *flee
objs, err = r.fetchClusterScopedResources(ctx, selector, placement.GetName())
}
if err != nil {
return nil, errors.Wrapf(err, "selector = %v", selector)
return nil, fmt.Errorf("selector = %v: %w", selector, err)
}
resources = append(resources, objs...)
}
Expand Down Expand Up @@ -112,7 +111,7 @@ func (r *Reconciler) fetchClusterScopedResources(ctx context.Context, selector f
}
restMapping, err := r.RestMapper.RESTMapping(gk, selector.Version)
if err != nil {
return nil, errors.Wrap(err, "Failed to get GVR of the selector")
return nil, fmt.Errorf("failed to get GVR of the selector: %w", err)
}
gvr := restMapping.Resource
gvk := schema.GroupVersionKind{
Expand All @@ -121,10 +120,10 @@ func (r *Reconciler) fetchClusterScopedResources(ctx context.Context, selector f
Kind: selector.Kind,
}
if !r.InformerManager.IsClusterScopedResources(gvk) {
return nil, errors.New(fmt.Sprintf("%+v is not a cluster scoped resource", restMapping.Resource))
return nil, fmt.Errorf("%+v is not a cluster scoped resource", restMapping.Resource)
}
if !r.InformerManager.IsInformerSynced(gvr) {
return nil, errors.New(fmt.Sprintf("informer cache for %+v is not synced yet", restMapping.Resource))
return nil, fmt.Errorf("informer cache for %+v is not synced yet", restMapping.Resource)
}

lister := r.InformerManager.Lister(gvr)
Expand Down Expand Up @@ -152,13 +151,13 @@ func (r *Reconciler) fetchClusterScopedResources(ctx context.Context, selector f
// TODO: validator should enforce the validity of the labelSelector
labelSelector, err = metav1.LabelSelectorAsSelector(selector.LabelSelector)
if err != nil {
return nil, errors.Wrap(err, "cannot convert the label selector to a selector")
return nil, fmt.Errorf("cannot convert the label selector to a selector: %w", err)
}
}
var selectedObjs []runtime.Object
objects, err := lister.List(labelSelector)
if err != nil {
return nil, errors.Wrap(err, "cannot list all the objets")
return nil, fmt.Errorf("cannot list all the objets: %w", err)
}
// go ahead and claim all objects by adding a finalizer and insert the placement in its annotation
for i := 0; i < len(objects); i++ {
Expand Down Expand Up @@ -198,18 +197,18 @@ func (r *Reconciler) fetchNamespaceResources(ctx context.Context, selector fleet
} else {
labelSelector, err = metav1.LabelSelectorAsSelector(selector.LabelSelector)
if err != nil {
return nil, errors.Wrap(err, "cannot convert the label selector to a selector")
return nil, fmt.Errorf("cannot convert the label selector to a selector: %w", err)
}
}
namespaces, err := r.InformerManager.Lister(utils.NamespaceGVR).List(labelSelector)
if err != nil {
return nil, errors.Wrap(err, "cannot list all the namespaces given the label selector")
return nil, fmt.Errorf("cannot list all the namespaces given the label selector: %w", err)
}

for _, namespace := range namespaces {
ns, err := meta.Accessor(namespace)
if err != nil {
return nil, errors.Wrap(err, "cannot get the name of a namespace object")
return nil, fmt.Errorf("cannot get the name of a namespace object: %w", err)
}
objs, err := r.fetchAllResourcesInOneNamespace(ctx, ns.GetName(), placeName)
if err != nil {
Expand All @@ -226,7 +225,7 @@ func (r *Reconciler) fetchAllResourcesInOneNamespace(ctx context.Context, namesp
var resources []runtime.Object

if !utils.ShouldPropagateNamespace(namespaceName, r.SkippedNamespaces) {
return nil, errors.New(fmt.Sprintf("namespace %s is not allowed to propagate", namespaceName))
return nil, fmt.Errorf("namespace %s is not allowed to propagate", namespaceName)
}

klog.V(2).InfoS("start to fetch all the resources inside a namespace", "namespace", namespaceName)
Expand Down Expand Up @@ -256,13 +255,13 @@ func (r *Reconciler) fetchAllResourcesInOneNamespace(ctx context.Context, namesp
lister := r.InformerManager.Lister(gvr)
objs, err := lister.ByNamespace(namespaceName).List(labels.Everything())
if err != nil {
return nil, errors.Wrapf(err, "cannot list all the objects of type %+v in namespace %s", gvr, namespaceName)
return nil, fmt.Errorf("cannot list all the objects of type %+v in namespace %s: %w", gvr, namespaceName, err)
}
for _, obj := range objs {
uObj := obj.DeepCopyObject().(*unstructured.Unstructured)
shouldInclude, err := utils.ShouldPropagateObj(r.InformerManager, uObj)
if err != nil {
return nil, errors.Wrap(err, "cannot determine if we should propagate an object")
return nil, fmt.Errorf("cannot determine if we should propagate an object: %w", err)
}
if shouldInclude {
resources = append(resources, obj)
Expand Down Expand Up @@ -339,13 +338,13 @@ func generateManifest(object *unstructured.Unstructured) (*workv1alpha1.Manifest
}
}
if err != nil {
return nil, errors.Wrapf(err, "failed to get the ports field in Serivce object, name =%s", object.GetName())
return nil, fmt.Errorf("failed to get the ports field in Serivce object, name =%s: %w", object.GetName(), err)
}
}

rawContent, err := object.MarshalJSON()
if err != nil {
return nil, errors.Wrapf(err, "failed to marshal the unstructured object gvk = %s, name =%s", object.GroupVersionKind(), object.GetName())
return nil, fmt.Errorf("failed to marshal the unstructured object gvk = %s, name =%s: %w", object.GroupVersionKind(), object.GetName(), err)
}
return &workv1alpha1.Manifest{
RawExtension: runtime.RawExtension{Raw: rawContent},
Expand Down
Loading