def check_pg_quota()

in nuvolaris/quota_checker.py [0:0]


def check_pg_quota(pg_client:PostgresClient, pg_dbsize_list, pg_wsku, check_ferretdb=False):
    """
    Check PG quota as both Postgres and FerretDB databases.
    """
    logging.info("***** Checking Postgres database limit ****") 

    if len(pg_wsku) == 0:
        logging.info("no nuvolaris wsku resource with PG based database quota limit found!")
        return

    for wsku in pg_wsku:
        spec = wsku['spec']
        metadata = wsku['metadata']
        wsku_name = metadata['name']
        quota_annotation = check_ferretdb and FERRRET_DB_QUOTA_ANNOTATION or POSTGRES_DB_QUOTA_ANNOTATION
        quota_applied = "false"

        pg_db= check_ferretdb and f"{spec['mongodb']['database']}_ferretdb" or spec['postgres']['database']
        pg_db_quota = check_ferretdb and int(spec['mongodb']['quota'])*1014*1024 or int(spec['postgres']['quota'])*1024*1024

        # Check if the quota annotations has been already applied
        if "annotations" in metadata and quota_annotation in metadata["annotations"]:
            quota_applied = metadata["annotations"][quota_annotation]

        logging.info(f"PG database {pg_db} enforced quota is set to {pg_db_quota} bytes")
        if pg_db in pg_dbsize_list:
            current_pg_db_quota = pg_dbsize_list[pg_db]
            
            if current_pg_db_quota >= pg_db_quota:
                if quota_applied in ["false"]:
                    block_pg_user_quota(pg_client, 
                                wsku_name, 
                                pg_db,
                                quota_annotation)
                    continue    
                else:
                    logging.info(f"***** Postgres DB {pg_db} size {current_pg_db_quota} is exceeding quota limit {pg_db_quota}, but revoke has been already executed ****")
                    continue
            
            if current_pg_db_quota < pg_db_quota and quota_applied in ["true"]:
                reset_pg_user_quota(pg_client, 
                              wsku_name, 
                              pg_db,
                              quota_annotation)
                continue 

            logging.info(f"***** Postgres DB {pg_db} size {current_pg_db_quota} is not exceeding quota limit {pg_db_quota} ****")    
        else:
            logging.warning(f"**** PG database {pg_db} missing from Postgres DB allocated size")