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"
"strings"
"time"
cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"
"github.com/rook/rook/pkg/clusterd"
cephclient "github.com/rook/rook/pkg/daemon/ceph/client"
kerrors "k8s.io/apimachinery/pkg/api/errors"
"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 }
// WaitForRequeueIfCephClusterNotReadyAfter requeue after 10sec if the operator is not ready
WaitForRequeueIfCephClusterNotReadyAfter = 10 * time . Second
// WaitForRequeueIfCephClusterNotReady waits for the CephCluster to be ready
WaitForRequeueIfCephClusterNotReady = reconcile . Result { Requeue : true , RequeueAfter : WaitForRequeueIfCephClusterNotReadyAfter }
)
// IsReadyToReconcile determines if a controller is ready to reconcile or not
2020-03-25 16:32:01 +01:00
func IsReadyToReconcile ( client client . Client , clustercontext * clusterd . Context , namespacedName types . NamespacedName , controllerName string ) ( cephv1 . ClusterSpec , bool , bool , reconcile . Result ) {
2020-02-25 18:07:01 +01:00
namespacedName . Name = namespacedName . Namespace
2020-03-02 17:26:50 +01:00
cephClusterExists := true
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
cephCluster := & cephv1 . CephCluster {}
err := client . Get ( context . TODO (), namespacedName , cephCluster )
if err != nil {
if kerrors . IsNotFound ( err ) {
2020-03-02 17:26:50 +01:00
cephClusterExists = false
2020-03-25 16:32:01 +01:00
logger . Errorf ( "%q: CephCluster resource %q not found in namespace %q" , controllerName , namespacedName . Name , namespacedName . Namespace )
2020-03-25 16:28:53 +01:00
return cephv1 . ClusterSpec {}, false , cephClusterExists , WaitForRequeueIfCephClusterNotReady
2020-02-25 18:07:01 +01:00
} else if err != nil {
2020-03-25 16:32:01 +01:00
logger . Errorf ( "%q:failed to fetch CephCluster %v" , controllerName , err )
2020-03-02 17:26:50 +01:00
return cephv1 . ClusterSpec {}, false , cephClusterExists , ImmediateRetryResult
2020-02-25 18:07:01 +01:00
}
}
2020-03-25 16:32:01 +01:00
logger . Debugf ( "%q: CephCluster resource %q found in namespace %q" , controllerName , namespacedName . Name , namespacedName . Namespace )
2020-02-28 12:33:32 +01:00
2020-03-25 16:30:03 +01:00
// If the cluster is healthy
// Test a Ceph command to verify the Operator is ready
// This is done to silence errors when the operator just started and cannot reconcile yet
status , err := cephclient . Status ( clustercontext , namespacedName . Namespace )
if err != nil {
if strings . Contains ( err . Error (), "error calling conf_read_file" ) {
2020-03-25 16:32:01 +01:00
logger . Infof ( "%q: operator is not ready to run ceph command, cannot reconcile yet." , controllerName )
2020-03-25 16:30:03 +01:00
return cephCluster . Spec , false , cephClusterExists , WaitForRequeueIfCephClusterNotReady
2020-02-25 18:07:01 +01:00
}
2020-03-25 16:30:03 +01:00
// We should not arrive there
2020-03-25 16:32:01 +01:00
logger . Errorf ( "%q: ceph command error %v" , controllerName , err )
2020-03-25 16:30:03 +01:00
return cephCluster . Spec , false , cephClusterExists , ImmediateRetryResult
}
// If Ceph status is ok we can reconcile
if status . Health . Status == "HEALTH_OK" || status . Health . Status == "HEALTH_WARN" {
2020-03-25 16:32:01 +01:00
logger . Debugf ( "%q: ceph status is %q, operator is ready to run ceph command, reconciling" , controllerName , status . Health . Status )
2020-03-02 17:26:50 +01:00
return cephCluster . Spec , true , cephClusterExists , reconcile . Result {}
2020-02-25 18:07:01 +01:00
}
2020-03-25 16:32:01 +01:00
logger . Debugf ( "%q: CephCluster resource %q found in namespace %q but cluster state is not appropriate %q" , controllerName , namespacedName . Name , namespacedName . Namespace , status . Health . Status )
2020-03-23 15:13:53 +01:00
return cephCluster . Spec , false , cephClusterExists , WaitForRequeueIfCephClusterNotReady
2020-02-25 18:07:01 +01:00
}