Skip to content

Commit

Permalink
Migrate remaining piece of pkg/controllers and pkg/controller to …
Browse files Browse the repository at this point in the history
…errkit
  • Loading branch information
e-sumin committed Nov 25, 2024
1 parent 5c7ac49 commit 00959b8
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 69 deletions.
32 changes: 16 additions & 16 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import (
"strings"
"sync"

"github.com/kanisterio/errkit"
osversioned "github.com/openshift/client-go/apps/clientset/versioned"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"gopkg.in/tomb.v2"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -86,23 +86,23 @@ func New(c *rest.Config, reg prometheus.Registerer) *Controller {
func (c *Controller) StartWatch(ctx context.Context, namespace string) error {
crClient, err := versioned.NewForConfig(c.config)
if err != nil {
return errors.Wrap(err, "failed to get a CustomResource client")
return errkit.Wrap(err, "failed to get a CustomResource client")
}
if err := checkCRAccess(ctx, crClient, namespace); err != nil {
return err
}
clientset, err := kubernetes.NewForConfig(c.config)
if err != nil {
return errors.Wrap(err, "failed to get a k8s client")
return errkit.Wrap(err, "failed to get a k8s client")
}
dynClient, err := dynamic.NewForConfig(c.config)
if err != nil {
return errors.Wrap(err, "failed to get a k8s dynamic client")
return errkit.Wrap(err, "failed to get a k8s dynamic client")
}

osClient, err := osversioned.NewForConfig(c.config)
if err != nil {
return errors.Wrap(err, "failed to get a openshift client")
return errkit.Wrap(err, "failed to get a openshift client")
}

c.crClient = crClient
Expand Down Expand Up @@ -134,13 +134,13 @@ func (c *Controller) StartWatch(ctx context.Context, namespace string) error {

func checkCRAccess(ctx context.Context, cli versioned.Interface, ns string) error {
if _, err := cli.CrV1alpha1().ActionSets(ns).List(ctx, metav1.ListOptions{}); err != nil {
return errors.Wrap(err, "Could not list ActionSets")
return errkit.Wrap(err, "Could not list ActionSets")
}
if _, err := cli.CrV1alpha1().Blueprints(ns).List(ctx, metav1.ListOptions{}); err != nil {
return errors.Wrap(err, "Could not list Blueprints")
return errkit.Wrap(err, "Could not list Blueprints")
}
if _, err := cli.CrV1alpha1().Profiles(ns).List(ctx, metav1.ListOptions{}); err != nil {
return errors.Wrap(err, "Could not list Profiles")
return errkit.Wrap(err, "Could not list Profiles")
}
return nil
}
Expand Down Expand Up @@ -220,7 +220,7 @@ func (c *Controller) onAddActionSet(ctx context.Context, t *tomb.Tomb, as *crv1a
}
as, err := c.crClient.CrV1alpha1().ActionSets(as.GetNamespace()).Get(ctx, as.GetName(), metav1.GetOptions{})
if err != nil {
return errors.WithStack(err)
return errkit.WithStack(err)
}
if err = validate.ActionSet(as); err != nil {
return err
Expand Down Expand Up @@ -313,13 +313,13 @@ func (c *Controller) initActionSetStatus(ctx context.Context, as *crv1alpha1.Act
for _, a := range as.Spec.Actions {
if a.Blueprint == "" {
// TODO: If no blueprint is specified, we should consider a default.
err = errors.New("Blueprint is not specified for action")
err = errkit.New("Blueprint is not specified for action")
c.logAndErrorEvent(ctx, "Could not get blueprint:", "Blueprint not specified", err, as)
break
}
var bp *crv1alpha1.Blueprint
if bp, err = c.crClient.CrV1alpha1().Blueprints(as.GetNamespace()).Get(ctx, a.Blueprint, metav1.GetOptions{}); err != nil {
err = errors.Wrap(err, "Failed to query blueprint")
err = errkit.Wrap(err, "Failed to query blueprint")
c.logAndErrorEvent(ctx, "Could not get blueprint:", "Error", err, as)
break
}
Expand Down Expand Up @@ -349,7 +349,7 @@ func (c *Controller) initActionSetStatus(ctx context.Context, as *crv1alpha1.Act
func (c *Controller) initialActionStatus(a crv1alpha1.ActionSpec, bp *crv1alpha1.Blueprint) (*crv1alpha1.ActionStatus, error) {
bpa, ok := bp.Actions[a.Name]
if !ok {
return nil, errors.Errorf("Action %s for object kind %s not found in blueprint %s", a.Name, a.Object.Kind, a.Blueprint)
return nil, errkit.New(fmt.Sprintf("Action %s for object kind %s not found in blueprint %s", a.Name, a.Object.Kind, a.Blueprint))
}
phases := make([]crv1alpha1.Phase, 0, len(bpa.Phases))
for _, p := range bpa.Phases {
Expand Down Expand Up @@ -379,14 +379,14 @@ func (c *Controller) initialActionStatus(a crv1alpha1.ActionSpec, bp *crv1alpha1

func (c *Controller) handleActionSet(ctx context.Context, t *tomb.Tomb, as *crv1alpha1.ActionSet) (err error) {
if as.Status == nil {
return errors.New("ActionSet was not initialized")
return errkit.New("ActionSet was not initialized")
}
if as.Status.State != crv1alpha1.StatePending {
return nil
}
as.Status.State = crv1alpha1.StateRunning
if as, err = c.crClient.CrV1alpha1().ActionSets(as.GetNamespace()).Update(ctx, as, metav1.UpdateOptions{}); err != nil {
return errors.WithStack(err)
return errkit.WithStack(err)
}
ctx = field.Context(ctx, consts.ActionsetNameKey, as.GetName())
// adding labels with prefix "kanister.io/" in the context as field for better logging
Expand All @@ -399,7 +399,7 @@ func (c *Controller) handleActionSet(ctx context.Context, t *tomb.Tomb, as *crv1
for i, a := range as.Status.Actions {
var bp *crv1alpha1.Blueprint
if bp, err = c.crClient.CrV1alpha1().Blueprints(as.GetNamespace()).Get(ctx, a.Blueprint, metav1.GetOptions{}); err != nil {
err = errors.Wrap(err, "Failed to query blueprint")
err = errkit.Wrap(err, "Failed to query blueprint")
c.logAndErrorEvent(ctx, "Could not get blueprint:", "Error", err, as)
break
}
Expand All @@ -419,7 +419,7 @@ func (c *Controller) handleActionSet(ctx context.Context, t *tomb.Tomb, as *crv1
Message: err.Error(),
}
_, err = c.crClient.CrV1alpha1().ActionSets(as.GetNamespace()).Update(ctx, as, metav1.UpdateOptions{})
return errors.WithStack(err)
return errkit.WithStack(err)
}
log.WithContext(ctx).Print("Created actionset and started executing actions", field.M{"NewActionSetName": as.GetName()})
return nil
Expand Down
16 changes: 8 additions & 8 deletions pkg/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"testing"
"time"

"github.com/pkg/errors"
"github.com/kanisterio/errkit"
"github.com/prometheus/client_golang/prometheus"
promgomodel "github.com/prometheus/client_model/go"
"gopkg.in/check.v1"
Expand Down Expand Up @@ -171,12 +171,12 @@ func (s *ControllerSuite) waitOnActionSetState(as *crv1alpha1.ActionSet, state c
if as.Status.State == crv1alpha1.StatePending || as.Status.State == crv1alpha1.StateRunning {
return false, nil
}
return false, errors.New(fmt.Sprintf("Unexpected state: %s", as.Status.State))
return false, errkit.New(fmt.Sprintf("Unexpected state: %s", as.Status.State))
})
if err == nil {
return nil
}
return errors.Wrapf(err, "State '%s' never reached", state)
return errkit.Wrap(err, fmt.Sprintf("State '%s' never reached", state))
}

func (s *ControllerSuite) waitOnDeferPhaseState(as *crv1alpha1.ActionSet, state crv1alpha1.State) error {
Expand All @@ -201,7 +201,7 @@ func (s *ControllerSuite) waitOnDeferPhaseState(as *crv1alpha1.ActionSet, state
if err == nil {
return nil
}
return errors.Wrapf(err, "State '%s' never reached", state)
return errkit.Wrap(err, fmt.Sprintf("State '%s' never reached", state))
}

func (s *ControllerSuite) waitOnActionSetCompleteWithRunningPhases(as *crv1alpha1.ActionSet, rp *sets.Set[string]) error {
Expand Down Expand Up @@ -229,12 +229,12 @@ func (s *ControllerSuite) waitOnActionSetCompleteWithRunningPhases(as *crv1alpha
}
return false, nil
}
return false, errors.New(fmt.Sprintf("Unexpected state: %s", as.Status.State))
return false, errkit.New(fmt.Sprintf("Unexpected state: %s", as.Status.State))
})
if err == nil {
return nil
}
return errors.Wrapf(err, "ActionSet did not reach '%s' state", crv1alpha1.StateComplete)
return errkit.Wrap(err, fmt.Sprintf("ActionSet did not reach '%s' state", crv1alpha1.StateComplete))
}

func newBPWithOutputArtifact() *crv1alpha1.Blueprint {
Expand Down Expand Up @@ -731,7 +731,7 @@ func (s *ControllerSuite) TestRuntimeObjEventLogs(c *check.C) {
config, err := kube.LoadConfig()
c.Assert(err, check.IsNil)
ctlr := New(config, nil)
ctlr.logAndErrorEvent(ctx, msg, reason, errors.New("Testing Event Logs"), as, nilAs, bp)
ctlr.logAndErrorEvent(ctx, msg, reason, errkit.New("Testing Event Logs"), as, nilAs, bp)

// Test ActionSet error event logging
events, err := s.cli.CoreV1().Events(as.Namespace).Search(scheme.Scheme, as)
Expand All @@ -754,7 +754,7 @@ func (s *ControllerSuite) TestRuntimeObjEventLogs(c *check.C) {

// Testing empty Blueprint
testbp := &crv1alpha1.Blueprint{}
ctlr.logAndErrorEvent(ctx, msg, reason, errors.New("Testing Event Logs"), testbp)
ctlr.logAndErrorEvent(ctx, msg, reason, errkit.New("Testing Event Logs"), testbp)
events, err = s.cli.CoreV1().Events(bp.Namespace).Search(scheme.Scheme, testbp)
c.Assert(err, check.NotNil)
c.Assert(len(events.Items), check.Equals, 0)
Expand Down
22 changes: 11 additions & 11 deletions pkg/controllers/repositoryserver/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

"github.com/go-logr/logr"
"github.com/jpillora/backoff"
"github.com/pkg/errors"
"github.com/kanisterio/errkit"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
apimeta "k8s.io/apimachinery/pkg/api/meta"
Expand Down Expand Up @@ -50,20 +50,20 @@ type RepoServerHandler struct {

func (h *RepoServerHandler) CreateOrUpdateOwnedResources(ctx context.Context) error {
if err := h.getSecretsFromCR(ctx); err != nil {
return errors.Wrap(err, "Failed to get Kopia API server secrets")
return errkit.Wrap(err, "Failed to get Kopia API server secrets")
}

svc, err := h.reconcileService(ctx)
if err != nil {
return errors.Wrap(err, "Failed to reconcile service")
return errkit.Wrap(err, "Failed to reconcile service")
}

envVars, pod, err := h.reconcilePod(ctx, svc)
if err != nil {
return errors.Wrap(err, "Failed to reconcile Kopia API server pod")
return errkit.Wrap(err, "Failed to reconcile Kopia API server pod")
}
if err := h.waitForPodReady(ctx, pod); err != nil {
return errors.Wrap(err, "Kopia API server pod not in ready state")
return errkit.Wrap(err, "Kopia API server pod not in ready state")
}

// envVars are set only when credentials are of type AWS/Azure.
Expand Down Expand Up @@ -95,7 +95,7 @@ func (h *RepoServerHandler) reconcileService(ctx context.Context) (*corev1.Servi
return nil, err
}
if err := h.updateServerInfoInCRStatus(ctx, h.RepositoryServer.Status.ServerInfo.PodName, svc.Name); err != nil {
return nil, errors.Wrap(err, "Failed to update service name in RepositoryServer /status")
return nil, errkit.Wrap(err, "Failed to update service name in RepositoryServer /status")
}
return svc, err
}
Expand Down Expand Up @@ -131,7 +131,7 @@ func (h *RepoServerHandler) createService(ctx context.Context, repoServerNamespa
}
err := h.Reconciler.Create(ctx, &svc)
if err != nil {
return nil, errors.Wrap(err, "Failed to create RepositoryServer service")
return nil, errkit.Wrap(err, "Failed to create RepositoryServer service")
}

err = poll.WaitWithBackoff(ctx, backoff.Backoff{
Expand Down Expand Up @@ -184,7 +184,7 @@ func (h *RepoServerHandler) createPodAndUpdateStatus(ctx context.Context, repoSe
return nil, nil, err
}
if err := h.updateServerInfoInCRStatus(ctx, pod.Name, h.RepositoryServer.Status.ServerInfo.ServiceName); err != nil {
return nil, nil, errors.Wrap(err, "Failed to update pod name in RepositoryServer /status")
return nil, nil, errkit.Wrap(err, "Failed to update pod name in RepositoryServer /status")
}
return envVars, pod, nil
}
Expand Down Expand Up @@ -236,7 +236,7 @@ func (h *RepoServerHandler) createPod(ctx context.Context, repoServerNamespace s
return nil, nil, err
}
if err := h.Reconciler.Create(ctx, pod); err != nil {
return nil, nil, errors.Wrap(err, "Failed to create RepositoryServer pod")
return nil, nil, errkit.Wrap(err, "Failed to create RepositoryServer pod")
}
return pod, envVars, err
}
Expand Down Expand Up @@ -284,7 +284,7 @@ func (h *RepoServerHandler) preparePodOverride(ctx context.Context, po *kube.Pod
h.RepositoryServerSecrets.serverTLS.Name,
po,
); err != nil {
return nil, errors.Wrap(err, "Failed to attach TLS Certificate configuration")
return nil, errkit.Wrap(err, "Failed to attach TLS Certificate configuration")
}
return podOverride, nil
}
Expand Down Expand Up @@ -315,7 +315,7 @@ func (h *RepoServerHandler) updateServerInfoInCRStatus(ctx context.Context, podN

func (h *RepoServerHandler) waitForPodReady(ctx context.Context, pod *corev1.Pod) error {
if err := kube.WaitForPodReady(ctx, h.KubeCli, pod.Namespace, pod.Name); err != nil {
return errors.Wrap(err, fmt.Sprintf("Failed while waiting for pod %s to be ready", pod.Name))
return errkit.Wrap(err, fmt.Sprintf("Failed while waiting for pod %s to be ready", pod.Name))
}
return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"context"

"github.com/go-logr/logr"
"github.com/pkg/errors"
"github.com/kanisterio/errkit"
corev1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -69,11 +69,11 @@ func (r *RepositoryServerReconciler) Reconcile(ctx context.Context, req ctrl.Req
logger = logger.V(1)
cnf, err := ctrl.GetConfig()
if err != nil {
return ctrl.Result{}, errors.Wrap(err, "Failed to get k8s config")
return ctrl.Result{}, errkit.Wrap(err, "Failed to get k8s config")
}
kubeCli, err := kubernetes.NewForConfig(cnf)
if err != nil {
return ctrl.Result{}, errors.Wrap(err, "Failed to get a k8s client")
return ctrl.Result{}, errkit.Wrap(err, "Failed to get a k8s client")
}

repositoryServer := &crv1alpha1.RepositoryServer{}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"testing"
"time"

"github.com/pkg/errors"
"github.com/kanisterio/errkit"
"gopkg.in/check.v1"
corev1 "k8s.io/api/core/v1"
k8sresource "k8s.io/apimachinery/pkg/api/resource"
Expand Down Expand Up @@ -477,11 +477,11 @@ func (s *RepoServerControllerSuite) waitForRepoServerInfoUpdateInCR(repoServerNa
})

if !serverInfoUpdated && err == nil {
err = errors.New("pod name or service name is not set on repository server CR")
err = errkit.New("pod name or service name is not set on repository server CR")
}

if err != nil {
return errors.Wrapf(err, "failed waiting for RepoServer Info updates in the CR")
return errkit.Wrap(err, "failed waiting for RepoServer Info updates in the CR")
}
return err
}
Expand All @@ -502,12 +502,12 @@ func (s *RepoServerControllerSuite) waitOnRepositoryServerState(reposerverName s
return false, nil
}
if repoServerCR.Status.Progress == crv1alpha1.Failed {
return false, errors.New(fmt.Sprintf(" There is failure in staring the repository server, server is in %s state, please check logs", repoServerCR.Status.Progress))
return false, errkit.New(fmt.Sprintf("There is failure in staring the repository server, server is in %s state, please check logs", repoServerCR.Status.Progress))
}
if repoServerCR.Status.Progress == crv1alpha1.Ready {
return true, nil
}
return false, errors.New(fmt.Sprintf("Unexpected Repository server state: %s", repoServerCR.Status.Progress))
return false, errkit.New(fmt.Sprintf("Unexpected Repository server state: %s", repoServerCR.Status.Progress))
})
return repoServerState, err
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/controllers/repositoryserver/secrets_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"context"
"fmt"

"github.com/pkg/errors"
"github.com/kanisterio/errkit"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
)
Expand Down Expand Up @@ -76,14 +76,14 @@ func (h *RepoServerHandler) getSecretsFromCR(ctx context.Context) error {

func (h *RepoServerHandler) fetchSecret(ctx context.Context, ref *corev1.SecretReference) (*corev1.Secret, error) {
if ref == nil {
return nil, errors.New("repository server CR does not have a secret reference set")
return nil, errkit.New("repository server CR does not have a secret reference set")
}

h.Logger.Info(fmt.Sprintf("Fetching secret %s from namespace %s", ref.Name, ref.Namespace))
secret := corev1.Secret{}
err := h.Reconciler.Get(ctx, types.NamespacedName{Name: ref.Name, Namespace: ref.Namespace}, &secret)
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("Error fetching secret %s from namespace %s", ref.Name, ref.Namespace))
return nil, errkit.Wrap(err, fmt.Sprintf("Error fetching secret %s from namespace %s", ref.Name, ref.Namespace))
}
return &secret, nil
}
Loading

0 comments on commit 00959b8

Please sign in to comment.