2017-03-09 15:24:33 -08:00
|
|
|
/*
|
|
|
|
|
Copyright 2016 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.
|
|
|
|
|
*/
|
2018-08-31 13:39:02 -06:00
|
|
|
|
2017-03-21 10:26:51 -07:00
|
|
|
package cluster
|
2017-03-09 15:24:33 -08:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2017-10-25 12:23:15 -07:00
|
|
|
"os"
|
2017-03-09 15:24:33 -08:00
|
|
|
"testing"
|
2017-10-25 12:23:15 -07:00
|
|
|
"time"
|
2017-03-09 15:24:33 -08:00
|
|
|
|
2018-12-03 15:08:29 -07:00
|
|
|
cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"
|
2018-05-18 14:32:20 -07:00
|
|
|
rookalpha "github.com/rook/rook/pkg/apis/rook.io/v1alpha2"
|
2018-07-06 18:47:05 -07:00
|
|
|
rookfake "github.com/rook/rook/pkg/client/clientset/versioned/fake"
|
2017-06-02 13:44:54 -07:00
|
|
|
"github.com/rook/rook/pkg/clusterd"
|
2018-05-18 14:32:20 -07:00
|
|
|
"github.com/rook/rook/pkg/daemon/ceph/agent/flexvolume/attachment"
|
2018-10-15 11:21:21 -06:00
|
|
|
"github.com/rook/rook/pkg/operator/ceph/cluster/mon"
|
2017-10-25 12:23:15 -07:00
|
|
|
"github.com/rook/rook/pkg/operator/k8sutil"
|
2017-03-09 15:24:33 -08:00
|
|
|
testop "github.com/rook/rook/pkg/operator/test"
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2017-04-07 17:28:17 -07:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2017-03-09 15:24:33 -08:00
|
|
|
)
|
|
|
|
|
|
2019-09-19 15:53:15 +02:00
|
|
|
func TestClusterDeleteFlexEnabled(t *testing.T) {
|
2017-10-25 12:23:15 -07:00
|
|
|
nodeName := "node841"
|
|
|
|
|
clusterName := "cluster684"
|
|
|
|
|
pvName := "pvc-540"
|
|
|
|
|
rookSystemNamespace := "rook-system-6413"
|
|
|
|
|
|
2019-09-19 15:53:15 +02:00
|
|
|
os.Setenv("ROOK_ENABLE_FLEX_DRIVER", "true")
|
2017-10-25 12:23:15 -07:00
|
|
|
os.Setenv(k8sutil.PodNamespaceEnvVar, rookSystemNamespace)
|
2019-09-19 15:53:15 +02:00
|
|
|
defer os.Unsetenv("ROOK_ENABLE_FLEX_DRIVER")
|
2017-10-25 12:23:15 -07:00
|
|
|
defer os.Unsetenv(k8sutil.PodNamespaceEnvVar)
|
|
|
|
|
|
|
|
|
|
clientset := testop.New(3)
|
|
|
|
|
context := &clusterd.Context{
|
|
|
|
|
Clientset: clientset,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
listCount := 0
|
2017-12-04 09:03:06 -08:00
|
|
|
volumeAttachmentController := &attachment.MockAttachment{
|
2018-05-18 14:32:20 -07:00
|
|
|
MockList: func(namespace string) (*rookalpha.VolumeList, error) {
|
2017-10-25 12:23:15 -07:00
|
|
|
listCount++
|
|
|
|
|
if listCount == 1 {
|
|
|
|
|
// first listing returns an existing volume attachment, so the controller should wait
|
2018-05-18 14:32:20 -07:00
|
|
|
return &rookalpha.VolumeList{
|
|
|
|
|
Items: []rookalpha.Volume{
|
2017-10-25 12:23:15 -07:00
|
|
|
{
|
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
|
Name: pvName,
|
|
|
|
|
Namespace: rookSystemNamespace,
|
|
|
|
|
},
|
2017-12-04 08:33:19 -08:00
|
|
|
Attachments: []rookalpha.Attachment{
|
2017-10-25 12:23:15 -07:00
|
|
|
{
|
|
|
|
|
Node: nodeName,
|
|
|
|
|
ClusterName: clusterName,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
2018-03-16 08:30:54 -07:00
|
|
|
|
|
|
|
|
// subsequent listings should return no volume attachments, meaning that they have all
|
|
|
|
|
// been cleaned up and the controller can move on.
|
2018-05-18 14:32:20 -07:00
|
|
|
return &rookalpha.VolumeList{Items: []rookalpha.Volume{}}, nil
|
2018-03-16 08:30:54 -07:00
|
|
|
|
2017-10-25 12:23:15 -07:00
|
|
|
},
|
|
|
|
|
}
|
2019-08-28 16:34:13 +05:30
|
|
|
callbacks := []func(clusterSpec *cephv1.ClusterSpec) error{
|
|
|
|
|
func(clusterSpec *cephv1.ClusterSpec) error {
|
|
|
|
|
logger.Infof("test success callback")
|
|
|
|
|
return nil
|
|
|
|
|
},
|
2019-06-17 15:44:33 -06:00
|
|
|
}
|
2017-10-25 12:23:15 -07:00
|
|
|
|
|
|
|
|
// create the cluster controller and tell it that the cluster has been deleted
|
2019-08-28 16:34:13 +05:30
|
|
|
controller := NewClusterController(context, "", volumeAttachmentController, callbacks)
|
2018-12-03 23:48:40 -07:00
|
|
|
clusterToDelete := &cephv1.CephCluster{ObjectMeta: metav1.ObjectMeta{Namespace: clusterName}}
|
2017-10-25 12:23:15 -07:00
|
|
|
controller.handleDelete(clusterToDelete, time.Microsecond)
|
|
|
|
|
|
|
|
|
|
// listing of volume attachments should have been called twice. the first time there were volume attachments
|
|
|
|
|
// that the controller needed to wait on to be cleaned up and the second time they were all cleaned up.
|
|
|
|
|
assert.Equal(t, 2, listCount)
|
|
|
|
|
}
|
2017-12-21 19:41:50 -08:00
|
|
|
|
2019-09-19 15:53:15 +02:00
|
|
|
func TestClusterDeleteFlexDisabled(t *testing.T) {
|
|
|
|
|
clusterName := "cluster684"
|
|
|
|
|
rookSystemNamespace := "rook-system-6413"
|
|
|
|
|
|
|
|
|
|
os.Setenv("ROOK_ENABLE_FLEX_DRIVER", "false")
|
|
|
|
|
os.Setenv(k8sutil.PodNamespaceEnvVar, rookSystemNamespace)
|
|
|
|
|
defer os.Unsetenv("ROOK_ENABLE_FLEX_DRIVER")
|
|
|
|
|
defer os.Unsetenv(k8sutil.PodNamespaceEnvVar)
|
|
|
|
|
|
|
|
|
|
clientset := testop.New(3)
|
|
|
|
|
context := &clusterd.Context{
|
|
|
|
|
Clientset: clientset,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
listCount := 0
|
|
|
|
|
volumeAttachmentController := &attachment.MockAttachment{
|
|
|
|
|
MockList: func(namespace string) (*rookalpha.VolumeList, error) {
|
|
|
|
|
listCount++
|
|
|
|
|
return &rookalpha.VolumeList{Items: []rookalpha.Volume{}}, nil
|
|
|
|
|
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
callbacks := []func(clusterSpec *cephv1.ClusterSpec) error{
|
|
|
|
|
func(clusterSpec *cephv1.ClusterSpec) error {
|
|
|
|
|
logger.Infof("test success callback")
|
|
|
|
|
os.Setenv("ROOK_ENABLE_FLEX_DRIVER", "true")
|
|
|
|
|
os.Setenv(k8sutil.PodNamespaceEnvVar, rookSystemNamespace)
|
|
|
|
|
defer os.Unsetenv("ROOK_ENABLE_FLEX_DRIVER")
|
|
|
|
|
return nil
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// create the cluster controller and tell it that the cluster has been deleted
|
|
|
|
|
controller := NewClusterController(context, "", volumeAttachmentController, callbacks)
|
|
|
|
|
clusterToDelete := &cephv1.CephCluster{ObjectMeta: metav1.ObjectMeta{Namespace: clusterName}}
|
|
|
|
|
controller.handleDelete(clusterToDelete, time.Microsecond)
|
|
|
|
|
|
|
|
|
|
// Ensure that the listing of volume attachments was never called.
|
|
|
|
|
assert.Equal(t, 0, listCount)
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-21 19:41:50 -08:00
|
|
|
func TestClusterChanged(t *testing.T) {
|
|
|
|
|
// a new node added, should be a change
|
2018-12-03 15:08:29 -07:00
|
|
|
old := cephv1.ClusterSpec{
|
2018-05-18 14:32:20 -07:00
|
|
|
Storage: rookalpha.StorageScopeSpec{
|
2017-12-21 19:41:50 -08:00
|
|
|
Nodes: []rookalpha.Node{
|
2018-05-18 14:32:20 -07:00
|
|
|
{Name: "node1", Selection: rookalpha.Selection{Devices: []rookalpha.Device{{Name: "sda"}}}},
|
2017-12-21 19:41:50 -08:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
2018-12-03 15:08:29 -07:00
|
|
|
new := cephv1.ClusterSpec{
|
2018-05-18 14:32:20 -07:00
|
|
|
Storage: rookalpha.StorageScopeSpec{
|
2017-12-21 19:41:50 -08:00
|
|
|
Nodes: []rookalpha.Node{
|
2018-05-18 14:32:20 -07:00
|
|
|
{Name: "node1", Selection: rookalpha.Selection{Devices: []rookalpha.Device{{Name: "sda"}}}},
|
|
|
|
|
{Name: "node2", Selection: rookalpha.Selection{Devices: []rookalpha.Device{{Name: "sda"}}}},
|
2017-12-21 19:41:50 -08:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
2018-12-03 15:08:29 -07:00
|
|
|
c := &cluster{Spec: &cephv1.ClusterSpec{}, mons: &mon.Cluster{}}
|
2019-03-13 12:37:52 -06:00
|
|
|
changed, diff := clusterChanged(old, new, c)
|
|
|
|
|
assert.True(t, changed)
|
2019-08-28 16:34:13 +05:30
|
|
|
assert.NotEqual(t, diff, "")
|
2018-10-15 11:21:21 -06:00
|
|
|
assert.Equal(t, 0, c.Spec.Mon.Count)
|
2017-12-21 19:41:50 -08:00
|
|
|
|
|
|
|
|
// a node was removed, should be a change
|
|
|
|
|
old.Storage.Nodes = []rookalpha.Node{
|
2018-05-18 14:32:20 -07:00
|
|
|
{Name: "node1", Selection: rookalpha.Selection{Devices: []rookalpha.Device{{Name: "sda"}}}},
|
|
|
|
|
{Name: "node2", Selection: rookalpha.Selection{Devices: []rookalpha.Device{{Name: "sda"}}}},
|
2017-12-21 19:41:50 -08:00
|
|
|
}
|
|
|
|
|
new.Storage.Nodes = []rookalpha.Node{
|
2018-05-18 14:32:20 -07:00
|
|
|
{Name: "node1", Selection: rookalpha.Selection{Devices: []rookalpha.Device{{Name: "sda"}}}},
|
2017-12-21 19:41:50 -08:00
|
|
|
}
|
2019-03-13 12:37:52 -06:00
|
|
|
changed, diff = clusterChanged(old, new, c)
|
|
|
|
|
assert.True(t, changed)
|
2019-08-28 16:34:13 +05:30
|
|
|
assert.NotEqual(t, diff, "")
|
2017-12-21 19:41:50 -08:00
|
|
|
|
|
|
|
|
// the nodes being in a different order should not be a change
|
|
|
|
|
old.Storage.Nodes = []rookalpha.Node{
|
2018-05-18 14:32:20 -07:00
|
|
|
{Name: "node1", Selection: rookalpha.Selection{Devices: []rookalpha.Device{{Name: "sda"}}}},
|
|
|
|
|
{Name: "node2", Selection: rookalpha.Selection{Devices: []rookalpha.Device{{Name: "sda"}}}},
|
2017-12-21 19:41:50 -08:00
|
|
|
}
|
|
|
|
|
new.Storage.Nodes = []rookalpha.Node{
|
2018-05-18 14:32:20 -07:00
|
|
|
{Name: "node2", Selection: rookalpha.Selection{Devices: []rookalpha.Device{{Name: "sda"}}}},
|
|
|
|
|
{Name: "node1", Selection: rookalpha.Selection{Devices: []rookalpha.Device{{Name: "sda"}}}},
|
2017-12-21 19:41:50 -08:00
|
|
|
}
|
2019-03-13 12:37:52 -06:00
|
|
|
changed, diff = clusterChanged(old, new, c)
|
|
|
|
|
assert.False(t, changed)
|
2018-10-15 11:21:21 -06:00
|
|
|
assert.Equal(t, 0, c.Spec.Mon.Count)
|
2019-03-13 12:37:52 -06:00
|
|
|
assert.Equal(t, "", diff)
|
2018-10-15 11:21:21 -06:00
|
|
|
|
2019-03-06 21:22:25 -07:00
|
|
|
// If the number of mons changes, the cluster would be updated
|
2018-10-15 11:21:21 -06:00
|
|
|
new.Mon.Count = 3
|
|
|
|
|
new.Mon.AllowMultiplePerNode = true
|
2019-03-13 12:37:52 -06:00
|
|
|
changed, diff = clusterChanged(old, new, c)
|
|
|
|
|
assert.True(t, changed)
|
2019-08-28 16:34:13 +05:30
|
|
|
assert.NotEqual(t, diff, "")
|
2017-12-21 19:41:50 -08:00
|
|
|
}
|
2018-07-06 18:47:05 -07:00
|
|
|
|
|
|
|
|
func TestRemoveFinalizer(t *testing.T) {
|
|
|
|
|
clientset := testop.New(3)
|
|
|
|
|
context := &clusterd.Context{
|
|
|
|
|
Clientset: clientset,
|
|
|
|
|
RookClientset: rookfake.NewSimpleClientset(),
|
|
|
|
|
}
|
2019-08-28 16:34:13 +05:30
|
|
|
callbacks := []func(clusterSpec *cephv1.ClusterSpec) error{
|
|
|
|
|
func(clusterSpec *cephv1.ClusterSpec) error {
|
|
|
|
|
logger.Infof("test success callback")
|
|
|
|
|
return fmt.Errorf("test failed callback")
|
|
|
|
|
},
|
2019-06-17 15:44:33 -06:00
|
|
|
}
|
2019-08-28 16:34:13 +05:30
|
|
|
|
|
|
|
|
controller := NewClusterController(context, "", &attachment.MockAttachment{}, callbacks)
|
2018-07-06 18:47:05 -07:00
|
|
|
|
|
|
|
|
// *****************************************
|
|
|
|
|
// start with a current version ceph cluster
|
|
|
|
|
// *****************************************
|
2018-12-03 23:48:40 -07:00
|
|
|
cluster := &cephv1.CephCluster{
|
2018-07-06 18:47:05 -07:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
|
Name: "cluster-1893",
|
|
|
|
|
Namespace: "namespace-6551",
|
|
|
|
|
Finalizers: []string{finalizerName},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// create the cluster initially so it exists in the k8s api
|
2018-12-03 23:48:40 -07:00
|
|
|
cluster, err := context.RookClientset.CephV1().CephClusters(cluster.Namespace).Create(cluster)
|
2018-07-06 18:47:05 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
assert.Len(t, cluster.Finalizers, 1)
|
|
|
|
|
|
|
|
|
|
// remove the finalizer from the cluster object
|
|
|
|
|
controller.removeFinalizer(cluster)
|
|
|
|
|
|
2018-07-26 10:52:19 -06:00
|
|
|
// verify the finalizer was removed
|
2018-12-03 23:48:40 -07:00
|
|
|
cluster, err = context.RookClientset.CephV1().CephClusters(cluster.Namespace).Get(cluster.Name, metav1.GetOptions{})
|
2018-07-06 18:47:05 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
assert.NotNil(t, cluster)
|
|
|
|
|
assert.Len(t, cluster.Finalizers, 0)
|
|
|
|
|
}
|
2019-07-23 22:31:17 +02:00
|
|
|
|
|
|
|
|
func TestValidateExternalClusterSpec(t *testing.T) {
|
|
|
|
|
c := &cluster{Spec: &cephv1.ClusterSpec{}, mons: &mon.Cluster{}}
|
|
|
|
|
err := validateExternalClusterSpec(c)
|
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
|
|
c.Spec.DataDirHostPath = "path"
|
|
|
|
|
err = validateExternalClusterSpec(c)
|
2019-10-02 10:45:21 +02:00
|
|
|
assert.NoError(t, err, err)
|
2019-07-23 22:31:17 +02:00
|
|
|
|
2019-09-17 15:08:15 +02:00
|
|
|
c.Spec.CephVersion.Image = "ceph/ceph:v14.2.4-20190917"
|
2019-07-23 22:31:17 +02:00
|
|
|
err = validateExternalClusterSpec(c)
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
}
|