Skip to content

Commit af6a728

Browse files
authored
[Nebius] Don't cache session across multiple requests (#5347)
* Add force_update flag for Nebius credential refresh This update introduces a `force_update` flag to `sdk`, `get_iam_token`, and `get_tenant_id` functions, ensuring credentials can be refreshed on demand. It addresses scenarios where cached values are outdated or invalid, improving reliability during authentication. This change ensures up-to-date credentials are used when necessary. * Optimize Nebius credential management with caching. Added `@annotations.lru_cache(scope='request')` to cache credential-related methods, reducing redundant file reads and improving performance. Simplified credential-handling logic by removing global variables and forced updates, streamlining access for Nebius SDK integration. * Remove unused global variables in Nebius adaptor Eliminated redundant _iam_token, _sdk, _tenant_id, and _project_id variables as they were no longer in use. This helps in reducing unnecessary clutter and improves code maintainability.
1 parent 2ddce82 commit af6a728

File tree

2 files changed

+34
-45
lines changed

2 files changed

+34
-45
lines changed

sky/adaptors/nebius.py

Lines changed: 28 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,6 @@
2929

3030
POLL_INTERVAL = 5
3131

32-
_iam_token = None
33-
_sdk = None
34-
_tenant_id = None
35-
_project_id = None
36-
3732
_IMPORT_ERROR_MESSAGE = ('Failed to import dependencies for Nebius AI Cloud.'
3833
'Try pip install "skypilot[nebius]"')
3934

@@ -81,56 +76,49 @@ def vpc():
8176
return vpc_v1
8277

8378

79+
@annotations.lru_cache(scope='request')
8480
def get_iam_token():
85-
global _iam_token
86-
if _iam_token is None:
87-
try:
88-
with open(os.path.expanduser(NEBIUS_IAM_TOKEN_PATH),
89-
encoding='utf-8') as file:
90-
_iam_token = file.read().strip()
91-
except FileNotFoundError:
92-
return None
93-
return _iam_token
81+
try:
82+
with open(os.path.expanduser(NEBIUS_IAM_TOKEN_PATH),
83+
encoding='utf-8') as file:
84+
return file.read().strip()
85+
except FileNotFoundError:
86+
return None
9487

9588

89+
@annotations.lru_cache(scope='request')
9690
def is_token_or_cred_file_exist():
9791
return (os.path.exists(os.path.expanduser(NEBIUS_IAM_TOKEN_PATH)) or
9892
os.path.exists(os.path.expanduser(NEBIUS_CREDENTIALS_PATH)))
9993

10094

95+
@annotations.lru_cache(scope='request')
10196
def get_project_id():
102-
global _project_id
103-
if _project_id is None:
104-
try:
105-
with open(os.path.expanduser(NEBIUS_PROJECT_ID_PATH),
106-
encoding='utf-8') as file:
107-
_project_id = file.read().strip()
108-
except FileNotFoundError:
109-
return None
110-
return _project_id
97+
try:
98+
with open(os.path.expanduser(NEBIUS_PROJECT_ID_PATH),
99+
encoding='utf-8') as file:
100+
return file.read().strip()
101+
except FileNotFoundError:
102+
return None
111103

112104

105+
@annotations.lru_cache(scope='request')
113106
def get_tenant_id():
114-
global _tenant_id
115-
if _tenant_id is None:
116-
try:
117-
with open(os.path.expanduser(NEBIUS_TENANT_ID_PATH),
118-
encoding='utf-8') as file:
119-
_tenant_id = file.read().strip()
120-
except FileNotFoundError:
121-
return None
122-
return _tenant_id
107+
try:
108+
with open(os.path.expanduser(NEBIUS_TENANT_ID_PATH),
109+
encoding='utf-8') as file:
110+
return file.read().strip()
111+
except FileNotFoundError:
112+
return None
123113

124114

115+
@annotations.lru_cache(scope='request')
125116
def sdk():
126-
global _sdk
127-
if _sdk is None:
128-
if get_iam_token() is not None:
129-
_sdk = nebius.sdk.SDK(credentials=get_iam_token())
130-
return _sdk
131-
_sdk = nebius.sdk.SDK(
132-
credentials_file_name=os.path.expanduser(NEBIUS_CREDENTIALS_PATH))
133-
return _sdk
117+
token = get_iam_token()
118+
if token is not None:
119+
return nebius.sdk.SDK(credentials=token)
120+
return nebius.sdk.SDK(
121+
credentials_file_name=os.path.expanduser(NEBIUS_CREDENTIALS_PATH))
134122

135123

136124
def get_nebius_credentials(boto3_session):

sky/clouds/nebius.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
""" Nebius Cloud. """
2-
import logging
32
import os
43
import typing
54
from typing import Dict, Iterator, List, Optional, Tuple, Union
65

76
from sky import clouds
87
from sky.adaptors import nebius
98
from sky.clouds import service_catalog
9+
from sky.utils import annotations
1010
from sky.utils import registry
1111
from sky.utils import resources_utils
1212

@@ -277,16 +277,16 @@ def _make(instance_list):
277277
fuzzy_candidate_list, None)
278278

279279
@classmethod
280+
@annotations.lru_cache(scope='request')
280281
def _check_compute_credentials(cls) -> Tuple[bool, Optional[str]]:
281282
"""Checks if the user has access credentials to
282283
Nebius's compute service."""
283-
logging.debug('Nebius cloud check credentials')
284284
token_cred_msg = (
285285
f'{_INDENT_PREFIX}Credentials can be set up by running: \n'
286286
f'{_INDENT_PREFIX} $ nebius iam get-access-token > {nebius.NEBIUS_IAM_TOKEN_PATH} \n' # pylint: disable=line-too-long
287-
f'{_INDENT_PREFIX} or generate ~/.nebius/credentials.json')
287+
f'{_INDENT_PREFIX} or generate ~/.nebius/credentials.json \n')
288288

289-
tenant_msg = (f'{_INDENT_PREFIX}Copy your tenat ID from the web console and save it to file \n' # pylint: disable=line-too-long
289+
tenant_msg = (f'{_INDENT_PREFIX} Copy your tenat ID from the web console and save it to file \n' # pylint: disable=line-too-long
290290
f'{_INDENT_PREFIX} $ echo $NEBIUS_TENANT_ID_PATH > {nebius.NEBIUS_TENANT_ID_PATH} \n' # pylint: disable=line-too-long
291291
f'{_INDENT_PREFIX} Or if you have 1 tenant you can run:\n' # pylint: disable=line-too-long
292292
f'{_INDENT_PREFIX} $ nebius --format json iam whoami|jq -r \'.user_profile.tenants[0].tenant_id\' > {nebius.NEBIUS_TENANT_ID_PATH} \n') # pylint: disable=line-too-long
@@ -303,11 +303,12 @@ def _check_compute_credentials(cls) -> Tuple[bool, Optional[str]]:
303303
except nebius.request_error() as e:
304304
return False, (
305305
f'{e.status} \n' # First line is indented by 4 spaces
306-
f'{token_cred_msg}'
306+
f'{token_cred_msg} \n'
307307
f'{tenant_msg}')
308308
return True, None
309309

310310
@classmethod
311+
@annotations.lru_cache(scope='request')
311312
def _check_storage_credentials(cls) -> Tuple[bool, Optional[str]]:
312313
"""Checks if the user has access credentials to Nebius Object Storage.
313314

0 commit comments

Comments
 (0)