Skip to content

Commit

Permalink
Notifications - PR notes, take2 - generalize pvc code
Browse files Browse the repository at this point in the history
Signed-off-by: Amit Prinz Setter <[email protected]>
  • Loading branch information
alphaprinz committed Nov 14, 2024
1 parent dc6a391 commit f36d0bb
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 93 deletions.
35 changes: 21 additions & 14 deletions pkg/system/phase1_verifying.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,13 @@ func (r *Reconciler) ReconcilePhaseVerifying() error {
}

if r.NooBaa.Spec.BucketLogging.LoggingType == nbv1.BucketLoggingTypeGuaranteed {
if err := r.checkBucketLoggingPVC(); err != nil {
if err := r.checkPersistentLoggingPVC(r.NooBaa.Spec.BucketLogging.BucketLoggingPVC, r.BucketLoggingPVC, "InvalidBucketLoggingConfiguration"); err != nil {
return err
}
}

if r.NooBaa.Spec.BucketNotifications.Enabled {
if err := r.checkPersistentLoggingPVC(r.NooBaa.Spec.BucketNotifications.PVC, r.BucketNotificationsPVC, "InvalidBucketNotificationConfiguration"); err != nil {
return err
}
}
Expand Down Expand Up @@ -266,10 +272,11 @@ func (r *Reconciler) checkExternalPg(postgresDbURL string) error {
}

// checkBucketLoggingPVC validates the configuration of bucket logging pvc
func (r *Reconciler) checkBucketLoggingPVC() error {
// Rejecting if 'BucketLoggingPVC' is not provided for 'guaranteed' logging and
// also the operator is not running in the ODF environment.
if r.NooBaa.Spec.BucketLogging.BucketLoggingPVC == nil {
func (r *Reconciler) checkPersistentLoggingPVC(
pvcName *string,
pvc *corev1.PersistentVolumeClaim,
errorName string) error {
if pvc == nil {
sc := &storagev1.StorageClass{
TypeMeta: metav1.TypeMeta{Kind: "StorageClass"},
ObjectMeta: metav1.ObjectMeta{Name: "ocs-storagecluster-cephfs"},
Expand All @@ -278,29 +285,29 @@ func (r *Reconciler) checkBucketLoggingPVC() error {
if util.KubeCheck(sc) {
return nil
}
return util.NewPersistentError("InvalidBucketLoggingConfiguration",
return util.NewPersistentError(errorName,
"'Guaranteed' BucketLogging requires a Persistent Volume Claim (PVC) with ReadWriteMany (RWX) access mode. Please specify the 'BucketLoggingPVC' to ensure guaranteed logging")
}

// Check if pvc exists in the cluster
BucketLoggingPVC := &corev1.PersistentVolumeClaim{
PersistentLoggingPVC := &corev1.PersistentVolumeClaim{
TypeMeta: metav1.TypeMeta{Kind: "PersistenVolumeClaim"},
ObjectMeta: metav1.ObjectMeta{
Name: *r.NooBaa.Spec.BucketLogging.BucketLoggingPVC,
Name: *pvcName,
Namespace: r.Request.Namespace,
},
}
if !util.KubeCheck(BucketLoggingPVC) {
return util.NewPersistentError("InvalidBucketLoggingConfiguration",
fmt.Sprintf("The specified BucketLoggingPVC '%s' was not found", BucketLoggingPVC.Name))
if !util.KubeCheck(PersistentLoggingPVC) {
return util.NewPersistentError(errorName,
fmt.Sprintf("The specified persistent logging pvc '%s' was not found", *pvcName))
}

// Check if pvc supports RWX access mode
for _, accessMode := range BucketLoggingPVC.Spec.AccessModes {
for _, accessMode := range PersistentLoggingPVC.Spec.AccessModes {
if accessMode == corev1.ReadWriteMany {
return nil
}
}
return util.NewPersistentError("InvalidBucketLoggingConfiguration",
fmt.Sprintf("The specified BucketLoggingPVC '%s' does not support RWX access mode", BucketLoggingPVC.Name))
return util.NewPersistentError(errorName,
fmt.Sprintf("The specified persistent logging pvc '%s' does not support RWX access mode", *pvcName))
}
120 changes: 41 additions & 79 deletions pkg/system/phase2_creating.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,25 @@ func (r *Reconciler) ReconcilePhaseCreatingForMainClusters() error {
}
// create bucket logging pvc if not provided by user for 'Guaranteed' logging in ODF env
if r.NooBaa.Spec.BucketLogging.LoggingType == nbv1.BucketLoggingTypeGuaranteed {
if err := r.ReconcileODFBucketLoggingPVC(); err != nil {
if err := r.ReconcileODFPersistentLoggingPVC(
"BucketLoggingPVC",
"InvalidBucketLoggingConfiguration",
"'Guaranteed' BucketLogging requires a Persistent Volume Claim (PVC) with ReadWriteMany (RWX) access mode. Please specify the 'BucketLoggingPVC' to ensure guaranteed logging",
r.NooBaa.Spec.BucketLogging.BucketLoggingPVC,
r.BucketLoggingPVC); err != nil {
return err
}
}

// create notification log pvc if bucket notifications is enabled and pvc was not set explicitly
if r.NooBaa.Spec.BucketNotifications.Enabled {
if err := r.ReconcileODFNotificationsPVC(); err != nil {
return err
if err := r.ReconcileODFPersistentLoggingPVC(
"bucketNotifications.pvc",
"InvalidBucketNotificationConfiguration",
"Bucket notifications requires a Persistent Volume Claim (PVC) with ReadWriteMany (RWX) access mode. Please specify the 'bucketNotifications.pvc'.",
r.NooBaa.Spec.BucketNotifications.PVC,
r.BucketNotificationsPVC); err != nil {
return err
}
}

Expand Down Expand Up @@ -1219,108 +1229,60 @@ func (r *Reconciler) ReconcileDBConfigMap(cm *corev1.ConfigMap, desiredFunc func
return r.isObjectUpdated(result), nil
}

// ReconcileODFBucketLoggingPVC ensures the bucket logging PVC is properly configured for 'guaranteed' logging
func (r *Reconciler) ReconcileODFBucketLoggingPVC() error {
log := r.Logger.WithField("func", "ReconcileBucketLoggingPVC")

// Return if bucket logging PVC already exist
if r.NooBaa.Spec.BucketLogging.BucketLoggingPVC != nil {
r.BucketLoggingPVC.Name = *r.NooBaa.Spec.BucketLogging.BucketLoggingPVC
log.Infof("BucketLoggingPVC %s already exists and supports RWX access mode. Skipping ReconcileODFBucketLoggingPVC.", r.BucketLoggingPVC.Name)
return nil
}

util.KubeCheck(r.BucketLoggingPVC)
if r.BucketLoggingPVC.UID != "" {
log.Infof("BucketLoggingPVC %s already exists. Skipping creation.", r.BucketLoggingPVC.Name)
return nil
}

if err := r.prepareODFBucketLoggingPVC(); err != nil {
return err
}
r.Own(r.BucketLoggingPVC)

log.Infof("BucketLoggingPVC %s does not exist. Creating...", r.BucketLoggingPVC.Name)
err := r.Client.Create(r.Ctx, r.BucketLoggingPVC)
if err != nil {
return err
}

return nil
}

// prepareODFBucketLoggingPVC configures the bucket logging pvc
func (r *Reconciler) prepareODFBucketLoggingPVC() error {
r.BucketLoggingPVC.Spec.AccessModes = []corev1.PersistentVolumeAccessMode{corev1.ReadWriteMany}
func (r *Reconciler) ReconcileODFPersistentLoggingPVC(
fieldName string,
errorName string,
errorText string,
pvcName *string,
pvc *corev1.PersistentVolumeClaim) error {

sc := &storagev1.StorageClass{
TypeMeta: metav1.TypeMeta{Kind: "StorageClass"},
ObjectMeta: metav1.ObjectMeta{Name: "ocs-storagecluster-cephfs"},
}

// fallback to cephfs storageclass to create bucket logging pvc if running on ODF
if util.KubeCheck(sc) {
r.Logger.Infof("BucketLoggingPVC not provided, defaulting to 'cephfs' storage class %s to create bucket logging pvc", sc.Name)
r.BucketLoggingPVC.Spec.StorageClassName = &sc.Name
} else {
return util.NewPersistentError("InvalidBucketLoggingConfiguration",
"'Guaranteed' BucketLogging requires a Persistent Volume Claim (PVC) with ReadWriteMany (RWX) access mode. Please specify the 'BucketLoggingPVC' to ensure guaranteed logging")
}
log := r.Logger.WithField("func", "ReconcileODFPersistentLoggingPVC")

return nil
}

// ReconcileODFNotificationsPVC ensures the bucket notifications PVC is properly configured
func (r *Reconciler) ReconcileODFNotificationsPVC() error {
log := r.Logger.WithField("func", "ReconcileNotificationsPVC")

// Return if notifications PVC already exist
if r.NooBaa.Spec.BucketNotifications.PVC != nil {
r.BucketNotificationsPVC.Name = *r.NooBaa.Spec.BucketNotifications.PVC
log.Infof("BucketNotifications.pvc %s already exists and supports RWX access mode. Skipping ReconcileODFBucketLoggingPVC.", r.BucketNotificationsPVC.Name)
// Return if persistent logging PVC already exists
if pvc != nil {
pvc.Name = *pvcName;
log.Infof("PersistentLoggingPVC %s already exists and supports RWX access mode. Skipping ReconcileODFPersistentLoggingPVC.", *pvcName)
return nil
}

util.KubeCheck(r.BucketNotificationsPVC)
if r.BucketNotificationsPVC.UID != "" {
log.Infof("BucketNotifications.pvc %s already exists. Skipping creation.", r.BucketNotificationsPVC.Name)
util.KubeCheck(pvc)
if pvc.UID != "" {
log.Infof("Persistent logging PVC %s already exists. Skipping creation.", *pvcName )
return nil
}

if err := r.prepareODFNotificationsPVC(); err != nil {
return err
if !r.preparePersistentLoggingPVC(pvc, fieldName) {
return util.NewPersistentError(errorName, errorText)
}
r.Own(r.BucketNotificationsPVC)
r.Own(pvc);

log.Infof("BucketNotifications.pvc %s does not exist. Creating...", r.BucketNotificationsPVC.Name)
err := r.Client.Create(r.Ctx, r.BucketNotificationsPVC)
log.Infof("Persistent loggin PVC %s does not exist. Creating...", *pvcName)
err := r.Client.Create(r.Ctx, pvc)
if err != nil {
return err
}

return nil

}

// prepareODFNotificationsPVC configures the bucket notifications pvc
func (r *Reconciler) prepareODFNotificationsPVC() error {
r.BucketNotificationsPVC.Spec.AccessModes = []corev1.PersistentVolumeAccessMode{corev1.ReadWriteMany}
//prepare persistent logging pvc
func (r *Reconciler) preparePersistentLoggingPVC(pvc *corev1.PersistentVolumeClaim, fieldName string) bool {
pvc.Spec.AccessModes = []corev1.PersistentVolumeAccessMode{corev1.ReadWriteMany}

sc := &storagev1.StorageClass{
TypeMeta: metav1.TypeMeta{Kind: "StorageClass"},
ObjectMeta: metav1.ObjectMeta{Name: "ocs-storagecluster-cephfs"},
}

// fallback to cephfs storageclass to create bucket logging pvc if running on ODF
// fallback to cephfs storageclass to create persistent logging pvc if running on ODF
if util.KubeCheck(sc) {
r.Logger.Infof("BucketNotifications.pvc not provided, defaulting to 'cephfs' storage class %s to create bucket notifications pvc", sc.Name)
r.BucketNotificationsPVC.Spec.StorageClassName = &sc.Name
r.Logger.Infof("%s not provided, defaulting to 'cephfs' storage class %s to create persistent logging pvc", fieldName, sc.Name)
pvc.Spec.StorageClassName = &sc.Name
return true;
} else {
return util.NewPersistentError("InvalidBucketNotificationsConfiguration",
"Bucket Notifications requires a Persistent Volume Claim (PVC) with ReadWriteMany (RWX) access mode. Please specify 'BucketNotifications.pvc'.")
return false;
}

return nil
}

// SetDesiredPostgresDBConf fill desired postgres db config map
Expand Down

0 comments on commit f36d0bb

Please sign in to comment.