diff --git a/cmd/operator/operator.go b/cmd/operator/operator.go index c99189213..fcdf72ca7 100644 --- a/cmd/operator/operator.go +++ b/cmd/operator/operator.go @@ -27,6 +27,7 @@ import ( "github.com/openshift/lvm-operator/internal/controllers/persistent-volume" "github.com/openshift/lvm-operator/internal/controllers/persistent-volume-claim" "github.com/spf13/cobra" + appsv1 "k8s.io/api/apps/v1" "k8s.io/apimachinery/pkg/api/meta" "k8s.io/apimachinery/pkg/runtime" "sigs.k8s.io/controller-runtime/pkg/cache" @@ -161,7 +162,13 @@ func run(cmd *cobra.Command, _ []string, opts *Options) error { Port: 9443, }}, Cache: cache.Options{ - DefaultNamespaces: map[string]cache.Config{operatorNamespace: {}}, + ByObject: map[client.Object]cache.ByObject{ + &lvmv1alpha1.LVMCluster{}: {Namespaces: map[string]cache.Config{operatorNamespace: {}}}, + &lvmv1alpha1.LVMVolumeGroup{}: {Namespaces: map[string]cache.Config{operatorNamespace: {}}}, + &lvmv1alpha1.LVMVolumeGroupNodeStatus{}: {Namespaces: map[string]cache.Config{operatorNamespace: {}}}, + &appsv1.Deployment{}: {Namespaces: map[string]cache.Config{operatorNamespace: {}}}, + &appsv1.DaemonSet{}: {Namespaces: map[string]cache.Config{operatorNamespace: {}}}, + }, }, HealthProbeBindAddress: opts.healthProbeAddr, LeaderElectionResourceLockInterface: le.Lock, diff --git a/internal/controllers/persistent-volume-claim/controller.go b/internal/controllers/persistent-volume-claim/controller.go index c76bd8aa7..8dda9a6a9 100644 --- a/internal/controllers/persistent-volume-claim/controller.go +++ b/internal/controllers/persistent-volume-claim/controller.go @@ -5,6 +5,7 @@ import ( "fmt" "math" "strings" + "time" "github.com/openshift/lvm-operator/internal/controllers/constants" "github.com/pkg/errors" @@ -111,12 +112,12 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu // Publish an event if the requested storage is greater than the available capacity if !found { - r.Recorder.Event(pvc, "Warning", "NotEnoughCapacity", - fmt.Sprintf("Requested storage (%s) is greater than available capacity on any node (%s).", requestedStorage.String(), strings.Join(nodeMessage, ","))) - logger.Info("Event published for the PVC", "PVC", req.NamespacedName) + msg := fmt.Sprintf("Requested storage (%s) is greater than available capacity on any node (%s).", requestedStorage.String(), strings.Join(nodeMessage, ",")) + r.Recorder.Event(pvc, "Warning", "NotEnoughCapacity", msg) + logger.V(7).Info(msg) } - return ctrl.Result{}, nil + return ctrl.Result{RequeueAfter: 15 * time.Second}, nil } // SetupWithManager sets up the controller with the Manager. diff --git a/internal/controllers/persistent-volume-claim/controller_test.go b/internal/controllers/persistent-volume-claim/controller_test.go index 2d89c3d96..429ea9c63 100644 --- a/internal/controllers/persistent-volume-claim/controller_test.go +++ b/internal/controllers/persistent-volume-claim/controller_test.go @@ -30,6 +30,7 @@ func TestPersistentVolumeClaimReconciler_Reconcile(t *testing.T) { objs []client.Object wantErr bool expectNoStorageAvailable bool + expectRequeue bool }{ { name: "testing set deletionTimestamp", @@ -107,6 +108,7 @@ func TestPersistentVolumeClaimReconciler_Reconcile(t *testing.T) { }, }, expectNoStorageAvailable: true, + expectRequeue: true, }, { name: "testing PVC requesting more storage than capacity in the node", @@ -134,6 +136,7 @@ func TestPersistentVolumeClaimReconciler_Reconcile(t *testing.T) { }, }, expectNoStorageAvailable: true, + expectRequeue: true, }, { name: "testing PVC requesting less storage than capacity in the node", @@ -161,6 +164,7 @@ func TestPersistentVolumeClaimReconciler_Reconcile(t *testing.T) { }, }, expectNoStorageAvailable: false, + expectRequeue: true, }, { name: "testing PVC requesting less storage than capacity in one node, having another node without annotation", @@ -191,6 +195,7 @@ func TestPersistentVolumeClaimReconciler_Reconcile(t *testing.T) { }, }, expectNoStorageAvailable: false, + expectRequeue: true, }, } for _, tt := range tests { @@ -205,10 +210,14 @@ func TestPersistentVolumeClaimReconciler_Reconcile(t *testing.T) { t.Errorf("Reconcile() error = %v, wantErr %v", err, tt.wantErr) return } - if !reflect.DeepEqual(got, controllerruntime.Result{}) { + if !tt.expectRequeue && !reflect.DeepEqual(got, controllerruntime.Result{}) { t.Errorf("Reconcile() got non default Result") return } + if tt.expectRequeue && !reflect.DeepEqual(got, controllerruntime.Result{RequeueAfter: 15 * time.Second}) { + t.Errorf("Reconcile() got an unexpected Result") + return + } select { case event := <-recorder.Events: