Skip to content

Commit

Permalink
controllers: define OwnersIndexFieldFunc()
Browse files Browse the repository at this point in the history
Signed-off-by: Jose A. Rivera <[email protected]>
  • Loading branch information
jarrpa committed Mar 19, 2024
1 parent b460b29 commit 1a3ca59
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 63 deletions.
18 changes: 5 additions & 13 deletions controllers/storageclassrequest/storageclassrequest_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
v1 "github.com/red-hat-storage/ocs-operator/api/v4/v1"
"github.com/red-hat-storage/ocs-operator/api/v4/v1alpha1"
controllers "github.com/red-hat-storage/ocs-operator/v4/controllers/storageconsumer"
"github.com/red-hat-storage/ocs-operator/v4/controllers/util"
rookCephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"
storagev1 "k8s.io/api/storage/v1"
"k8s.io/apimachinery/pkg/api/errors"
Expand All @@ -45,8 +46,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

const ownerUIDIndexName = "ownerUID"

// StorageClassRequestReconciler reconciles a StorageClassRequest object
// nolint:revive
type StorageClassRequestReconciler struct {
Expand Down Expand Up @@ -143,17 +142,10 @@ func (r *StorageClassRequestReconciler) SetupWithManager(mgr ctrl.Manager) error
if err := mgr.GetCache().IndexField(
context.TODO(),
&rookCephv1.CephFilesystemSubVolumeGroup{},
ownerUIDIndexName,
func(obj client.Object) []string {
refs := obj.GetOwnerReferences()
owners := []string{}
for i := range refs {
owners = append(owners, string(refs[i].UID))
}
return owners
},
util.OwnerUIDIndexName,
util.OwnersIndexFieldFunc,
); err != nil {
return fmt.Errorf("unable to set up FieldIndexer for owner reference UIDs: %v", err)
return fmt.Errorf("unable to set up FieldIndexer on CephFilesystemSubVolumeGroups for owner reference UIDs: %v", err)
}

enqueueStorageConsumerRequest := handler.EnqueueRequestsFromMapFunc(
Expand Down Expand Up @@ -266,7 +258,7 @@ func (r *StorageClassRequestReconciler) initPhase(storageProfile *v1.StorageProf
r.ctx,
cephFilesystemSubVolumeGroupList,
client.InNamespace(r.OperatorNamespace),
client.MatchingFields{ownerUIDIndexName: string(r.StorageClassRequest.UID)})
client.MatchingFields{util.OwnerUIDIndexName: string(r.StorageClassRequest.UID)})
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ import (
v1 "github.com/red-hat-storage/ocs-operator/api/v4/v1"
"github.com/red-hat-storage/ocs-operator/api/v4/v1alpha1"
controllers "github.com/red-hat-storage/ocs-operator/v4/controllers/storageconsumer"
"github.com/red-hat-storage/ocs-operator/v4/controllers/util"
rookCephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"
"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/cache/informertest"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/log"
Expand Down Expand Up @@ -176,6 +176,12 @@ func createFakeReconciler(t *testing.T) StorageClassRequestReconciler {
return fakeReconciler
}

func newFakeClientBuilder(scheme *runtime.Scheme) *fake.ClientBuilder {
return fake.NewClientBuilder().
WithScheme(scheme).
WithIndex(&rookCephv1.CephFilesystemSubVolumeGroup{}, util.OwnerUIDIndexName, util.OwnersIndexFieldFunc)
}

func TestProfileReconcile(t *testing.T) {
var err error
var caseCounter int
Expand Down Expand Up @@ -246,18 +252,9 @@ func TestProfileReconcile(t *testing.T) {
c.createObjects = append(c.createObjects, fakeStorageProfile)

r.Cache = &informertest.FakeInformers{Scheme: r.Scheme}
fakeClient := fake.NewClientBuilder().
WithScheme(r.Scheme).
fakeClient := newFakeClientBuilder(r.Scheme).
WithRuntimeObjects(c.createObjects...).
WithStatusSubresource(fakeStorageClassRequest).
WithIndex(&rookCephv1.CephFilesystemSubVolumeGroup{}, ownerUIDIndexName, func(obj client.Object) []string {
refs := obj.GetOwnerReferences()
owners := []string{}
for i := range refs {
owners = append(owners, string(refs[i].UID))
}
return owners
})
WithStatusSubresource(fakeStorageClassRequest)
r.Client = fakeClient.Build()

req := reconcile.Request{}
Expand Down Expand Up @@ -363,7 +360,7 @@ func TestStorageProfileCephBlockPool(t *testing.T) {
c.createObjects = append(c.createObjects, c.storageProfile)
c.createObjects = append(c.createObjects, fakeStorageConsumer)

fakeClient := fake.NewClientBuilder().WithScheme(r.Scheme).WithRuntimeObjects(c.createObjects...)
fakeClient := newFakeClientBuilder(r.Scheme).WithRuntimeObjects(c.createObjects...)
r.Client = fakeClient.Build()

_, err = r.reconcilePhases()
Expand Down Expand Up @@ -469,18 +466,8 @@ func TestStorageProfileCephFsSubVolGroup(t *testing.T) {
c.createObjects = append(c.createObjects, c.cephFs)
c.createObjects = append(c.createObjects, c.storageProfile)
c.createObjects = append(c.createObjects, fakeStorageConsumer)
fakeClient := fake.NewClientBuilder().
WithScheme(r.Scheme).
WithRuntimeObjects(c.createObjects...).
WithIndex(&rookCephv1.CephFilesystemSubVolumeGroup{}, ownerUIDIndexName, func(obj client.Object) []string {
refs := obj.GetOwnerReferences()
owners := []string{}
for i := range refs {
owners = append(owners, string(refs[i].UID))
}
return owners
})

fakeClient := newFakeClientBuilder(r.Scheme).
WithRuntimeObjects(c.createObjects...)
r.Client = fakeClient.Build()

_, err = r.reconcilePhases()
Expand Down Expand Up @@ -620,7 +607,7 @@ func TestCephBlockPool(t *testing.T) {

c.createObjects = append(c.createObjects, fakeStorageProfile)
c.createObjects = append(c.createObjects, fakeStorageConsumer)
fakeClient := fake.NewClientBuilder().WithScheme(r.Scheme).WithRuntimeObjects(c.createObjects...)
fakeClient := newFakeClientBuilder(r.Scheme).WithRuntimeObjects(c.createObjects...)
r.Client = fakeClient.Build()

_, err = r.reconcilePhases()
Expand Down Expand Up @@ -653,7 +640,7 @@ func TestCephBlockPool(t *testing.T) {
r := createFakeReconciler(t)
r.StorageClassRequest.Spec.Type = "blockpool"
r.StorageClassRequest.Spec.StorageProfile = badStorageProfile.Name
fakeClient := fake.NewClientBuilder().WithScheme(r.Scheme)
fakeClient := newFakeClientBuilder(r.Scheme)
r.Client = fakeClient.WithRuntimeObjects(badStorageProfile, fakeStorageConsumer).Build()

_, err = r.reconcilePhases()
Expand Down Expand Up @@ -711,17 +698,8 @@ func TestCephFsSubVolGroup(t *testing.T) {
c.createObjects = append(c.createObjects, fakeCephFs)
c.createObjects = append(c.createObjects, fakeStorageProfile)
c.createObjects = append(c.createObjects, fakeStorageConsumer)
fakeClient := fake.NewClientBuilder().
WithScheme(r.Scheme).
WithRuntimeObjects(c.createObjects...).
WithIndex(&rookCephv1.CephFilesystemSubVolumeGroup{}, ownerUIDIndexName, func(obj client.Object) []string {
refs := obj.GetOwnerReferences()
owners := []string{}
for i := range refs {
owners = append(owners, string(refs[i].UID))
}
return owners
})
fakeClient := newFakeClientBuilder(r.Scheme).
WithRuntimeObjects(c.createObjects...)
r.Client = fakeClient.Build()

_, err = r.reconcilePhases()
Expand All @@ -747,17 +725,8 @@ func TestCephFsSubVolGroup(t *testing.T) {
r := createFakeReconciler(t)
r.StorageClassRequest.Spec.Type = "sharedfilesystem"
r.StorageClassRequest.Spec.StorageProfile = fakeStorageProfile.Name
fakeClient := fake.NewClientBuilder().
WithScheme(r.Scheme).
WithRuntimeObjects(fakeStorageProfile, fakeStorageConsumer).
WithIndex(&rookCephv1.CephFilesystemSubVolumeGroup{}, ownerUIDIndexName, func(obj client.Object) []string {
refs := obj.GetOwnerReferences()
owners := []string{}
for i := range refs {
owners = append(owners, string(refs[i].UID))
}
return owners
})
fakeClient := newFakeClientBuilder(r.Scheme).
WithRuntimeObjects(fakeStorageProfile, fakeStorageConsumer)
r.Client = fakeClient.Build()

_, err = r.reconcilePhases()
Expand All @@ -773,7 +742,7 @@ func TestCephFsSubVolGroup(t *testing.T) {
r = createFakeReconciler(t)
r.StorageClassRequest.Spec.Type = "sharedfilesystem"
r.StorageClassRequest.Spec.StorageProfile = badStorageProfile.Name
fakeClient = fake.NewClientBuilder().WithScheme(r.Scheme)
fakeClient = newFakeClientBuilder(r.Scheme)
r.Client = fakeClient.WithRuntimeObjects(badStorageProfile, fakeStorageConsumer, fakeCephFs).Build()

_, err = r.reconcilePhases()
Expand Down
12 changes: 12 additions & 0 deletions controllers/util/k8sutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ const (
EnableTopologyKey = "CSI_ENABLE_TOPOLOGY"
TopologyDomainLabelsKey = "CSI_TOPOLOGY_DOMAIN_LABELS"
EnableNFSKey = "ROOK_CSI_ENABLE_NFS"

// This is the name for the OwnerUID FieldIndex
OwnerUIDIndexName = "ownerUID"
)

// GetWatchNamespace returns the namespace the operator should be watching for changes
Expand Down Expand Up @@ -116,3 +119,12 @@ func GetCountOfRunningPods(podList *corev1.PodList) int {
}
return count
}

func OwnersIndexFieldFunc(obj client.Object) []string {
refs := obj.GetOwnerReferences()
owners := []string{}
for i := range refs {
owners = append(owners, string(refs[i].UID))
}
return owners
}

0 comments on commit 1a3ca59

Please sign in to comment.