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>
This commit is contained in:
8 files changed
+118
-124
No files matched your search
@@ -36,7 +36,6 @@ import (
|
||||
"github.com/spf13/pflag"
|
||||
v1 "k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/util/uuid"
|
||||
"k8s.io/client-go/dynamic"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
"k8s.io/client-go/rest"
|
||||
"k8s.io/client-go/tools/clientcmd"
|
||||
@@ -143,11 +142,6 @@ func NewContext() *clusterd.Context {
|
||||
context.RemoteExecutor.ClientSet = context.Clientset
|
||||
context.RemoteExecutor.RestClient = context.KubeConfig
|
||||
|
||||
// Dynamic clientset allows dealing with resources that aren't statically typed but determined
|
||||
// at runtime.
|
||||
context.DynamicClientset, err = dynamic.NewForConfig(context.KubeConfig)
|
||||
TerminateOnError(err, "failed to create dynamic clientset")
|
||||
|
||||
context.RookClientset, err = rookclient.NewForConfig(context.KubeConfig)
|
||||
TerminateOnError(err, "failed to create rook clientset")
|
||||
|
||||
|
||||
@@ -21,7 +21,6 @@ import (
|
||||
rookclient "github.com/rook/rook/pkg/client/clientset/versioned"
|
||||
"github.com/rook/rook/pkg/util/exec"
|
||||
"github.com/rook/rook/pkg/util/sys"
|
||||
"k8s.io/client-go/dynamic"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
"k8s.io/client-go/rest"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
@@ -35,9 +34,6 @@ type Context struct {
|
||||
// Clientset is a connection to the core kubernetes API
|
||||
Clientset kubernetes.Interface
|
||||
|
||||
// DynamicClientset is a dynamic connection to the Kubernetes API
|
||||
DynamicClientset dynamic.Interface
|
||||
|
||||
// Represents the Client provided by the controller-runtime package to interact with Kubernetes objects
|
||||
Client client.Client
|
||||
|
||||
|
||||
@@ -33,7 +33,6 @@ import (
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
dynamicfake "k8s.io/client-go/dynamic/fake"
|
||||
k8sfake "k8s.io/client-go/kubernetes/fake"
|
||||
"k8s.io/client-go/tools/record"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
@@ -78,8 +77,6 @@ func TestReconcileDeleteCephCluster(t *testing.T) {
|
||||
// set up clusterd.Context
|
||||
clusterdCtx := &clusterd.Context{
|
||||
Clientset: k8sfake.NewSimpleClientset(),
|
||||
// reconcile looks for fake dependencies in the dynamic clientset
|
||||
DynamicClientset: dynamicfake.NewSimpleDynamicClient(scheme, fakePool),
|
||||
}
|
||||
|
||||
// create the cluster controller and tell it that the cluster has been deleted
|
||||
@@ -89,7 +86,7 @@ func TestReconcileDeleteCephCluster(t *testing.T) {
|
||||
|
||||
// Create a fake client to mock API calls
|
||||
// Make sure it has the fake CephCluster that is to be deleted in it
|
||||
client := clientfake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(fakeCluster).Build()
|
||||
client := clientfake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(fakeCluster, fakePool).Build()
|
||||
|
||||
err := corev1.AddToScheme(scheme)
|
||||
assert.NoError(t, err)
|
||||
@@ -108,7 +105,7 @@ func TestReconcileDeleteCephCluster(t *testing.T) {
|
||||
assert.NoError(t, err)
|
||||
assert.NotZero(t, resp.RequeueAfter)
|
||||
event := <-fakeRecorder.Events
|
||||
assert.Contains(t, event, "CephBlockPools")
|
||||
assert.Contains(t, event, "CephBlockPool")
|
||||
assert.Contains(t, event, "my-block-pool")
|
||||
|
||||
blockedCluster := &cephv1.CephCluster{}
|
||||
@@ -121,8 +118,7 @@ func TestReconcileDeleteCephCluster(t *testing.T) {
|
||||
assert.Equal(t, corev1.ConditionTrue, cephv1.FindStatusCondition(status.Conditions, cephv1.ConditionDeletionIsBlocked).Status)
|
||||
|
||||
// delete blocking dependency
|
||||
gvr := cephv1.SchemeGroupVersion.WithResource("cephblockpools")
|
||||
err = clusterdCtx.DynamicClientset.Resource(gvr).Namespace(cephNs).Delete(ctx, "my-block-pool", metav1.DeleteOptions{})
|
||||
err = client.Delete(ctx, fakePool)
|
||||
assert.NoError(t, err)
|
||||
|
||||
resp, err = reconcileCephCluster.Reconcile(ctx, req)
|
||||
|
||||
@@ -25,23 +25,25 @@ import (
|
||||
"github.com/rook/rook/pkg/clusterd"
|
||||
"github.com/rook/rook/pkg/util"
|
||||
"github.com/rook/rook/pkg/util/dependents"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"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
|
||||
cephClusterDependentPluralKinds []string = []string{
|
||||
"CephBlockPools",
|
||||
"CephRBDMirrors",
|
||||
"CephFilesystems",
|
||||
"CephFilesystemMirrors",
|
||||
"CephObjectStores",
|
||||
"CephObjectStoreUsers",
|
||||
"CephObjectZones",
|
||||
"CephObjectZoneGroups",
|
||||
"CephObjectRealms",
|
||||
"CephNFSes",
|
||||
"CephClients",
|
||||
cephClusterDependentListKinds []string = []string{
|
||||
"CephBlockPoolList",
|
||||
"CephRBDMirrorList",
|
||||
"CephFilesystemList",
|
||||
"CephFilesystemMirrorList",
|
||||
"CephObjectStoreList",
|
||||
"CephObjectStoreUserList",
|
||||
"CephObjectZoneList",
|
||||
"CephObjectZoneGroupList",
|
||||
"CephObjectRealmList",
|
||||
"CephNFSList",
|
||||
"CephClientList",
|
||||
}
|
||||
)
|
||||
|
||||
@@ -52,17 +54,22 @@ func CephClusterDependents(c *clusterd.Context, namespace string) (*dependents.D
|
||||
dependents := dependents.NewDependentList()
|
||||
errs := []error{}
|
||||
|
||||
for _, pluralKind := range cephClusterDependentPluralKinds {
|
||||
resource := pluralKindToResource(pluralKind)
|
||||
gvr := cephv1.SchemeGroupVersion.WithResource(resource)
|
||||
list, err := c.DynamicClientset.Resource(gvr).Namespace(namespace).List(ctx, metav1.ListOptions{})
|
||||
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 list %s", pluralKind))
|
||||
errs = append(errs, errors.Wrapf(err, "failed to get %s", listKind))
|
||||
continue
|
||||
}
|
||||
if len(list.Items) > 0 {
|
||||
for _, obj := range list.Items {
|
||||
dependents.Add(pluralKind, obj.GetName())
|
||||
dependents.Add(listKindToSingularKind(listKind), obj.GetName())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -72,8 +79,6 @@ func CephClusterDependents(c *clusterd.Context, namespace string) (*dependents.D
|
||||
return dependents, outErr
|
||||
}
|
||||
|
||||
func pluralKindToResource(pluralKind string) string {
|
||||
// The dynamic client wants resources which are lower-case versions of the plural Kinds for
|
||||
// Kubernetes CRDs in almost all cases.
|
||||
return strings.ToLower(pluralKind)
|
||||
func listKindToSingularKind(listKind string) string {
|
||||
return strings.TrimSuffix(listKind, "List")
|
||||
}
|
||||
@@ -19,14 +19,13 @@ package cluster
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"
|
||||
"github.com/rook/rook/pkg/clusterd"
|
||||
"github.com/stretchr/testify/assert"
|
||||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
dynamicfake "k8s.io/client-go/dynamic/fake"
|
||||
k8stesting "k8s.io/client-go/testing"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
fakeclient "sigs.k8s.io/controller-runtime/pkg/client/fake"
|
||||
)
|
||||
|
||||
func TestCephClusterDependents(t *testing.T) {
|
||||
@@ -37,10 +36,9 @@ func TestCephClusterDependents(t *testing.T) {
|
||||
|
||||
var c *clusterd.Context
|
||||
|
||||
newClusterdCtx := func(objects ...runtime.Object) *clusterd.Context {
|
||||
dynInt := dynamicfake.NewSimpleDynamicClient(scheme, objects...)
|
||||
newClusterdCtx := func(objects ...client.Object) *clusterd.Context {
|
||||
return &clusterd.Context{
|
||||
DynamicClientset: dynInt,
|
||||
Client: fakeclient.NewClientBuilder().WithScheme(scheme).WithObjects(objects...).Build(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,8 +58,8 @@ func TestCephClusterDependents(t *testing.T) {
|
||||
deps, err := CephClusterDependents(c, ns)
|
||||
assert.NoError(t, err)
|
||||
assert.False(t, deps.Empty())
|
||||
assert.ElementsMatch(t, []string{"CephBlockPools"}, deps.PluralKinds())
|
||||
assert.ElementsMatch(t, []string{"block-pool-1", "block-pool-2"}, deps.OfPluralKind("CephBlockPools"))
|
||||
assert.ElementsMatch(t, []string{"CephBlockPool"}, deps.PluralKinds())
|
||||
assert.ElementsMatch(t, []string{"block-pool-1", "block-pool-2"}, deps.OfKind("CephBlockPool"))
|
||||
})
|
||||
|
||||
t.Run("CephRBDMirrors", func(t *testing.T) {
|
||||
@@ -71,8 +69,8 @@ func TestCephClusterDependents(t *testing.T) {
|
||||
deps, err := CephClusterDependents(c, ns)
|
||||
assert.NoError(t, err)
|
||||
assert.False(t, deps.Empty())
|
||||
assert.ElementsMatch(t, []string{"CephRBDMirrors"}, deps.PluralKinds())
|
||||
assert.ElementsMatch(t, []string{"rbdmirror"}, deps.OfPluralKind("CephRBDMirrors"))
|
||||
assert.ElementsMatch(t, []string{"CephRBDMirror"}, deps.PluralKinds())
|
||||
assert.ElementsMatch(t, []string{"rbdmirror"}, deps.OfKind("CephRBDMirror"))
|
||||
})
|
||||
|
||||
t.Run("CephFilesystems", func(t *testing.T) {
|
||||
@@ -84,8 +82,8 @@ func TestCephClusterDependents(t *testing.T) {
|
||||
deps, err := CephClusterDependents(c, ns)
|
||||
assert.NoError(t, err)
|
||||
assert.False(t, deps.Empty())
|
||||
assert.ElementsMatch(t, []string{"CephFilesystems"}, deps.PluralKinds())
|
||||
assert.ElementsMatch(t, []string{"filesystem-1", "filesystem-2", "filesystem-3"}, deps.OfPluralKind("CephFilesystems"))
|
||||
assert.ElementsMatch(t, []string{"CephFilesystem"}, deps.PluralKinds())
|
||||
assert.ElementsMatch(t, []string{"filesystem-1", "filesystem-2", "filesystem-3"}, deps.OfKind("CephFilesystem"))
|
||||
})
|
||||
|
||||
t.Run("CephFilesystemMirrors", func(t *testing.T) {
|
||||
@@ -95,8 +93,8 @@ func TestCephClusterDependents(t *testing.T) {
|
||||
deps, err := CephClusterDependents(c, ns)
|
||||
assert.NoError(t, err)
|
||||
assert.False(t, deps.Empty())
|
||||
assert.ElementsMatch(t, []string{"CephFilesystemMirrors"}, deps.PluralKinds())
|
||||
assert.ElementsMatch(t, []string{"fsmirror"}, deps.OfPluralKind("CephFilesystemMirrors"))
|
||||
assert.ElementsMatch(t, []string{"CephFilesystemMirror"}, deps.PluralKinds())
|
||||
assert.ElementsMatch(t, []string{"fsmirror"}, deps.OfKind("CephFilesystemMirror"))
|
||||
})
|
||||
|
||||
t.Run("CephObjectStores", func(t *testing.T) {
|
||||
@@ -107,8 +105,8 @@ func TestCephClusterDependents(t *testing.T) {
|
||||
deps, err := CephClusterDependents(c, ns)
|
||||
assert.NoError(t, err)
|
||||
assert.False(t, deps.Empty())
|
||||
assert.ElementsMatch(t, []string{"CephObjectStores"}, deps.PluralKinds())
|
||||
assert.ElementsMatch(t, []string{"objectstore-1", "objectstore-2"}, deps.OfPluralKind("CephObjectStores"))
|
||||
assert.ElementsMatch(t, []string{"CephObjectStore"}, deps.PluralKinds())
|
||||
assert.ElementsMatch(t, []string{"objectstore-1", "objectstore-2"}, deps.OfKind("CephObjectStore"))
|
||||
})
|
||||
|
||||
t.Run("CephObjectStoreUsers", func(t *testing.T) {
|
||||
@@ -122,8 +120,8 @@ func TestCephClusterDependents(t *testing.T) {
|
||||
deps, err := CephClusterDependents(c, ns)
|
||||
assert.NoError(t, err)
|
||||
assert.False(t, deps.Empty())
|
||||
assert.ElementsMatch(t, []string{"CephObjectStoreUsers"}, deps.PluralKinds())
|
||||
assert.ElementsMatch(t, []string{"u1", "u2", "u3", "u4", "u5"}, deps.OfPluralKind("CephObjectStoreUsers"))
|
||||
assert.ElementsMatch(t, []string{"CephObjectStoreUser"}, deps.PluralKinds())
|
||||
assert.ElementsMatch(t, []string{"u1", "u2", "u3", "u4", "u5"}, deps.OfKind("CephObjectStoreUser"))
|
||||
})
|
||||
|
||||
t.Run("CephObjectZones", func(t *testing.T) {
|
||||
@@ -134,8 +132,8 @@ func TestCephClusterDependents(t *testing.T) {
|
||||
deps, err := CephClusterDependents(c, ns)
|
||||
assert.NoError(t, err)
|
||||
assert.False(t, deps.Empty())
|
||||
assert.ElementsMatch(t, []string{"CephObjectZones"}, deps.PluralKinds())
|
||||
assert.ElementsMatch(t, []string{"zone-1", "zone-2"}, deps.OfPluralKind("CephObjectZones"))
|
||||
assert.ElementsMatch(t, []string{"CephObjectZone"}, deps.PluralKinds())
|
||||
assert.ElementsMatch(t, []string{"zone-1", "zone-2"}, deps.OfKind("CephObjectZone"))
|
||||
})
|
||||
|
||||
t.Run("CephObjectZoneGroups", func(t *testing.T) {
|
||||
@@ -146,8 +144,8 @@ func TestCephClusterDependents(t *testing.T) {
|
||||
deps, err := CephClusterDependents(c, ns)
|
||||
assert.NoError(t, err)
|
||||
assert.False(t, deps.Empty())
|
||||
assert.ElementsMatch(t, []string{"CephObjectZoneGroups"}, deps.PluralKinds())
|
||||
assert.ElementsMatch(t, []string{"group-1", "group-2"}, deps.OfPluralKind("CephObjectZoneGroups"))
|
||||
assert.ElementsMatch(t, []string{"CephObjectZoneGroup"}, deps.PluralKinds())
|
||||
assert.ElementsMatch(t, []string{"group-1", "group-2"}, deps.OfKind("CephObjectZoneGroup"))
|
||||
})
|
||||
|
||||
t.Run("CephObjectRealms", func(t *testing.T) {
|
||||
@@ -158,8 +156,8 @@ func TestCephClusterDependents(t *testing.T) {
|
||||
deps, err := CephClusterDependents(c, ns)
|
||||
assert.NoError(t, err)
|
||||
assert.False(t, deps.Empty())
|
||||
assert.ElementsMatch(t, []string{"CephObjectRealms"}, deps.PluralKinds())
|
||||
assert.ElementsMatch(t, []string{"realm-1", "realm-2"}, deps.OfPluralKind("CephObjectRealms"))
|
||||
assert.ElementsMatch(t, []string{"CephObjectRealm"}, deps.PluralKinds())
|
||||
assert.ElementsMatch(t, []string{"realm-1", "realm-2"}, deps.OfKind("CephObjectRealm"))
|
||||
})
|
||||
|
||||
t.Run("CephNFSes", func(t *testing.T) {
|
||||
@@ -170,8 +168,8 @@ func TestCephClusterDependents(t *testing.T) {
|
||||
deps, err := CephClusterDependents(c, ns)
|
||||
assert.NoError(t, err)
|
||||
assert.False(t, deps.Empty())
|
||||
assert.ElementsMatch(t, []string{"CephNFSes"}, deps.PluralKinds())
|
||||
assert.ElementsMatch(t, []string{"nfs-1", "nfs-2"}, deps.OfPluralKind("CephNFSes"))
|
||||
assert.ElementsMatch(t, []string{"CephNFS"}, deps.PluralKinds())
|
||||
assert.ElementsMatch(t, []string{"nfs-1", "nfs-2"}, deps.OfKind("CephNFS"))
|
||||
})
|
||||
|
||||
t.Run("CephClients", func(t *testing.T) {
|
||||
@@ -183,8 +181,8 @@ func TestCephClusterDependents(t *testing.T) {
|
||||
deps, err := CephClusterDependents(c, ns)
|
||||
assert.NoError(t, err)
|
||||
assert.False(t, deps.Empty())
|
||||
assert.ElementsMatch(t, []string{"CephClients"}, deps.PluralKinds())
|
||||
assert.ElementsMatch(t, []string{"client-1", "client-2", "client-3"}, deps.OfPluralKind("CephClients"))
|
||||
assert.ElementsMatch(t, []string{"CephClient"}, deps.PluralKinds())
|
||||
assert.ElementsMatch(t, []string{"client-1", "client-2", "client-3"}, deps.OfKind("CephClient"))
|
||||
})
|
||||
|
||||
t.Run("All", func(t *testing.T) {
|
||||
@@ -206,20 +204,20 @@ func TestCephClusterDependents(t *testing.T) {
|
||||
deps, err := CephClusterDependents(c, ns)
|
||||
assert.NoError(t, err)
|
||||
assert.False(t, deps.Empty())
|
||||
assert.ElementsMatch(t, []string{"CephBlockPools", "CephRBDMirrors", "CephFilesystems",
|
||||
"CephFilesystemMirrors", "CephObjectStores", "CephObjectStoreUsers", "CephObjectZones",
|
||||
"CephObjectZoneGroups", "CephObjectRealms", "CephNFSes", "CephClients"}, deps.PluralKinds())
|
||||
assert.ElementsMatch(t, []string{"pool-1"}, deps.OfPluralKind("CephBlockPools"))
|
||||
assert.ElementsMatch(t, []string{"rbdmirror-1", "rbdmirror-2"}, deps.OfPluralKind("CephRBDMirrors"))
|
||||
assert.ElementsMatch(t, []string{"filesystem-1"}, deps.OfPluralKind("CephFilesystems"))
|
||||
assert.ElementsMatch(t, []string{"fsmirror-1", "fsmirror-2"}, deps.OfPluralKind("CephFilesystemMirrors"))
|
||||
assert.ElementsMatch(t, []string{"objectstore-1"}, deps.OfPluralKind("CephObjectStores"))
|
||||
assert.ElementsMatch(t, []string{"u1"}, deps.OfPluralKind("CephObjectStoreUsers"))
|
||||
assert.ElementsMatch(t, []string{"zone-1"}, deps.OfPluralKind("CephObjectZones"))
|
||||
assert.ElementsMatch(t, []string{"group-1"}, deps.OfPluralKind("CephObjectZoneGroups"))
|
||||
assert.ElementsMatch(t, []string{"realm-1"}, deps.OfPluralKind("CephObjectRealms"))
|
||||
assert.ElementsMatch(t, []string{"nfs-1"}, deps.OfPluralKind("CephNFSes"))
|
||||
assert.ElementsMatch(t, []string{"client-1"}, deps.OfPluralKind("CephClients"))
|
||||
assert.ElementsMatch(t, []string{"CephBlockPool", "CephRBDMirror", "CephFilesystem",
|
||||
"CephFilesystemMirror", "CephObjectStore", "CephObjectStoreUser", "CephObjectZone",
|
||||
"CephObjectZoneGroup", "CephObjectRealm", "CephNFS", "CephClient"}, deps.PluralKinds())
|
||||
assert.ElementsMatch(t, []string{"pool-1"}, deps.OfKind("CephBlockPool"))
|
||||
assert.ElementsMatch(t, []string{"rbdmirror-1", "rbdmirror-2"}, deps.OfKind("CephRBDMirror"))
|
||||
assert.ElementsMatch(t, []string{"filesystem-1"}, deps.OfKind("CephFilesystem"))
|
||||
assert.ElementsMatch(t, []string{"fsmirror-1", "fsmirror-2"}, deps.OfKind("CephFilesystemMirror"))
|
||||
assert.ElementsMatch(t, []string{"objectstore-1"}, deps.OfKind("CephObjectStore"))
|
||||
assert.ElementsMatch(t, []string{"u1"}, deps.OfKind("CephObjectStoreUser"))
|
||||
assert.ElementsMatch(t, []string{"zone-1"}, deps.OfKind("CephObjectZone"))
|
||||
assert.ElementsMatch(t, []string{"group-1"}, deps.OfKind("CephObjectZoneGroup"))
|
||||
assert.ElementsMatch(t, []string{"realm-1"}, deps.OfKind("CephObjectRealm"))
|
||||
assert.ElementsMatch(t, []string{"nfs-1"}, deps.OfKind("CephNFS"))
|
||||
assert.ElementsMatch(t, []string{"client-1"}, deps.OfKind("CephClient"))
|
||||
|
||||
t.Run("and no dependencies in another namespace", func(t *testing.T) {
|
||||
deps, err := CephClusterDependents(c, "other-namespace")
|
||||
@@ -228,31 +226,38 @@ func TestCephClusterDependents(t *testing.T) {
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("With errors", func(t *testing.T) {
|
||||
dynInt := dynamicfake.NewSimpleDynamicClient(scheme,
|
||||
&cephv1.CephBlockPool{ObjectMeta: meta("pool-1")},
|
||||
&cephv1.CephFilesystem{ObjectMeta: meta("filesystem-1")},
|
||||
&cephv1.CephObjectStore{ObjectMeta: meta("objectstore-1")},
|
||||
)
|
||||
// add reactor to cause failures when listing block and nfs (but not object, fs, or any others)
|
||||
var listReactor k8stesting.ReactionFunc = func(action k8stesting.Action) (handled bool, ret runtime.Object, err error) {
|
||||
r := action.GetResource().Resource
|
||||
if r == "cephblockpools" || r == "cephnfses" {
|
||||
return true, nil, errors.Errorf("fake error listing %q", r)
|
||||
}
|
||||
return false, nil, nil
|
||||
}
|
||||
dynInt.PrependReactor("list", "*", listReactor)
|
||||
c := &clusterd.Context{
|
||||
DynamicClientset: dynInt,
|
||||
}
|
||||
deps, err := CephClusterDependents(c, ns)
|
||||
assert.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "CephBlockPools")
|
||||
assert.Contains(t, err.Error(), "CephNFSes")
|
||||
assert.False(t, deps.Empty())
|
||||
assert.ElementsMatch(t, []string{"CephFilesystems", "CephObjectStores"}, deps.PluralKinds())
|
||||
assert.ElementsMatch(t, []string{"filesystem-1"}, deps.OfPluralKind("CephFilesystems"))
|
||||
assert.ElementsMatch(t, []string{"objectstore-1"}, deps.OfPluralKind("CephObjectStores"))
|
||||
})
|
||||
// TODO: how do we get this to return errors without a reactor? Do we need to add reactors
|
||||
// to controller-runtime?
|
||||
// Keep the below test commented-out until we can check off the above TODO. For now we will have
|
||||
// to assume the errors are handled properly.
|
||||
// t.Run("With errors", func(t *testing.T) {
|
||||
// // // add reactor to cause failures when listing block and nfs (but not object, fs, or any others)
|
||||
// // var listReactor k8stesting.ReactionFunc = func(action k8stesting.Action) (handled bool, ret runtime.Object, err error) {
|
||||
// // r := action.GetResource().Resource
|
||||
// // if r == "cephblockpools" || r == "cephnfses" {
|
||||
// // return true, nil, errors.Errorf("fake error listing %q", r)
|
||||
// // }
|
||||
// // return false, nil, nil
|
||||
// // }
|
||||
// // dynInt.PrependReactor("list", "*", listReactor)
|
||||
|
||||
// client := fakeclient.NewClientBuilder().WithScheme(scheme).WithObjects(
|
||||
// &cephv1.CephBlockPool{ObjectMeta: meta("pool-1")},
|
||||
// &cephv1.CephFilesystem{ObjectMeta: meta("filesystem-1")},
|
||||
// &cephv1.CephObjectStore{ObjectMeta: meta("objectstore-1")},
|
||||
// ).Build()
|
||||
|
||||
// c := &clusterd.Context{
|
||||
// Client: client,
|
||||
// }
|
||||
|
||||
// deps, err := CephClusterDependents(c, ns)
|
||||
// assert.Error(t, err)
|
||||
// assert.Contains(t, err.Error(), "CephBlockPool")
|
||||
// assert.Contains(t, err.Error(), "CephNFS")
|
||||
// assert.False(t, deps.Empty())
|
||||
// assert.ElementsMatch(t, []string{"CephFilesystem", "CephObjectStore"}, deps.PluralKinds())
|
||||
// assert.ElementsMatch(t, []string{"filesystem-1"}, deps.OfKind("CephFilesystem"))
|
||||
// assert.ElementsMatch(t, []string{"objectstore-1"}, deps.OfKind("CephObjectStore"))
|
||||
// })
|
||||
}
|
||||
@@ -37,7 +37,6 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
dynamicfake "k8s.io/client-go/dynamic/fake"
|
||||
)
|
||||
|
||||
func TestCephObjectStoreDependents(t *testing.T) {
|
||||
@@ -62,9 +61,8 @@ func TestCephObjectStoreDependents(t *testing.T) {
|
||||
|
||||
newClusterdCtx := func(executor exec.Executor, objects ...runtime.Object) *clusterd.Context {
|
||||
return &clusterd.Context{
|
||||
DynamicClientset: dynamicfake.NewSimpleDynamicClient(scheme, objects...),
|
||||
RookClientset: rookclient.NewSimpleClientset(),
|
||||
Executor: executor,
|
||||
RookClientset: rookclient.NewSimpleClientset(),
|
||||
Executor: executor,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -208,7 +206,7 @@ func TestCephObjectStoreDependents(t *testing.T) {
|
||||
deps, err := CephObjectStoreDependents(c, clusterInfo, store, NewContext(c, clusterInfo, store.Name), &AdminOpsContext{AdminOpsClient: client})
|
||||
assert.NoError(t, err)
|
||||
assert.False(t, deps.Empty())
|
||||
assert.ElementsMatch(t, []string{"u1"}, deps.OfPluralKind("CephObjectStoreUsers"))
|
||||
assert.ElementsMatch(t, []string{"u1"}, deps.OfKind("CephObjectStoreUsers"))
|
||||
})
|
||||
|
||||
t.Run("no objectstore users and buckets", func(t *testing.T) {
|
||||
@@ -237,6 +235,6 @@ func TestCephObjectStoreDependents(t *testing.T) {
|
||||
deps, err := CephObjectStoreDependents(c, clusterInfo, store, NewContext(c, clusterInfo, store.Name), &AdminOpsContext{AdminOpsClient: client})
|
||||
assert.NoError(t, err)
|
||||
assert.False(t, deps.Empty())
|
||||
assert.ElementsMatch(t, []string{"my-bucket"}, deps.OfPluralKind("buckets in the object store (could be from ObjectBucketClaims or COSI Buckets)"), deps)
|
||||
assert.ElementsMatch(t, []string{"my-bucket"}, deps.OfKind("buckets in the object store (could be from ObjectBucketClaims or COSI Buckets)"), deps)
|
||||
})
|
||||
}
|
||||
@@ -77,9 +77,9 @@ func (d *DependentList) PluralKinds() []string {
|
||||
return kinds
|
||||
}
|
||||
|
||||
// OfPluralKind returns the names of dependents of the Kind (plural), or an empty list if no
|
||||
// OfKind returns the names of dependents of the Kind (plural), or an empty list if no
|
||||
// dependents exist.
|
||||
func (d *DependentList) OfPluralKind(pluralKind string) []string {
|
||||
func (d *DependentList) OfKind(pluralKind string) []string {
|
||||
names, ok := d.d[pluralKind]
|
||||
if !ok {
|
||||
return []string{}
|
||||
|
||||
@@ -41,7 +41,7 @@ func TestDependentList(t *testing.T) {
|
||||
d.Add("MyResources", "my-resource-1")
|
||||
assert.False(t, d.Empty())
|
||||
assert.ElementsMatch(t, []string{"MyResources"}, d.PluralKinds())
|
||||
assert.ElementsMatch(t, []string{"my-resource-1"}, d.OfPluralKind("MyResources"))
|
||||
assert.ElementsMatch(t, []string{"my-resource-1"}, d.OfKind("MyResources"))
|
||||
toString := d.StringWithHeader("header")
|
||||
containsExactlyOne(toString, "header:")
|
||||
containsExactlyOne(toString, "MyResources")
|
||||
@@ -55,8 +55,8 @@ func TestDependentList(t *testing.T) {
|
||||
d.Add("MyResources", "my-resource-3")
|
||||
assert.False(t, d.Empty())
|
||||
assert.ElementsMatch(t, []string{"MyResources"}, d.PluralKinds())
|
||||
assert.ElementsMatch(t, []string{"my-resource-1", "my-resource-2", "my-resource-3"}, d.OfPluralKind("MyResources"))
|
||||
assert.ElementsMatch(t, []string{}, d.OfPluralKind("OtherKinds"))
|
||||
assert.ElementsMatch(t, []string{"my-resource-1", "my-resource-2", "my-resource-3"}, d.OfKind("MyResources"))
|
||||
assert.ElementsMatch(t, []string{}, d.OfKind("OtherKinds"))
|
||||
toString := d.StringWithHeader("head with arg %d", 1)
|
||||
containsExactlyOne(toString, "head with arg 1:")
|
||||
containsExactlyOne(toString, "MyResources")
|
||||
@@ -75,10 +75,10 @@ func TestDependentList(t *testing.T) {
|
||||
d.Add("TheirResources", "their-resource-6")
|
||||
assert.False(t, d.Empty())
|
||||
assert.ElementsMatch(t, []string{"MyResources", "YourResources", "TheirResources"}, d.PluralKinds())
|
||||
assert.ElementsMatch(t, []string{"my-resource-2", "my-resource-4"}, d.OfPluralKind("MyResources"))
|
||||
assert.ElementsMatch(t, []string{"your-resource-1"}, d.OfPluralKind("YourResources"))
|
||||
assert.ElementsMatch(t, []string{"their-resource-5", "their-resource-6"}, d.OfPluralKind("TheirResources"))
|
||||
assert.ElementsMatch(t, []string{}, d.OfPluralKind("OtherKinds"))
|
||||
assert.ElementsMatch(t, []string{"my-resource-2", "my-resource-4"}, d.OfKind("MyResources"))
|
||||
assert.ElementsMatch(t, []string{"your-resource-1"}, d.OfKind("YourResources"))
|
||||
assert.ElementsMatch(t, []string{"their-resource-5", "their-resource-6"}, d.OfKind("TheirResources"))
|
||||
assert.ElementsMatch(t, []string{}, d.OfKind("OtherKinds"))
|
||||
toString := d.StringWithHeader("head with arg %s", "mom")
|
||||
t.Log(toString)
|
||||
containsExactlyOne(toString, "head with arg mom:")
|
||||
|
||||
Reference in new issue
Block a user