Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OCPBUGS-22109: fix: restrict namespaced cache to relevant CRDs only #453

Merged
merged 2 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion cmd/operator/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down
9 changes: 5 additions & 4 deletions internal/controllers/persistent-volume-claim/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"math"
"strings"
"time"

"github.com/openshift/lvm-operator/internal/controllers/constants"
"github.com/pkg/errors"
Expand Down Expand Up @@ -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.
Expand Down
11 changes: 10 additions & 1 deletion internal/controllers/persistent-volume-claim/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func TestPersistentVolumeClaimReconciler_Reconcile(t *testing.T) {
objs []client.Object
wantErr bool
expectNoStorageAvailable bool
expectRequeue bool
}{
{
name: "testing set deletionTimestamp",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -191,6 +195,7 @@ func TestPersistentVolumeClaimReconciler_Reconcile(t *testing.T) {
},
},
expectNoStorageAvailable: false,
expectRequeue: true,
},
}
for _, tt := range tests {
Expand All @@ -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:
Expand Down