Files
my-rook-config/pkg/operator/ceph/pool/status.go
T
Divyansh Kamboj 9008409f87 core: add context parameter to functions
This commit adds context parameter to various functions, and remove the
usage of context.TODO.

Closes: https://github.com/rook/rook/issues/8701
Signed-off-by: Divyansh Kamboj <dkamboj@redhat.com>
2022-03-22 08:19:07 +05:30

133 lines
5.2 KiB
Go

/*
Copyright 2020 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.
*/
// Package pool to manage a rook pool.
package pool
import (
"context"
"time"
cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"
"github.com/rook/rook/pkg/operator/ceph/reporting"
"github.com/rook/rook/pkg/operator/k8sutil"
kerrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// updateStatus updates a pool CR with the given status
func updateStatus(ctx context.Context, client client.Client, poolName types.NamespacedName, status cephv1.ConditionType, info map[string]string, observedGeneration int64) {
pool := &cephv1.CephBlockPool{}
err := client.Get(ctx, poolName, pool)
if err != nil {
if kerrors.IsNotFound(err) {
logger.Debug("CephBlockPool resource not found. Ignoring since object must be deleted.")
return
}
logger.Warningf("failed to retrieve pool %q to update status to %q. %v", poolName, status, err)
return
}
if pool.Status == nil {
pool.Status = &cephv1.CephBlockPoolStatus{}
}
pool.Status.Phase = status
pool.Status.Info = info
if observedGeneration != k8sutil.ObservedGenerationNotAvailable {
pool.Status.ObservedGeneration = observedGeneration
}
if err := reporting.UpdateStatus(client, pool); err != nil {
logger.Warningf("failed to set pool %q status to %q. %v", pool.Name, status, err)
return
}
logger.Debugf("pool %q status updated to %q", poolName, status)
}
// updateStatusBucket updates an object with a given status
func (c *mirrorChecker) updateStatusMirroring(mirrorStatus *cephv1.PoolMirroringStatusSummarySpec, mirrorInfo *cephv1.PoolMirroringInfo, snapSchedStatus []cephv1.SnapshotSchedulesSpec, details string) {
blockPool := &cephv1.CephBlockPool{}
if err := c.client.Get(c.clusterInfo.Context, c.namespacedName, blockPool); err != nil {
if kerrors.IsNotFound(err) {
logger.Debug("CephBlockPool resource not found. Ignoring since object must be deleted.")
return
}
logger.Warningf("failed to retrieve ceph block pool %q to update mirroring status. %v", c.namespacedName.Name, err)
return
}
if blockPool.Status == nil {
blockPool.Status = &cephv1.CephBlockPoolStatus{}
}
// Update the CephBlockPool CR status field
blockPool.Status.MirroringStatus, blockPool.Status.MirroringInfo, blockPool.Status.SnapshotScheduleStatus = toCustomResourceStatus(blockPool.Status.MirroringStatus, mirrorStatus, blockPool.Status.MirroringInfo, mirrorInfo, blockPool.Status.SnapshotScheduleStatus, snapSchedStatus, details)
if err := reporting.UpdateStatus(c.client, blockPool); err != nil {
logger.Errorf("failed to set ceph block pool %q mirroring status. %v", c.namespacedName.Name, err)
return
}
logger.Debugf("ceph block pool %q mirroring status updated", c.namespacedName.Name)
}
func toCustomResourceStatus(currentStatus *cephv1.MirroringStatusSpec, mirroringStatus *cephv1.PoolMirroringStatusSummarySpec,
currentInfo *cephv1.MirroringInfoSpec, mirroringInfo *cephv1.PoolMirroringInfo,
currentSnapSchedStatus *cephv1.SnapshotScheduleStatusSpec, snapSchedStatus []cephv1.SnapshotSchedulesSpec,
details string) (*cephv1.MirroringStatusSpec, *cephv1.MirroringInfoSpec, *cephv1.SnapshotScheduleStatusSpec) {
mirroringStatusSpec := &cephv1.MirroringStatusSpec{}
mirroringInfoSpec := &cephv1.MirroringInfoSpec{}
snapshotScheduleStatusSpec := &cephv1.SnapshotScheduleStatusSpec{}
// mirroringStatus will be nil in case of an error to fetch it
if mirroringStatus != nil {
mirroringStatusSpec.LastChecked = time.Now().UTC().Format(time.RFC3339)
mirroringStatusSpec.Summary = mirroringStatus
}
// Always display the details, typically an error
mirroringStatusSpec.Details = details
if currentStatus != nil {
mirroringStatusSpec.LastChanged = currentStatus.LastChanged
}
// mirroringInfo will be nil in case of an error to fetch it
if mirroringInfo != nil {
mirroringInfoSpec.LastChecked = time.Now().UTC().Format(time.RFC3339)
mirroringInfoSpec.PoolMirroringInfo = mirroringInfo
}
// Always display the details, typically an error
mirroringInfoSpec.Details = details
if currentInfo != nil {
mirroringInfoSpec.LastChanged = currentInfo.LastChecked
}
// snapSchedStatus will be nil in case of an error to fetch it
if len(snapSchedStatus) != 0 {
snapshotScheduleStatusSpec.LastChecked = time.Now().UTC().Format(time.RFC3339)
snapshotScheduleStatusSpec.SnapshotSchedules = snapSchedStatus
}
// Always display the details, typically an error
snapshotScheduleStatusSpec.Details = details
if currentSnapSchedStatus != nil {
snapshotScheduleStatusSpec.LastChanged = currentSnapSchedStatus.LastChecked
}
return mirroringStatusSpec, mirroringInfoSpec, snapshotScheduleStatusSpec
}