22from datetime import datetime
33from azure .storage .blob import BlobServiceClient
44from azure .core .exceptions import ResourceExistsError
5- from src .storage .base import BaseStorage
5+ from src .storage .base import Storage
6+ from src .kubernetes import Storage
67
7- class AzureStorage (BaseStorage ):
8- """
9- Azure Blob Storage provider implementation in pure Python.
10- Container format: <cluster_name-namespace>
11- """
128
13- def __init__ (self , storage_address : str , storage_username : str , storage_password : str ):
14- if ".net/" in storage_address :
15- self .account_url = storage_address .split (".net/" )[0 ] + ".net"
16- else :
17- self .account_url = storage_address
18-
19- self .account_key = storage_password
20-
21- namespace = "unknown-namespace"
22- ns_path = "/var/run/secrets/kubernetes.io/serviceaccount/namespace"
23- if os .path .exists (ns_path ):
24- with open (ns_path , "r" ) as f :
25- namespace = f .read ().strip ()
26-
27- cluster_name = os .getenv ("CLUSTER_NAME" )
28- if not cluster_name :
29- account_name = self .account_url .replace ("https://" , "" ).replace ("http://" , "" ).split ("." )[0 ]
30- cluster_name = account_name .replace ("-backup" , "" ).replace ("backup" , "" )
9+ class AzureStorage (Storage ):
3110
11+ def __init__ (self , kube , storage_address : str , storage_username : str , storage_password : str ):
12+ cluster_name = kube .get_cluster_name ()
13+ namespace = kube .get_namespace ()
3214 self .blob_container = f"{ cluster_name } -{ namespace } " .lower ().replace ("_" , "-" )
33-
15+ self .storage_address = storage_address
16+ self .storage_password = storage_password
3417 self .blob_service_client = BlobServiceClient (
35- account_url = self .account_url ,
36- credential = self .account_key
37- )
38-
39- self . _ensure_container_exists ()
18+ account_url = self .storage_address ,
19+ credential = self .storage_password
20+ )
21+ self . _ensure_blob_container_exists ()
22+
4023
41- def _ensure_container_exists (self ):
42- """
43- Creates the Azure container if it does not exist.
44- """
24+ def _ensure_blob_container_exists (self ):
25+ """Creates the Azure container if it does not exist."""
4526 try :
46- print (f"info: checking or creating Azure container '{ self .blob_container } '..." , flush = True )
27+ print (f"info: checking or creating Azure container '{ self .blob_container } '..." )
4728 self .blob_service_client .create_container (name = self .blob_container )
48- print (f"info: container '{ self .blob_container } ' successfully created." , flush = True )
29+ print (f"info: container '{ self .blob_container } ' successfully created." )
4930 except ResourceExistsError :
50- print (f"info: container '{ self .blob_container } ' already exists. Reusing it." , flush = True )
31+ print (f"info: container '{ self .blob_container } ' already exists. Reusing it." )
5132 except Exception as e :
52- print (f"error: failed to verify/create Azure container: { e } " , flush = True )
33+ raise RuntimeError (f"Critical: Failed to verify/create Azure container: { e } " )
34+
5335
5436 def upload_stream (self , pvc_name : str , path : str , data_generator ) -> bool :
5537 date_str = datetime .utcnow ().strftime ("%Y%m%d-%H%M%S" )
@@ -63,11 +45,11 @@ def upload_stream(self, pvc_name: str, path: str, data_generator) -> bool:
6345 blob = blob_name
6446 )
6547
66- print (f"info: uploading directly to '{ blob_name } '..." , flush = True )
48+ print (f"info: uploading directly to '{ blob_name } '..." )
6749
6850 # The Azure SDK natively supports data generators for chunked upload
6951 blob_client .upload_blob (data_generator , overwrite = True , max_concurrency = 2 )
7052 return True
7153 except Exception as e :
72- print (f"error: azure upload failed: { e } " , flush = True )
54+ print (f"error: azure upload failed: { e } " )
7355 return False
0 commit comments