Files
my-rook-config/pkg/operator/ceph/cluster/dependents.go
T
Eng Zer Jun 77bff6a07c core: remove redundant len check
From the Go specification [1]:

  "1. For a nil slice, the number of iterations is 0."
  "3. If the map is nil, the number of iterations is 0."

`len` returns 0 if the slice or map is nil [2]. Therefore, checking
`len(v) > 0` before a loop is unnecessary.

[1]: https://go.dev/ref/spec#For_range
[2]: https://pkg.go.dev/builtin#len

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
2023-10-05 20:16:46 +08:00

87 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",
"CephBucketTopic",
"CephBucketNotification",
"CephFilesystemSubVolumeGroup",
"CephBlockPoolRadosNamespace",
}
)
// 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
}
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")
}