core: rework usage of ReportReconcileResult
ReportReconcileResult should never be given an object that is nil. It is
impossible to force compile-time checking for this because the object's
type is an interface. We can't force this, but we can rework the
function definition to handle more cases where the object may not be
returned fully-complete, and we can rework callers to return structs
(not pointers-to-structs) so it is less likely a caller will pass nil
to the function.
Signed-off-by: Blaine Gardner <blaine.gardner@redhat.com>
(cherry picked from commit c92c6fbc36)
This commit is contained in:
1 parent
4353b35aaa
commit
ae1eab91fc
6 files changed
+347
-73
No files matched your search
@@ -216,11 +216,11 @@ func (r *ReconcileCephCluster) Reconcile(context context.Context, request reconc
|
||||
// workaround because the rook logging mechanism is not compatible with the controller-runtime logging interface
|
||||
reconcileResponse, cephCluster, err := r.reconcile(request)
|
||||
|
||||
return reporting.ReportReconcileResult(logger, r.clusterController.recorder,
|
||||
cephCluster, reconcileResponse, err)
|
||||
return reporting.ReportReconcileResult(logger, r.clusterController.recorder, request,
|
||||
&cephCluster, reconcileResponse, err)
|
||||
}
|
||||
|
||||
func (r *ReconcileCephCluster) reconcile(request reconcile.Request) (reconcile.Result, *cephv1.CephCluster, error) {
|
||||
func (r *ReconcileCephCluster) reconcile(request reconcile.Request) (reconcile.Result, cephv1.CephCluster, error) {
|
||||
// Pass the client context to the ClusterController
|
||||
r.clusterController.client = r.client
|
||||
|
||||
@@ -236,16 +236,16 @@ func (r *ReconcileCephCluster) reconcile(request reconcile.Request) (reconcile.R
|
||||
if err != nil {
|
||||
if kerrors.IsNotFound(err) {
|
||||
logger.Debug("cephCluster resource not found. Ignoring since object must be deleted.")
|
||||
return reconcile.Result{}, cephCluster, nil
|
||||
return reconcile.Result{}, *cephCluster, nil
|
||||
}
|
||||
// Error reading the object - requeue the request.
|
||||
return reconcile.Result{}, cephCluster, errors.Wrap(err, "failed to get cephCluster")
|
||||
return reconcile.Result{}, *cephCluster, errors.Wrap(err, "failed to get cephCluster")
|
||||
}
|
||||
|
||||
// Set a finalizer so we can do cleanup before the object goes away
|
||||
err = opcontroller.AddFinalizerIfNotPresent(r.opManagerContext, r.client, cephCluster)
|
||||
if err != nil {
|
||||
return reconcile.Result{}, cephCluster, errors.Wrap(err, "failed to add finalizer")
|
||||
return reconcile.Result{}, *cephCluster, errors.Wrap(err, "failed to add finalizer")
|
||||
}
|
||||
|
||||
// DELETE: the CR was deleted
|
||||
@@ -256,14 +256,14 @@ func (r *ReconcileCephCluster) reconcile(request reconcile.Request) (reconcile.R
|
||||
// Do reconcile here!
|
||||
ownerInfo := k8sutil.NewOwnerInfo(cephCluster, r.scheme)
|
||||
if err := r.clusterController.reconcileCephCluster(cephCluster, ownerInfo); err != nil {
|
||||
return reconcile.Result{}, cephCluster, errors.Wrapf(err, "failed to reconcile cluster %q", cephCluster.Name)
|
||||
return reconcile.Result{}, *cephCluster, errors.Wrapf(err, "failed to reconcile cluster %q", cephCluster.Name)
|
||||
}
|
||||
|
||||
// Return and do not requeue
|
||||
return reconcile.Result{}, cephCluster, nil
|
||||
return reconcile.Result{}, *cephCluster, nil
|
||||
}
|
||||
|
||||
func (r *ReconcileCephCluster) reconcileDelete(cephCluster *cephv1.CephCluster) (reconcile.Result, *cephv1.CephCluster, error) {
|
||||
func (r *ReconcileCephCluster) reconcileDelete(cephCluster *cephv1.CephCluster) (reconcile.Result, cephv1.CephCluster, error) {
|
||||
nsName := r.clusterController.namespacedName
|
||||
var err error
|
||||
|
||||
@@ -274,11 +274,11 @@ func (r *ReconcileCephCluster) reconcileDelete(cephCluster *cephv1.CephCluster)
|
||||
|
||||
deps, err := CephClusterDependents(r.context, cephCluster.Namespace)
|
||||
if err != nil {
|
||||
return reconcile.Result{}, cephCluster, err
|
||||
return reconcile.Result{}, *cephCluster, err
|
||||
}
|
||||
if !deps.Empty() {
|
||||
err := reporting.ReportDeletionBlockedDueToDependents(logger, r.client, cephCluster, deps)
|
||||
return opcontroller.WaitForRequeueIfFinalizerBlocked, cephCluster, err
|
||||
return opcontroller.WaitForRequeueIfFinalizerBlocked, *cephCluster, err
|
||||
}
|
||||
reporting.ReportDeletionNotBlockedDueToDependents(logger, r.client, r.clusterController.recorder, cephCluster)
|
||||
|
||||
@@ -296,7 +296,7 @@ func (r *ReconcileCephCluster) reconcileDelete(cephCluster *cephv1.CephCluster)
|
||||
if doCleanup {
|
||||
cephHosts, err := r.clusterController.getCephHosts(cephCluster.Namespace)
|
||||
if err != nil {
|
||||
return reconcile.Result{}, cephCluster, errors.Wrapf(err, "failed to find valid ceph hosts in the cluster %q", cephCluster.Namespace)
|
||||
return reconcile.Result{}, *cephCluster, errors.Wrapf(err, "failed to find valid ceph hosts in the cluster %q", cephCluster.Namespace)
|
||||
}
|
||||
go r.clusterController.startClusterCleanUp(internalCtx, cephCluster, cephHosts, monSecret, clusterFSID)
|
||||
}
|
||||
@@ -308,18 +308,18 @@ func (r *ReconcileCephCluster) reconcileDelete(cephCluster *cephv1.CephCluster)
|
||||
if err != nil {
|
||||
// If the cluster cannot be deleted, requeue the request for deletion to see if the conditions
|
||||
// will eventually be satisfied such as the volumes being removed
|
||||
return response, cephCluster, errors.Wrapf(err, "failed to clean up CephCluster %q", nsName.String())
|
||||
return response, *cephCluster, errors.Wrapf(err, "failed to clean up CephCluster %q", nsName.String())
|
||||
}
|
||||
}
|
||||
|
||||
// Remove finalizers
|
||||
err = r.removeFinalizers(r.client, nsName)
|
||||
if err != nil {
|
||||
return reconcile.Result{}, cephCluster, errors.Wrap(err, "failed to remove finalizers")
|
||||
return reconcile.Result{}, *cephCluster, errors.Wrap(err, "failed to remove finalizers")
|
||||
}
|
||||
|
||||
// Return and do not requeue. Successful deletion.
|
||||
return reconcile.Result{}, cephCluster, nil
|
||||
return reconcile.Result{}, *cephCluster, nil
|
||||
}
|
||||
|
||||
// NewClusterController create controller for watching cluster custom resources created
|
||||
|
||||
@@ -156,10 +156,11 @@ func (r *ReconcileCephObjectStore) Reconcile(context context.Context, request re
|
||||
// workaround because the rook logging mechanism is not compatible with the controller-runtime logging interface
|
||||
reconcileResponse, objectStore, err := r.reconcile(request)
|
||||
|
||||
return reporting.ReportReconcileResult(logger, r.recorder, objectStore, reconcileResponse, err)
|
||||
return reporting.ReportReconcileResult(logger, r.recorder, request,
|
||||
&objectStore, reconcileResponse, err)
|
||||
}
|
||||
|
||||
func (r *ReconcileCephObjectStore) reconcile(request reconcile.Request) (reconcile.Result, *cephv1.CephObjectStore, error) {
|
||||
func (r *ReconcileCephObjectStore) reconcile(request reconcile.Request) (reconcile.Result, cephv1.CephObjectStore, error) {
|
||||
// Fetch the cephObjectStore instance
|
||||
cephObjectStore := &cephv1.CephObjectStore{}
|
||||
err := r.client.Get(r.opManagerContext, request.NamespacedName, cephObjectStore)
|
||||
@@ -172,16 +173,16 @@ func (r *ReconcileCephObjectStore) reconcile(request reconcile.Request) (reconci
|
||||
cephObjectStore.Name = request.Name
|
||||
cephObjectStore.Namespace = request.Namespace
|
||||
r.stopMonitoring(cephObjectStore)
|
||||
return reconcile.Result{}, cephObjectStore, nil
|
||||
return reconcile.Result{}, *cephObjectStore, nil
|
||||
}
|
||||
// Error reading the object - requeue the request.
|
||||
return reconcile.Result{}, cephObjectStore, errors.Wrap(err, "failed to get cephObjectStore")
|
||||
return reconcile.Result{}, *cephObjectStore, errors.Wrap(err, "failed to get cephObjectStore")
|
||||
}
|
||||
|
||||
// Set a finalizer so we can do cleanup before the object goes away
|
||||
err = opcontroller.AddFinalizerIfNotPresent(r.opManagerContext, r.client, cephObjectStore)
|
||||
if err != nil {
|
||||
return reconcile.Result{}, cephObjectStore, errors.Wrap(err, "failed to add finalizer")
|
||||
return reconcile.Result{}, *cephObjectStore, errors.Wrap(err, "failed to add finalizer")
|
||||
}
|
||||
|
||||
// The CR was just created, initializing status fields
|
||||
@@ -206,14 +207,14 @@ func (r *ReconcileCephObjectStore) reconcile(request reconcile.Request) (reconci
|
||||
// Remove finalizer
|
||||
err := opcontroller.RemoveFinalizer(r.opManagerContext, r.client, cephObjectStore)
|
||||
if err != nil {
|
||||
return reconcile.Result{}, cephObjectStore, errors.Wrap(err, "failed to remove finalizer")
|
||||
return reconcile.Result{}, *cephObjectStore, errors.Wrap(err, "failed to remove finalizer")
|
||||
}
|
||||
|
||||
// Return and do not requeue. Successful deletion.
|
||||
return reconcile.Result{}, cephObjectStore, nil
|
||||
return reconcile.Result{}, *cephObjectStore, nil
|
||||
}
|
||||
|
||||
return reconcileResponse, cephObjectStore, nil
|
||||
return reconcileResponse, *cephObjectStore, nil
|
||||
}
|
||||
r.clusterSpec = &cephCluster.Spec
|
||||
|
||||
@@ -231,7 +232,7 @@ func (r *ReconcileCephObjectStore) reconcile(request reconcile.Request) (reconci
|
||||
// Populate clusterInfo during each reconcile
|
||||
r.clusterInfo, _, _, err = mon.LoadClusterInfo(r.context, r.opManagerContext, request.NamespacedName.Namespace)
|
||||
if err != nil {
|
||||
return reconcile.Result{}, cephObjectStore, errors.Wrap(err, "failed to populate cluster info")
|
||||
return reconcile.Result{}, *cephObjectStore, errors.Wrap(err, "failed to populate cluster info")
|
||||
}
|
||||
|
||||
// DELETE: the CR was deleted
|
||||
@@ -241,7 +242,7 @@ func (r *ReconcileCephObjectStore) reconcile(request reconcile.Request) (reconci
|
||||
// Detect running Ceph version
|
||||
runningCephVersion, err := cephclient.LeastUptodateDaemonVersion(r.context, r.clusterInfo, config.MonType)
|
||||
if err != nil {
|
||||
return reconcile.Result{}, cephObjectStore, errors.Wrapf(err, "failed to retrieve current ceph %q version", config.MonType)
|
||||
return reconcile.Result{}, *cephObjectStore, errors.Wrapf(err, "failed to retrieve current ceph %q version", config.MonType)
|
||||
}
|
||||
r.clusterInfo.CephVersion = runningCephVersion
|
||||
r.clusterInfo.Context = r.opManagerContext
|
||||
@@ -249,23 +250,23 @@ func (r *ReconcileCephObjectStore) reconcile(request reconcile.Request) (reconci
|
||||
// get the latest version of the object to check dependencies
|
||||
err = r.client.Get(r.opManagerContext, request.NamespacedName, cephObjectStore)
|
||||
if err != nil {
|
||||
return reconcile.Result{}, cephObjectStore, errors.Wrapf(err, "failed to get latest CephObjectStore %q", request.NamespacedName.String())
|
||||
return reconcile.Result{}, *cephObjectStore, errors.Wrapf(err, "failed to get latest CephObjectStore %q", request.NamespacedName.String())
|
||||
}
|
||||
objCtx, err := NewMultisiteContext(r.context, r.clusterInfo, cephObjectStore)
|
||||
if err != nil {
|
||||
return reconcile.Result{}, cephObjectStore, errors.Wrapf(err, "failed to check for object buckets. failed to get object context")
|
||||
return reconcile.Result{}, *cephObjectStore, errors.Wrapf(err, "failed to check for object buckets. failed to get object context")
|
||||
}
|
||||
opsCtx, err := NewMultisiteAdminOpsContext(objCtx, &cephObjectStore.Spec)
|
||||
if err != nil {
|
||||
return reconcile.Result{}, cephObjectStore, errors.Wrapf(err, "failed to check for object buckets. failed to get admin ops API context")
|
||||
return reconcile.Result{}, *cephObjectStore, errors.Wrapf(err, "failed to check for object buckets. failed to get admin ops API context")
|
||||
}
|
||||
deps, err := cephObjectStoreDependents(r.context, r.clusterInfo, cephObjectStore, objCtx, opsCtx)
|
||||
if err != nil {
|
||||
return reconcile.Result{}, cephObjectStore, err
|
||||
return reconcile.Result{}, *cephObjectStore, err
|
||||
}
|
||||
if !deps.Empty() {
|
||||
err := reporting.ReportDeletionBlockedDueToDependents(logger, r.client, cephObjectStore, deps)
|
||||
return opcontroller.WaitForRequeueIfFinalizerBlocked, cephObjectStore, err
|
||||
return opcontroller.WaitForRequeueIfFinalizerBlocked, *cephObjectStore, err
|
||||
}
|
||||
reporting.ReportDeletionNotBlockedDueToDependents(logger, r.client, r.recorder, cephObjectStore)
|
||||
|
||||
@@ -283,18 +284,18 @@ func (r *ReconcileCephObjectStore) reconcile(request reconcile.Request) (reconci
|
||||
// Remove finalizer
|
||||
err = opcontroller.RemoveFinalizer(r.opManagerContext, r.client, cephObjectStore)
|
||||
if err != nil {
|
||||
return reconcile.Result{}, cephObjectStore, errors.Wrap(err, "failed to remove finalizer")
|
||||
return reconcile.Result{}, *cephObjectStore, errors.Wrap(err, "failed to remove finalizer")
|
||||
}
|
||||
|
||||
// Return and do not requeue. Successful deletion.
|
||||
return reconcile.Result{}, cephObjectStore, nil
|
||||
return reconcile.Result{}, *cephObjectStore, nil
|
||||
}
|
||||
|
||||
if cephObjectStore.Spec.IsExternal() {
|
||||
// Check the ceph version of the running monitors
|
||||
desiredCephVersion, err := cephclient.LeastUptodateDaemonVersion(r.context, r.clusterInfo, config.MonType)
|
||||
if err != nil {
|
||||
return reconcile.Result{}, nil, errors.Wrapf(err, "failed to retrieve current ceph %q version", config.MonType)
|
||||
return reconcile.Result{}, *cephObjectStore, errors.Wrapf(err, "failed to retrieve current ceph %q version", config.MonType)
|
||||
}
|
||||
r.clusterInfo.CephVersion = desiredCephVersion
|
||||
} else {
|
||||
@@ -312,9 +313,9 @@ func (r *ReconcileCephObjectStore) reconcile(request reconcile.Request) (reconci
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), opcontroller.UninitializedCephConfigError) {
|
||||
logger.Info(opcontroller.OperatorNotInitializedMessage)
|
||||
return opcontroller.WaitForRequeueIfOperatorNotInitialized, cephObjectStore, nil
|
||||
return opcontroller.WaitForRequeueIfOperatorNotInitialized, *cephObjectStore, nil
|
||||
}
|
||||
return reconcile.Result{}, cephObjectStore, errors.Wrap(err, "failed to detect running and desired ceph version")
|
||||
return reconcile.Result{}, *cephObjectStore, errors.Wrap(err, "failed to detect running and desired ceph version")
|
||||
}
|
||||
|
||||
// If the version of the Ceph monitor differs from the CephCluster CR image version we assume
|
||||
@@ -323,7 +324,7 @@ func (r *ReconcileCephObjectStore) reconcile(request reconcile.Request) (reconci
|
||||
if !reflect.DeepEqual(*runningCephVersion, *desiredCephVersion) {
|
||||
// Upgrade is in progress, let's wait for the mons to be done
|
||||
return opcontroller.WaitForRequeueIfCephClusterIsUpgrading,
|
||||
cephObjectStore,
|
||||
*cephObjectStore,
|
||||
opcontroller.ErrorCephUpgradingRequeue(desiredCephVersion, runningCephVersion)
|
||||
}
|
||||
r.clusterInfo.CephVersion = *desiredCephVersion
|
||||
@@ -331,17 +332,17 @@ func (r *ReconcileCephObjectStore) reconcile(request reconcile.Request) (reconci
|
||||
|
||||
// validate the store settings
|
||||
if err := r.validateStore(cephObjectStore); err != nil {
|
||||
return reconcile.Result{}, cephObjectStore, errors.Wrapf(err, "invalid object store %q arguments", cephObjectStore.Name)
|
||||
return reconcile.Result{}, *cephObjectStore, errors.Wrapf(err, "invalid object store %q arguments", cephObjectStore.Name)
|
||||
}
|
||||
|
||||
// CREATE/UPDATE
|
||||
_, err = r.reconcileCreateObjectStore(cephObjectStore, request.NamespacedName, cephCluster.Spec)
|
||||
if err != nil && kerrors.IsNotFound(err) {
|
||||
logger.Info(opcontroller.OperatorNotInitializedMessage)
|
||||
return opcontroller.WaitForRequeueIfOperatorNotInitialized, cephObjectStore, nil
|
||||
return opcontroller.WaitForRequeueIfOperatorNotInitialized, *cephObjectStore, nil
|
||||
} else if err != nil {
|
||||
result, err := r.setFailedStatus(request.NamespacedName, "failed to create object store deployments", err)
|
||||
return result, cephObjectStore, err
|
||||
return result, *cephObjectStore, err
|
||||
}
|
||||
|
||||
// Set Progressing status, we are done reconciling, the health check go routine will update the status
|
||||
@@ -349,7 +350,7 @@ func (r *ReconcileCephObjectStore) reconcile(request reconcile.Request) (reconci
|
||||
|
||||
// Return and do not requeue
|
||||
logger.Debug("done reconciling")
|
||||
return reconcile.Result{}, cephObjectStore, nil
|
||||
return reconcile.Result{}, *cephObjectStore, nil
|
||||
}
|
||||
|
||||
func (r *ReconcileCephObjectStore) reconcileCreateObjectStore(cephObjectStore *cephv1.CephObjectStore, namespacedName types.NamespacedName, cluster cephv1.ClusterSpec) (reconcile.Result, error) {
|
||||
|
||||
@@ -112,10 +112,10 @@ func (r *ReconcileNotifications) Reconcile(context context.Context, request reco
|
||||
logger.Errorf("failed to reconcile %v", err)
|
||||
}
|
||||
|
||||
return reporting.ReportReconcileResult(logger, r.recorder, notification, reconcileResponse, err)
|
||||
return reporting.ReportReconcileResult(logger, r.recorder, request, ¬ification, reconcileResponse, err)
|
||||
}
|
||||
|
||||
func (r *ReconcileNotifications) reconcile(request reconcile.Request) (reconcile.Result, *cephv1.CephBucketNotification, error) {
|
||||
func (r *ReconcileNotifications) reconcile(request reconcile.Request) (reconcile.Result, cephv1.CephBucketNotification, error) {
|
||||
// fetch the CephBucketNotification instance
|
||||
notification := &cephv1.CephBucketNotification{ObjectMeta: metav1.ObjectMeta{Name: request.Name, Namespace: request.Namespace}}
|
||||
bnName := request.NamespacedName
|
||||
@@ -124,33 +124,33 @@ func (r *ReconcileNotifications) reconcile(request reconcile.Request) (reconcile
|
||||
if err != nil {
|
||||
if kerrors.IsNotFound(err) {
|
||||
logger.Debugf("CephBucketNotification %q resource not found. Ignoring since resource must be deleted.", bnName)
|
||||
return reconcile.Result{}, notification, nil
|
||||
return reconcile.Result{}, *notification, nil
|
||||
}
|
||||
// Error reading the object - requeue the request.
|
||||
return reconcile.Result{}, notification, errors.Wrapf(err, "failed to retrieve CephBucketNotification %q", bnName)
|
||||
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", bnName)
|
||||
// Return and do not requeue. Successful deletion.
|
||||
return reconcile.Result{}, notification, 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 {
|
||||
return waitForRequeueIfTopicNotReady, notification, errors.Wrapf(err, "topic %q not provisioned yet", topicName)
|
||||
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, notification, 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, notification, errors.New("cluster is not ready")
|
||||
return opcontroller.WaitForRequeueIfCephClusterNotReady, *notification, errors.New("cluster is not ready")
|
||||
}
|
||||
|
||||
// fetch all OBCs that has a label matching this CephBucketNotification
|
||||
@@ -161,30 +161,30 @@ func (r *ReconcileNotifications) reconcile(request reconcile.Request) (reconcile
|
||||
obcList := &bktv1alpha1.ObjectBucketClaimList{}
|
||||
err = r.client.List(r.opManagerContext, obcList, namespaceListOpt, labelListOpt)
|
||||
if err != nil {
|
||||
return reconcile.Result{}, notification, 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{}, notification, 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 == "" {
|
||||
return waitForRequeueIfObjectBucketNotReady, notification, errors.Errorf("ObjectBucketClaim %q did not create the bucket yet",
|
||||
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: notification.Namespace, Name: obc.Spec.ObjectBucketName}
|
||||
if err := r.client.Get(r.opManagerContext, bucketName, &ob); err != nil {
|
||||
return reconcile.Result{}, notification, 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{}, notification, 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{}, notification, err
|
||||
return reconcile.Result{}, *notification, err
|
||||
}
|
||||
|
||||
err = createNotificationFunc(
|
||||
@@ -201,12 +201,12 @@ func (r *ReconcileNotifications) reconcile(request reconcile.Request) (reconcile
|
||||
notification,
|
||||
)
|
||||
if err != nil {
|
||||
return reconcile.Result{}, notification, errors.Wrapf(err, "failed to provision notification for ObjectBucketClaims %q", 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{}, notification, nil
|
||||
return reconcile.Result{}, *notification, nil
|
||||
}
|
||||
|
||||
func getCephObjectStoreName(ob bktv1alpha1.ObjectBucket) (types.NamespacedName, error) {
|
||||
|
||||
@@ -182,7 +182,7 @@ func (r *ReconcileOBCLabels) reconcile(request reconcile.Request) (reconcile.Res
|
||||
// add new notifications to the list
|
||||
for _, label := range labelList {
|
||||
reconcileResponse, notification, err := r.addNewNotification(p, ob, label, objectStoreName, obc.Namespace)
|
||||
_, _ = reporting.ReportReconcileResult(logger, r.recorder, notification, reconcileResponse, err)
|
||||
_, _ = reporting.ReportReconcileResult(logger, r.recorder, request, ¬ification, reconcileResponse, err)
|
||||
if err != nil {
|
||||
return reconcileResponse, err
|
||||
}
|
||||
@@ -190,7 +190,7 @@ func (r *ReconcileOBCLabels) reconcile(request reconcile.Request) (reconcile.Res
|
||||
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) {
|
||||
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}
|
||||
@@ -198,28 +198,28 @@ func (r *ReconcileOBCLabels) addNewNotification(p provisioner, ob bktv1alpha1.Ob
|
||||
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 waitForRequeueIfNotificationNotReady, *notification, errors.Wrapf(err, "CephBucketNotification %q not provisioned yet", bnName)
|
||||
}
|
||||
return reconcile.Result{}, notification, errors.Wrapf(err, "failed to retrieve CephBucketNotification %q", 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)
|
||||
return waitForRequeueIfTopicNotReady, *notification, errors.Wrapf(err, "topic %q not provisioned yet", topicName)
|
||||
}
|
||||
|
||||
if err = validateObjectStoreName(bucketTopic, objectStoreName); err != nil {
|
||||
return reconcile.Result{}, notification, err
|
||||
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)
|
||||
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
|
||||
return reconcile.Result{}, *notification, nil
|
||||
}
|
||||
@@ -20,6 +20,7 @@ package reporting
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"reflect"
|
||||
|
||||
"github.com/coreos/pkg/capnslog"
|
||||
"github.com/pkg/errors"
|
||||
@@ -33,25 +34,109 @@ import (
|
||||
"sigs.k8s.io/controller-runtime/pkg/reconcile"
|
||||
)
|
||||
|
||||
const unknownKind = "<UnknownObjectKind>"
|
||||
|
||||
// an object of a given type that has a nil reference is not the same as obj==nil (untyped nil)
|
||||
// (e.g., var cluster cephv1.CephCluster = nil ), so we must also check for nil via reflection
|
||||
func objIsNil(obj client.Object) bool {
|
||||
return obj == nil || reflect.ValueOf(obj).IsNil()
|
||||
}
|
||||
|
||||
// get the kind through the object API, but if that is empty, make a best guess via golang reflection
|
||||
func objKindOrBestGuess(obj client.Object) string {
|
||||
// can't get any type info from an untyped nil
|
||||
if obj == nil {
|
||||
return unknownKind
|
||||
}
|
||||
|
||||
if !reflect.ValueOf(obj).IsNil() {
|
||||
kind := obj.GetObjectKind().GroupVersionKind().Kind
|
||||
if kind != "" {
|
||||
return kind
|
||||
}
|
||||
}
|
||||
|
||||
// typed nil, or object's typemeta is empty
|
||||
t := reflect.TypeOf(obj)
|
||||
if t.Kind() == reflect.Ptr {
|
||||
return t.Elem().Name()
|
||||
}
|
||||
return t.Name()
|
||||
}
|
||||
|
||||
func copyObject(obj client.Object) client.Object {
|
||||
if obj == nil {
|
||||
return nil // cannot copy nil object
|
||||
}
|
||||
|
||||
if !reflect.ValueOf(obj).IsNil() {
|
||||
return obj.DeepCopyObject().(client.Object) // deep copy the object if it's non-nil
|
||||
}
|
||||
|
||||
// Otherwise, it is a nil object, but it has a type. We can use reflection to make an empty
|
||||
// object to use as the copy in this case.
|
||||
var nonNilCopy client.Object
|
||||
t := reflect.TypeOf(obj)
|
||||
if t.Kind() == reflect.Ptr {
|
||||
innerType := t.Elem()
|
||||
newObj := reflect.New(innerType)
|
||||
nonNilCopy = newObj.Interface().(client.Object)
|
||||
} else {
|
||||
newObj := reflect.Zero(t)
|
||||
nonNilCopy = newObj.Interface().(client.Object)
|
||||
}
|
||||
|
||||
return nonNilCopy
|
||||
}
|
||||
|
||||
// ReportReconcileResult will report the result of an object's reconcile in 2 ways:
|
||||
// 1. to the given logger
|
||||
// 2. as an event on the object (via the given event recorder)
|
||||
// The results of the object's reconcile should include the object, the reconcile response, and the
|
||||
// error returned by the reconcile.
|
||||
//
|
||||
// The results of the object's reconcile should include the object (NEVER NIL),
|
||||
// the reconcile response, and the error returned by the reconcile.
|
||||
//
|
||||
// The reconcile request is used to reference a given object that doesn't have a name.
|
||||
//
|
||||
// The function is designed to return the appropriate values needed for the controller-runtime
|
||||
// framework's Reconcile() method.
|
||||
func ReportReconcileResult(logger *capnslog.PackageLogger, recorder record.EventRecorder,
|
||||
obj client.Object, reconcileResponse reconcile.Result, err error,
|
||||
func ReportReconcileResult(
|
||||
logger *capnslog.PackageLogger,
|
||||
recorder record.EventRecorder,
|
||||
reconcileRequest reconcile.Request,
|
||||
obj client.Object,
|
||||
reconcileResponse reconcile.Result,
|
||||
err error,
|
||||
) (reconcile.Result, error) {
|
||||
kind := obj.GetObjectKind().GroupVersionKind().Kind
|
||||
nsName := fmt.Sprintf("%s/%s", obj.GetNamespace(), obj.GetName())
|
||||
kind := objKindOrBestGuess(obj)
|
||||
|
||||
if objIsNil(obj) {
|
||||
logger.Errorf("object associated with reconcile request %s %q should not be nil", kind, reconcileRequest)
|
||||
}
|
||||
|
||||
objCopy := copyObject(obj)
|
||||
|
||||
// If object is empty, this may be because (a) the object was deleted and so the reconciler only
|
||||
// had an empty object, (b)) the api server didn't give the object, or (c)the reconciler
|
||||
// returned nil accidentally. The object needs full metadata in order to have an event
|
||||
// associated with it, but even with an empty object we can at least create an event that
|
||||
// references the namespaced name of the object, even if event won't show up in the output of
|
||||
// `kubectl describe object`.
|
||||
if objCopy != nil && objCopy.GetName() == "" {
|
||||
objCopy.SetName(reconcileRequest.Name)
|
||||
objCopy.SetNamespace(reconcileRequest.Namespace)
|
||||
}
|
||||
|
||||
nsName := reconcileRequest.NamespacedName.String()
|
||||
|
||||
if err != nil {
|
||||
errorMsg := fmt.Sprintf("failed to reconcile %s %q. %v", kind, nsName, err)
|
||||
|
||||
// 1. log
|
||||
logger.Errorf("failed to reconcile %s %q. %v", kind, nsName, err)
|
||||
logger.Errorf(errorMsg)
|
||||
|
||||
// 2. event
|
||||
recorder.Event(obj, corev1.EventTypeWarning, string(cephv1.ReconcileFailed), err.Error())
|
||||
recorder.Event(objCopy, corev1.EventTypeWarning, string(cephv1.ReconcileFailed), errorMsg)
|
||||
|
||||
if !reconcileResponse.IsZero() {
|
||||
// The framework will requeue immediately if there is an error. If we get an error with
|
||||
@@ -67,7 +152,7 @@ func ReportReconcileResult(logger *capnslog.PackageLogger, recorder record.Event
|
||||
logger.Debug(successMsg)
|
||||
|
||||
// 2. event
|
||||
recorder.Event(obj, corev1.EventTypeNormal, string(cephv1.ReconcileSucceeded), successMsg)
|
||||
recorder.Event(objCopy, corev1.EventTypeNormal, string(cephv1.ReconcileSucceeded), successMsg)
|
||||
}
|
||||
|
||||
return reconcileResponse, err
|
||||
|
||||
@@ -0,0 +1,188 @@
|
||||
/*
|
||||
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.
|
||||
*/
|
||||
|
||||
// Reporting focuses on reporting Events, Status Conditions, and the like to users.
|
||||
package reporting
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/coreos/pkg/capnslog"
|
||||
"github.com/pkg/errors"
|
||||
cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"
|
||||
"github.com/stretchr/testify/assert"
|
||||
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/reconcile"
|
||||
)
|
||||
|
||||
func Test_objKindOrBestGuess(t *testing.T) {
|
||||
var nilCephCluster *cephv1.CephCluster = nil
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
inObj client.Object
|
||||
want string
|
||||
}{
|
||||
{"CephCluster with API", &cephv1.CephCluster{
|
||||
TypeMeta: metav1.TypeMeta{
|
||||
Kind: "CephCluster",
|
||||
},
|
||||
}, "CephCluster"},
|
||||
{"CephCluster by inference", &cephv1.CephCluster{}, "CephCluster"},
|
||||
{"CephCluster with wrong API", &cephv1.CephCluster{
|
||||
TypeMeta: metav1.TypeMeta{
|
||||
Kind: "WRONG",
|
||||
},
|
||||
}, "WRONG"},
|
||||
{"untyped nil", nil, unknownKind},
|
||||
{"nil CephCluster", nilCephCluster, "CephCluster"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert.Equal(t, tt.want, objKindOrBestGuess(tt.inObj))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestReportReconcileResult(t *testing.T) {
|
||||
setupTest := func() (*capnslog.PackageLogger, *bytes.Buffer, *record.FakeRecorder) {
|
||||
logBuf := bytes.NewBuffer([]byte{})
|
||||
logFmt := capnslog.NewLogFormatter(logBuf, "", 0)
|
||||
capnslog.SetFormatter(logFmt)
|
||||
logger := capnslog.NewPackageLogger("github.com/rook/rook", "")
|
||||
capnslog.SetGlobalLogLevel(capnslog.TRACE)
|
||||
|
||||
recorder := record.NewFakeRecorder(3)
|
||||
|
||||
return logger, logBuf, recorder
|
||||
}
|
||||
|
||||
name := "my-cluster"
|
||||
namespace := "rook-ceph"
|
||||
|
||||
cephCluster := &cephv1.CephCluster{
|
||||
TypeMeta: metav1.TypeMeta{
|
||||
Kind: "CephCluster",
|
||||
APIVersion: cephv1.SchemeGroupVersion.String(),
|
||||
},
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: name,
|
||||
Namespace: namespace,
|
||||
},
|
||||
}
|
||||
|
||||
reconcileRequest := reconcile.Request{NamespacedName: types.NamespacedName{Name: name, Namespace: namespace}}
|
||||
|
||||
successMsg := `successfully configured CephCluster "rook-ceph/my-cluster"`
|
||||
successEvent := `Normal ReconcileSucceeded ` + successMsg
|
||||
|
||||
fakeErr := errors.New("fake-err")
|
||||
errorMsg := `failed to reconcile CephCluster "rook-ceph/my-cluster". fake-err`
|
||||
errorEvent := `Warning ReconcileFailed ` + errorMsg
|
||||
|
||||
t.Run("successful reconcile", func(t *testing.T) {
|
||||
logger, logBuf, recorder := setupTest()
|
||||
|
||||
result, err := ReportReconcileResult(logger, recorder, reconcileRequest,
|
||||
cephCluster, reconcile.Result{}, nil)
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, result.IsZero())
|
||||
assert.Equal(t, successMsg, strings.TrimSpace(logBuf.String()))
|
||||
assert.Len(t, recorder.Events, 1)
|
||||
assert.Equal(t, successEvent, <-recorder.Events)
|
||||
})
|
||||
|
||||
t.Run("reconcile with error", func(t *testing.T) {
|
||||
logger, logBuf, recorder := setupTest()
|
||||
|
||||
result, err := ReportReconcileResult(logger, recorder, reconcileRequest,
|
||||
cephCluster, reconcile.Result{}, fakeErr)
|
||||
assert.Error(t, err)
|
||||
assert.True(t, result.IsZero())
|
||||
assert.Equal(t, errorMsg, strings.TrimSpace(logBuf.String()))
|
||||
assert.Len(t, recorder.Events, 1)
|
||||
assert.Equal(t, errorEvent, <-recorder.Events)
|
||||
})
|
||||
|
||||
t.Run("reconcile with requeue", func(t *testing.T) {
|
||||
logger, logBuf, recorder := setupTest()
|
||||
|
||||
inResult := reconcile.Result{Requeue: true}
|
||||
result, err := ReportReconcileResult(logger, recorder, reconcileRequest,
|
||||
cephCluster, inResult, nil)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, inResult, result)
|
||||
assert.Equal(t, successMsg, strings.TrimSpace(logBuf.String()))
|
||||
assert.Len(t, recorder.Events, 1)
|
||||
assert.Equal(t, successEvent, <-recorder.Events)
|
||||
})
|
||||
|
||||
t.Run("failed reconcile with requeue", func(t *testing.T) {
|
||||
logger, logBuf, recorder := setupTest()
|
||||
|
||||
inResult := reconcile.Result{RequeueAfter: 567 * time.Second}
|
||||
result, err := ReportReconcileResult(logger, recorder, reconcileRequest,
|
||||
cephCluster, inResult, fakeErr)
|
||||
// this is the trick: when we get a request to requeue after a time _AND_ an error as input,
|
||||
// we do not propagate the error to the output, _BUT_ we still record the error in log/event.
|
||||
// this is so we can still report errors to users, but the controller-runtime will still
|
||||
// obey our desire to reconcile with a delay
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, inResult, result)
|
||||
assert.Equal(t, errorMsg, strings.TrimSpace(logBuf.String()))
|
||||
assert.Len(t, recorder.Events, 1)
|
||||
assert.Equal(t, errorEvent, <-recorder.Events)
|
||||
})
|
||||
|
||||
t.Run("success with empty object", func(t *testing.T) {
|
||||
// success returning an empty object might be because a reconcile request is for an
|
||||
// already-deleted object
|
||||
|
||||
logger, logBuf, recorder := setupTest()
|
||||
|
||||
result, err := ReportReconcileResult(logger, recorder, reconcileRequest,
|
||||
&cephv1.CephCluster{}, reconcile.Result{}, nil)
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, result.IsZero())
|
||||
assert.Equal(t, successMsg, strings.TrimSpace(logBuf.String()))
|
||||
assert.Len(t, recorder.Events, 1)
|
||||
assert.Equal(t, successEvent, <-recorder.Events)
|
||||
})
|
||||
|
||||
t.Run("failure with nil object", func(t *testing.T) {
|
||||
// failure with a nil object might be because a reconcile returned nil for the object after
|
||||
// an error accidentally
|
||||
|
||||
logger, logBuf, recorder := setupTest()
|
||||
|
||||
var obj *cephv1.CephCluster = nil
|
||||
|
||||
result, err := ReportReconcileResult(logger, recorder, reconcileRequest,
|
||||
obj, reconcile.Result{}, fakeErr)
|
||||
assert.Error(t, err)
|
||||
assert.True(t, result.IsZero())
|
||||
assert.Contains(t, logBuf.String(), `object associated with reconcile request CephCluster "rook-ceph/my-cluster" should not be nil`)
|
||||
assert.Contains(t, logBuf.String(), errorMsg)
|
||||
assert.Len(t, recorder.Events, 1)
|
||||
assert.Equal(t, errorEvent, <-recorder.Events)
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user