ceph: convert the CephCluster controller to the controller-runtime

This is the final conversion to controller-runtime conversion. This time the
CephCluster CRD has been converted to use the controller-runtime
library.
The controller incorporates all the previous watchers too, so the Node
and hot-plug configmap are been watched too.
Only the operator setting configmap is not being watcher since it's not
related to the CephCluster CRD.
Not only the patch converts to controller-runtime but also tries to
re-organize the tree of the repo to actually make the code more
readable and have better functions/methods/tests separations.

Closes: https://github.com/rook/rook/issues/4939
Signed-off-by: Sébastien Han <seb@redhat.com>
This commit is contained in:
Sébastien Han committed 2020-04-28 09:40:35 +02:00
1 parent 008670bc79
commit f27fd207ce
55 files changed
+2635 -1848

No files matched your search

@@ -21,14 +21,10 @@ import (
"testing"
"time"
"github.com/pkg/errors"
cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"
rookv1 "github.com/rook/rook/pkg/apis/rook.io/v1"
rookalpha "github.com/rook/rook/pkg/apis/rook.io/v1alpha2"
rookfake "github.com/rook/rook/pkg/client/clientset/versioned/fake"
"github.com/rook/rook/pkg/clusterd"
"github.com/rook/rook/pkg/daemon/ceph/agent/flexvolume/attachment"
"github.com/rook/rook/pkg/operator/ceph/cluster/mon"
"github.com/rook/rook/pkg/operator/k8sutil"
testop "github.com/rook/rook/pkg/operator/test"
"github.com/stretchr/testify/assert"
@@ -146,121 +142,3 @@ func TestClusterDeleteFlexDisabled(t *testing.T) {
// Ensure that the listing of volume attachments was never called.
assert.Equal(t, 0, listCount)
}
func TestClusterChanged(t *testing.T) {
// a new node added, should be a change
old := cephv1.ClusterSpec{
Storage: rookv1.StorageScopeSpec{
Nodes: []rookv1.Node{
{Name: "node1", Selection: rookv1.Selection{Devices: []rookv1.Device{{Name: "sda"}}}},
},
},
}
new := cephv1.ClusterSpec{
Storage: rookv1.StorageScopeSpec{
Nodes: []rookv1.Node{
{Name: "node1", Selection: rookv1.Selection{Devices: []rookv1.Device{{Name: "sda"}}}},
{Name: "node2", Selection: rookv1.Selection{Devices: []rookv1.Device{{Name: "sda"}}}},
},
},
}
c := &cluster{Spec: &cephv1.ClusterSpec{}, mons: &mon.Cluster{}}
changed, diff := clusterChanged(old, new, c)
assert.True(t, changed)
assert.NotEqual(t, diff, "")
assert.Equal(t, 0, c.Spec.Mon.Count)
// a node was removed, should be a change
old.Storage.Nodes = []rookv1.Node{
{Name: "node1", Selection: rookv1.Selection{Devices: []rookv1.Device{{Name: "sda"}}}},
{Name: "node2", Selection: rookv1.Selection{Devices: []rookv1.Device{{Name: "sda"}}}},
}
new.Storage.Nodes = []rookv1.Node{
{Name: "node1", Selection: rookv1.Selection{Devices: []rookv1.Device{{Name: "sda"}}}},
}
changed, diff = clusterChanged(old, new, c)
assert.True(t, changed)
assert.NotEqual(t, diff, "")
// the nodes being in a different order should not be a change
old.Storage.Nodes = []rookv1.Node{
{Name: "node1", Selection: rookv1.Selection{Devices: []rookv1.Device{{Name: "sda"}}}},
{Name: "node2", Selection: rookv1.Selection{Devices: []rookv1.Device{{Name: "sda"}}}},
}
new.Storage.Nodes = []rookv1.Node{
{Name: "node2", Selection: rookv1.Selection{Devices: []rookv1.Device{{Name: "sda"}}}},
{Name: "node1", Selection: rookv1.Selection{Devices: []rookv1.Device{{Name: "sda"}}}},
}
changed, diff = clusterChanged(old, new, c)
assert.False(t, changed)
assert.Equal(t, 0, c.Spec.Mon.Count)
assert.Equal(t, "", diff)
// If the number of mons changes, the cluster would be updated
new.Mon.Count = 3
new.Mon.AllowMultiplePerNode = true
changed, diff = clusterChanged(old, new, c)
assert.True(t, changed)
assert.NotEqual(t, diff, "")
}
func TestRemoveFinalizer(t *testing.T) {
clientset := testop.New(t, 3)
context := &clusterd.Context{
Clientset: clientset,
RookClientset: rookfake.NewSimpleClientset(),
}
operatorConfigCallbacks := []func() error{
func() error {
logger.Infof("test success callback")
return nil
},
}
addCallbacks := []func() error{
func() error {
logger.Infof("test success callback")
return errors.New("test failed callback")
},
}
controller := NewClusterController(context, "", &attachment.MockAttachment{}, operatorConfigCallbacks, addCallbacks)
// *****************************************
// start with a current version ceph cluster
// *****************************************
cluster := &cephv1.CephCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "cluster-1893",
Namespace: "namespace-6551",
Finalizers: []string{finalizerName},
},
}
// create the cluster initially so it exists in the k8s api
cluster, err := context.RookClientset.CephV1().CephClusters(cluster.Namespace).Create(cluster)
assert.NoError(t, err)
assert.Len(t, cluster.Finalizers, 1)
// remove the finalizer from the cluster object
controller.removeFinalizer(cluster)
// verify the finalizer was removed
cluster, err = context.RookClientset.CephV1().CephClusters(cluster.Namespace).Get(cluster.Name, metav1.GetOptions{})
assert.NoError(t, err)
assert.NotNil(t, cluster)
assert.Len(t, cluster.Finalizers, 0)
}
func TestValidateExternalClusterSpec(t *testing.T) {
c := &cluster{Spec: &cephv1.ClusterSpec{}, mons: &mon.Cluster{}}
err := validateExternalClusterSpec(c)
assert.NoError(t, err)
c.Spec.CephVersion.Image = "ceph/ceph:v14.2.9"
err = validateExternalClusterSpec(c)
assert.Error(t, err)
c.Spec.DataDirHostPath = "path"
err = validateExternalClusterSpec(c)
assert.NoError(t, err, err)
}