diff --git a/metrics/vendor/github.com/red-hat-storage/ocs-operator/v4/controllers/util/k8sutil.go b/metrics/vendor/github.com/red-hat-storage/ocs-operator/v4/controllers/util/k8sutil.go index fb1e5504ea..87103da9d1 100644 --- a/metrics/vendor/github.com/red-hat-storage/ocs-operator/v4/controllers/util/k8sutil.go +++ b/metrics/vendor/github.com/red-hat-storage/ocs-operator/v4/controllers/util/k8sutil.go @@ -166,3 +166,29 @@ func GenerateNameForNonResilientCephBlockPoolSC(initData *ocsv1.StorageCluster) } return fmt.Sprintf("%s-ceph-non-resilient-rbd", initData.Name) } + +func GetStorageClusterInNamespace(ctx context.Context, cl client.Client, namespace string) (*ocsv1.StorageCluster, error) { + storageClusterList := &ocsv1.StorageClusterList{} + err := cl.List(ctx, storageClusterList, client.InNamespace(namespace)) + if err != nil { + return nil, fmt.Errorf("unable to list storageCluster(s) in namespace %s: %v", namespace, err) + } + + var foundSc *ocsv1.StorageCluster + for i := range storageClusterList.Items { + sc := &storageClusterList.Items[i] + if sc.Status.Phase == PhaseIgnored { + continue // Skip Ignored storage cluster + } + if foundSc != nil { + // This means we have already found one storage cluster, so this is a second one + return nil, fmt.Errorf("multiple storageClusters found in namespace %s, expected: 1 actual: %v", namespace, len(storageClusterList.Items)) + } + foundSc = sc + } + + if foundSc == nil { + return nil, fmt.Errorf("no storageCluster found in namespace %s, expected: 1", namespace) + } + return foundSc, nil +}