Files
my-rook-config/pkg/operator/ceph/pool/status.go
T
Sébastien Han 31db03fece ceph: auto-gen crds
Finally! We can now stop editing manually our crds definition. Simply
run `make crds-gen`. These two files:

* `cluster/examples/kubernetes/ceph/crds.yaml`
* `cluster/charts/rook-ceph/templates/resources.yaml`

will be autogenerated for us.

We rely on the controller-gen tool, it reads our API definitions from
`pkg/apis/` and produces the CRD files accordingly.

It uses "markers" to add extra yaml fields to the CRD, for instance we
have a lot fields with:

```
nullable: true
x-kubernetes-preserve-unknown-fields: true
```

The corresponding API type markers are:

```
// +kubebuilder:pruning:PreserveUnknownFields
// +nullable
```

For more API convention see
https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#optional-vs-required
and for the markers see: https://book.kubebuilder.io/reference/markers/crd-validation.html

Signed-off-by: Sébastien Han <seb@redhat.com>
2021-03-16 09:16:24 +01:00

129 lines
5.0 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"
opcontroller "github.com/rook/rook/pkg/operator/ceph/controller"
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(client client.Client, poolName types.NamespacedName, status cephv1.ConditionType, info map[string]string) {
pool := &cephv1.CephBlockPool{}
err := client.Get(context.TODO(), 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 err := opcontroller.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(context.TODO(), 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 := opcontroller.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
}