Skip to content

Commit

Permalink
make not found errors more detailed
Browse files Browse the repository at this point in the history
  • Loading branch information
maciaszczykm committed Dec 31, 2024
1 parent c5ec408 commit 56b93c8
Show file tree
Hide file tree
Showing 13 changed files with 35 additions and 32 deletions.
4 changes: 2 additions & 2 deletions go/controller/internal/controller/catalog_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (r *CatalogReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_
project, err := r.getProject(ctx, catalog)
if err != nil {
if errors.IsNotFound(err) {
utils.MarkCondition(catalog.SetCondition, v1alpha1.SynchronizedConditionType, v1.ConditionFalse, v1alpha1.SynchronizedConditionReasonError, notFoundOrReadyError)
utils.MarkCondition(catalog.SetCondition, v1alpha1.SynchronizedConditionType, v1.ConditionFalse, v1alpha1.SynchronizedConditionReasonError, notFoundOrReadyErrorMessage(err))
return RequeueAfter(requeueWaitForResources), nil
}
utils.MarkCondition(catalog.SetCondition, v1alpha1.SynchronizedConditionType, v1.ConditionFalse, v1alpha1.SynchronizedConditionReason, err.Error())
Expand Down Expand Up @@ -139,7 +139,7 @@ func (r *CatalogReconciler) getProject(ctx context.Context, catalog *v1alpha1.Ca

if project.Status.ID == nil {
logger.Info("Project is not ready")
return project, apierrors.NewNotFound(schema.GroupResource{}, catalog.Spec.ProjectRef.Name)
return project, apierrors.NewNotFound(schema.GroupResource{Resource: "Project", Group: "deployments.plural.sh"}, catalog.Spec.ProjectRef.Name)
}

if err := controllerutil.SetOwnerReference(project, catalog, r.Scheme); err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (r *ClusterRestoreReconciler) Reconcile(ctx context.Context, req ctrl.Reque
apiRestore, err := r.sync(ctx, restore)
if err != nil {
if errors.IsNotFound(err) {
utils.MarkCondition(restore.SetCondition, v1alpha1.SynchronizedConditionType, v1.ConditionFalse, v1alpha1.SynchronizedConditionReasonError, notFoundOrReadyError)
utils.MarkCondition(restore.SetCondition, v1alpha1.SynchronizedConditionType, v1.ConditionFalse, v1alpha1.SynchronizedConditionReasonError, notFoundOrReadyErrorMessage(err))
return RequeueAfter(requeueWaitForResources), nil
}
utils.MarkCondition(restore.SetCondition, v1alpha1.SynchronizedConditionType, v1.ConditionFalse, v1alpha1.SynchronizedConditionReason, err.Error())
Expand Down Expand Up @@ -140,7 +140,7 @@ func (r *ClusterRestoreReconciler) sync(ctx context.Context, restore *v1alpha1.C
return nil, err
}
if clusterID == nil {
return nil, errors.NewNotFound(schema.GroupResource{}, restore.Spec.BackupClusterRef.Name)
return nil, errors.NewNotFound(schema.GroupResource{Resource: "Cluster", Group: "deployments.plural.sh"}, restore.Spec.BackupClusterRef.Name)
}

backup, err := r.ConsoleClient.GetClusterBackup(clusterID, restore.Spec.BackupNamespace, restore.Spec.BackupName)
Expand Down
9 changes: 6 additions & 3 deletions go/controller/internal/controller/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package controller
import (
"context"
"encoding/json"
"fmt"
"time"

"k8s.io/apimachinery/pkg/types"

"github.com/pluralsh/polly/algorithms"
"github.com/samber/lo"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
runtimeclient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/yaml"
Expand All @@ -27,7 +27,6 @@ const (
// requeueAfter is the time between scheduled reconciles if there are no changes to the CRD.
requeueAfter = 30 * time.Second
requeueWaitForResources = 5 * time.Second
notFoundOrReadyError = "unable to find referenced object or is not ready yet"
)

var (
Expand Down Expand Up @@ -309,3 +308,7 @@ func defaultErrMessage(err error, defaultMessage string) string {

return defaultMessage
}

func notFoundOrReadyErrorMessage(err error) string {
return fmt.Sprintf("unable to find referenced object or is not ready yet, got error: %s", err.Error())
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (r *CustomStackRunReconciler) Reconcile(ctx context.Context, req ctrl.Reque
attr, err := r.genCustomStackRunAttr(ctx, stack)
if err != nil {
if errors.IsNotFound(err) {
utils.MarkCondition(stack.SetCondition, v1alpha1.SynchronizedConditionType, v1.ConditionFalse, v1alpha1.SynchronizedConditionReasonError, notFoundOrReadyError)
utils.MarkCondition(stack.SetCondition, v1alpha1.SynchronizedConditionType, v1.ConditionFalse, v1alpha1.SynchronizedConditionReasonError, notFoundOrReadyErrorMessage(err))
return RequeueAfter(requeueWaitForResources), nil
}
utils.MarkCondition(stack.SetCondition, v1alpha1.SynchronizedConditionType, v1.ConditionFalse, v1alpha1.SynchronizedConditionReasonError, err.Error())
Expand All @@ -130,7 +130,7 @@ func (r *CustomStackRunReconciler) Reconcile(ctx context.Context, req ctrl.Reque
attr, err := r.genCustomStackRunAttr(ctx, stack)
if err != nil {
if errors.IsNotFound(err) {
utils.MarkCondition(stack.SetCondition, v1alpha1.SynchronizedConditionType, v1.ConditionFalse, v1alpha1.SynchronizedConditionReasonError, notFoundOrReadyError)
utils.MarkCondition(stack.SetCondition, v1alpha1.SynchronizedConditionType, v1.ConditionFalse, v1alpha1.SynchronizedConditionReasonError, notFoundOrReadyErrorMessage(err))
return RequeueAfter(requeueWaitForResources), nil
}
utils.MarkCondition(stack.SetCondition, v1alpha1.SynchronizedConditionType, v1.ConditionFalse, v1alpha1.SynchronizedConditionReasonError, err.Error())
Expand Down Expand Up @@ -204,7 +204,7 @@ func (r *CustomStackRunReconciler) genCustomStackRunAttr(ctx context.Context, st
return nil, err
}
if stack.Status.ID == nil {
return nil, errors.NewNotFound(schema.GroupResource{}, stackRun.Spec.StackRef.Name)
return nil, errors.NewNotFound(schema.GroupResource{Resource: "InfrastructureStack", Group: "deployments.plural.sh"}, stackRun.Spec.StackRef.Name)
}
attr.StackID = stack.Status.ID
}
Expand Down
4 changes: 2 additions & 2 deletions go/controller/internal/controller/gitrepository_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (r *GitRepositoryReconciler) Reconcile(ctx context.Context, req ctrl.Reques
attrs, err := r.getRepositoryAttributes(ctx, repo)
if err != nil {
if errors.IsNotFound(err) {
utils.MarkCondition(repo.SetCondition, v1alpha1.SynchronizedConditionType, v1.ConditionFalse, v1alpha1.SynchronizedConditionReasonError, notFoundOrReadyError)
utils.MarkCondition(repo.SetCondition, v1alpha1.SynchronizedConditionType, v1.ConditionFalse, v1alpha1.SynchronizedConditionReasonError, notFoundOrReadyErrorMessage(err))
return RequeueAfter(requeueWaitForResources), nil
}
utils.MarkCondition(repo.SetCondition, v1alpha1.SynchronizedConditionType, v1.ConditionFalse, v1alpha1.SynchronizedConditionReasonError, err.Error())
Expand Down Expand Up @@ -179,7 +179,7 @@ func (r *GitRepositoryReconciler) getRepositoryAttributes(ctx context.Context, r
return nil, err
}
if connection.Status.ID == nil {
return nil, apierrors.NewNotFound(schema.GroupResource{}, repo.Spec.ConnectionRef.Name)
return nil, apierrors.NewNotFound(schema.GroupResource{Resource: "ScmConnection", Group: "deployments.plural.sh"}, repo.Spec.ConnectionRef.Name)
}

if err := utils.TryAddOwnerRef(ctx, r.Client, repo, connection, r.Scheme); err != nil {
Expand Down
8 changes: 4 additions & 4 deletions go/controller/internal/controller/globalservice_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (r *GlobalServiceReconciler) Reconcile(ctx context.Context, req ctrl.Reques
provider, err := r.getProvider(ctx, globalService)
if err != nil {
if errors.IsNotFound(err) {
utils.MarkCondition(globalService.SetCondition, v1alpha1.SynchronizedConditionType, v1.ConditionFalse, v1alpha1.SynchronizedConditionReasonError, notFoundOrReadyError)
utils.MarkCondition(globalService.SetCondition, v1alpha1.SynchronizedConditionType, v1.ConditionFalse, v1alpha1.SynchronizedConditionReasonError, notFoundOrReadyErrorMessage(err))
return RequeueAfter(requeueWaitForResources), nil
}
utils.MarkCondition(globalService.SetCondition, v1alpha1.SynchronizedConditionType, v1.ConditionFalse, v1alpha1.SynchronizedConditionReasonError, err.Error())
Expand All @@ -121,7 +121,7 @@ func (r *GlobalServiceReconciler) Reconcile(ctx context.Context, req ctrl.Reques
project, err := r.getProject(ctx, globalService)
if err != nil {
if errors.IsNotFound(err) {
utils.MarkCondition(globalService.SetCondition, v1alpha1.SynchronizedConditionType, v1.ConditionFalse, v1alpha1.SynchronizedConditionReasonError, notFoundOrReadyError)
utils.MarkCondition(globalService.SetCondition, v1alpha1.SynchronizedConditionType, v1.ConditionFalse, v1alpha1.SynchronizedConditionReasonError, notFoundOrReadyErrorMessage(err))
return RequeueAfter(requeueWaitForResources), nil
}
utils.MarkCondition(globalService.SetCondition, v1alpha1.SynchronizedConditionType, v1.ConditionFalse, v1alpha1.SynchronizedConditionReason, err.Error())
Expand Down Expand Up @@ -233,7 +233,7 @@ func (r *GlobalServiceReconciler) getProvider(ctx context.Context, globalService
}
if provider.Status.ID == nil {
logger.Info("Provider is not ready")
return provider, apierrors.NewNotFound(schema.GroupResource{}, globalService.Spec.ProviderRef.Name)
return provider, apierrors.NewNotFound(schema.GroupResource{Resource: "Provider", Group: "deployments.plural.sh"}, globalService.Spec.ProviderRef.Name)
}
}

Expand All @@ -250,7 +250,7 @@ func (r *GlobalServiceReconciler) getProject(ctx context.Context, globalService

if project.Status.ID == nil {
logger.Info("Project is not ready")
return project, apierrors.NewNotFound(schema.GroupResource{}, globalService.Spec.ProjectRef.Name)
return project, apierrors.NewNotFound(schema.GroupResource{Resource: "Project", Group: "deployments.plural.sh"}, globalService.Spec.ProjectRef.Name)
}

if err := controllerutil.SetOwnerReference(project, globalService, r.Scheme); err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (r *ManagedNamespaceReconciler) Reconcile(ctx context.Context, req ctrl.Req
attr, err := r.getNamespaceAttributes(ctx, managedNamespace)
if err != nil {
if errors.IsNotFound(err) {
utils.MarkCondition(managedNamespace.SetCondition, v1alpha1.SynchronizedConditionType, v1.ConditionFalse, v1alpha1.SynchronizedConditionReasonError, notFoundOrReadyError)
utils.MarkCondition(managedNamespace.SetCondition, v1alpha1.SynchronizedConditionType, v1.ConditionFalse, v1alpha1.SynchronizedConditionReasonError, notFoundOrReadyErrorMessage(err))
return RequeueAfter(requeueWaitForResources), nil
}
utils.MarkCondition(managedNamespace.SetCondition, v1alpha1.SynchronizedConditionType, v1.ConditionFalse, v1alpha1.SynchronizedConditionReasonError, err.Error())
Expand Down Expand Up @@ -296,7 +296,7 @@ func (r *ManagedNamespaceReconciler) getNamespaceAttributes(ctx context.Context,
if project.Status.ID == nil {
logger.Info("Project is not ready")
utils.MarkCondition(ns.SetCondition, v1alpha1.SynchronizedConditionType, v1.ConditionFalse, v1alpha1.SynchronizedConditionReason, "project is not ready")
return nil, errors.NewNotFound(schema.GroupResource{}, ns.Spec.ProjectRef.Name)
return nil, errors.NewNotFound(schema.GroupResource{Resource: "Project", Group: "deployments.plural.sh"}, ns.Spec.ProjectRef.Name)
}

if err := controllerutil.SetOwnerReference(project, ns, r.Scheme); err != nil {
Expand All @@ -317,7 +317,7 @@ func (r *ManagedNamespaceReconciler) getRepository(ctx context.Context, ns *v1al
}
if repository.Status.ID == nil {
utils.MarkCondition(ns.SetCondition, v1alpha1.SynchronizedConditionType, v1.ConditionFalse, v1alpha1.SynchronizedConditionReason, "repository is not ready")
return nil, errors.NewNotFound(schema.GroupResource{}, ns.Spec.Service.RepositoryRef.Name)
return nil, errors.NewNotFound(schema.GroupResource{Resource: "GitRepository", Group: "deployments.plural.sh"}, ns.Spec.Service.RepositoryRef.Name)
}
if repository.Status.Health == v1alpha1.GitHealthFailed {
return nil, fmt.Errorf("repository %s is not healthy", repository.Name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func (r *NotificationRouterReconciler) Reconcile(ctx context.Context, req ctrl.R
attr, err := r.genNotificationRouterAttr(ctx, notificationRouter)
if err != nil {
if errors.IsNotFound(err) {
utils.MarkCondition(notificationRouter.SetCondition, v1alpha1.SynchronizedConditionType, v1.ConditionFalse, v1alpha1.SynchronizedConditionReasonError, notFoundOrReadyError)
utils.MarkCondition(notificationRouter.SetCondition, v1alpha1.SynchronizedConditionType, v1.ConditionFalse, v1alpha1.SynchronizedConditionReasonError, notFoundOrReadyErrorMessage(err))
return RequeueAfter(requeueWaitForResources), nil
}
utils.MarkCondition(notificationRouter.SetCondition, v1alpha1.SynchronizedConditionType, v1.ConditionFalse, v1alpha1.SynchronizedConditionReasonError, err.Error())
Expand Down Expand Up @@ -191,7 +191,7 @@ func (r *NotificationRouterReconciler) genNotificationRouterAttr(ctx context.Con
}

if notifSink.Status.ID == nil {
return nil, errors.NewNotFound(schema.GroupResource{}, sink.Name)
return nil, errors.NewNotFound(schema.GroupResource{Resource: "NotificationSink", Group: "deployments.plural.sh"}, sink.Name)
}

attr.RouterSinks = append(attr.RouterSinks, &console.RouterSinkAttributes{SinkID: *notifSink.Status.ID})
Expand All @@ -209,7 +209,7 @@ func (r *NotificationRouterReconciler) getClusterID(ctx context.Context, obj *co
return nil, err
}
if !cluster.Status.HasID() {
return nil, errors.NewNotFound(schema.GroupResource{}, obj.Name)
return nil, errors.NewNotFound(schema.GroupResource{Resource: "Cluster", Group: "deployments.plural.sh"}, obj.Name)
}
return cluster.Status.ID, nil
}
Expand All @@ -223,7 +223,7 @@ func (r *NotificationRouterReconciler) getServiceID(ctx context.Context, objRef
return nil, err
}
if !resource.Status.HasID() {
return nil, errors.NewNotFound(schema.GroupResource{}, objRef.Name)
return nil, errors.NewNotFound(schema.GroupResource{Resource: "ServiceDeployment", Group: "deployments.plural.sh"}, objRef.Name)
}
return resource.Status.ID, nil
}
Expand All @@ -237,7 +237,7 @@ func (r *NotificationRouterReconciler) getPipelineID(ctx context.Context, objRef
return nil, err
}
if !resource.Status.HasID() {
return nil, errors.NewNotFound(schema.GroupResource{}, objRef.Name)
return nil, errors.NewNotFound(schema.GroupResource{Resource: "Pipeline", Group: "deployments.plural.sh"}, objRef.Name)
}
return resource.Status.ID, nil
}
Expand Down
8 changes: 4 additions & 4 deletions go/controller/internal/controller/observer_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (r *ObserverReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_
if err != nil {
logger.Error(err, "unable to create or update observer")
if errors.IsNotFound(err) {
utils.MarkCondition(observer.SetCondition, v1alpha1.SynchronizedConditionType, metav1.ConditionFalse, v1alpha1.SynchronizedConditionReasonError, notFoundOrReadyError)
utils.MarkCondition(observer.SetCondition, v1alpha1.SynchronizedConditionType, metav1.ConditionFalse, v1alpha1.SynchronizedConditionReasonError, notFoundOrReadyErrorMessage(err))
return RequeueAfter(requeueWaitForResources), nil
}
utils.MarkCondition(observer.SetCondition, v1alpha1.SynchronizedConditionType, metav1.ConditionFalse, v1alpha1.SynchronizedConditionReasonError, err.Error())
Expand Down Expand Up @@ -182,7 +182,7 @@ func (r *ObserverReconciler) getAttributes(ctx context.Context, observer *v1alph
return target, actions, projectID, err
}
if !gitRepo.Status.HasID() {
err = errors.NewNotFound(schema.GroupResource{}, gitRepo.Name)
err = errors.NewNotFound(schema.GroupResource{Resource: "GitRepository", Group: "deployments.plural.sh"}, gitRepo.Name)
return target, actions, projectID, err
}
target.Git = &console.ObserverGitAttributes{
Expand Down Expand Up @@ -217,7 +217,7 @@ func (r *ObserverReconciler) getAttributes(ctx context.Context, observer *v1alph
return target, actions, projectID, err
}
if !prAutomation.Status.HasID() {
err = errors.NewNotFound(schema.GroupResource{}, prAutomation.Name)
err = errors.NewNotFound(schema.GroupResource{Resource: "PrAutomation", Group: "deployments.plural.sh"}, prAutomation.Name)
return target, actions, projectID, err
}

Expand All @@ -237,7 +237,7 @@ func (r *ObserverReconciler) getAttributes(ctx context.Context, observer *v1alph
return target, actions, projectID, err
}
if !pipeline.Status.HasID() {
err = errors.NewNotFound(schema.GroupResource{}, pipeline.Name)
err = errors.NewNotFound(schema.GroupResource{Resource: "Pipeline", Group: "deployments.plural.sh"}, pipeline.Name)
return target, actions, projectID, err
}
a.Configuration.Pipeline = &console.ObserverPipelineActionAttributes{
Expand Down
2 changes: 1 addition & 1 deletion go/controller/internal/controller/pipeline_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (r *PipelineReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_
return requeue, nil
}
if apierrors.IsNotFound(err) {
utils.MarkCondition(pipeline.SetCondition, v1alpha1.SynchronizedConditionType, v1.ConditionFalse, v1alpha1.SynchronizedConditionReasonError, notFoundOrReadyError)
utils.MarkCondition(pipeline.SetCondition, v1alpha1.SynchronizedConditionType, v1.ConditionFalse, v1alpha1.SynchronizedConditionReasonError, notFoundOrReadyErrorMessage(err))
return RequeueAfter(requeueWaitForResources), nil
}
utils.MarkCondition(pipeline.SetCondition, v1alpha1.SynchronizedConditionType, v1.ConditionFalse, v1alpha1.SynchronizedConditionReasonError, err.Error())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (r *PipelineReconciler) pipelineStageServiceAttributes(ctx context.Context,
}

if service.Status.ID == nil {
return nil, errors.NewNotFound(schema.GroupResource{}, service.Name)
return nil, errors.NewNotFound(schema.GroupResource{Resource: "ServiceDeployment", Group: "deployments.plural.sh"}, service.Name)
}

// Extracting cluster ref from the service, not from the custom resource field (i.e. PipelineStageService.ClusterRef).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (in *PrAutomationReconciler) Attributes(ctx context.Context, pra *v1alpha1.
return nil, err
}
if connectionID == nil {
return nil, errors.NewNotFound(schema.GroupResource{}, pra.Spec.ScmConnectionRef.Name)
return nil, errors.NewNotFound(schema.GroupResource{Resource: "ScmConnection", Group: "deployments.plural.sh"}, pra.Spec.ScmConnectionRef.Name)
}

projectID, err := helper.IDFromRef(pra.Spec.ProjectRef, &v1alpha1.Project{})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (in *PrAutomationReconciler) Reconcile(ctx context.Context, req reconcile.R
apiPrAutomation, sha, err := in.sync(ctx, prAutomation)
if err != nil {
if errors.IsNotFound(err) {
utils.MarkCondition(prAutomation.SetCondition, v1alpha1.SynchronizedConditionType, metav1.ConditionFalse, v1alpha1.SynchronizedConditionReasonError, notFoundOrReadyError)
utils.MarkCondition(prAutomation.SetCondition, v1alpha1.SynchronizedConditionType, metav1.ConditionFalse, v1alpha1.SynchronizedConditionReasonError, notFoundOrReadyErrorMessage(err))
return requeue, nil
}
logger.Error(err, "unable to create or update prAutomation")
Expand Down

0 comments on commit 56b93c8

Please sign in to comment.