2018-05-18 14:32:20 -07:00
/*
Copyright 2016 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.
*/
2018-09-24 11:43:34 -06:00
// Package file manages a CephFS filesystem and the required daemons.
2018-05-18 14:32:20 -07:00
package file
import (
2020-03-17 13:28:09 +01:00
"context"
2018-05-18 14:32:20 -07:00
"fmt"
"reflect"
2021-01-22 16:00:06 +01:00
"strings"
2018-05-18 14:32:20 -07:00
"github.com/coreos/pkg/capnslog"
2019-10-08 12:51:36 +02:00
"github.com/pkg/errors"
2018-12-03 15:08:29 -07:00
cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"
2019-02-14 09:16:21 -07:00
"github.com/rook/rook/pkg/clusterd"
2020-03-17 13:28:09 +01:00
cephclient "github.com/rook/rook/pkg/daemon/ceph/client"
"github.com/rook/rook/pkg/operator/ceph/cluster/mon"
2021-08-06 17:46:01 +02:00
"github.com/rook/rook/pkg/operator/ceph/config"
2025-07-16 10:52:19 +05:30
"github.com/rook/rook/pkg/operator/ceph/config/keyring"
2020-03-17 13:28:09 +01:00
opcontroller "github.com/rook/rook/pkg/operator/ceph/controller"
2021-12-09 17:16:05 +01:00
"github.com/rook/rook/pkg/operator/ceph/reporting"
2019-11-25 19:53:31 +05:30
"github.com/rook/rook/pkg/operator/k8sutil"
2025-12-03 10:35:58 -07:00
"github.com/rook/rook/pkg/util/log"
2021-06-08 16:48:01 -06:00
appsv1 "k8s.io/api/apps/v1"
2020-03-17 13:28:09 +01:00
corev1 "k8s.io/api/core/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
2018-05-18 14:32:20 -07:00
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2020-03-17 13:28:09 +01:00
"k8s.io/apimachinery/pkg/runtime"
2021-06-08 16:48:01 -06:00
"k8s.io/apimachinery/pkg/types"
2026-02-09 16:38:26 +05:30
"k8s.io/client-go/tools/events"
2020-03-17 13:28:09 +01:00
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
2018-05-18 14:32:20 -07:00
)
2020-03-17 13:28:09 +01:00
const (
controllerName = "ceph-file-controller"
)
2018-05-18 14:32:20 -07:00
2025-11-21 13:12:56 -07:00
var logger = capnslog . NewPackageLogger ( "github.com/rook/rook" , "file-controller" )
2020-03-17 13:28:09 +01:00
// List of object resources to watch by the controller
2021-01-12 13:14:07 +01:00
var objectsToWatch = [] client . Object {
2020-03-17 13:28:09 +01:00
& corev1 . Secret { TypeMeta : metav1 . TypeMeta { Kind : "Secret" , APIVersion : corev1 . SchemeGroupVersion . String ()}},
2020-03-23 15:11:52 +01:00
& appsv1 . Deployment { TypeMeta : metav1 . TypeMeta { Kind : "Deployment" , APIVersion : appsv1 . SchemeGroupVersion . String ()}},
2018-05-18 14:32:20 -07:00
}
2020-03-17 13:28:09 +01:00
// Sets the type meta for the controller main object
var controllerTypeMeta = metav1 . TypeMeta {
2025-08-28 21:31:04 +08:00
Kind : reflect . TypeFor [ cephv1 . CephFilesystem ](). Name (),
2020-03-17 13:28:09 +01:00
APIVersion : fmt . Sprintf ( "%s/%s" , cephv1 . CustomResourceGroup , cephv1 . Version ),
2018-05-18 14:32:20 -07:00
}
2021-08-06 17:46:01 +02:00
var currentAndDesiredCephVersion = opcontroller . CurrentAndDesiredCephVersion
2020-03-17 13:28:09 +01:00
// ReconcileCephFilesystem reconciles a CephFilesystem object
type ReconcileCephFilesystem struct {
2025-07-16 10:52:19 +05:30
client client . Client
2026-02-09 16:38:26 +05:30
recorder events . EventRecorder
2025-07-16 10:52:19 +05:30
scheme * runtime . Scheme
context * clusterd . Context
cephClusterSpec * cephv1 . ClusterSpec
clusterInfo * cephclient . ClusterInfo
fsContexts map [ string ] * fsHealth
opManagerContext context . Context
opConfig opcontroller . OperatorConfig
shouldRotateCephxKeys bool
2021-04-08 15:44:27 +02:00
}
type fsHealth struct {
2021-08-06 17:46:01 +02:00
internalCtx context . Context
internalCancel context . CancelFunc
started bool
2020-03-17 13:28:09 +01:00
}
// Add creates a new CephFilesystem Controller and adds it to the Manager. The Manager will set fields on the Controller
// and Start it when the Manager is Started.
2021-08-06 17:46:01 +02:00
func Add ( mgr manager . Manager , context * clusterd . Context , opManagerContext context . Context , opConfig opcontroller . OperatorConfig ) error {
2021-10-23 20:29:41 +09:00
return add ( opManagerContext , mgr , newReconciler ( mgr , context , opManagerContext , opConfig ))
2020-03-17 13:28:09 +01:00
}
// newReconciler returns a new reconcile.Reconciler
2021-08-06 17:46:01 +02:00
func newReconciler ( mgr manager . Manager , context * clusterd . Context , opManagerContext context . Context , opConfig opcontroller . OperatorConfig ) reconcile . Reconciler {
2020-03-17 13:28:09 +01:00
return & ReconcileCephFilesystem {
2021-08-06 17:46:01 +02:00
client : mgr . GetClient (),
2026-02-09 16:38:26 +05:30
recorder : mgr . GetEventRecorder ( "rook-" + controllerName ),
2021-08-06 17:46:01 +02:00
scheme : mgr . GetScheme (),
context : context ,
fsContexts : make ( map [ string ] * fsHealth ),
opManagerContext : opManagerContext ,
opConfig : opConfig ,
2018-05-18 14:32:20 -07:00
}
}
2025-03-31 08:46:53 -07:00
func watchOwnedCoreObject [ T client . Object ]( c controller . Controller , mgr manager . Manager , obj T ) error {
return c . Watch (
source . Kind (
mgr . GetCache (),
obj ,
handler . TypedEnqueueRequestForOwner [ T ](
mgr . GetScheme (),
mgr . GetRESTMapper (),
& cephv1 . CephFilesystem {},
),
opcontroller . WatchPredicateForNonCRDObject [ T ]( & cephv1 . CephFilesystem { TypeMeta : controllerTypeMeta }, mgr . GetScheme ()),
),
)
}
2021-10-23 20:29:41 +09:00
func add ( opManagerContext context . Context , mgr manager . Manager , r reconcile . Reconciler ) error {
2020-03-17 13:28:09 +01:00
// Create a new controller
c , err := controller . New ( controllerName , mgr , controller . Options { Reconciler : r })
2018-05-18 14:32:20 -07:00
if err != nil {
2020-03-17 13:28:09 +01:00
return err
2018-05-18 14:32:20 -07:00
}
2020-04-17 18:40:54 +02:00
logger . Info ( "successfully started" )
2018-05-18 14:32:20 -07:00
2020-03-17 13:28:09 +01:00
// Watch for changes on the CephFilesystem CRD object
2025-03-31 08:46:53 -07:00
err = c . Watch (
source . Kind (
mgr . GetCache (),
& cephv1 . CephFilesystem { TypeMeta : controllerTypeMeta },
& handler . TypedEnqueueRequestForObject [ * cephv1 . CephFilesystem ]{},
opcontroller . WatchControllerPredicate [ * cephv1 . CephFilesystem ]( mgr . GetScheme ()),
),
)
2020-03-17 13:28:09 +01:00
if err != nil {
return err
}
2019-04-19 14:31:38 -06:00
2020-03-17 13:28:09 +01:00
// Watch all other resources
for _ , t := range objectsToWatch {
2025-03-31 08:46:53 -07:00
err = watchOwnedCoreObject ( c , mgr , t )
2020-03-17 13:28:09 +01:00
if err != nil {
return err
}
}
2021-01-21 18:56:22 +01:00
// Build Handler function to return the list of ceph filesystems
// This is used by the watchers below
2025-03-31 08:46:53 -07:00
handlerFunc , err := opcontroller . ObjectToCRMapper [ * cephv1 . CephFilesystemList , * corev1 . ConfigMap ]( opManagerContext , mgr . GetClient (), & cephv1 . CephFilesystemList {}, mgr . GetScheme ())
2021-01-21 18:56:22 +01:00
if err != nil {
return err
}
2021-04-08 15:44:27 +02:00
// Watch for ConfigMap "rook-ceph-mon-endpoints" update and reconcile, which will reconcile update the bootstrap peer token
2025-03-31 08:46:53 -07:00
err = c . Watch (
source . Kind (
mgr . GetCache (),
& corev1 . ConfigMap { TypeMeta : metav1 . TypeMeta { Kind : "ConfigMap" , APIVersion : corev1 . SchemeGroupVersion . String ()}},
handler . TypedEnqueueRequestsFromMapFunc ( handlerFunc ),
mon . PredicateMonEndpointChanges (),
),
)
2021-04-08 15:44:27 +02:00
if err != nil {
return err
}
2020-03-17 13:28:09 +01:00
return nil
}
// Reconcile reads that state of the cluster for a CephFilesystem object and makes changes based on the state read
// and what is in the cephFilesystem.Spec
// 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.
2021-01-12 13:14:07 +01:00
func ( r * ReconcileCephFilesystem ) Reconcile ( context context . Context , request reconcile . Request ) ( reconcile . Result , error ) {
2025-07-17 13:06:51 +03:00
defer opcontroller . RecoverAndLogException ()
2020-11-06 09:46:09 +01:00
// workaround because the rook logging mechanism is not compatible with the controller-runtime logging interface
2022-03-14 10:29:25 -06:00
reconcileResponse , cephFilesystem , err := r . reconcile ( request )
2020-03-17 13:28:09 +01:00
if err != nil {
2025-12-03 10:35:58 -07:00
log . NamedError ( request . NamespacedName , logger , "failed to reconcile %v" , err )
2020-03-17 13:28:09 +01:00
}
2022-03-14 10:29:25 -06:00
return reporting . ReportReconcileResult ( logger , r . recorder , request , & cephFilesystem , reconcileResponse , err )
2020-03-17 13:28:09 +01:00
}
2022-03-14 10:29:25 -06:00
func ( r * ReconcileCephFilesystem ) reconcile ( request reconcile . Request ) ( reconcile . Result , cephv1 . CephFilesystem , error ) {
2020-03-17 13:28:09 +01:00
// Fetch the cephFilesystem instance
cephFilesystem := & cephv1 . CephFilesystem {}
2021-08-06 17:46:01 +02:00
err := r . client . Get ( r . opManagerContext , request . NamespacedName , cephFilesystem )
2020-03-17 13:28:09 +01:00
if err != nil {
if kerrors . IsNotFound ( err ) {
2025-12-03 10:35:58 -07:00
log . NamedDebug ( request . NamespacedName , logger , "cephFilesystem resource not found. Ignoring since object must be deleted." )
2021-11-03 12:56:11 -06:00
// If there was a previous error or if a user removed this resource's finalizer, it's
// possible Rook didn't clean up the monitoring routine for this resource. Ensure the
// routine is stopped when we see the resource is gone.
2021-12-13 16:07:01 -07:00
cephFilesystem . Name = request . Name
cephFilesystem . Namespace = request . Namespace
2021-11-03 12:56:11 -06:00
r . cancelMirrorMonitoring ( cephFilesystem )
2022-03-14 10:29:25 -06:00
return reconcile . Result {}, * cephFilesystem , nil
2020-03-17 13:28:09 +01:00
}
// Error reading the object - requeue the request.
2022-03-14 10:29:25 -06:00
return reconcile . Result {}, * cephFilesystem , errors . Wrap ( err , "failed to get cephFilesystem" )
2020-03-17 13:28:09 +01:00
}
2022-03-16 19:58:35 +05:30
// update observedGeneration local variable with current generation value,
2022-06-21 19:59:31 -04:00
// because generation can be changed before reconcile got completed
2022-03-16 19:58:35 +05:30
// CR status will be updated at end of reconcile, so to reflect the reconcile has finished
observedGeneration := cephFilesystem . ObjectMeta . Generation
2020-11-18 18:16:59 +01:00
// Set a finalizer so we can do cleanup before the object goes away
2025-03-21 13:48:30 -06:00
generationUpdated , err := opcontroller . AddFinalizerIfNotPresent ( r . opManagerContext , r . client , cephFilesystem )
2020-11-18 18:16:59 +01:00
if err != nil {
2022-03-14 10:29:25 -06:00
return reconcile . Result {}, * cephFilesystem , errors . Wrap ( err , "failed to add finalizer" )
2020-11-18 18:16:59 +01:00
}
2025-03-21 13:48:30 -06:00
if generationUpdated {
2025-12-03 10:35:58 -07:00
log . NamedInfo ( request . NamespacedName , logger , "reconciling the filesystem after adding finalizer" )
2025-03-21 13:48:30 -06:00
return reconcile . Result {}, * cephFilesystem , nil
}
2020-11-18 18:16:59 +01:00
2022-12-01 20:37:07 +05:30
// The CR was just created, initialize status as 'Progressing'
2020-03-17 13:28:09 +01:00
if cephFilesystem . Status == nil {
2025-07-16 10:52:19 +05:30
cephxUninitialized := keyring . UninitializedCephxStatus ()
2025-07-24 07:21:06 +05:30
updatedCephFS , err := r . updateStatus ( k8sutil . ObservedGenerationNotAvailable , request . NamespacedName , cephv1 . ConditionProgressing , nil , & cephxUninitialized )
if err != nil {
return reconcile . Result {}, * cephFilesystem , errors . Wrapf ( err , "failed to initialize cephx status for cephFileSystem %q" , request . NamespacedName )
}
2022-12-01 20:37:07 +05:30
if updatedCephFS == nil || updatedCephFS . Status == nil {
return reconcile . Result {}, * cephFilesystem , errors . Errorf ( "failed to update ceph filesystem status" )
}
cephFilesystem = updatedCephFS
2020-03-17 13:28:09 +01:00
}
// Make sure a CephCluster is present otherwise do nothing
2021-10-26 13:23:11 -06:00
cephCluster , isReadyToReconcile , cephClusterExists , reconcileResponse := opcontroller . IsReadyToReconcile ( r . opManagerContext , r . client , request . NamespacedName , controllerName )
2020-03-17 13:28:09 +01:00
if ! isReadyToReconcile {
// This handles the case where the Ceph Cluster is gone and we want to delete that CR
// We skip the deleteFilesystem() function since everything is gone already
//
// Also, only remove the finalizer if the CephCluster is gone
// If not, we should wait for it to be ready
// This handles the case where the operator is not ready to accept Ceph command but the cluster exists
if ! cephFilesystem . GetDeletionTimestamp (). IsZero () && ! cephClusterExists {
2021-11-03 12:56:11 -06:00
// don't leak the health checker routine if we are force deleting
r . cancelMirrorMonitoring ( cephFilesystem )
2020-03-17 13:28:09 +01:00
// Remove finalizer
2021-10-23 20:29:41 +09:00
err := opcontroller . RemoveFinalizer ( r . opManagerContext , r . client , cephFilesystem )
2020-03-17 13:28:09 +01:00
if err != nil {
2022-03-14 10:29:25 -06:00
return reconcile . Result {}, * cephFilesystem , errors . Wrap ( err , "failed to remove finalizer" )
2020-03-17 13:28:09 +01:00
}
// Return and do not requeue. Successful deletion.
2022-03-14 10:29:25 -06:00
return reconcile . Result {}, * cephFilesystem , nil
2020-03-17 13:28:09 +01:00
}
2022-03-14 10:29:25 -06:00
return reconcileResponse , * cephFilesystem , nil
2020-03-17 13:28:09 +01:00
}
2020-06-09 18:16:58 +02:00
r . cephClusterSpec = & cephCluster . Spec
2020-03-17 13:28:09 +01:00
2021-08-06 17:46:01 +02:00
// Initialize the contexts, they allow us to track multiple CephFilesystems in the same namespace
_ , fsContextsExists := r . fsContexts [ fsChannelKeyName ( cephFilesystem )]
if ! fsContextsExists {
internalCtx , internalCancel := context . WithCancel ( r . opManagerContext )
r . fsContexts [ fsChannelKeyName ( cephFilesystem )] = & fsHealth {
internalCtx : internalCtx ,
internalCancel : internalCancel ,
2021-04-08 15:44:27 +02:00
}
}
2020-03-17 13:28:09 +01:00
// Populate clusterInfo
// Always populate it during each reconcile
2022-09-22 14:46:51 +05:30
clusterInfo , _ , _ , err := opcontroller . LoadClusterInfo ( r . context , r . opManagerContext , request . NamespacedName . Namespace , r . cephClusterSpec )
2020-03-17 13:28:09 +01:00
if err != nil {
2022-03-14 10:29:25 -06:00
return reconcile . Result {}, * cephFilesystem , errors . Wrap ( err , "failed to populate cluster info" )
2020-03-17 13:28:09 +01:00
}
r . clusterInfo = clusterInfo
// DELETE: the CR was deleted
if ! cephFilesystem . GetDeletionTimestamp (). IsZero () {
2022-03-15 15:40:41 -06:00
deps , err := CephFilesystemDependents ( r . context , r . clusterInfo , cephFilesystem )
2021-12-09 17:16:05 +01:00
if err != nil {
2022-03-14 10:29:25 -06:00
return reconcile . Result {}, * cephFilesystem , err
2021-12-09 17:16:05 +01:00
}
if ! deps . Empty () {
2022-02-21 13:03:51 +05:30
err := reporting . ReportDeletionBlockedDueToDependents ( r . opManagerContext , logger , r . client , cephFilesystem , deps )
2022-03-14 10:29:25 -06:00
return opcontroller . WaitForRequeueIfFinalizerBlocked , * cephFilesystem , err
2021-12-09 17:16:05 +01:00
}
2022-02-21 13:03:51 +05:30
reporting . ReportDeletionNotBlockedDueToDependents ( r . opManagerContext , logger , r . client , r . recorder , cephFilesystem )
2021-12-09 17:16:05 +01:00
2021-08-06 17:46:01 +02:00
runningCephVersion , err := cephclient . LeastUptodateDaemonVersion ( r . context , clusterInfo , config . MonType )
if err != nil {
2022-03-14 10:29:25 -06:00
return reconcile . Result {}, * cephFilesystem ,
errors . Wrapf ( err , "failed to retrieve current ceph %q version" , config . MonType )
2021-08-06 17:46:01 +02:00
}
r . clusterInfo . CephVersion = runningCephVersion
// Detect against running version only
2025-12-03 10:35:58 -07:00
log . NamedDebug ( request . NamespacedName , logger , "deleting filesystem" )
2020-04-15 23:02:17 +02:00
err = r . reconcileDeleteFilesystem ( cephFilesystem )
2020-03-17 13:28:09 +01:00
if err != nil {
2022-03-14 10:29:25 -06:00
return reconcile . Result {}, * cephFilesystem ,
errors . Wrapf ( err , "failed to delete filesystem %q. " , cephFilesystem . Name )
2020-03-17 13:28:09 +01:00
}
2021-04-08 15:44:27 +02:00
// If the ceph fs still in the map, we must remove it during CR deletion
2021-11-03 12:56:11 -06:00
r . cancelMirrorMonitoring ( cephFilesystem )
2021-04-08 15:44:27 +02:00
2020-03-17 13:28:09 +01:00
// Remove finalizer
2021-10-23 20:29:41 +09:00
err = opcontroller . RemoveFinalizer ( r . opManagerContext , r . client , cephFilesystem )
2020-03-17 13:28:09 +01:00
if err != nil {
2022-03-14 10:29:25 -06:00
return reconcile . Result {}, * cephFilesystem , errors . Wrap ( err , "failed to remove finalizer" )
2020-03-17 13:28:09 +01:00
}
// Return and do not requeue. Successful deletion.
2022-03-14 10:29:25 -06:00
return reconcile . Result {}, * cephFilesystem , nil
2020-03-17 13:28:09 +01:00
}
2021-08-06 17:46:01 +02:00
// Detect desired CephCluster version
runningCephVersion , desiredCephVersion , err := currentAndDesiredCephVersion (
2021-11-13 15:15:21 +09:00
r . opManagerContext ,
2021-08-06 17:46:01 +02:00
r . opConfig . Image ,
cephFilesystem . Namespace ,
controllerName ,
k8sutil . NewOwnerInfo ( cephFilesystem , r . scheme ),
r . context ,
r . cephClusterSpec ,
r . clusterInfo ,
)
if err != nil {
if strings . Contains ( err . Error (), opcontroller . UninitializedCephConfigError ) {
2025-12-03 10:35:58 -07:00
log . NamedInfo ( request . NamespacedName , logger , opcontroller . OperatorNotInitializedMessage )
2022-03-14 10:29:25 -06:00
return opcontroller . WaitForRequeueIfOperatorNotInitialized , * cephFilesystem , nil
2021-08-06 17:46:01 +02:00
}
2022-03-14 10:29:25 -06:00
return reconcile . Result {}, * cephFilesystem , errors . Wrap ( err , "failed to detect running and desired ceph version" )
2021-08-06 17:46:01 +02:00
}
r . clusterInfo . CephVersion = * runningCephVersion
// If the version of the Ceph monitor differs from the CephCluster CR image version we assume
// the cluster is being upgraded. So the controller will just wait for the upgrade to finish and
// then versions should match. Obviously using the cmd reporter job adds up to the deployment time
2022-08-11 19:09:40 +09:00
// Skip waiting for upgrades to finish in case of external cluster.
if ! cephCluster . Spec . External . Enable && ! reflect . DeepEqual ( * runningCephVersion , * desiredCephVersion ) {
2021-08-06 17:46:01 +02:00
// Upgrade is in progress, let's wait for the mons to be done
2022-03-14 10:29:25 -06:00
return opcontroller . WaitForRequeueIfCephClusterIsUpgrading , * cephFilesystem ,
2021-08-06 17:46:01 +02:00
opcontroller . ErrorCephUpgradingRequeue ( desiredCephVersion , runningCephVersion )
}
2020-03-17 13:28:09 +01:00
// validate the filesystem settings
2020-10-26 19:16:18 -06:00
if err := validateFilesystem ( r . context , r . clusterInfo , r . cephClusterSpec , cephFilesystem ); err != nil {
2021-01-22 16:00:06 +01:00
if strings . Contains ( err . Error (), opcontroller . UninitializedCephConfigError ) {
2025-12-03 10:35:58 -07:00
log . NamedInfo ( request . NamespacedName , logger , opcontroller . OperatorNotInitializedMessage )
2022-03-14 10:29:25 -06:00
return opcontroller . WaitForRequeueIfOperatorNotInitialized , * cephFilesystem , nil
2021-01-22 16:00:06 +01:00
}
2022-03-14 10:29:25 -06:00
return reconcile . Result {}, * cephFilesystem ,
errors . Wrapf ( err , "invalid object filesystem %q arguments" , cephFilesystem . Name )
2020-03-17 13:28:09 +01:00
}
2025-07-16 10:52:19 +05:30
r . shouldRotateCephxKeys , err = keyring . ShouldRotateCephxKeys ( cephCluster . Spec . Security . CephX . Daemon , * runningCephVersion ,
* desiredCephVersion , cephFilesystem . Status . Cephx . Daemon )
if err != nil {
return reconcile . Result {}, * cephFilesystem , errors . Wrap ( err , "failed to determine if cephx keys should be rotated" )
}
if r . shouldRotateCephxKeys {
2025-12-03 10:35:58 -07:00
log . NamedInfo ( request . NamespacedName , logger , "cephx keys for CephFileSystem will be rotated" )
2025-07-16 10:52:19 +05:30
}
2020-03-17 13:28:09 +01:00
// RECONCILE
2025-12-03 10:35:58 -07:00
log . NamedDebug ( request . NamespacedName , logger , "reconciling ceph filesystem store deployments" )
2020-03-17 13:28:09 +01:00
reconcileResponse , err = r . reconcileCreateFilesystem ( cephFilesystem )
if err != nil {
2025-07-24 07:21:06 +05:30
_ , err := r . updateStatus ( k8sutil . ObservedGenerationNotAvailable , request . NamespacedName , cephv1 . ConditionFailure , nil , nil )
if err != nil {
return reconcile . Result {}, * cephFilesystem , errors . Wrapf ( err , "failed to set failure status on cephFileSystem %q when file system creation failed" , request . NamespacedName )
}
2022-03-14 10:29:25 -06:00
return reconcileResponse , * cephFilesystem , err
2020-03-17 13:28:09 +01:00
}
2025-07-16 10:52:19 +05:30
// update Mds cephx status
cephxStatus := keyring . UpdatedCephxStatus ( r . shouldRotateCephxKeys , cephCluster . Spec . Security . CephX . Daemon , r . clusterInfo . CephVersion , cephFilesystem . Status . Cephx . Daemon )
2025-07-24 07:21:06 +05:30
_ , err = r . updateStatus ( observedGeneration , request . NamespacedName , cephv1 . ConditionProgressing , nil , & cephxStatus )
if err != nil {
return reconcile . Result {}, * cephFilesystem , errors . Wrapf ( err , "failed to set cephx status for cephFileSystem %q" , request . NamespacedName )
}
2021-08-27 17:19:16 -06:00
2025-07-16 10:52:19 +05:30
statusUpdated := false
2021-04-08 15:44:27 +02:00
// Enable mirroring if needed
2024-05-15 00:42:20 +00:00
if cephFilesystem . Spec . Mirroring != nil {
2021-04-08 15:44:27 +02:00
// Disable mirroring on that filesystem if needed
2024-05-15 00:42:20 +00:00
if ! cephFilesystem . Spec . Mirroring . Enabled {
err = cephclient . DisableFilesystemSnapshotMirror ( r . context , r . clusterInfo , cephFilesystem . Name )
if err != nil {
return reconcile . Result {}, * cephFilesystem ,
errors . Wrapf ( err , "failed to disable mirroring on filesystem %q" , cephFilesystem . Name )
}
} else {
2025-12-03 10:35:58 -07:00
log . NamedInfo ( request . NamespacedName , logger , "reconciling cephfs-mirror mirroring configuration" )
2024-05-15 00:42:20 +00:00
err = r . reconcileMirroring ( cephFilesystem , request . NamespacedName )
if err != nil {
return opcontroller . ImmediateRetryResult , * cephFilesystem ,
errors . Wrapf ( err , "failed to configure mirroring for filesystem %q." , cephFilesystem . Name )
}
2021-04-08 15:44:27 +02:00
2024-05-15 00:42:20 +00:00
// Always create a bootstrap peer token in case another cluster wants to add us as a peer
2025-12-03 10:35:58 -07:00
log . NamedInfo ( request . NamespacedName , logger , "reconciling create cephfs-mirror peer configuration" )
2024-05-15 00:42:20 +00:00
reconcileResponse , err = opcontroller . CreateBootstrapPeerSecret ( r . context , r . clusterInfo , cephFilesystem , k8sutil . NewOwnerInfo ( cephFilesystem , r . scheme ))
if err != nil {
2025-07-24 07:21:06 +05:30
_ , err := r . updateStatus ( k8sutil . ObservedGenerationNotAvailable , request . NamespacedName , cephv1 . ConditionFailure , nil , nil )
if err != nil {
return reconcile . Result {}, * cephFilesystem , errors . Wrapf ( err , "failed to set failure status on cephFileSystem %q when peer secret bootstrap failed" , request . NamespacedName )
}
2024-05-15 00:42:20 +00:00
return reconcileResponse , * cephFilesystem ,
errors . Wrapf ( err , "failed to create cephfs-mirror bootstrap peer for filesystem %q." , cephFilesystem . Name )
}
2021-04-08 15:44:27 +02:00
2025-12-03 10:35:58 -07:00
log . NamedInfo ( request . NamespacedName , logger , "reconciling add cephfs-mirror peer configuration" )
2024-05-15 00:42:20 +00:00
err = r . reconcileAddBootstrapPeer ( cephFilesystem , request . NamespacedName )
if err != nil {
return opcontroller . ImmediateRetryResult , * cephFilesystem ,
errors . Wrapf ( err , "failed to configure mirroring for filesystem %q." , cephFilesystem . Name )
}
2021-04-08 15:44:27 +02:00
2024-05-15 00:42:20 +00:00
// update ObservedGeneration in status at the end of reconcile
// Set Ready status, we are done reconciling
2025-07-24 07:21:06 +05:30
_ , err = r . updateStatus ( observedGeneration , request . NamespacedName , cephv1 . ConditionReady , opcontroller . GenerateStatusInfo ( cephFilesystem ), & cephxStatus )
if err != nil {
return reconcile . Result {}, * cephFilesystem , errors . Wrapf ( err , "failed to set ready status for cephFileSystem %q" , request . NamespacedName )
2024-05-15 00:42:20 +00:00
}
2025-07-24 07:21:06 +05:30
statusUpdated = true
2021-04-08 15:44:27 +02:00
2024-05-15 00:42:20 +00:00
// Run go routine check for mirroring status
if ! cephFilesystem . Spec . StatusCheck . Mirror . Disabled {
// Start monitoring cephfs-mirror status
if r . fsContexts [ fsChannelKeyName ( cephFilesystem )]. started {
2025-12-03 10:35:58 -07:00
log . NamedDebug ( request . NamespacedName , logger , "ceph filesystem mirror status monitoring go routine already running!" )
2024-05-15 00:42:20 +00:00
} else {
checker := newMirrorChecker ( r . context , r . client , r . clusterInfo , request . NamespacedName , & cephFilesystem . Spec , cephFilesystem . Name )
go checker . checkMirroring ( r . fsContexts [ fsChannelKeyName ( cephFilesystem )]. internalCtx )
r . fsContexts [ fsChannelKeyName ( cephFilesystem )]. started = true
2021-04-08 15:44:27 +02:00
}
}
}
2021-08-27 17:19:16 -06:00
}
2024-05-15 00:42:20 +00:00
2021-08-27 17:19:16 -06:00
if ! statusUpdated {
2022-03-16 19:58:35 +05:30
// update ObservedGeneration in status at the end of reconcile
2021-12-09 17:16:05 +01:00
// Set Ready status, we are done reconciling$
// TODO: set status to Ready **only** if the filesystem is ready
2025-07-24 07:21:06 +05:30
_ , err := r . updateStatus ( observedGeneration , request . NamespacedName , cephv1 . ConditionReady , nil , & cephxStatus )
if err != nil {
return reconcile . Result {}, * cephFilesystem , errors . Wrapf ( err , "failed to set ready status for cephFileSystem %q" , request . NamespacedName )
}
2021-04-08 15:44:27 +02:00
}
2020-03-17 13:28:09 +01:00
2022-03-14 10:29:25 -06:00
return reconcile . Result {}, * cephFilesystem , nil
2020-03-17 13:28:09 +01:00
}
func ( r * ReconcileCephFilesystem ) reconcileCreateFilesystem ( cephFilesystem * cephv1 . CephFilesystem ) ( reconcile . Result , error ) {
if r . cephClusterSpec . External . Enable {
2020-07-10 16:40:16 -06:00
_ , err := opcontroller . ValidateCephVersionsBetweenLocalAndExternalClusters ( r . context , r . clusterInfo )
2019-08-30 17:24:26 +02:00
if err != nil {
// This handles the case where the operator is running, the external cluster has been upgraded and a CR creation is called
// If that's a major version upgrade we fail, if it's a minor version, we continue, it's not ideal but not critical
2020-03-17 13:28:09 +01:00
return reconcile . Result {}, errors . Wrapf ( err , "refusing to run new crd" )
2019-08-30 17:24:26 +02:00
}
}
2020-11-12 16:42:08 -07:00
// preservePoolsOnDelete being set to true has data-loss concerns and is deprecated (see #6492).
// If preservePoolsOnDelete is set to true, assume the user means preserveFilesystemOnDelete instead.
2020-10-27 02:20:11 +00:00
if cephFilesystem . Spec . PreservePoolsOnDelete {
2020-11-12 16:42:08 -07:00
if ! cephFilesystem . Spec . PreserveFilesystemOnDelete {
2025-12-03 10:35:58 -07:00
log . NamedWarning ( opcontroller . NsName ( cephFilesystem . Namespace , cephFilesystem . Name ), logger , "preservePoolsOnDelete (currently set 'true') has been deprecated in favor of preserveFilesystemOnDelete (currently set 'false') due to data loss concerns so Rook will assume preserveFilesystemOnDelete 'true'" )
2020-11-12 16:42:08 -07:00
cephFilesystem . Spec . PreserveFilesystemOnDelete = true
}
2020-10-27 02:20:11 +00:00
}
2021-02-18 13:10:11 +00:00
ownerInfo := k8sutil . NewOwnerInfo ( cephFilesystem , r . scheme )
2025-07-16 10:52:19 +05:30
err := createFilesystem ( r . context , r . clusterInfo , * cephFilesystem , r . cephClusterSpec , ownerInfo , r . cephClusterSpec . DataDirHostPath , r . shouldRotateCephxKeys )
2018-05-18 14:32:20 -07:00
if err != nil {
2020-03-17 13:28:09 +01:00
return reconcile . Result {}, errors . Wrapf ( err , "failed to create filesystem %q" , cephFilesystem . Name )
2019-11-25 19:53:31 +05:30
}
2020-03-17 13:28:09 +01:00
return reconcile . Result {}, nil
2019-11-25 19:53:31 +05:30
}
2020-04-08 17:27:09 -06:00
2020-04-15 23:02:17 +02:00
func ( r * ReconcileCephFilesystem ) reconcileDeleteFilesystem ( cephFilesystem * cephv1 . CephFilesystem ) error {
2021-02-18 13:10:11 +00:00
ownerInfo := k8sutil . NewOwnerInfo ( cephFilesystem , r . scheme )
err := deleteFilesystem ( r . context , r . clusterInfo , * cephFilesystem , r . cephClusterSpec , ownerInfo , r . cephClusterSpec . DataDirHostPath )
2020-04-15 23:02:17 +02:00
if err != nil {
return err
}
return nil
}
2021-04-08 15:44:27 +02:00
func ( r * ReconcileCephFilesystem ) reconcileMirroring ( cephFilesystem * cephv1 . CephFilesystem , namespacedName types . NamespacedName ) error {
// Enable the mgr module
err := cephclient . MgrEnableModule ( r . context , r . clusterInfo , "mirroring" , false )
2020-04-08 17:27:09 -06:00
if err != nil {
2021-04-08 15:44:27 +02:00
return errors . Wrap ( err , "failed to enable mirroring mgr module" )
}
// Enable snapshot mirroring on that filesystem
err = cephclient . EnableFilesystemSnapshotMirror ( r . context , r . clusterInfo , cephFilesystem . Name )
if err != nil {
return errors . Wrapf ( err , "failed to enable mirroring on filesystem %q" , cephFilesystem . Name )
}
// Add snapshot schedules
if cephFilesystem . Spec . Mirroring . SnapShotScheduleEnabled () {
// Enable the snap_schedule module
err = cephclient . MgrEnableModule ( r . context , r . clusterInfo , "snap_schedule" , false )
if err != nil {
return errors . Wrap ( err , "failed to enable snap_schedule mgr module" )
}
// Enable snapshot schedules
for _ , snap := range cephFilesystem . Spec . Mirroring . SnapshotSchedules {
err = cephclient . AddSnapshotSchedule ( r . context , r . clusterInfo , snap . Path , snap . Interval , snap . StartTime , cephFilesystem . Name )
if err != nil {
return errors . Wrapf ( err , "failed to add snapshot schedules on filesystem %q" , cephFilesystem . Name )
}
}
// Enable snapshot retention
for _ , retention := range cephFilesystem . Spec . Mirroring . SnapshotRetention {
err = cephclient . AddSnapshotScheduleRetention ( r . context , r . clusterInfo , retention . Path , retention . Duration , cephFilesystem . Name )
if err != nil {
return errors . Wrapf ( err , "failed to add snapshot retention on filesystem %q" , cephFilesystem . Name )
}
2020-04-08 17:27:09 -06:00
}
}
2021-04-08 15:44:27 +02:00
return nil
}
2022-06-21 19:59:31 -04:00
func ( r * ReconcileCephFilesystem ) reconcileAddBootstrapPeer ( cephFilesystem * cephv1 . CephFilesystem , namespacedName types . NamespacedName ) error {
2021-04-08 15:44:27 +02:00
if cephFilesystem . Spec . Mirroring . Peers == nil {
return nil
2020-04-08 17:27:09 -06:00
}
2021-04-08 15:44:27 +02:00
// List all the peers secret, we can have more than one peer we might want to configure
// For each, get the Kubernetes Secret and import the "peer token" so that we can configure the mirroring
for _ , peerSecret := range cephFilesystem . Spec . Mirroring . Peers . SecretNames {
2025-12-03 10:35:58 -07:00
log . NamedDebug ( opcontroller . NsName ( cephFilesystem . Namespace , cephFilesystem . Name ), logger , "fetching bootstrap peer kubernetes secret %q" , peerSecret )
2021-08-06 17:46:01 +02:00
s , err := r . context . Clientset . CoreV1 (). Secrets ( r . clusterInfo . Namespace ). Get ( r . opManagerContext , peerSecret , metav1 . GetOptions {})
2021-04-08 15:44:27 +02:00
// We don't care about IsNotFound here, we still need to fail
if err != nil {
return errors . Wrapf ( err , "failed to fetch kubernetes secret %q fs-mirror bootstrap peer" , peerSecret )
}
// Validate peer secret content
err = opcontroller . ValidatePeerToken ( cephFilesystem , s . Data )
if err != nil {
return errors . Wrapf ( err , "failed to validate fs-mirror bootstrap peer secret %q data" , peerSecret )
}
2020-04-08 17:27:09 -06:00
2021-04-08 15:44:27 +02:00
// Add fs-mirror peer
err = cephclient . ImportFSMirrorBootstrapPeer ( r . context , r . clusterInfo , cephFilesystem . Name , string ( s . Data [ "token" ]))
if err != nil {
return errors . Wrap ( err , "failed to import filesystem bootstrap peer token" )
}
2020-04-08 17:27:09 -06:00
}
2021-04-08 15:44:27 +02:00
return nil
}
2021-11-03 12:56:11 -06:00
func fsChannelKeyName ( f * cephv1 . CephFilesystem ) string {
return types . NamespacedName { Namespace : f . Namespace , Name : f . Name }. String ()
}
// cancel mirror monitoring. This is a noop if monitoring is not running.
func ( r * ReconcileCephFilesystem ) cancelMirrorMonitoring ( cephFilesystem * cephv1 . CephFilesystem ) {
_ , fsContextsExists := r . fsContexts [ fsChannelKeyName ( cephFilesystem )]
if fsContextsExists {
// Cancel the context to stop the mirroring status
r . fsContexts [ fsChannelKeyName ( cephFilesystem )]. internalCancel ()
// Remove ceph fs from the map
delete ( r . fsContexts , fsChannelKeyName ( cephFilesystem ))
}
2020-04-08 17:27:09 -06:00
}