func (r *ReconcileBackup) isAllCreated(bkp *v1alpha1.Backup) error {
	if !r.isDbPodFound() {
		err := fmt.Errorf("Error: Database Pod is missing")
		return err
	}
	if !r.isDbServiceFound() {
		err := fmt.Errorf("Error: Database Service is missing")
		return err
	}
	dbSecretName := utils.DbSecretPrefix + bkp.Name
	_, err := service.FetchSecret(bkp.Namespace, dbSecretName, r.client)
	if err != nil {
		err = fmt.Errorf("Error: DB Secret is missing. (%v)", dbSecretName)
		return err
	}
	awsSecretName := utils.GetAWSSecretName(bkp)
	awsSecretNamespace := utils.GetAwsSecretNamespace(bkp)
	_, err = service.FetchSecret(awsSecretNamespace, awsSecretName, r.client)
	if err != nil {
		err := fmt.Errorf("Error: AWS Secret is missing. (name:%v,namespace:%v)", awsSecretName, awsSecretNamespace)
		return err
	}
	if utils.IsEncryptionKeyOptionConfig(bkp) {
		encSecretName := utils.GetEncSecretName(bkp)
		encSecretNamespace := utils.GetEncSecretNamespace(bkp)
		_, err := service.FetchSecret(encSecretNamespace, encSecretName, r.client)
		if err != nil {
			err := fmt.Errorf("Error: Encript Key Secret is missing. (name:%v,namespace:%v)", encSecretName, encSecretNamespace)
			return err
		}
	}
	_, err = service.FetchCronJob(bkp.Name, bkp.Namespace, r.client)
	if err != nil {
		err := fmt.Errorf("Error: CronJob is missing")
		return err
	}
	return nil
}
isAllCreated returns error when some resource is missing
package backup
import (
	"context"
	"fmt"
	"github.com/dev4devs-com/postgresql-operator/pkg/apis/postgresql/v1alpha1"
	"github.com/dev4devs-com/postgresql-operator/pkg/service"
	"github.com/dev4devs-com/postgresql-operator/pkg/utils"
	"k8s.io/api/batch/v1beta1"
	corev1 "k8s.io/api/core/v1"
	"reflect"
	"sigs.k8s.io/controller-runtime/pkg/reconcile"
)
const statusOk = "OK"
func (r *ReconcileBackup) updateBackupStatus(request reconcile.Request) error {
	bkp, err := service.FetchBackupCR(request.Name, request.Namespace, r.client)
	if err != nil {
		return err
	}
	statusMsgUpdate := statusOk
	if err := r.isAllCreated(bkp); err != nil {
		statusMsgUpdate = err.Error()
	}
	if err := r.insertUpdateBackupStatus(bkp, statusMsgUpdate); err != nil {
		return err
	}
	return nil
}
func (r *ReconcileBackup) insertUpdateBackupStatus(bkp *v1alpha1.Backup, statusMsgUpdate string) error {
	if statusMsgUpdate != bkp.Status.BackupStatus {
		bkp.Status.BackupStatus = statusMsgUpdate
		if err := r.client.Status().Update(context.TODO(), bkp); err != nil {
			return err
		}
	}
	return nil
}
func (r *ReconcileBackup) updateCronJobStatus(request reconcile.Request) error {
	bkp, err := service.FetchBackupCR(request.Name, request.Namespace, r.client)
	if err != nil {
		return err
	}
	cronJob, err := service.FetchCronJob(bkp.Name, bkp.Namespace, r.client)
	if err != nil {
		return err
	}
	if err := r.insertUpdateCronJobStatus(cronJob, bkp); err != nil {
		return err
	}
	return nil
}
func (r *ReconcileBackup) insertUpdateCronJobStatus(cronJob *v1beta1.CronJob, bkp *v1alpha1.Backup) error {
	if cronJob.Name != bkp.Status.CronJobName || !reflect.DeepEqual(cronJob.Status, bkp.Status.CronJobStatus) {
		bkp.Status.CronJobStatus = cronJob.Status
		bkp.Status.CronJobName = cronJob.Name
		if err := r.client.Status().Update(context.TODO(), bkp); err != nil {
			return err
		}
	}
	return nil
}
func (r *ReconcileBackup) updateAWSSecretStatus(request reconcile.Request) error {
	bkp, err := service.FetchBackupCR(request.Name, request.Namespace, r.client)
	if err != nil {
		return err
	}
	aws, err := service.FetchSecret(utils.GetAwsSecretNamespace(bkp), utils.GetAWSSecretName(bkp), r.client)
	if err != nil {
		return err
	}
	if err := r.insertUpdateAwsSecretStatus(aws, bkp); err != nil {
		return err
	}
	return nil
}
func (r *ReconcileBackup) insertUpdateAwsSecretStatus(aws *corev1.Secret, bkp *v1alpha1.Backup) error {
	if isAwsStatusEqual(aws, bkp) {
		bkp.Status.AWSSecretName = aws.Name
		bkp.Status.AwsCredentialsSecretNamespace = aws.Namespace
		if err := r.client.Status().Update(context.TODO(), bkp); err != nil {
			return err
		}
	}
	return nil
}
func isAwsStatusEqual(aws *corev1.Secret, bkp *v1alpha1.Backup) bool {
	return aws.Name != bkp.Status.AWSSecretName || aws.Namespace != bkp.Status.AwsCredentialsSecretNamespace
}
func (r *ReconcileBackup) updateEncSecretStatus(request reconcile.Request) error {
	bkp, err := service.FetchBackupCR(request.Name, request.Namespace, r.client)
	if err != nil {
		return err
	}
	isEncryptionKeyOptionConfig := utils.IsEncryptionKeyOptionConfig(bkp)
	if isEncryptionKeyOptionConfig {
		secret, err := service.FetchSecret(utils.GetEncSecretNamespace(bkp), utils.GetEncSecretName(bkp), r.client)
		if err != nil {
			return err
		}
		if err := r.insertUpdateEncKeyStatus(secret, bkp); err != nil {
			return err
		}
	}
	if isEncryptionKeyOptionConfig != bkp.Status.HasEncryptKey {
		bkp.Status.HasEncryptKey = isEncryptionKeyOptionConfig
		if err := r.client.Status().Update(context.TODO(), bkp); err != nil {
			return err
		}
	}
	return nil
}
func (r *ReconcileBackup) insertUpdateEncKeyStatus(secret *corev1.Secret, bkp *v1alpha1.Backup) error {
	if isEncryptKeyStatusEquals(secret, bkp) {
		bkp.Status.EncryptKeySecretName = secret.Name
		bkp.Status.EncryptKeySecretNamespace = secret.Namespace
		if err := r.client.Status().Update(context.TODO(), bkp); err != nil {
			return err
		}
	}
	return nil
}
func isEncryptKeyStatusEquals(secret *corev1.Secret, bkp *v1alpha1.Backup) bool {
	return secret.Name != bkp.Status.EncryptKeySecretName || secret.Namespace != bkp.Status.EncryptKeySecretNamespace
}
func (r *ReconcileBackup) updateDBSecretStatus(request reconcile.Request) error {
	bkp, err := service.FetchBackupCR(request.Name, request.Namespace, r.client)
	if err != nil {
		return err
	}
	dbSecret, err := service.FetchSecret(bkp.Namespace, utils.DbSecretPrefix+bkp.Name, r.client)
	if err != nil {
		return err
	}
	if err := r.insertUpdateDBSecretStatus(dbSecret, bkp); err != nil {
		return err
	}
	return nil
}
func (r *ReconcileBackup) insertUpdateDBSecretStatus(dbSecret *corev1.Secret, bkp *v1alpha1.Backup) error {
	if dbSecret.Name != bkp.Status.DBSecretName {
		bkp.Status.DBSecretName = dbSecret.Name
		if err := r.client.Status().Update(context.TODO(), bkp); err != nil {
			return err
		}
	}
	return nil
}
func (r *ReconcileBackup) updatePodDatabaseFoundStatus(request reconcile.Request) error {
	bkp, err := service.FetchBackupCR(request.Name, request.Namespace, r.client)
	if err != nil {
		return err
	}
	if err := r.insertUpdatePodDbFoundStatus(bkp); err != nil {
		return err
	}
	return nil
}
func (r *ReconcileBackup) insertUpdatePodDbFoundStatus(bkp *v1alpha1.Backup) error {
	if r.isDbPodFound() != bkp.Status.IsDatabasePodFound {
		bkp.Status.IsDatabasePodFound = r.isDbPodFound()
		if err := r.client.Status().Update(context.TODO(), bkp); err != nil {
			return err
		}
	}
	return nil
}
func (r *ReconcileBackup) updateDbServiceFoundStatus(request reconcile.Request) error {
	bkp, err := service.FetchBackupCR(request.Name, request.Namespace, r.client)
	if err != nil {
		return err
	}
	if err := r.insertUpdateDbServiceFoundStatus(bkp); err != nil {
		return err
	}
	return nil
}
func (r *ReconcileBackup) insertUpdateDbServiceFoundStatus(bkp *v1alpha1.Backup) error {
	if r.isDbServiceFound() != bkp.Status.IsDatabaseServiceFound {
		bkp.Status.IsDatabaseServiceFound = r.isDbServiceFound()
		if err := r.client.Status().Update(context.TODO(), bkp); err != nil {
			return err
		}
	}
	return nil
}
func (r *ReconcileBackup) isDbServiceFound() bool {
	return &r.dbService != nil && len(r.dbService.Name) > 0
}
func (r *ReconcileBackup) isDbPodFound() bool {
	return &r.dbService != nil && len(r.dbService.Name) > 0
}