Skip to content

Commit

Permalink
Refactored logs for storagecluster controller
Browse files Browse the repository at this point in the history
Signed off by: Priyanka Jiandani <pjiandan@redhat.com>
  • Loading branch information
priyanka19-98 committed Jul 1, 2021
1 parent bc98813 commit 852b8c7
Show file tree
Hide file tree
Showing 19 changed files with 254 additions and 192 deletions.
24 changes: 15 additions & 9 deletions controllers/storagecluster/cephblockpools.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)

Expand All @@ -33,6 +34,7 @@ func (r *StorageClusterReconciler) newCephBlockPoolInstances(initData *ocsv1.Sto
for _, obj := range ret {
err := controllerutil.SetControllerReference(initData, obj, r.Scheme)
if err != nil {
r.Log.Error(err, "Unable to set controller reference for CephBlockPool.", "CephBlockPool", klog.KRef(obj.Namespace, obj.Name))
return nil, err
}
}
Expand Down Expand Up @@ -61,21 +63,23 @@ func (obj *ocsCephBlockPools) ensureCreated(r *StorageClusterReconciler, instanc
return nil
}
if existing.DeletionTimestamp != nil {
r.Log.Info(fmt.Sprintf("Unable to restore init object because %s is marked for deletion", existing.Name))
r.Log.Info("Unable to restore CephBlockPool because it is marked for deletion.", "CephBlockPool", klog.KRef(existing.Namespace, existing.Name))
return fmt.Errorf("failed to restore initialization object %s because it is marked for deletion", existing.Name)
}

r.Log.Info(fmt.Sprintf("Restoring original cephBlockPool %s", cephBlockPool.Name))
r.Log.Info("Restoring original CephBlockPool.", "CephBlockPool", klog.KRef(cephBlockPool.Namespace, cephBlockPool.Name))
existing.ObjectMeta.OwnerReferences = cephBlockPool.ObjectMeta.OwnerReferences
cephBlockPool.ObjectMeta = existing.ObjectMeta
err = r.Client.Update(context.TODO(), cephBlockPool)
if err != nil {
r.Log.Error(err, "Failed to update CephBlockPool.", "CephBlockPool", klog.KRef(cephBlockPool.Namespace, cephBlockPool.Name))
return err
}
case errors.IsNotFound(err):
r.Log.Info(fmt.Sprintf("Creating cephBlockPool %s", cephBlockPool.Name))
r.Log.Info("Creating CephBlockPool.", "CephBlockPool", klog.KRef(cephBlockPool.Namespace, cephBlockPool.Name))
err = r.Client.Create(context.TODO(), cephBlockPool)
if err != nil {
r.Log.Error(err, "Failed to create CephBlockPool.", "CephBlockPool", klog.KRef(cephBlockPool.Namespace, cephBlockPool.Name))
return err
}
}
Expand All @@ -96,28 +100,30 @@ func (obj *ocsCephBlockPools) ensureDeleted(r *StorageClusterReconciler, sc *ocs
err := r.Client.Get(context.TODO(), types.NamespacedName{Name: cephBlockPool.Name, Namespace: sc.Namespace}, foundCephBlockPool)
if err != nil {
if errors.IsNotFound(err) {
r.Log.Info("Uninstall: CephBlockPool not found", "CephBlockPool Name", cephBlockPool.Name)
r.Log.Info("Uninstall: CephBlockPool not found.", "CephBlockPool", klog.KRef(cephBlockPool.Namespace, cephBlockPool.Name))
continue
}
return fmt.Errorf("Uninstall: Unable to retrieve cephBlockPool %v: %v", cephBlockPool.Name, err)
return fmt.Errorf("uninstall: unable to retrieve CephBlockPool %v: %v", cephBlockPool.Name, err)
}

if cephBlockPool.GetDeletionTimestamp().IsZero() {
r.Log.Info("Uninstall: Deleting cephBlockPool", "CephBlockPool Name", cephBlockPool.Name)
r.Log.Info("Uninstall: Deleting CephBlockPool.", "CephBlockPool", klog.KRef(cephBlockPool.Namespace, cephBlockPool.Name))
err = r.Client.Delete(context.TODO(), foundCephBlockPool)
if err != nil {
return fmt.Errorf("Uninstall: Failed to delete cephBlockPool %v: %v", foundCephBlockPool.Name, err)
r.Log.Error(err, "Uninstall: Failed to delete CephBlockPool.", "CephBlockPool", klog.KRef(foundCephBlockPool.Namespace, foundCephBlockPool.Name))
return fmt.Errorf("uninstall: Failed to delete CephBlockPool %v: %v", foundCephBlockPool.Name, err)
}
}

err = r.Client.Get(context.TODO(), types.NamespacedName{Name: cephBlockPool.Name, Namespace: sc.Namespace}, foundCephBlockPool)
if err != nil {
if errors.IsNotFound(err) {
r.Log.Info("Uninstall: CephBlockPool is deleted", "CephBlockPool Name", cephBlockPool.Name)
r.Log.Info("Uninstall: CephBlockPool is deleted.", "CephBlockPool", klog.KRef(cephBlockPool.Namespace, cephBlockPool.Name))
continue
}
}
return fmt.Errorf("Uninstall: Waiting for cephBlockPool %v to be deleted", cephBlockPool.Name)
r.Log.Error(err, "Uninstall: Waiting for CephBlockPool to be deleted.", "CephBlockPool", klog.KRef(cephBlockPool.Namespace, cephBlockPool.Name))
return fmt.Errorf("uninstall: Waiting for CephBlockPool %v to be deleted", cephBlockPool.Name)

}
return nil
Expand Down
45 changes: 29 additions & 16 deletions controllers/storagecluster/cephcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/version"
"k8s.io/client-go/tools/reference"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)

Expand Down Expand Up @@ -96,6 +97,7 @@ func (obj *ocsCephCluster) ensureCreated(r *StorageClusterReconciler, sc *ocsv1.
if isMultus(sc.Spec.Network) {
err := validateMultusSelectors(sc.Spec.Network.Selectors)
if err != nil {
r.Log.Error(err, "Failed to validate Multus Selectors specified in StorageCluster.", "StorageCluster", klog.KRef(sc.Namespace, sc.Name))
return err
}
}
Expand All @@ -107,11 +109,12 @@ func (obj *ocsCephCluster) ensureCreated(r *StorageClusterReconciler, sc *ocsv1.
} else {
kmsConfigMap, err := getKMSConfigMap(KMSConfigMapName, sc, r.Client)
if err != nil {
r.Log.Error(err, "failed to procure KMS config")
r.Log.Error(err, "Failed to procure KMS ConfigMap.", "KMSConfigMap", klog.KRef(sc.Namespace, KMSConfigMapName))
return err
}
if kmsConfigMap != nil {
if err = reachKMSProvider(kmsConfigMap); err != nil {
r.Log.Error(err, "Address provided in KMS ConfigMap is not reachable.", "KMSConfigMap", klog.KRef(kmsConfigMap.Namespace, kmsConfigMap.Name))
return err
}
}
Expand All @@ -120,14 +123,15 @@ func (obj *ocsCephCluster) ensureCreated(r *StorageClusterReconciler, sc *ocsv1.

// Set StorageCluster instance as the owner and controller
if err := controllerutil.SetControllerReference(sc, cephCluster, r.Scheme); err != nil {
r.Log.Error(err, "Unable to set controller reference for CephCluster.", "CephCluster", klog.KRef(cephCluster.Namespace, cephCluster.Name))
return err
}

platform, err := r.platform.GetPlatform(r.Client)
if err != nil {
r.Log.Error(err, "Failed to get platform")
r.Log.Error(err, "Failed to get Platform.", "Platform", platform)
} else if platform == v1.IBMCloudPlatformType || platform == IBMCloudCosPlatformType {
r.Log.Info(fmt.Sprintf("Increasing Mon failover timeout to 15m for %s", platform))
r.Log.Info("Increasing Mon failover timeout to 15m.", "Platform", platform)
cephCluster.Spec.HealthCheck.DaemonHealth.Monitor.Timeout = "15m"
}

Expand All @@ -137,11 +141,12 @@ func (obj *ocsCephCluster) ensureCreated(r *StorageClusterReconciler, sc *ocsv1.
if err != nil {
if errors.IsNotFound(err) {
if sc.Spec.ExternalStorage.Enable {
r.Log.Info("Creating external CephCluster")
r.Log.Info("Creating external CephCluster.", "CephCluster", klog.KRef(cephCluster.Namespace, cephCluster.Name))
} else {
r.Log.Info("Creating CephCluster")
r.Log.Info("Creating CephCluster.", "CephCluster", klog.KRef(cephCluster.Namespace, cephCluster.Name))
}
if err := r.Client.Create(context.TODO(), cephCluster); err != nil {
r.Log.Error(err, "Unable to create CephCluster.", "CephCluster", klog.KRef(cephCluster.Namespace, cephCluster.Name))
return err
}
// Need to happen after the ceph cluster CR creation was confirmed
Expand All @@ -152,6 +157,7 @@ func (obj *ocsCephCluster) ensureCreated(r *StorageClusterReconciler, sc *ocsv1.
statusutil.MapCephClusterNoConditions(&r.conditions, reason, message)
return nil
}
r.Log.Error(err, "Unable to fetch CephCluster.", "CephCluster", klog.KRef(cephCluster.Namespace, cephCluster.Name))
return err
}

Expand All @@ -161,16 +167,18 @@ func (obj *ocsCephCluster) ensureCreated(r *StorageClusterReconciler, sc *ocsv1.
// Add it to the list of RelatedObjects if found
objectRef, err := reference.GetReference(r.Scheme, found)
if err != nil {
r.Log.Error(err, "Unable to get CephCluster ObjectReference.", "CephCluster", klog.KRef(found.Namespace, found.Name))
return err
}
err = objectreferencesv1.SetObjectReference(&sc.Status.RelatedObjects, *objectRef)
if err != nil {
r.Log.Error(err, "Unable to add CephCluster to the list of Related Objects in StorageCluster.", "CephCluster", klog.KRef(objectRef.Namespace, objectRef.Name), "StorageCluster", klog.KRef(sc.Namespace, sc.Name))
return err
}

// Handle CephCluster resource status
if found.Status.State == "" {
r.Log.Info("CephCluster resource is not reporting status.")
r.Log.Info("CephCluster resource is not reporting status.", "CephCluster", klog.KRef(found.Namespace, found.Name))
// What does this mean to OCS status? Assuming progress.
reason := "CephClusterStatus"
message := "CephCluster resource is not reporting status"
Expand Down Expand Up @@ -204,7 +212,7 @@ func (obj *ocsCephCluster) ensureCreated(r *StorageClusterReconciler, sc *ocsv1.

// Update the CephCluster if it is not in the desired state
if !reflect.DeepEqual(cephCluster.Spec, found.Spec) {
r.Log.Info("Updating spec for CephCluster")
r.Log.Info("Updating spec for CephCluster.", "CephCluster", klog.KRef(found.Namespace, found.Name))
if !sc.Spec.ExternalStorage.Enable {
// Check if Cluster is Expanding
if len(found.Spec.Storage.StorageClassDeviceSets) < len(cephCluster.Spec.Storage.StorageClassDeviceSets) {
Expand All @@ -225,6 +233,7 @@ func (obj *ocsCephCluster) ensureCreated(r *StorageClusterReconciler, sc *ocsv1.
}
found.Spec = cephCluster.Spec
if err := r.Client.Update(context.TODO(), found); err != nil {
r.Log.Error(err, "Unable to update CephCluster.", "CephCluster", klog.KRef(found.Namespace, found.Name))
return err
}
}
Expand All @@ -235,31 +244,35 @@ func (obj *ocsCephCluster) ensureCreated(r *StorageClusterReconciler, sc *ocsv1.
// ensureDeleted deletes the CephCluster owned by the StorageCluster
func (obj *ocsCephCluster) ensureDeleted(r *StorageClusterReconciler, sc *ocsv1.StorageCluster) error {
cephCluster := &cephv1.CephCluster{}
err := r.Client.Get(context.TODO(), types.NamespacedName{Name: generateNameForCephCluster(sc), Namespace: sc.Namespace}, cephCluster)
cephClusterName := generateNameForCephCluster(sc)
err := r.Client.Get(context.TODO(), types.NamespacedName{Name: cephClusterName, Namespace: sc.Namespace}, cephCluster)
if err != nil {
if errors.IsNotFound(err) {
r.Log.Info("Uninstall: CephCluster not found")
r.Log.Info("Uninstall: CephCluster not found.", "CephCluster", klog.KRef(sc.Namespace, cephClusterName))
return nil
}
r.Log.Error(err, "Uninstall: Unable to retrieve CephCluster.", "CephCluster", klog.KRef(sc.Namespace, cephClusterName))
return fmt.Errorf("Uninstall: Unable to retrieve cephCluster: %v", err)
}

if cephCluster.GetDeletionTimestamp().IsZero() {
r.Log.Info("Uninstall: Deleting cephCluster")
r.Log.Info("Uninstall: Deleting CephCluster.", "CephCluster", klog.KRef(sc.Namespace, cephClusterName))
err = r.Client.Delete(context.TODO(), cephCluster)
if err != nil {
return fmt.Errorf("Uninstall: Failed to delete cephCluster: %v", err)
r.Log.Error(err, "Uninstall: Failed to delete CephCluster.", "CephCluster", klog.KRef(sc.Namespace, cephClusterName))
return fmt.Errorf("uninstall: Failed to delete CephCluster: %v", err)
}
}

err = r.Client.Get(context.TODO(), types.NamespacedName{Name: generateNameForCephCluster(sc), Namespace: sc.Namespace}, cephCluster)
if err != nil {
if errors.IsNotFound(err) {
r.Log.Info("Uninstall: CephCluster is deleted")
r.Log.Info("Uninstall: CephCluster is deleted.", "CephCluster", klog.KRef(sc.Namespace, cephClusterName))
return nil
}
}
return fmt.Errorf("Uninstall: Waiting for cephCluster to be deleted")
r.Log.Error(err, "Uninstall: Waiting for CephCluster to be deleted.", "CephCluster", klog.KRef(sc.Namespace, cephClusterName))
return fmt.Errorf("uninstall: Waiting for CephCluster to be deleted")

}

Expand Down Expand Up @@ -345,7 +358,7 @@ func newCephCluster(sc *ocsv1.StorageCluster, cephImage string, nodeCount int, s
},
}
} else {
reqLogger.Info(fmt.Sprintf("No monDataDirHostPath, monPVCTemplate or storageDeviceSets configured for storageCluster %s", sc.GetName()))
reqLogger.Info("No monDataDirHostPath, monPVCTemplate or storageDeviceSets configured for StorageCluster.", "StorageCluster", klog.KRef(sc.Namespace, sc.Name))
}
if isMultus(sc.Spec.Network) {
cephCluster.Spec.Network.NetworkSpec = *sc.Spec.Network
Expand All @@ -372,7 +385,7 @@ func validateMultusSelectors(selectors map[string]string) error {
return fmt.Errorf("invalid value of the keys for the network selectors. keys should be public and cluster only")
}
if publicNetwork == "" && clusterNetwork == "" {
return fmt.Errorf("Both public and cluster network selector values can't be empty")
return fmt.Errorf("both public and cluster network selector values can't be empty")
}
if publicNetwork == "" {
return fmt.Errorf("public network selector values can't be empty")
Expand Down Expand Up @@ -489,7 +502,7 @@ func getMonCount(nodeCount int, arbiter bool) int {
if override != "" {
count, err := strconv.Atoi(override)
if err != nil {
log.Error(err, "could not decode env var %s", monCountOverrideEnvVar)
log.Error(err, "Could not decode env var.", "monCountOverrideEnvVar", monCountOverrideEnvVar)
} else {
return count
}
Expand Down
25 changes: 16 additions & 9 deletions controllers/storagecluster/cephfilesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)

Expand Down Expand Up @@ -49,6 +50,7 @@ func (r *StorageClusterReconciler) newCephFilesystemInstances(initData *ocsv1.St
for _, obj := range ret {
err := controllerutil.SetControllerReference(initData, obj, r.Scheme)
if err != nil {
r.Log.Error(err, "Unable to set Controller Reference for CephFileSystem.", "CephFileSystem", klog.KRef(obj.Namespace, obj.Name))
return nil, err
}
}
Expand Down Expand Up @@ -76,21 +78,23 @@ func (obj *ocsCephFilesystems) ensureCreated(r *StorageClusterReconciler, instan
return nil
}
if existing.DeletionTimestamp != nil {
r.Log.Info(fmt.Sprintf("Unable to restore init object because %s is marked for deletion", existing.Name))
r.Log.Info("Unable to restore CephFileSystem because it is marked for deletion.", "CephFileSystem", klog.KRef(existing.Namespace, existing.Name))
return fmt.Errorf("failed to restore initialization object %s because it is marked for deletion", existing.Name)
}

r.Log.Info(fmt.Sprintf("Restoring original cephFilesystem %s", cephFilesystem.Name))
r.Log.Info("Restoring original CephFilesystem.", "CephFileSystem", klog.KRef(cephFilesystem.Namespace, cephFilesystem.Name))
existing.ObjectMeta.OwnerReferences = cephFilesystem.ObjectMeta.OwnerReferences
cephFilesystem.ObjectMeta = existing.ObjectMeta
err = r.Client.Update(context.TODO(), cephFilesystem)
if err != nil {
r.Log.Error(err, "Unable to update CephFileSystem.", "CephFileSystem", klog.KRef(cephFilesystem.Namespace, cephFilesystem.Name))
return err
}
case errors.IsNotFound(err):
r.Log.Info(fmt.Sprintf("Creating cephFilesystem %s", cephFilesystem.Name))
r.Log.Info("Creating CephFileSystem.", "CephFileSystem", klog.KRef(cephFilesystem.Namespace, cephFilesystem.Name))
err = r.Client.Create(context.TODO(), cephFilesystem)
if err != nil {
r.Log.Error(err, "Unable to create CephFileSystem.", "CephFileSystem", klog.KRef(cephFilesystem.Namespace, cephFilesystem.Name))
return err
}
}
Expand All @@ -111,28 +115,31 @@ func (obj *ocsCephFilesystems) ensureDeleted(r *StorageClusterReconciler, sc *oc
err := r.Client.Get(context.TODO(), types.NamespacedName{Name: cephFilesystem.Name, Namespace: sc.Namespace}, foundCephFilesystem)
if err != nil {
if errors.IsNotFound(err) {
r.Log.Info("Uninstall: CephFilesystem not found", "CephFilesystem Name", cephFilesystem.Name)
r.Log.Info("Uninstall: CephFileSystem not found.", "CephFileSystem", klog.KRef(cephFilesystem.Namespace, cephFilesystem.Name))
continue
}
return fmt.Errorf("Uninstall: Unable to retrieve cephFilesystem %v: %v", cephFilesystem.Name, err)
r.Log.Error(err, "Uninstall: Unable to retrieve CephFileSystem.", "CephFileSystem", klog.KRef(cephFilesystem.Namespace, cephFilesystem.Name))
return fmt.Errorf("uninstall: Unable to retrieve CephFileSystem %v: %v", cephFilesystem.Name, err)
}

if cephFilesystem.GetDeletionTimestamp().IsZero() {
r.Log.Info("Uninstall: Deleting cephFilesystem", "CephFilesystem Name", cephFilesystem.Name)
r.Log.Info("Uninstall: Deleting cephFilesystem.", "CephFileSystem", klog.KRef(foundCephFilesystem.Namespace, foundCephFilesystem.Name))
err = r.Client.Delete(context.TODO(), foundCephFilesystem)
if err != nil {
return fmt.Errorf("Uninstall: Failed to delete cephFilesystem %v: %v", foundCephFilesystem.Name, err)
r.Log.Error(err, "Uninstall: Failed to delete CephFileSystem.", "CephFileSystem", klog.KRef(foundCephFilesystem.Namespace, foundCephFilesystem.Name))
return fmt.Errorf("uninstall: Failed to delete CephFileSystem %v: %v", foundCephFilesystem.Name, err)
}
}

err = r.Client.Get(context.TODO(), types.NamespacedName{Name: cephFilesystem.Name, Namespace: sc.Namespace}, foundCephFilesystem)
if err != nil {
if errors.IsNotFound(err) {
r.Log.Info("Uninstall: CephFilesystem is deleted", "CephFilesystem Name", cephFilesystem.Name)
r.Log.Info("Uninstall: CephFilesystem is deleted.", "CephFileSystem", klog.KRef(cephFilesystem.Namespace, cephFilesystem.Name))
continue
}
}
return fmt.Errorf("Uninstall: Waiting for cephFilesystem %v to be deleted", cephFilesystem.Name)
r.Log.Error(err, "Uninstall: Waiting for CephFileSystem to be deleted.", "CephFileSystem", klog.KRef(cephFilesystem.Namespace, cephFilesystem.Name))
return fmt.Errorf("uninstall: Waiting for CephFileSystem %v to be deleted", cephFilesystem.Name)

}
return nil
Expand Down
Loading

0 comments on commit 852b8c7

Please sign in to comment.