2020-06-16 09:49:58 +02:00
|
|
|
/*
|
|
|
|
|
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 object
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"
|
2021-06-08 16:48:01 -06:00
|
|
|
"github.com/rook/rook/pkg/operator/ceph/reporting"
|
2022-03-16 19:58:35 +05:30
|
|
|
"github.com/rook/rook/pkg/operator/k8sutil"
|
2020-06-16 09:49:58 +02:00
|
|
|
kerrors "k8s.io/apimachinery/pkg/api/errors"
|
|
|
|
|
"k8s.io/apimachinery/pkg/types"
|
2021-06-08 16:48:01 -06:00
|
|
|
"k8s.io/client-go/util/retry"
|
2020-06-16 09:49:58 +02:00
|
|
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
|
|
|
|
"sigs.k8s.io/controller-runtime/pkg/reconcile"
|
|
|
|
|
)
|
|
|
|
|
|
2022-03-16 19:58:35 +05:30
|
|
|
func (r *ReconcileCephObjectStore) setFailedStatus(observedGeneration int64, name types.NamespacedName, errMessage string, err error) (reconcile.Result, error) {
|
2022-02-21 13:03:51 +05:30
|
|
|
updateStatus(r.opManagerContext, observedGeneration, r.client, name, cephv1.ConditionFailure, map[string]string{})
|
2020-06-16 09:49:58 +02:00
|
|
|
return reconcile.Result{}, errors.Wrapf(err, "%s", errMessage)
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-07 10:17:24 +02:00
|
|
|
// updateStatus updates an object with a given status
|
2022-02-21 13:03:51 +05:30
|
|
|
func updateStatus(ctx context.Context, observedGeneration int64, client client.Client, namespacedName types.NamespacedName, status cephv1.ConditionType, info map[string]string) {
|
2021-06-08 16:48:01 -06:00
|
|
|
// Updating the status is important to users, but we can still keep operating if there is a
|
|
|
|
|
// failure. Retry a few times to give it our best effort attempt.
|
|
|
|
|
err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
|
|
|
|
|
objectStore := &cephv1.CephObjectStore{}
|
2022-02-21 13:03:51 +05:30
|
|
|
if err := client.Get(ctx, namespacedName, objectStore); err != nil {
|
2021-06-08 16:48:01 -06:00
|
|
|
if kerrors.IsNotFound(err) {
|
|
|
|
|
logger.Debug("CephObjectStore resource not found. Ignoring since object must be deleted.")
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return errors.Wrapf(err, "failed to retrieve object store %q to update status to %q", namespacedName.String(), status)
|
2020-06-16 09:49:58 +02:00
|
|
|
}
|
2021-06-08 16:48:01 -06:00
|
|
|
if objectStore.Status == nil {
|
|
|
|
|
objectStore.Status = &cephv1.ObjectStoreStatus{}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if objectStore.Status.Phase == cephv1.ConditionDeleting {
|
|
|
|
|
logger.Debugf("object store %q status not updated to %q because it is deleting", namespacedName.String(), status)
|
|
|
|
|
return nil // do not transition to other statuses once deletion begins
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
objectStore.Status.Phase = status
|
|
|
|
|
objectStore.Status.Info = info
|
2022-03-16 19:58:35 +05:30
|
|
|
if observedGeneration != k8sutil.ObservedGenerationNotAvailable {
|
|
|
|
|
objectStore.Status.ObservedGeneration = observedGeneration
|
|
|
|
|
}
|
2021-06-08 16:48:01 -06:00
|
|
|
|
|
|
|
|
if err := reporting.UpdateStatus(client, objectStore); err != nil {
|
|
|
|
|
return errors.Wrapf(err, "failed to set object store %q status to %q", namespacedName.String(), status)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
logger.Error(err)
|
2020-06-16 09:49:58 +02:00
|
|
|
}
|
|
|
|
|
|
2021-06-08 16:48:01 -06:00
|
|
|
logger.Debugf("object store %q status updated to %q", namespacedName.String(), status)
|
2020-06-16 09:49:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// updateStatusBucket updates an object with a given status
|
2022-02-21 13:03:51 +05:30
|
|
|
func updateStatusBucket(ctx context.Context, client client.Client, name types.NamespacedName, status cephv1.ConditionType, details string) {
|
2021-06-08 16:48:01 -06:00
|
|
|
// Updating the status is important to users, but we can still keep operating if there is a
|
|
|
|
|
// failure. Retry a few times to give it our best effort attempt.
|
|
|
|
|
err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
|
|
|
|
|
objectStore := &cephv1.CephObjectStore{}
|
2022-02-21 13:03:51 +05:30
|
|
|
if err := client.Get(ctx, name, objectStore); err != nil {
|
2021-06-08 16:48:01 -06:00
|
|
|
if kerrors.IsNotFound(err) {
|
|
|
|
|
logger.Debug("CephObjectStore resource not found. Ignoring since object must be deleted.")
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return errors.Wrapf(err, "failed to retrieve object store %q to update status to %v", name.String(), status)
|
2020-06-16 09:49:58 +02:00
|
|
|
}
|
2021-06-08 16:48:01 -06:00
|
|
|
if objectStore.Status == nil {
|
|
|
|
|
objectStore.Status = &cephv1.ObjectStoreStatus{}
|
|
|
|
|
}
|
|
|
|
|
objectStore.Status.BucketStatus = toCustomResourceStatus(objectStore.Status.BucketStatus, details, status)
|
|
|
|
|
|
2021-09-20 17:32:16 -06:00
|
|
|
// do not transition to other statuses once deletion begins
|
2021-06-08 16:48:01 -06:00
|
|
|
if objectStore.Status.Phase != cephv1.ConditionDeleting {
|
|
|
|
|
objectStore.Status.Phase = status
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-20 17:32:16 -06:00
|
|
|
// but we still need to update the health checker status
|
2021-06-08 16:48:01 -06:00
|
|
|
if err := reporting.UpdateStatus(client, objectStore); err != nil {
|
|
|
|
|
return errors.Wrapf(err, "failed to set object store %q status to %v", name.String(), status)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
logger.Error(err)
|
2020-06-16 09:49:58 +02:00
|
|
|
}
|
|
|
|
|
|
2021-06-08 16:48:01 -06:00
|
|
|
logger.Debugf("object store %q status updated to %v", name.String(), status)
|
2020-06-16 09:49:58 +02:00
|
|
|
}
|
2020-07-07 10:17:24 +02:00
|
|
|
|
|
|
|
|
func buildStatusInfo(cephObjectStore *cephv1.CephObjectStore) map[string]string {
|
|
|
|
|
m := make(map[string]string)
|
2020-07-07 10:18:40 +02:00
|
|
|
|
2020-08-05 23:44:05 +05:30
|
|
|
if cephObjectStore.Spec.Gateway.SecurePort != 0 && cephObjectStore.Spec.Gateway.Port != 0 {
|
2021-05-26 17:54:23 +02:00
|
|
|
m["secureEndpoint"] = BuildDNSEndpoint(BuildDomainName(cephObjectStore.Name, cephObjectStore.Namespace), cephObjectStore.Spec.Gateway.SecurePort, true)
|
|
|
|
|
m["endpoint"] = BuildDNSEndpoint(BuildDomainName(cephObjectStore.Name, cephObjectStore.Namespace), cephObjectStore.Spec.Gateway.Port, false)
|
2020-08-05 23:44:05 +05:30
|
|
|
} else if cephObjectStore.Spec.Gateway.SecurePort != 0 {
|
2021-05-26 17:54:23 +02:00
|
|
|
m["endpoint"] = BuildDNSEndpoint(BuildDomainName(cephObjectStore.Name, cephObjectStore.Namespace), cephObjectStore.Spec.Gateway.SecurePort, true)
|
2020-08-05 23:44:05 +05:30
|
|
|
} else {
|
2021-05-26 17:54:23 +02:00
|
|
|
m["endpoint"] = BuildDNSEndpoint(BuildDomainName(cephObjectStore.Name, cephObjectStore.Namespace), cephObjectStore.Spec.Gateway.Port, false)
|
2020-07-07 10:18:40 +02:00
|
|
|
}
|
2020-07-07 10:17:24 +02:00
|
|
|
|
|
|
|
|
return m
|
|
|
|
|
}
|