From 8d88a5ead1c8ce30e2077c04a02a4b0c5ca761e6 Mon Sep 17 00:00:00 2001 From: Flavio Castelli Date: Wed, 13 Sep 2023 14:22:28 +0200 Subject: [PATCH] chore(deps): remove `github.com/pkg/errors` dep Replace the `github.com/pkg/errors` dependency with the `errors` module part of the Go std library Signed-off-by: Flavio Castelli --- .golangci.yml | 2 +- controllers/admissionpolicy_controller.go | 7 +++++-- controllers/clusteradmissionpolicy_controller.go | 7 +++++-- controllers/policy_utils.go | 16 ++++++++-------- controllers/policyserver_controller.go | 11 +++++++---- controllers/policystatus_utils.go | 4 ++-- controllers/utils_test.go | 9 +++++---- 7 files changed, 33 insertions(+), 23 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 457f954ed..1961946b5 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -86,7 +86,7 @@ linters-settings: # List of file globs that will match this list of settings to compare against. # Default: $all files: - - "!$test" + - "$all" # List of allowed packages. allow: - $gostd diff --git a/controllers/admissionpolicy_controller.go b/controllers/admissionpolicy_controller.go index 0e197ca5d..44991e01d 100644 --- a/controllers/admissionpolicy_controller.go +++ b/controllers/admissionpolicy_controller.go @@ -18,6 +18,7 @@ package controllers import ( "context" + "errors" "fmt" "github.com/go-logr/logr" @@ -25,7 +26,6 @@ import ( "github.com/kubewarden/kubewarden-controller/internal/pkg/constants" "github.com/kubewarden/kubewarden-controller/internal/pkg/naming" policiesv1 "github.com/kubewarden/kubewarden-controller/pkg/apis/policies/v1" - "github.com/pkg/errors" corev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/runtime" @@ -91,7 +91,10 @@ func (r *AdmissionPolicyReconciler) SetupWithManager(mgr ctrl.Manager) error { ). Complete(r) - return errors.Wrap(err, "failed enrolling controller with manager") + if err != nil { + return errors.Join(errors.New("failed enrolling controller with manager"), err) + } + return nil } func (r *AdmissionPolicyReconciler) findAdmissionPoliciesForConfigMap(object client.Object) []reconcile.Request { diff --git a/controllers/clusteradmissionpolicy_controller.go b/controllers/clusteradmissionpolicy_controller.go index 0b98a9e13..f7febed00 100644 --- a/controllers/clusteradmissionpolicy_controller.go +++ b/controllers/clusteradmissionpolicy_controller.go @@ -18,6 +18,7 @@ package controllers import ( "context" + "errors" "fmt" "github.com/go-logr/logr" @@ -25,7 +26,6 @@ import ( "github.com/kubewarden/kubewarden-controller/internal/pkg/constants" "github.com/kubewarden/kubewarden-controller/internal/pkg/naming" policiesv1 "github.com/kubewarden/kubewarden-controller/pkg/apis/policies/v1" - "github.com/pkg/errors" corev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/runtime" @@ -91,7 +91,10 @@ func (r *ClusterAdmissionPolicyReconciler) SetupWithManager(mgr ctrl.Manager) er ). Complete(r) - return errors.Wrap(err, "failed enrolling controller with manager") + if err != nil { + return errors.Join(errors.New("failed enrolling controller with manager"), err) + } + return nil } func (r *ClusterAdmissionPolicyReconciler) findClusterAdmissionPoliciesForConfigMap(object client.Object) []reconcile.Request { diff --git a/controllers/policy_utils.go b/controllers/policy_utils.go index 6403ae408..6caaa079b 100644 --- a/controllers/policy_utils.go +++ b/controllers/policy_utils.go @@ -18,6 +18,7 @@ package controllers import ( "context" + "errors" "fmt" "time" @@ -26,7 +27,6 @@ import ( "github.com/kubewarden/kubewarden-controller/internal/pkg/metrics" "github.com/kubewarden/kubewarden-controller/internal/pkg/naming" policiesv1 "github.com/kubewarden/kubewarden-controller/pkg/apis/policies/v1" - "github.com/pkg/errors" admissionregistrationv1 "k8s.io/api/admissionregistration/v1" appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" @@ -42,12 +42,12 @@ import ( func setPolicyStatus(ctx context.Context, deploymentsNamespace string, apiReader client.Reader, policy policiesv1.Policy) error { policyServerDeployment := appsv1.Deployment{} if err := apiReader.Get(ctx, types.NamespacedName{Namespace: deploymentsNamespace, Name: naming.PolicyServerDeploymentNameForPolicyServerName(policy.GetPolicyServer())}, &policyServerDeployment); err != nil { - return errors.Wrap(err, "could not get policy server deployment") + return errors.Join(errors.New("could not get policy server deployment"), err) } policyServerConfigMap := corev1.ConfigMap{} if err := apiReader.Get(ctx, types.NamespacedName{Namespace: deploymentsNamespace, Name: naming.PolicyServerDeploymentNameForPolicyServerName(policy.GetPolicyServer())}, &policyServerConfigMap); err != nil { - return errors.Wrap(err, "could not get configmap") + return errors.Join(errors.New("could not get configmap"), err) } policyMap, err := getPolicyMapFromConfigMap(&policyServerConfigMap) @@ -117,7 +117,7 @@ func reconcilePolicy(ctx context.Context, client client.Client, reconciler admis if apierrors.IsNotFound(err) { return ctrl.Result{Requeue: true}, nil } - return ctrl.Result{}, errors.Wrap(err, "could not read policy server Deployment") + return ctrl.Result{}, errors.Join(errors.New("could not read policy server Deployment"), err) } if !isPolicyUniquelyReachable(ctx, client, &policyServerDeployment, policy.GetUniqueName()) { @@ -145,16 +145,16 @@ func reconcilePolicy(ctx context.Context, client client.Client, reconciler admis secret := corev1.Secret{} if err := client.Get(ctx, types.NamespacedName{Namespace: reconciler.DeploymentsNamespace, Name: constants.PolicyServerCARootSecretName}, &secret); err != nil { - return ctrl.Result{}, errors.Wrap(err, "cannot find policy server secret") + return ctrl.Result{}, errors.Join(errors.New("cannot find policy server secret"), err) } if policy.IsMutating() { if err := reconciler.ReconcileMutatingWebhookConfiguration(ctx, policy, &secret, policyServer.NameWithPrefix()); err != nil { - return ctrl.Result{}, errors.Wrap(err, "error reconciling mutating webhook") + return ctrl.Result{}, errors.Join(errors.New("error reconciling mutating webhook"), err) } } else { if err := reconciler.ReconcileValidatingWebhookConfiguration(ctx, policy, &secret, policyServer.NameWithPrefix()); err != nil { - return ctrl.Result{}, errors.Wrap(err, "error reconciling validating webhook") + return ctrl.Result{}, errors.Join(errors.New("error reconciling validating webhook"), err) } } setPolicyAsActive(policy) @@ -178,7 +178,7 @@ func setPolicyAsActive(policy policiesv1.Policy) { func getPolicyServer(ctx context.Context, client client.Client, policy policiesv1.Policy) (*policiesv1.PolicyServer, error) { policyServer := policiesv1.PolicyServer{} if err := client.Get(ctx, types.NamespacedName{Name: policy.GetPolicyServer()}, &policyServer); err != nil { - return nil, errors.Wrap(err, "could not get policy server") + return nil, errors.Join(errors.New("could not get policy server"), err) } return &policyServer, nil } diff --git a/controllers/policyserver_controller.go b/controllers/policyserver_controller.go index 047848a29..926d63c13 100644 --- a/controllers/policyserver_controller.go +++ b/controllers/policyserver_controller.go @@ -18,6 +18,7 @@ package controllers import ( "context" + "errors" "fmt" "time" @@ -25,7 +26,6 @@ import ( "github.com/kubewarden/kubewarden-controller/internal/pkg/admission" "github.com/kubewarden/kubewarden-controller/internal/pkg/constants" policiesv1 "github.com/kubewarden/kubewarden-controller/pkg/apis/policies/v1" - "github.com/pkg/errors" apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/runtime" ctrl "sigs.k8s.io/controller-runtime" @@ -70,7 +70,7 @@ func (r *PolicyServerReconciler) Reconcile(ctx context.Context, req ctrl.Request policies, err := r.Reconciler.GetPolicies(ctx, &policyServer, admission.SkipDeleted) if err != nil { - return ctrl.Result{}, errors.Wrap(err, "could not get policies") + return ctrl.Result{}, errors.Join(errors.New("could not get policies"), err) } if policyServer.ObjectMeta.DeletionTimestamp != nil { @@ -112,7 +112,7 @@ func (r *PolicyServerReconciler) reconcileDeletion(ctx context.Context, policySe } if len(policies) == 0 { if err := r.Reconciler.ReconcileDeletion(ctx, policyServer); err != nil { - return ctrl.Result{}, errors.Wrap(err, "could not reconcile policy server deletion") + return ctrl.Result{}, errors.Join(errors.New("could not reconcile policy server deletion"), err) } controllerutil.RemoveFinalizer(policyServer, constants.KubewardenFinalizer) if err := r.Update(ctx, policyServer); err != nil { @@ -193,5 +193,8 @@ func (r *PolicyServerReconciler) SetupWithManager(mgr ctrl.Manager) error { })). Complete(r) - return errors.Wrap(err, "failed enrolling controller with manager") + if err != nil { + return errors.Join(errors.New("failed enrolling controller with manager"), err) + } + return nil } diff --git a/controllers/policystatus_utils.go b/controllers/policystatus_utils.go index 9831dde38..4ca9f2b85 100644 --- a/controllers/policystatus_utils.go +++ b/controllers/policystatus_utils.go @@ -19,12 +19,12 @@ package controllers import ( "context" "encoding/json" + "errors" "fmt" "github.com/kubewarden/kubewarden-controller/internal/pkg/admission" "github.com/kubewarden/kubewarden-controller/internal/pkg/constants" policiesv1 "github.com/kubewarden/kubewarden-controller/pkg/apis/policies/v1" - "github.com/pkg/errors" appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" apimeta "k8s.io/apimachinery/pkg/api/meta" @@ -36,7 +36,7 @@ func getPolicyMapFromConfigMap(configMap *corev1.ConfigMap) (admission.PolicyCon policyMap := admission.PolicyConfigEntryMap{} if policies, ok := configMap.Data[constants.PolicyServerConfigPoliciesEntry]; ok { if err := json.Unmarshal([]byte(policies), &policyMap); err != nil { - return policyMap, errors.Wrap(err, "failed to unmarshal policy mapping") + return policyMap, errors.Join(errors.New("failed to unmarshal policy mapping"), err) } } else { return policyMap, nil diff --git a/controllers/utils_test.go b/controllers/utils_test.go index efee947f2..806cda9bb 100644 --- a/controllers/utils_test.go +++ b/controllers/utils_test.go @@ -17,9 +17,10 @@ limitations under the License. package controllers import ( + "errors" + policiesv1 "github.com/kubewarden/kubewarden-controller/pkg/apis/policies/v1" "github.com/onsi/gomega/types" - "github.com/pkg/errors" . "github.com/onsi/gomega" @@ -104,7 +105,7 @@ func admissionPolicyWithPolicyServerName(name, policyServerName string) *policie func getFreshAdmissionPolicy(namespace, name string) (*policiesv1.AdmissionPolicy, error) { newAdmissionPolicy := policiesv1.AdmissionPolicy{} if err := reconciler.APIReader.Get(ctx, client.ObjectKey{Namespace: namespace, Name: name}, &newAdmissionPolicy); err != nil { - return nil, errors.Wrap(err, "could not find admission policy") + return nil, errors.Join(errors.New("could not find admission policy"), err) } return &newAdmissionPolicy, nil } @@ -123,7 +124,7 @@ func clusterAdmissionPolicyWithPolicyServerName(name, policyServerName string) * func getFreshClusterAdmissionPolicy(name string) (*policiesv1.ClusterAdmissionPolicy, error) { newClusterAdmissionPolicy := policiesv1.ClusterAdmissionPolicy{} if err := reconciler.APIReader.Get(ctx, client.ObjectKey{Name: name}, &newClusterAdmissionPolicy); err != nil { - return nil, errors.Wrap(err, "could not find cluster admission policy") + return nil, errors.Join(errors.New("could not find cluster admission policy"), err) } return &newClusterAdmissionPolicy, nil } @@ -131,7 +132,7 @@ func getFreshClusterAdmissionPolicy(name string) (*policiesv1.ClusterAdmissionPo func getFreshPolicyServer(name string) (*policiesv1.PolicyServer, error) { newPolicyServer := policiesv1.PolicyServer{} if err := reconciler.APIReader.Get(ctx, client.ObjectKey{Name: name}, &newPolicyServer); err != nil { - return nil, errors.Wrap(err, "could not find policy server") + return nil, errors.Join(errors.New("could not find policy server"), err) } return &newPolicyServer, nil }