2020-02-25 18:07:01 +01: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 controller
import (
"context"
2020-11-23 18:17:59 +01:00
"errors"
2020-04-17 18:40:54 +02:00
"fmt"
"reflect"
2021-01-07 17:24:13 -07:00
"strings"
2020-02-25 18:07:01 +01:00
"time"
cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"
"github.com/rook/rook/pkg/clusterd"
2020-04-17 18:40:54 +02:00
"github.com/rook/rook/pkg/operator/k8sutil"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2020-02-25 18:07:01 +01:00
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)
2020-03-05 11:50:22 +05:30
// OperatorSettingConfigMapName refers to ConfigMap that configures rook ceph operator
const OperatorSettingConfigMapName string = "rook-ceph-operator-config"
2020-02-25 18:07:01 +01:00
var (
// ImmediateRetryResult Return this for a immediate retry of the reconciliation loop with the same request object.
ImmediateRetryResult = reconcile . Result { Requeue : true }
2020-06-16 14:20:50 +02:00
2020-02-25 18:07:01 +01:00
// WaitForRequeueIfCephClusterNotReady waits for the CephCluster to be ready
2020-05-14 15:33:16 -06:00
WaitForRequeueIfCephClusterNotReady = reconcile . Result { Requeue : true , RequeueAfter : 10 * time . Second }
2020-06-16 14:20:50 +02:00
2020-05-14 16:52:10 -06:00
// WaitForRequeueIfFinalizerBlocked waits for resources to be cleaned up before the finalizer can be removed
WaitForRequeueIfFinalizerBlocked = reconcile . Result { Requeue : true , RequeueAfter : 10 * time . Second }
2020-06-16 14:20:50 +02:00
// OperatorCephBaseImageVersion is the ceph version in the operator image
OperatorCephBaseImageVersion string
2020-02-25 18:07:01 +01:00
)
2020-11-23 18:17:59 +01:00
// CheckForCancelledOrchestration checks whether a cancellation has been requested
func CheckForCancelledOrchestration ( context * clusterd . Context ) error {
defer context . RequestCancelOrchestration . UnSet ()
// Check whether we need to cancel the orchestration
if context . RequestCancelOrchestration . IsSet () {
2020-12-03 15:37:47 -07:00
return errors . New ( "CANCELLING CURRENT ORCHESTRATION" )
2020-11-23 18:17:59 +01:00
}
return nil
}
2020-10-27 00:53:51 +00:00
// canIgnoreHealthErrStatusInReconcile determines whether a status of HEALTH_ERR in the CephCluster can be ignored safely.
func canIgnoreHealthErrStatusInReconcile ( cephCluster cephv1 . CephCluster , controllerName string ) bool {
// Get a list of all the keys causing the HEALTH_ERR status.
var healthErrKeys = make ([] string , 0 )
for key , health := range cephCluster . Status . CephStatus . Details {
if health . Severity == "HEALTH_ERR" {
healthErrKeys = append ( healthErrKeys , key )
}
}
// If there is only one cause for HEALTH_ERR and it's on the allowed list of errors, ignore it.
var allowedErrStatus = [] string { "MDS_ALL_DOWN" }
var ignoreHealthErr = len ( healthErrKeys ) == 1 && contains ( allowedErrStatus , healthErrKeys [ 0 ])
if ignoreHealthErr {
logger . Debugf ( "%q: ignoring ceph status %q because only cause is %q (full status is %q)" , controllerName , cephCluster . Status . CephStatus . Health , healthErrKeys [ 0 ], cephCluster . Status . CephStatus )
}
return ignoreHealthErr
}
2020-02-25 18:07:01 +01:00
// IsReadyToReconcile determines if a controller is ready to reconcile or not
2020-06-09 18:16:58 +02:00
func IsReadyToReconcile ( c client . Client , clustercontext * clusterd . Context , namespacedName types . NamespacedName , controllerName string ) ( cephv1 . CephCluster , bool , bool , reconcile . Result ) {
2020-04-21 13:28:39 -06:00
cephClusterExists := false
2020-02-25 18:07:01 +01:00
// Running ceph commands won't work and the controller will keep re-queuing so I believe it's fine not to check
// Make sure a CephCluster exists before doing anything
2020-04-21 13:28:39 -06:00
var cephCluster cephv1 . CephCluster
clusterList := & cephv1 . CephClusterList {}
err := c . List ( context . TODO (), clusterList , client . InNamespace ( namespacedName . Namespace ))
2020-02-25 18:07:01 +01:00
if err != nil {
2020-08-04 17:55:09 +02:00
logger . Errorf ( "%q: failed to fetch CephCluster %v" , controllerName , err )
2020-06-09 18:16:58 +02:00
return cephCluster , false , cephClusterExists , ImmediateRetryResult
2020-02-25 18:07:01 +01:00
}
2020-04-21 13:28:39 -06:00
if len ( clusterList . Items ) == 0 {
2020-08-04 17:55:09 +02:00
logger . Debugf ( "%q: no CephCluster resource found in namespace %q" , controllerName , namespacedName . Namespace )
2020-06-09 18:16:58 +02:00
return cephCluster , false , cephClusterExists , WaitForRequeueIfCephClusterNotReady
2020-04-21 13:28:39 -06:00
}
cephClusterExists = true
cephCluster = clusterList . Items [ 0 ]
2020-02-25 18:07:01 +01:00
2020-04-21 13:28:39 -06:00
logger . Debugf ( "%q: CephCluster resource %q found in namespace %q" , controllerName , cephCluster . Name , namespacedName . Namespace )
2020-02-28 12:33:32 +01:00
2020-06-17 18:21:12 +02:00
// read the CR status of the cluster
if cephCluster . Status . CephStatus != nil {
2020-10-27 00:53:51 +00:00
var operatorDeploymentOk = cephCluster . Status . CephStatus . Health == "HEALTH_OK" || cephCluster . Status . CephStatus . Health == "HEALTH_WARN"
if operatorDeploymentOk || canIgnoreHealthErrStatusInReconcile ( cephCluster , controllerName ) {
2020-06-17 18:21:12 +02:00
logger . Debugf ( "%q: ceph status is %q, operator is ready to run ceph command, reconciling" , controllerName , cephCluster . Status . CephStatus . Health )
return cephCluster , true , cephClusterExists , WaitForRequeueIfCephClusterNotReady
2020-02-25 18:07:01 +01:00
}
2020-10-27 00:53:51 +00:00
2021-01-07 17:24:13 -07:00
details := cephCluster . Status . CephStatus . Details
message , ok := details [ "error" ]
if ok && len ( details ) == 1 && strings . Contains ( message . Message , "Error initializing cluster client" ) {
logger . Infof ( "%s: skipping reconcile since operator is still initializing" , controllerName )
} else {
logger . Infof ( "%s: CephCluster %q found but skipping reconcile since ceph health is %q" , controllerName , cephCluster . Name , cephCluster . Status . CephStatus )
}
2020-03-25 16:30:03 +01:00
}
2020-06-09 18:16:58 +02:00
return cephCluster , false , cephClusterExists , WaitForRequeueIfCephClusterNotReady
2020-02-25 18:07:01 +01:00
}
2020-04-17 18:40:54 +02:00
// ClusterOwnerRef represents the owner reference of the CephCluster CR
func ClusterOwnerRef ( clusterName , clusterID string ) metav1 . OwnerReference {
blockOwner := true
return metav1 . OwnerReference {
APIVersion : fmt . Sprintf ( "%s/%s" , ClusterResource . Group , ClusterResource . Version ),
Kind : ClusterResource . Kind ,
Name : clusterName ,
UID : types . UID ( clusterID ),
BlockOwnerDeletion : & blockOwner ,
}
}
// ClusterResource operator-kit Custom Resource Definition
var ClusterResource = k8sutil . CustomResource {
Name : "cephcluster" ,
Plural : "cephclusters" ,
Group : cephv1 . CustomResourceGroup ,
Version : cephv1 . Version ,
Kind : reflect . TypeOf ( cephv1 . CephCluster {}). Name (),
APIVersion : fmt . Sprintf ( "%s/%s" , cephv1 . CustomResourceGroup , cephv1 . Version ),
}