rgw: add events to CephBucketNotification CR

the events are used to mark the start/success/failure of the reconciliation
and also to log the bucket names on which the CephbucketNotification is
created

Signed-off-by: Yuval Lifshitz <ylifshit@redhat.com>
(cherry picked from commit 11727b1673)
This commit is contained in:
Yuval Lifshitz authored and mergify-bot committed 2022-01-04 19:59:17 +00:00
1 parent f5bcc0144b
commit e13d8c17fc
5 files changed
+221 -331

No files matched your search

+2
View File
@@ -416,6 +416,8 @@ const (
ReconcileSucceeded ConditionReason = "ReconcileSucceeded"
// ReconcileFailed represents when a resource reconciliation failed.
ReconcileFailed ConditionReason = "ReconcileFailed"
// ReconcileStarted represents when a resource reconciliation started.
ReconcileStarted ConditionReason = "ReconcileStarted"
// DeletingReason represents when Rook has detected a resource object should be deleted.
DeletingReason ConditionReason = "Deleting"
@@ -32,8 +32,12 @@ import (
"github.com/rook/rook/pkg/operator/ceph/object"
"github.com/rook/rook/pkg/operator/ceph/object/bucket"
"github.com/rook/rook/pkg/operator/ceph/object/topic"
"github.com/rook/rook/pkg/operator/ceph/reporting"
kapiv1 "k8s.io/api/core/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/handler"
@@ -59,6 +63,7 @@ type ReconcileNotifications struct {
client client.Client
context *clusterd.Context
opManagerContext context.Context
recorder record.EventRecorder
}
// Add creates a new CephBucketNotification controller and a new ObjectBucketClaim Controller and adds it to the Manager.
@@ -68,6 +73,7 @@ func Add(mgr manager.Manager, context *clusterd.Context, opManagerContext contex
client: mgr.GetClient(),
context: context,
opManagerContext: opManagerContext,
recorder: mgr.GetEventRecorderFor(controllerName),
}); err != nil {
return err
}
@@ -76,6 +82,7 @@ func Add(mgr manager.Manager, context *clusterd.Context, opManagerContext contex
client: mgr.GetClient(),
context: context,
opManagerContext: opManagerContext,
recorder: mgr.GetEventRecorderFor(controllerName),
})
}
@@ -100,86 +107,84 @@ func addNotificationReconciler(mgr manager.Manager, r reconcile.Reconciler) erro
// The Controller will requeue the Request to be processed again if the returned error is non-nil or
// Result.Requeue is true, otherwise upon completion it will remove the work from the queue.
func (r *ReconcileNotifications) Reconcile(context context.Context, request reconcile.Request) (reconcile.Result, error) {
reconcileResponse, err := r.reconcile(request)
reconcileResponse, notification, err := r.reconcile(request)
if err != nil {
logger.Errorf("failed to reconcile %v", err)
}
return reconcileResponse, err
return reporting.ReportReconcileResult(logger, r.recorder, notification, reconcileResponse, err)
}
func (r *ReconcileNotifications) reconcile(request reconcile.Request) (reconcile.Result, error) {
func (r *ReconcileNotifications) reconcile(request reconcile.Request) (reconcile.Result, *cephv1.CephBucketNotification, error) {
// fetch the CephBucketNotification instance
notification := &cephv1.CephBucketNotification{}
notification := &cephv1.CephBucketNotification{ObjectMeta: metav1.ObjectMeta{Name: request.Name, Namespace: request.Namespace}}
bnName := request.NamespacedName
r.recorder.Eventf(notification, kapiv1.EventTypeNormal, string(cephv1.ReconcileStarted), "Started reconciling CephBucketNotification %q", bnName)
err := r.client.Get(r.opManagerContext, request.NamespacedName, notification)
if err != nil {
if kerrors.IsNotFound(err) {
logger.Debugf("CephBucketNotification %q resource not found. Ignoring since resource must be deleted.", request.NamespacedName)
return reconcile.Result{}, nil
logger.Debugf("CephBucketNotification %q resource not found. Ignoring since resource must be deleted.", bnName)
return reconcile.Result{}, notification, nil
}
// Error reading the object - requeue the request.
return reconcile.Result{}, errors.Wrapf(err, "failed to retrieve CephBucketNotification %q", request.NamespacedName)
return reconcile.Result{}, notification, errors.Wrapf(err, "failed to retrieve CephBucketNotification %q", bnName)
}
// DELETE: the CR was deleted
if !notification.GetDeletionTimestamp().IsZero() {
logger.Debugf("CephBucketNotification %q was deleted", notification.Name)
logger.Debugf("CephBucketNotification %q was deleted", bnName)
// Return and do not requeue. Successful deletion.
return reconcile.Result{}, nil
return reconcile.Result{}, notification, nil
}
// get the topic associated with the notification, and make sure it is provisioned
topicName := types.NamespacedName{Namespace: notification.Namespace, Name: notification.Spec.Topic}
bucketTopic, err := topic.GetProvisioned(r.client, r.opManagerContext, topicName)
if err != nil {
logger.Infof("CephBucketTopic %q not provisioned yet", topicName)
return waitForRequeueIfTopicNotReady, nil
return waitForRequeueIfTopicNotReady, notification, errors.Wrapf(err, "topic %q not provisioned yet", topicName)
}
// Populate clusterInfo during each reconcile
clusterInfo, clusterSpec, err := getReadyCluster(r.client, r.opManagerContext, *r.context, bucketTopic.Spec.ObjectStoreNamespace)
if err != nil {
return opcontroller.WaitForRequeueIfCephClusterNotReady, errors.Wrapf(err, "cluster is not ready")
return opcontroller.WaitForRequeueIfCephClusterNotReady, notification, errors.Wrapf(err, "cluster is not ready")
}
if clusterInfo == nil || clusterSpec == nil {
return opcontroller.WaitForRequeueIfCephClusterNotReady, nil
return opcontroller.WaitForRequeueIfCephClusterNotReady, notification, errors.New("cluster is not ready")
}
// fetch all OBCs that has a label matching this CephBucketNotification
namespace := notification.Namespace
bnName := types.NamespacedName{Namespace: namespace, Name: notification.Name}
namespaceListOpt := client.InNamespace(namespace)
namespaceListOpt := client.InNamespace(notification.Namespace)
labelListOpt := client.MatchingLabels{
notificationLabelPrefix + notification.Name: notification.Name,
}
obcList := &bktv1alpha1.ObjectBucketClaimList{}
err = r.client.List(r.opManagerContext, obcList, namespaceListOpt, labelListOpt)
if err != nil {
return reconcile.Result{}, errors.Wrapf(err, "failed to list ObjectBucketClaims for CephBucketNotification %q", bnName)
return reconcile.Result{}, notification, errors.Wrapf(err, "failed to list ObjectBucketClaims for CephBucketNotification %q", bnName)
}
if len(obcList.Items) == 0 {
logger.Debugf("no ObjectbucketClaim associated with CephBucketNotification %q", bnName)
return reconcile.Result{}, nil
return reconcile.Result{}, notification, nil
}
// loop through all OBCs in the list and get their OBs
for _, obc := range obcList.Items {
if obc.Spec.ObjectBucketName == "" {
logger.Infof("ObjectBucketClaim %q resource did not create the bucket yet. will retry", types.NamespacedName{Name: obc.Name, Namespace: obc.Namespace})
return waitForRequeueIfObjectBucketNotReady, nil
return waitForRequeueIfObjectBucketNotReady, notification, errors.Errorf("ObjectBucketClaim %q did not create the bucket yet",
types.NamespacedName{Name: obc.Name, Namespace: obc.Namespace})
}
ob := bktv1alpha1.ObjectBucket{}
bucketName := types.NamespacedName{Namespace: namespace, Name: obc.Spec.ObjectBucketName}
bucketName := types.NamespacedName{Namespace: notification.Namespace, Name: obc.Spec.ObjectBucketName}
if err := r.client.Get(r.opManagerContext, bucketName, &ob); err != nil {
return reconcile.Result{}, errors.Wrapf(err, "failed to retrieve ObjectBucket %v", bucketName)
return reconcile.Result{}, notification, errors.Wrapf(err, "failed to retrieve ObjectBucket %v", bucketName)
}
objectStoreName, err := getCephObjectStoreName(ob)
if err != nil {
return reconcile.Result{}, errors.Wrapf(err, "failed to get object store from ObjectBucket %q", bucketName)
return reconcile.Result{}, notification, errors.Wrapf(err, "failed to get object store from ObjectBucket %q", bucketName)
}
if err = validateObjectStoreName(bucketTopic, objectStoreName); err != nil {
return reconcile.Result{}, err
return reconcile.Result{}, notification, err
}
err = createNotificationFunc(
@@ -196,11 +201,12 @@ func (r *ReconcileNotifications) reconcile(request reconcile.Request) (reconcile
notification,
)
if err != nil {
return reconcile.Result{}, errors.Wrapf(err, "failed to provision CephBucketNotification %q for ObjectBucketClaims %q", bnName, bucketName)
return reconcile.Result{}, notification, errors.Wrapf(err, "failed to provision notification for ObjectBucketClaims %q", bucketName)
}
logger.Infof("provisioned CephBucketNotification %q for ObjectBucketClaims %q", bnName, bucketName)
}
return reconcile.Result{}, nil
return reconcile.Result{}, notification, nil
}
func getCephObjectStoreName(ob bktv1alpha1.ObjectBucket) (types.NamespacedName, error) {
@@ -20,7 +20,9 @@ package notification
import (
"context"
"os"
"strings"
"testing"
"time"
"github.com/coreos/pkg/capnslog"
bktv1alpha1 "github.com/kube-object-storage/lib-bucket-provisioner/pkg/apis/objectbucket.io/v1alpha1"
@@ -38,6 +40,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)
@@ -55,11 +58,21 @@ var (
multipleCreateBucketName = "multi-create"
multipleDeleteBucketName = "multi-delete"
multipleBothBucketName = "multi-both"
startEvent = string(cephv1.ReconcileStarted)
finishedEvent = string(cephv1.ReconcileSucceeded)
failedEvent = string(cephv1.ReconcileFailed)
)
var getWasInvoked bool
var createdNotifications []string
var deletedNotifications []string
// global variables used inside mockSetup
var (
testCtx = context.TODO()
testContext *clusterd.Context
testScheme = scheme.Scheme
testRecorder = record.NewFakeRecorder(256)
getWasInvoked = false
createdNotifications []string
deletedNotifications []string
)
func resetValues() {
getWasInvoked = false
@@ -74,7 +87,52 @@ func mockCleanup() {
deleteNotificationFunc = deleteNotification
}
func mockSetup() {
func mockSetup(t *testing.T) {
// set log level
capnslog.SetGlobalLogLevel(capnslog.DEBUG)
os.Setenv("ROOK_LOG_LEVEL", "DEBUG")
// create clients
testContext = &clusterd.Context{
Executor: &exectest.MockExecutor{},
RookClientset: rookclient.NewSimpleClientset(),
Clientset: test.New(t, 3),
}
// create scheme
testScheme.AddKnownTypes(
cephv1.SchemeGroupVersion,
&cephv1.CephBucketNotification{},
&cephv1.CephBucketNotificationList{},
&cephv1.CephBucketTopic{},
&cephv1.CephBucketTopicList{},
&cephv1.CephCluster{},
&cephv1.CephClusterList{},
&bktv1alpha1.ObjectBucketClaim{},
&bktv1alpha1.ObjectBucketClaimList{},
&bktv1alpha1.ObjectBucket{},
&bktv1alpha1.ObjectBucketList{},
)
// create secrets
secrets := map[string][]byte{
"fsid": []byte("name"),
"mon-secret": []byte("monsecret"),
"admin-secret": []byte("adminsecret"),
}
secret := &v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "rook-ceph-mon",
Namespace: testNamespace,
},
Data: secrets,
Type: k8sutil.RookType,
}
_, err := testContext.Clientset.CoreV1().Secrets(testNamespace).Create(testCtx, secret, metav1.CreateOptions{})
assert.NoError(t, err)
// test mocks
createNotificationFunc = func(p provisioner, bucket *bktv1alpha1.ObjectBucket, topicARN string, notification *cephv1.CephBucketNotification) error {
createdNotifications = append(createdNotifications, notification.Name)
return nil
@@ -99,12 +157,55 @@ func mockSetup() {
}
}
func testReconciler(objects []runtime.Object, notificationName string) (reconcile.Result, error) {
defer func() { testRecorder.Events <- "END" }()
cl := fake.NewClientBuilder().WithScheme(testScheme).WithRuntimeObjects(objects...).Build()
r := &ReconcileNotifications{client: cl, context: testContext, opManagerContext: testCtx, recorder: testRecorder}
testRequest := reconcile.Request{
NamespacedName: types.NamespacedName{
Name: notificationName,
Namespace: testNamespace,
},
}
return r.Reconcile(testCtx, testRequest)
}
func testOBCLabelReconciler(objects []runtime.Object, bucketName string) (reconcile.Result, error) {
defer func() { testRecorder.Events <- "END" }()
cl := fake.NewClientBuilder().WithScheme(testScheme).WithRuntimeObjects(objects...).Build()
r := &ReconcileOBCLabels{client: cl, context: testContext, opManagerContext: testCtx, recorder: testRecorder}
testRequest := reconcile.Request{
NamespacedName: types.NamespacedName{
Name: bucketName,
Namespace: testNamespace,
},
}
return r.Reconcile(testCtx, testRequest)
}
func verifyEvents(t *testing.T, expectedEvents []string) {
expectedEvents = append(expectedEvents, "END")
for _, expectedEvent := range expectedEvents {
select {
case event := <-testRecorder.Events:
if expectedEvent != "END" {
splitEvent := strings.Split(event, " ")
// the event message must have at least 3 parts
assert.GreaterOrEqual(t, len(splitEvent), 3)
// the type of event (2nd part) must match
assert.Equal(t, splitEvent[1], expectedEvent)
} else {
assert.Equal(t, expectedEvent, event)
}
case <-time.After(1 * time.Second):
assert.Failf(t, "missing event", "missing event: \"%s\"", expectedEvent)
}
}
}
func TestCephBucketNotificationController(t *testing.T) {
mockSetup()
mockSetup(t)
defer mockCleanup()
ctx := context.TODO()
capnslog.SetGlobalLogLevel(capnslog.DEBUG)
os.Setenv("ROOK_LOG_LEVEL", "DEBUG")
bucketTopic := &cephv1.CephBucketTopic{
ObjectMeta: metav1.ObjectMeta{
@@ -149,48 +250,6 @@ func TestCephBucketNotificationController(t *testing.T) {
},
},
}
req := reconcile.Request{
NamespacedName: types.NamespacedName{
Name: testNotificationName,
Namespace: testNamespace,
},
}
s := scheme.Scheme
s.AddKnownTypes(
cephv1.SchemeGroupVersion,
&cephv1.CephBucketNotification{},
&cephv1.CephBucketNotificationList{},
&cephv1.CephBucketTopic{},
&cephv1.CephBucketTopicList{},
&cephv1.CephCluster{},
&cephv1.CephClusterList{},
&bktv1alpha1.ObjectBucketClaim{},
&bktv1alpha1.ObjectBucketClaimList{},
)
c := &clusterd.Context{
Executor: &exectest.MockExecutor{},
RookClientset: rookclient.NewSimpleClientset(),
Clientset: test.New(t, 3),
}
secrets := map[string][]byte{
"fsid": []byte("name"),
"mon-secret": []byte("monsecret"),
"admin-secret": []byte("adminsecret"),
}
secret := &v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "rook-ceph-mon",
Namespace: testNamespace,
},
Data: secrets,
Type: k8sutil.RookType,
}
_, err := c.Clientset.CoreV1().Secrets(testNamespace).Create(ctx, secret, metav1.CreateOptions{})
assert.NoError(t, err)
t.Run("create notification configuration without a topic", func(t *testing.T) {
// Objects to track in the fake client.
@@ -198,18 +257,13 @@ func TestCephBucketNotificationController(t *testing.T) {
bucketNotification,
}
cl := fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(objects...).Build()
r := &ReconcileNotifications{client: cl, context: c, opManagerContext: ctx}
res, err := r.Reconcile(ctx, req)
res, err := testReconciler(objects, testNotificationName)
// provisioning requeued because the topic is not configured
assert.NoError(t, err)
assert.True(t, res.Requeue)
// notification configuration is set
err = r.client.Get(ctx, types.NamespacedName{Name: testNotificationName, Namespace: testNamespace}, bucketNotification)
assert.NoError(t, err, bucketNotification)
assert.Equal(t, 0, len(createdNotifications))
verifyEvents(t, []string{startEvent, failedEvent})
})
t.Run("create notification and topic configuration when there is no cluster", func(t *testing.T) {
@@ -218,18 +272,13 @@ func TestCephBucketNotificationController(t *testing.T) {
bucketTopic,
}
cl := fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(objects...).Build()
r := &ReconcileNotifications{client: cl, context: c, opManagerContext: ctx}
res, err := r.Reconcile(ctx, req)
res, err := testReconciler(objects, testNotificationName)
// provisioning requeued because the cluster does not exist
assert.NoError(t, err)
assert.True(t, res.Requeue)
// notification configuration is set
err = r.client.Get(ctx, types.NamespacedName{Name: testNotificationName, Namespace: testNamespace}, bucketNotification)
assert.NoError(t, err, bucketNotification)
assert.Equal(t, 0, len(createdNotifications))
verifyEvents(t, []string{startEvent, failedEvent})
})
t.Run("create notification and topic configuration cluster is not ready", func(t *testing.T) {
@@ -239,18 +288,13 @@ func TestCephBucketNotificationController(t *testing.T) {
cephCluster,
}
cl := fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(objects...).Build()
r := &ReconcileNotifications{client: cl, context: c, opManagerContext: ctx}
res, err := r.Reconcile(ctx, req)
res, err := testReconciler(objects, testNotificationName)
// provisioning requeued because the cluster is not ready
assert.NoError(t, err)
assert.True(t, res.Requeue)
// notification configuration is set
err = r.client.Get(ctx, types.NamespacedName{Name: testNotificationName, Namespace: testNamespace}, bucketNotification)
assert.NoError(t, err, bucketNotification)
assert.Equal(t, 0, len(createdNotifications))
verifyEvents(t, []string{startEvent, failedEvent})
})
t.Run("create notification and topic configuration when topic is not yet provisioned", func(t *testing.T) {
@@ -262,18 +306,12 @@ func TestCephBucketNotificationController(t *testing.T) {
cephCluster,
}
cl := fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(objects...).Build()
r := &ReconcileNotifications{client: cl, context: c, opManagerContext: ctx}
res, err := r.Reconcile(ctx, req)
res, err := testReconciler(objects, testNotificationName)
// provisioning requeued because the topic is not provisioned on the RGW
assert.NoError(t, err)
assert.True(t, res.Requeue)
// notification configuration is set
err = r.client.Get(ctx, types.NamespacedName{Name: testNotificationName, Namespace: testNamespace}, bucketNotification)
assert.NoError(t, err, bucketNotification)
assert.Equal(t, 0, len(createdNotifications))
verifyEvents(t, []string{startEvent, failedEvent})
})
t.Run("create notification and topic configuration", func(t *testing.T) {
@@ -286,26 +324,17 @@ func TestCephBucketNotificationController(t *testing.T) {
cephCluster,
}
cl := fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(objects...).Build()
r := &ReconcileNotifications{client: cl, context: c, opManagerContext: ctx}
res, err := r.Reconcile(ctx, req)
res, err := testReconciler(objects, testNotificationName)
assert.NoError(t, err)
assert.False(t, res.Requeue)
// notification configuration is set
err = r.client.Get(ctx, types.NamespacedName{Name: testNotificationName, Namespace: testNamespace}, bucketNotification)
assert.NoError(t, err, bucketNotification)
assert.Equal(t, 0, len(createdNotifications))
verifyEvents(t, []string{startEvent, finishedEvent})
})
}
func TestCephBucketNotificationControllerWithOBC(t *testing.T) {
mockSetup()
mockSetup(t)
defer mockCleanup()
ctx := context.TODO()
capnslog.SetGlobalLogLevel(capnslog.DEBUG)
os.Setenv("ROOK_LOG_LEVEL", "DEBUG")
bucketTopic := &cephv1.CephBucketTopic{
ObjectMeta: metav1.ObjectMeta{
@@ -369,50 +398,6 @@ func TestCephBucketNotificationControllerWithOBC(t *testing.T) {
Phase: bktv1alpha1.ObjectBucketClaimStatusPhasePending,
},
}
req := reconcile.Request{
NamespacedName: types.NamespacedName{
Name: testNotificationName,
Namespace: testNamespace,
},
}
s := scheme.Scheme
s.AddKnownTypes(
cephv1.SchemeGroupVersion,
&cephv1.CephBucketNotification{},
&cephv1.CephBucketNotificationList{},
&cephv1.CephBucketTopic{},
&cephv1.CephBucketTopicList{},
&cephv1.CephCluster{},
&cephv1.CephClusterList{},
&bktv1alpha1.ObjectBucketClaim{},
&bktv1alpha1.ObjectBucketClaimList{},
&bktv1alpha1.ObjectBucket{},
&bktv1alpha1.ObjectBucketList{},
)
c := &clusterd.Context{
Executor: &exectest.MockExecutor{},
RookClientset: rookclient.NewSimpleClientset(),
Clientset: test.New(t, 3),
}
secrets := map[string][]byte{
"fsid": []byte("name"),
"mon-secret": []byte("monsecret"),
"admin-secret": []byte("adminsecret"),
}
secret := &v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "rook-ceph-mon",
Namespace: testNamespace,
},
Data: secrets,
Type: k8sutil.RookType,
}
_, err := c.Clientset.CoreV1().Secrets(testNamespace).Create(ctx, secret, metav1.CreateOptions{})
assert.NoError(t, err)
t.Run("provision notification when OBC exists but no OB", func(t *testing.T) {
objects := []runtime.Object{
@@ -422,17 +407,11 @@ func TestCephBucketNotificationControllerWithOBC(t *testing.T) {
obc,
}
cl := fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(objects...).Build()
r := &ReconcileNotifications{client: cl, context: c, opManagerContext: ctx}
res, err := r.Reconcile(ctx, req)
res, err := testReconciler(objects, testNotificationName)
assert.NoError(t, err)
assert.True(t, res.Requeue)
// notification configuration is set
err = r.client.Get(ctx, types.NamespacedName{Name: testNotificationName, Namespace: testNamespace}, bucketNotification)
assert.NoError(t, err, bucketNotification)
assert.Equal(t, 0, len(createdNotifications))
verifyEvents(t, []string{startEvent, failedEvent})
})
t.Run("provision notification when OB exists", func(t *testing.T) {
@@ -466,17 +445,11 @@ func TestCephBucketNotificationControllerWithOBC(t *testing.T) {
ob,
}
cl := fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(objects...).Build()
r := &ReconcileNotifications{client: cl, context: c, opManagerContext: ctx}
res, err := r.Reconcile(ctx, req)
res, err := testReconciler(objects, testNotificationName)
assert.NoError(t, err)
assert.False(t, res.Requeue)
// notification configuration is set
err = r.client.Get(ctx, types.NamespacedName{Name: testNotificationName, Namespace: testNamespace}, bucketNotification)
assert.NoError(t, err, bucketNotification)
assert.Equal(t, 1, len(createdNotifications))
verifyEvents(t, []string{startEvent, finishedEvent})
})
}
@@ -28,9 +28,13 @@ import (
opcontroller "github.com/rook/rook/pkg/operator/ceph/controller"
"github.com/rook/rook/pkg/operator/ceph/object/bucket"
"github.com/rook/rook/pkg/operator/ceph/object/topic"
"github.com/rook/rook/pkg/operator/ceph/reporting"
kapiv1 "k8s.io/api/core/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/handler"
@@ -48,6 +52,7 @@ type ReconcileOBCLabels struct {
client client.Client
context *clusterd.Context
opManagerContext context.Context
recorder record.EventRecorder
}
func addOBCLabelReconciler(mgr manager.Manager, r reconcile.Reconciler) error {
@@ -139,7 +144,7 @@ func (r *ReconcileOBCLabels) reconcile(request reconcile.Request) (reconcile.Res
}
bnList, err := getAllNotificationsFunc(p, &ob)
if err != nil {
return reconcile.Result{}, errors.Wrapf(err, "failed delete all bucket notifications from ObjectbucketClaim %q", bucketName)
return reconcile.Result{}, errors.Wrapf(err, "failed to list bucket notifications in ObjectbucketClaim %q", bucketName)
}
labelList := make([]string, 0)
@@ -175,42 +180,46 @@ func (r *ReconcileOBCLabels) reconcile(request reconcile.Request) (reconcile.Res
return waitForRequeueIfNotificationNotDeleted, nil
}
// add new notifications to the list
return r.addNewNotifications(p, ob, labelList, objectStoreName, obc.Namespace)
}
func (r *ReconcileOBCLabels) addNewNotifications(p provisioner, ob bktv1alpha1.ObjectBucket, notificationList []string, objectStoreName types.NamespacedName, namespace string) (reconcile.Result, error) {
for _, labelValue := range notificationList {
// for each notification label fetch the bucket notification CRD
notification := &cephv1.CephBucketNotification{}
bnName := types.NamespacedName{Namespace: namespace, Name: labelValue}
if err := r.client.Get(r.opManagerContext, bnName, notification); err != nil {
if kerrors.IsNotFound(err) {
logger.Infof("CephBucketNotification %q not found in %q ", bnName.Name, bnName.Namespace)
return waitForRequeueIfNotificationNotReady, nil
}
return reconcile.Result{}, errors.Wrapf(err, "failed to retrieve CephBucketNotification %q", bnName)
}
logger.Debugf("adding bucket notification %q in namespace %q to obc %q", bnName.Name, bnName.Namespace, ob.Spec.ClaimRef.Name)
// get the topic associated with the notification, and make sure it is provisioned
topicName := types.NamespacedName{Namespace: notification.Namespace, Name: notification.Spec.Topic}
bucketTopic, err := topic.GetProvisioned(r.client, r.opManagerContext, topicName)
for _, label := range labelList {
reconcileResponse, notification, err := r.addNewNotification(p, ob, label, objectStoreName, obc.Namespace)
_, _ = reporting.ReportReconcileResult(logger, r.recorder, notification, reconcileResponse, err)
if err != nil {
logger.Infof("CephBucketTopic %q not provisioned yet in %q", topicName.Name, topicName.Namespace)
return waitForRequeueIfTopicNotReady, nil
return reconcileResponse, err
}
if err = validateObjectStoreName(bucketTopic, objectStoreName); err != nil {
return reconcile.Result{}, err
}
// provision the notification
err = createNotificationFunc(p, &ob, *bucketTopic.Status.ARN, notification)
if err != nil {
return reconcile.Result{}, errors.Wrapf(err, "failed to provision CephBucketNotification %q", bnName)
}
logger.Infof("provisioned CephBucketNotification %q in namespace %q to obc %q", bnName.Name, bnName.Namespace, ob.Spec.ClaimRef.Name)
}
return reconcile.Result{}, nil
}
func (r *ReconcileOBCLabels) addNewNotification(p provisioner, ob bktv1alpha1.ObjectBucket, label string, objectStoreName types.NamespacedName, namespace string) (reconcile.Result, *cephv1.CephBucketNotification, error) {
// for each notification label fetch the bucket notification CRD
notification := &cephv1.CephBucketNotification{ObjectMeta: metav1.ObjectMeta{Name: label, Namespace: namespace}}
bnName := types.NamespacedName{Namespace: namespace, Name: label}
bucketName := types.NamespacedName{Name: ob.Spec.ClaimRef.Name, Namespace: namespace}
r.recorder.Eventf(notification, kapiv1.EventTypeNormal, string(cephv1.ReconcileStarted), "Started reconciling CephBucketNotification %q for ObjectBucketClaim %q", bnName, bucketName)
if err := r.client.Get(r.opManagerContext, bnName, notification); err != nil {
if kerrors.IsNotFound(err) {
return waitForRequeueIfNotificationNotReady, notification, errors.Wrapf(err, "CephBucketNotification %q not provisioned yet", bnName)
}
return reconcile.Result{}, notification, errors.Wrapf(err, "failed to retrieve CephBucketNotification %q", bnName)
}
// get the topic associated with the notification, and make sure it is provisioned
topicName := types.NamespacedName{Namespace: notification.Namespace, Name: notification.Spec.Topic}
bucketTopic, err := topic.GetProvisioned(r.client, r.opManagerContext, topicName)
if err != nil {
return waitForRequeueIfTopicNotReady, notification, errors.Wrapf(err, "topic %q not provisioned yet", topicName)
}
if err = validateObjectStoreName(bucketTopic, objectStoreName); err != nil {
return reconcile.Result{}, notification, err
}
// provision the notification
err = createNotificationFunc(p, &ob, *bucketTopic.Status.ARN, notification)
if err != nil {
return reconcile.Result{}, notification, errors.Wrapf(err, "failed to provision notification for ObjectBucketClaims %q", bucketName)
}
logger.Infof("provisioned CephBucketNotification %q for ObjectBucketClaims %q", bnName, bucketName)
return reconcile.Result{}, notification, nil
}
@@ -18,29 +18,17 @@ limitations under the License.
package notification
import (
"context"
"os"
"testing"
"github.com/coreos/pkg/capnslog"
bktv1alpha1 "github.com/kube-object-storage/lib-bucket-provisioner/pkg/apis/objectbucket.io/v1alpha1"
cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"
rookclient "github.com/rook/rook/pkg/client/clientset/versioned/fake"
"github.com/rook/rook/pkg/operator/test"
"github.com/rook/rook/pkg/clusterd"
"github.com/rook/rook/pkg/operator/ceph/object"
"github.com/rook/rook/pkg/operator/k8sutil"
exectest "github.com/rook/rook/pkg/util/exec/test"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)
func createOBResources(name string) (*bktv1alpha1.ObjectBucketClaim, *bktv1alpha1.ObjectBucket) {
@@ -107,11 +95,8 @@ func setNotificationLabels(labelList []string) map[string]string {
}
func TestCephBucketNotificationOBCLabelController(t *testing.T) {
mockSetup()
mockSetup(t)
defer mockCleanup()
ctx := context.TODO()
capnslog.SetGlobalLogLevel(capnslog.DEBUG)
os.Setenv("ROOK_LOG_LEVEL", "DEBUG")
bucketTopic := &cephv1.CephBucketTopic{
ObjectMeta: metav1.ObjectMeta{
@@ -146,54 +131,9 @@ func TestCephBucketNotificationOBCLabelController(t *testing.T) {
},
}
req := reconcile.Request{
NamespacedName: types.NamespacedName{
Name: testBucketName,
Namespace: testNamespace,
},
}
s := scheme.Scheme
s.AddKnownTypes(
cephv1.SchemeGroupVersion,
&cephv1.CephBucketNotification{},
&cephv1.CephBucketNotificationList{},
&cephv1.CephBucketTopic{},
&cephv1.CephBucketTopicList{},
&cephv1.CephCluster{},
&cephv1.CephClusterList{},
&bktv1alpha1.ObjectBucketClaim{},
&bktv1alpha1.ObjectBucketClaimList{},
&bktv1alpha1.ObjectBucket{},
&bktv1alpha1.ObjectBucketList{},
)
c := &clusterd.Context{
Executor: &exectest.MockExecutor{},
RookClientset: rookclient.NewSimpleClientset(),
Clientset: test.New(t, 3),
}
secrets := map[string][]byte{
"fsid": []byte("name"),
"mon-secret": []byte("monsecret"),
"admin-secret": []byte("adminsecret"),
}
secret := &v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "rook-ceph-mon",
Namespace: testNamespace,
},
Data: secrets,
Type: k8sutil.RookType,
}
obc, ob := createOBResources(testBucketName)
obc.Labels = setNotificationLabels([]string{testNotificationName})
_, err := c.Clientset.CoreV1().Secrets(testNamespace).Create(ctx, secret, metav1.CreateOptions{})
assert.NoError(t, err)
t.Run("provision OBC with notification label with no ob", func(t *testing.T) {
resetValues()
objects := []runtime.Object{
@@ -201,16 +141,13 @@ func TestCephBucketNotificationOBCLabelController(t *testing.T) {
obc,
}
cl := fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(objects...).Build()
r := &ReconcileOBCLabels{client: cl, context: c, opManagerContext: ctx}
res, err := r.Reconcile(ctx, req)
res, err := testOBCLabelReconciler(objects, testBucketName)
assert.NoError(t, err)
assert.True(t, res.Requeue)
assert.False(t, getWasInvoked)
assert.Equal(t, 0, len(createdNotifications))
assert.Equal(t, 0, len(deletedNotifications))
verifyEvents(t, []string{})
})
t.Run("provision OBC with notification label with not ready ob", func(t *testing.T) {
@@ -221,16 +158,13 @@ func TestCephBucketNotificationOBCLabelController(t *testing.T) {
ob,
}
cl := fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(objects...).Build()
r := &ReconcileOBCLabels{client: cl, context: c, opManagerContext: ctx}
res, err := r.Reconcile(ctx, req)
res, err := testOBCLabelReconciler(objects, testBucketName)
assert.NoError(t, err)
assert.True(t, res.Requeue)
assert.False(t, getWasInvoked)
assert.Equal(t, 0, len(createdNotifications))
assert.Equal(t, 0, len(deletedNotifications))
verifyEvents(t, []string{})
})
obc.Spec.ObjectBucketName = testBucketName
@@ -244,16 +178,13 @@ func TestCephBucketNotificationOBCLabelController(t *testing.T) {
ob,
}
cl := fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(objects...).Build()
r := &ReconcileOBCLabels{client: cl, context: c, opManagerContext: ctx}
res, err := r.Reconcile(ctx, req)
assert.NoError(t, err)
res, err := testOBCLabelReconciler(objects, testBucketName)
assert.Error(t, err)
assert.True(t, res.Requeue)
assert.True(t, getWasInvoked)
assert.Equal(t, 0, len(createdNotifications))
assert.Equal(t, 0, len(deletedNotifications))
verifyEvents(t, []string{startEvent, failedEvent})
})
bucketNotification := createBucketNotification(testNotificationName)
@@ -266,16 +197,13 @@ func TestCephBucketNotificationOBCLabelController(t *testing.T) {
bucketNotification,
}
cl := fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(objects...).Build()
r := &ReconcileOBCLabels{client: cl, context: c, opManagerContext: ctx}
res, err := r.Reconcile(ctx, req)
assert.NoError(t, err)
res, err := testOBCLabelReconciler(objects, testBucketName)
assert.Error(t, err)
assert.True(t, res.Requeue)
assert.True(t, getWasInvoked)
assert.Equal(t, 0, len(createdNotifications))
assert.Equal(t, 0, len(deletedNotifications))
verifyEvents(t, []string{startEvent, failedEvent})
})
t.Run("provision OBC with notification label", func(t *testing.T) {
@@ -288,11 +216,7 @@ func TestCephBucketNotificationOBCLabelController(t *testing.T) {
bucketTopic,
}
cl := fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(objects...).Build()
r := &ReconcileOBCLabels{client: cl, context: c, opManagerContext: ctx}
res, err := r.Reconcile(ctx, req)
res, err := testOBCLabelReconciler(objects, testBucketName)
assert.NoError(t, err)
assert.False(t, res.Requeue)
assert.True(t, getWasInvoked)
@@ -307,7 +231,6 @@ func TestCephBucketNotificationOBCLabelController(t *testing.T) {
noChangeOBC.Labels = setNotificationLabels([]string{testNotificationName})
noChangeOBC.Spec.ObjectBucketName = noChangeBucketName
noChangeOBC.Status.Phase = bktv1alpha1.ObjectBucketClaimStatusPhaseBound
req.NamespacedName.Name = noChangeBucketName
objects := []runtime.Object{
cephCluster,
noChangeOBC,
@@ -316,11 +239,7 @@ func TestCephBucketNotificationOBCLabelController(t *testing.T) {
bucketTopic,
}
cl := fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(objects...).Build()
r := &ReconcileOBCLabels{client: cl, context: c, opManagerContext: ctx}
res, err := r.Reconcile(ctx, req)
res, err := testOBCLabelReconciler(objects, noChangeBucketName)
assert.NoError(t, err)
assert.False(t, res.Requeue)
assert.True(t, getWasInvoked)
@@ -335,18 +254,13 @@ func TestCephBucketNotificationOBCLabelController(t *testing.T) {
deleteOBC.Spec.GenerateBucketName = deleteBucketName
deleteOBC.Spec.ObjectBucketName = deleteBucketName
deleteOBC.Status.Phase = bktv1alpha1.ObjectBucketClaimStatusPhaseBound
req.NamespacedName.Name = deleteBucketName
objects := []runtime.Object{
cephCluster,
deleteOBC,
deleteOB,
}
cl := fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(objects...).Build()
r := &ReconcileOBCLabels{client: cl, context: c, opManagerContext: ctx}
res, err := r.Reconcile(ctx, req)
res, err := testOBCLabelReconciler(objects, deleteBucketName)
assert.NoError(t, err)
assert.False(t, res.Requeue)
assert.True(t, getWasInvoked)
@@ -364,7 +278,6 @@ func TestCephBucketNotificationOBCLabelController(t *testing.T) {
multipleCreateOBC.Status.Phase = bktv1alpha1.ObjectBucketClaimStatusPhaseBound
bucketNotification1 := createBucketNotification(testNotificationName + "-1")
bucketNotification2 := createBucketNotification(testNotificationName + "-2")
req.NamespacedName.Name = multipleCreateBucketName
objects := []runtime.Object{
cephCluster,
@@ -375,11 +288,7 @@ func TestCephBucketNotificationOBCLabelController(t *testing.T) {
bucketTopic,
}
cl := fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(objects...).Build()
r := &ReconcileOBCLabels{client: cl, context: c, opManagerContext: ctx}
res, err := r.Reconcile(ctx, req)
res, err := testOBCLabelReconciler(objects, multipleCreateBucketName)
assert.NoError(t, err)
assert.False(t, res.Requeue)
assert.True(t, getWasInvoked)
@@ -394,18 +303,13 @@ func TestCephBucketNotificationOBCLabelController(t *testing.T) {
multipleDeleteOBC.Spec.GenerateBucketName = multipleDeleteBucketName
multipleDeleteOBC.Spec.ObjectBucketName = multipleDeleteBucketName
multipleDeleteOBC.Status.Phase = bktv1alpha1.ObjectBucketClaimStatusPhaseBound
req.NamespacedName.Name = multipleDeleteBucketName
objects := []runtime.Object{
cephCluster,
multipleDeleteOBC,
multipleDeleteOB,
}
cl := fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(objects...).Build()
r := &ReconcileOBCLabels{client: cl, context: c, opManagerContext: ctx}
res, err := r.Reconcile(ctx, req)
res, err := testOBCLabelReconciler(objects, multipleDeleteBucketName)
assert.NoError(t, err)
assert.False(t, res.Requeue)
assert.True(t, getWasInvoked)
@@ -422,7 +326,6 @@ func TestCephBucketNotificationOBCLabelController(t *testing.T) {
multipleBothOBC.Status.Phase = bktv1alpha1.ObjectBucketClaimStatusPhaseBound
bucketNotification1 := createBucketNotification(testNotificationName + "-1")
bucketNotification2 := createBucketNotification(testNotificationName + "-2")
req.NamespacedName.Name = multipleBothBucketName
objects := []runtime.Object{
cephCluster,
@@ -433,11 +336,7 @@ func TestCephBucketNotificationOBCLabelController(t *testing.T) {
bucketTopic,
}
cl := fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(objects...).Build()
r := &ReconcileOBCLabels{client: cl, context: c, opManagerContext: ctx}
res, err := r.Reconcile(ctx, req)
res, err := testOBCLabelReconciler(objects, multipleBothBucketName)
assert.NoError(t, err)
assert.False(t, res.Requeue)
assert.True(t, getWasInvoked)
@@ -446,5 +345,6 @@ func TestCephBucketNotificationOBCLabelController(t *testing.T) {
assert.Equal(t, 2, len(deletedNotifications))
assert.ElementsMatch(t, deletedNotifications,
[]string{multipleDeleteBucketName + testNotificationName + "-1", multipleDeleteBucketName + testNotificationName + "-2"})
verifyEvents(t, []string{startEvent, finishedEvent})
})
}