Files
my-rook-config/pkg/operator/ceph/cluster/dependents.go
T
Blaine Gardner 01e2feaef5 ceph: get rid of dynamic clientset
Stop using the dynamic clientset in favor of the controller-runtime
clientset.

Signed-off-by: Blaine Gardner <blaine.gardner@redhat.com>
2021-10-19 17:05:38 -06:00

85 lines
2.5 KiB
Go

/*
Copyright 2021 The Rook Authors. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cluster
import (
"context"
"strings"
"github.com/pkg/errors"
cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"
"github.com/rook/rook/pkg/clusterd"
"github.com/rook/rook/pkg/util"
"github.com/rook/rook/pkg/util/dependents"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/client"
)
var (
// must use plural kinds
cephClusterDependentListKinds []string = []string{
"CephBlockPoolList",
"CephRBDMirrorList",
"CephFilesystemList",
"CephFilesystemMirrorList",
"CephObjectStoreList",
"CephObjectStoreUserList",
"CephObjectZoneList",
"CephObjectZoneGroupList",
"CephObjectRealmList",
"CephNFSList",
"CephClientList",
}
)
// CephClusterDependents returns a DependentList of dependents of a CephCluster in the namespace.
func CephClusterDependents(c *clusterd.Context, namespace string) (*dependents.DependentList, error) {
ctx := context.TODO()
dependents := dependents.NewDependentList()
errs := []error{}
for _, listKind := range cephClusterDependentListKinds {
list := &unstructured.UnstructuredList{}
list.SetGroupVersionKind(schema.GroupVersionKind{
Group: cephv1.SchemeGroupVersion.Group,
Version: cephv1.SchemeGroupVersion.Version,
Kind: listKind,
})
err := c.Client.List(ctx, list, client.InNamespace(namespace))
if err != nil {
errs = append(errs, errors.Wrapf(err, "failed to get %s", listKind))
continue
}
if len(list.Items) > 0 {
for _, obj := range list.Items {
dependents.Add(listKindToSingularKind(listKind), obj.GetName())
}
}
}
// returns a nil error if there are no errors in the list
outErr := util.AggregateErrors(errs, "failed to list some dependents for CephCluster in namespace %q", namespace)
return dependents, outErr
}
func listKindToSingularKind(listKind string) string {
return strings.TrimSuffix(listKind, "List")
}