Skip to content

Commit

Permalink
Reuse getObject function
Browse files Browse the repository at this point in the history
Signed-off-by: Jason Parraga <sovietaced@gmail.com>
  • Loading branch information
Sovietaced committed Jan 2, 2025
1 parent e003a0b commit 801bf51
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 38 deletions.
29 changes: 29 additions & 0 deletions internal/controller/common/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package common

import (
"context"

"github.com/go-logr/logr"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// GetObject will get the object from Kubernetes and return if it is missing or an error.
func GetObject(
ctx context.Context,
client client.Client,
object client.Object,
namespacedName types.NamespacedName,
logger logr.Logger,
) (miss bool, err error) {
logger.Info("Fetching object from cache")
if err := client.Get(ctx, namespacedName, object); err != nil {
if k8serrors.IsNotFound(err) {
logger.Info("Object not found in cache, ending reconcile...")
return true, nil
}
return true, err
}
return false, nil
}
14 changes: 6 additions & 8 deletions internal/controller/core/queue_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ import (
"fmt"
"time"

"github.com/armadaproject/armada-operator/internal/controller/common"

"github.com/armadaproject/armada/pkg/api"
"github.com/google/go-cmp/cmp"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/testing/protocmp"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -52,16 +53,13 @@ type QueueReconciler struct {
// move the current state of the cluster closer to the desired state.
func (r *QueueReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
logger := log.FromContext(ctx).WithValues("namespace", req.Namespace, "name", req.Name)

started := time.Now()
logger.Info("Reconciling Queue object")

logger.Info("Fetching Queue object from cache")
logger.Info("Reconciling object")

var queue corev1alpha1.Queue
if err := r.Client.Get(ctx, req.NamespacedName, &queue); err != nil {
if k8serrors.IsNotFound(err) {
logger.Info("Queue not found in cache, ending reconcile...", "namespace", req.Namespace, "name", req.Name)
return ctrl.Result{}, nil
}
if miss, err := common.GetObject(ctx, r.Client, &queue, req.NamespacedName, logger); err != nil || miss {
return ctrl.Result{}, err
}

Expand Down
4 changes: 3 additions & 1 deletion internal/controller/install/armadaserver_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"fmt"
"time"

"github.com/armadaproject/armada-operator/internal/controller/common"

"k8s.io/utils/ptr"

"github.com/go-logr/logr"
Expand Down Expand Up @@ -69,7 +71,7 @@ func (r *ArmadaServerReconciler) Reconcile(ctx context.Context, req ctrl.Request
logger.Info("Reconciling object")

var server installv1alpha1.ArmadaServer
if miss, err := getObject(ctx, r.Client, &server, req.NamespacedName, logger); err != nil || miss {
if miss, err := common.GetObject(ctx, r.Client, &server, req.NamespacedName, logger); err != nil || miss {
return ctrl.Result{}, err
}

Expand Down
4 changes: 3 additions & 1 deletion internal/controller/install/binoculars_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"context"
"time"

"github.com/armadaproject/armada-operator/internal/controller/common"

"github.com/pkg/errors"

installv1alpha1 "github.com/armadaproject/armada-operator/api/install/v1alpha1"
Expand Down Expand Up @@ -65,7 +67,7 @@ func (r *BinocularsReconciler) Reconcile(ctx context.Context, req ctrl.Request)
logger.Info("Reconciling Binoculars object")

var binoculars installv1alpha1.Binoculars
if miss, err := getObject(ctx, r.Client, &binoculars, req.NamespacedName, logger); err != nil || miss {
if miss, err := common.GetObject(ctx, r.Client, &binoculars, req.NamespacedName, logger); err != nil || miss {
return ctrl.Result{}, err
}

Expand Down
21 changes: 0 additions & 21 deletions internal/controller/install/common_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import (
"k8s.io/apimachinery/pkg/util/wait"

"github.com/go-logr/logr"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
Expand Down Expand Up @@ -679,25 +677,6 @@ func deleteObjectIfNeeded(
return nil
}

// getObject will get the object from Kubernetes and return if it is missing or an error.
func getObject(
ctx context.Context,
client client.Client,
object client.Object,
namespacedName types.NamespacedName,
logger logr.Logger,
) (miss bool, err error) {
logger.Info("Fetching object from cache")
if err := client.Get(ctx, namespacedName, object); err != nil {
if k8serrors.IsNotFound(err) {
logger.Info("Object not found in cache, ending reconcile...")
return true, nil
}
return true, err
}
return false, nil
}

func newProfilingComponents(
object metav1.Object,
scheme *runtime.Scheme,
Expand Down
4 changes: 3 additions & 1 deletion internal/controller/install/common_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"testing"
"time"

"github.com/armadaproject/armada-operator/internal/controller/common"

"github.com/go-logr/logr"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand Down Expand Up @@ -1013,7 +1015,7 @@ func TestGetObjectFromCache(t *testing.T) {

// Call the function under test
namespacedName := types.NamespacedName{Name: "test-resource", Namespace: "default"}
miss, err := getObject(ctx, fakeClient, object, namespacedName, logger)
miss, err := common.GetObject(ctx, fakeClient, object, namespacedName, logger)
if tc.expectError {
assert.ErrorIs(t, err, tc.returnError)
} else {
Expand Down
4 changes: 3 additions & 1 deletion internal/controller/install/eventingester_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"context"
"time"

"github.com/armadaproject/armada-operator/internal/controller/common"

"github.com/pkg/errors"

appsv1 "k8s.io/api/apps/v1"
Expand Down Expand Up @@ -59,7 +61,7 @@ func (r *EventIngesterReconciler) Reconcile(ctx context.Context, req ctrl.Reques
logger.Info("Reconciling object")

var eventIngester installv1alpha1.EventIngester
if miss, err := getObject(ctx, r.Client, &eventIngester, req.NamespacedName, logger); err != nil || miss {
if miss, err := common.GetObject(ctx, r.Client, &eventIngester, req.NamespacedName, logger); err != nil || miss {
return ctrl.Result{}, err
}

Expand Down
4 changes: 3 additions & 1 deletion internal/controller/install/executor_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"fmt"
"time"

"github.com/armadaproject/armada-operator/internal/controller/common"

"k8s.io/apimachinery/pkg/util/intstr"

"k8s.io/utils/ptr"
Expand Down Expand Up @@ -85,7 +87,7 @@ func (r *ExecutorReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
logger.Info("Reconciling object")

var executor installv1alpha1.Executor
if miss, err := getObject(ctx, r.Client, &executor, req.NamespacedName, logger); err != nil || miss {
if miss, err := common.GetObject(ctx, r.Client, &executor, req.NamespacedName, logger); err != nil || miss {
return ctrl.Result{}, err
}

Expand Down
4 changes: 3 additions & 1 deletion internal/controller/install/lookout_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"context"
"time"

"github.com/armadaproject/armada-operator/internal/controller/common"

"github.com/pkg/errors"

monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
Expand Down Expand Up @@ -60,7 +62,7 @@ func (r *LookoutReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
logger.Info("Reconciling object")

var lookout installv1alpha1.Lookout
if miss, err := getObject(ctx, r.Client, &lookout, req.NamespacedName, logger); err != nil || miss {
if miss, err := common.GetObject(ctx, r.Client, &lookout, req.NamespacedName, logger); err != nil || miss {
return ctrl.Result{}, err
}

Expand Down
4 changes: 3 additions & 1 deletion internal/controller/install/lookoutingester_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"context"
"time"

"github.com/armadaproject/armada-operator/internal/controller/common"

"github.com/pkg/errors"

appsv1 "k8s.io/api/apps/v1"
Expand Down Expand Up @@ -56,7 +58,7 @@ func (r *LookoutIngesterReconciler) Reconcile(ctx context.Context, req ctrl.Requ
logger.Info("Reconciling object")

var lookoutIngester installv1alpha1.LookoutIngester
if miss, err := getObject(ctx, r.Client, &lookoutIngester, req.NamespacedName, logger); err != nil || miss {
if miss, err := common.GetObject(ctx, r.Client, &lookoutIngester, req.NamespacedName, logger); err != nil || miss {
return ctrl.Result{}, err
}

Expand Down
4 changes: 3 additions & 1 deletion internal/controller/install/scheduler_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"fmt"
"time"

"github.com/armadaproject/armada-operator/internal/controller/common"

"k8s.io/utils/ptr"

"k8s.io/apimachinery/pkg/util/duration"
Expand Down Expand Up @@ -70,7 +72,7 @@ func (r *SchedulerReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
logger.Info("Reconciling object")

var scheduler installv1alpha1.Scheduler
if miss, err := getObject(ctx, r.Client, &scheduler, req.NamespacedName, logger); err != nil || miss {
if miss, err := common.GetObject(ctx, r.Client, &scheduler, req.NamespacedName, logger); err != nil || miss {
return ctrl.Result{}, err
}

Expand Down
4 changes: 3 additions & 1 deletion internal/controller/install/scheduleringester_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"context"
"time"

"github.com/armadaproject/armada-operator/internal/controller/common"

"github.com/pkg/errors"

appsv1 "k8s.io/api/apps/v1"
Expand Down Expand Up @@ -59,7 +61,7 @@ func (r *SchedulerIngesterReconciler) Reconcile(ctx context.Context, req ctrl.Re
logger.Info("Reconciling object")

var schedulerIngester installv1alpha1.SchedulerIngester
if miss, err := getObject(ctx, r.Client, &schedulerIngester, req.NamespacedName, logger); err != nil || miss {
if miss, err := common.GetObject(ctx, r.Client, &schedulerIngester, req.NamespacedName, logger); err != nil || miss {
return ctrl.Result{}, err
}

Expand Down

0 comments on commit 801bf51

Please sign in to comment.