Skip to content

Commit 569eed7

Browse files
committed
small refactor, fix test
1 parent a92add0 commit 569eed7

File tree

6 files changed

+49
-40
lines changed

6 files changed

+49
-40
lines changed

datacrunch/containers/containers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
# API endpoints
1010
CONTAINER_DEPLOYMENTS_ENDPOINT = '/container-deployments'
1111
SERVERLESS_COMPUTE_RESOURCES_ENDPOINT = '/serverless-compute-resources'
12-
SECRETS_ENDPOINT = '/secrets'
1312
CONTAINER_REGISTRY_CREDENTIALS_ENDPOINT = '/container-registry-credentials'
13+
SECRETS_ENDPOINT = '/secrets'
1414

1515

1616
class EnvVarType(str, Enum):
@@ -478,7 +478,7 @@ def delete_secret(self, secret_name: str, force: bool = False) -> None:
478478
:type force: bool
479479
"""
480480
self.client.delete(
481-
f"{SECRETS_ENDPOINT}/{secret_name}", params={"force": force})
481+
f"{SECRETS_ENDPOINT}/{secret_name}", params={"force": str(force).lower()})
482482

483483
def get_registry_credentials(self) -> List[RegistryCredential]:
484484
"""Get all registry credentials

examples/containers/container_deployments.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
DATACRUNCH_CLIENT_SECRET = os.environ.get('DATACRUNCH_CLIENT_SECRET')
3636

3737
# DataCrunch client instance
38-
datacrunch = None
38+
datacrunch_client = None
3939

4040

4141
def wait_for_deployment_health(client: DataCrunchClient, deployment_name: str, max_attempts: int = 10, delay: int = 30) -> bool:
@@ -87,8 +87,8 @@ def main() -> None:
8787
return
8888

8989
# Initialize client
90-
global datacrunch
91-
datacrunch = DataCrunchClient(
90+
global datacrunch_client
91+
datacrunch_client = DataCrunchClient(
9292
DATACRUNCH_CLIENT_ID, DATACRUNCH_CLIENT_SECRET)
9393

9494
# Create container configuration
@@ -145,18 +145,19 @@ def main() -> None:
145145
)
146146

147147
# Create the deployment
148-
created_deployment = datacrunch.containers.create(deployment)
148+
created_deployment = datacrunch_client.containers.create(deployment)
149149
print(f"Created deployment: {created_deployment.name}")
150150

151151
# Wait for deployment to be healthy
152-
if not wait_for_deployment_health(datacrunch, DEPLOYMENT_NAME):
152+
if not wait_for_deployment_health(datacrunch_client, DEPLOYMENT_NAME):
153153
print("Deployment health check failed")
154-
cleanup_resources(datacrunch)
154+
cleanup_resources(datacrunch_client)
155155
return
156156

157157
# Update scaling configuration
158158
try:
159-
deployment = datacrunch.containers.get_by_name(DEPLOYMENT_NAME)
159+
deployment = datacrunch_client.containers.get_by_name(
160+
DEPLOYMENT_NAME)
160161
# Create new scaling options with increased replica counts
161162
deployment.scaling = ScalingOptions(
162163
min_replica_count=2,
@@ -177,7 +178,7 @@ def main() -> None:
177178
)
178179
)
179180
)
180-
updated_deployment = datacrunch.containers.update(
181+
updated_deployment = datacrunch_client.containers.update(
181182
DEPLOYMENT_NAME, deployment)
182183
print(f"Updated deployment scaling: {updated_deployment.name}")
183184
except APIException as e:
@@ -186,32 +187,32 @@ def main() -> None:
186187
# Demonstrate deployment operations
187188
try:
188189
# Pause deployment
189-
datacrunch.containers.pause(DEPLOYMENT_NAME)
190+
datacrunch_client.containers.pause(DEPLOYMENT_NAME)
190191
print("Deployment paused")
191192
time.sleep(60)
192193

193194
# Resume deployment
194-
datacrunch.containers.resume(DEPLOYMENT_NAME)
195+
datacrunch_client.containers.resume(DEPLOYMENT_NAME)
195196
print("Deployment resumed")
196197

197198
# Restart deployment
198-
datacrunch.containers.restart(DEPLOYMENT_NAME)
199+
datacrunch_client.containers.restart(DEPLOYMENT_NAME)
199200
print("Deployment restarted")
200201

201202
# Purge queue
202-
datacrunch.containers.purge_queue(DEPLOYMENT_NAME)
203+
datacrunch_client.containers.purge_queue(DEPLOYMENT_NAME)
203204
print("Queue purged")
204205
except APIException as e:
205206
print(f"Error in deployment operations: {e}")
206207

207208
# Clean up
208-
cleanup_resources(datacrunch)
209+
cleanup_resources(datacrunch_client)
209210

210211
except Exception as e:
211212
print(f"Unexpected error: {e}")
212213
# Attempt cleanup even if there was an error
213214
try:
214-
cleanup_resources(datacrunch)
215+
cleanup_resources(datacrunch_client)
215216
except Exception as cleanup_error:
216217
print(f"Error during cleanup after failure: {cleanup_error}")
217218

examples/containers/registry_credentials_example.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
DATACRUNCH_CLIENT_SECRET = os.environ.get('DATACRUNCH_CLIENT_SECRET')
88

99
# Initialize DataCrunch client
10-
client = DataCrunchClient(client_id=DATACRUNCH_CLIENT_ID,
11-
client_secret=DATACRUNCH_CLIENT_SECRET)
10+
datacrunch_client = DataCrunchClient(client_id=DATACRUNCH_CLIENT_ID,
11+
client_secret=DATACRUNCH_CLIENT_SECRET)
1212

1313
# Example 1: DockerHub Credentials
14-
client.containers.add_registry_credentials(
14+
datacrunch_client.containers.add_registry_credentials(
1515
name="my-dockerhub-creds",
1616
registry_type=ContainerRegistryType.DOCKERHUB,
1717
username="your-dockerhub-username",
@@ -20,7 +20,7 @@
2020
print("Created DockerHub credentials")
2121

2222
# Example 2: GitHub Container Registry Credentials
23-
client.containers.add_registry_credentials(
23+
datacrunch_client.containers.add_registry_credentials(
2424
name="my-github-creds",
2525
registry_type=ContainerRegistryType.GITHUB,
2626
username="your-github-username",
@@ -43,15 +43,15 @@
4343
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/your-service-account%40your-project.iam.gserviceaccount.com"
4444
}"""
4545

46-
client.containers.add_registry_credentials(
46+
datacrunch_client.containers.add_registry_credentials(
4747
name="my-gcr-creds",
4848
registry_type=ContainerRegistryType.GCR,
4949
service_account_key=gcr_service_account_key
5050
)
5151
print("Created GCR credentials")
5252

5353
# Example 4: AWS ECR Credentials
54-
client.containers.add_registry_credentials(
54+
datacrunch_client.containers.add_registry_credentials(
5555
name="my-aws-ecr-creds",
5656
registry_type=ContainerRegistryType.AWS_ECR,
5757
access_key_id="your-aws-access-key-id",
@@ -70,9 +70,17 @@
7070
}
7171
}"""
7272

73-
client.containers.add_registry_credentials(
73+
datacrunch_client.containers.add_registry_credentials(
7474
name="my-custom-registry-creds",
7575
registry_type=ContainerRegistryType.CUSTOM,
7676
docker_config_json=custom_docker_config
7777
)
7878
print("Created Custom registry credentials")
79+
80+
# Delete all registry credentials
81+
datacrunch_client.containers.delete_registry_credentials('my-dockerhub-creds')
82+
datacrunch_client.containers.delete_registry_credentials('my-github-creds')
83+
datacrunch_client.containers.delete_registry_credentials('my-gcr-creds')
84+
datacrunch_client.containers.delete_registry_credentials('my-aws-ecr-creds')
85+
datacrunch_client.containers.delete_registry_credentials(
86+
'my-custom-registry-creds')

examples/containers/sglang_deployment.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
CONTAINERS_API_URL = f'https://containers.datacrunch.io/{DEPLOYMENT_NAME}'
4545

4646
# DataCrunch client instance (global for graceful shutdown)
47-
datacrunch = None
47+
datacrunch_client = None
4848

4949

5050
def wait_for_deployment_health(client: DataCrunchClient, deployment_name: str, max_attempts: int = 20, delay: int = 30) -> bool:
@@ -92,7 +92,7 @@ def graceful_shutdown(signum, frame) -> None:
9292
"""Handle graceful shutdown on signals."""
9393
print(f"\nSignal {signum} received, cleaning up resources...")
9494
try:
95-
cleanup_resources(datacrunch)
95+
cleanup_resources(datacrunch_client)
9696
except Exception as e:
9797
print(f"Error during cleanup: {e}")
9898
sys.exit(0)
@@ -176,8 +176,8 @@ def main() -> None:
176176
return
177177

178178
# Initialize client
179-
global datacrunch
180-
datacrunch = DataCrunchClient(
179+
global datacrunch_client
180+
datacrunch_client = DataCrunchClient(
181181
DATACRUNCH_CLIENT_ID, DATACRUNCH_CLIENT_SECRET)
182182

183183
# Register signal handlers for cleanup
@@ -188,12 +188,12 @@ def main() -> None:
188188
print(f"Creating secret for Hugging Face token: {HF_SECRET_NAME}")
189189
try:
190190
# Check if secret already exists
191-
existing_secrets = datacrunch.containers.get_secrets()
191+
existing_secrets = datacrunch_client.containers.get_secrets()
192192
secret_exists = any(
193193
secret.name == HF_SECRET_NAME for secret in existing_secrets)
194194

195195
if not secret_exists:
196-
datacrunch.containers.create_secret(
196+
datacrunch_client.containers.create_secret(
197197
HF_SECRET_NAME, HF_TOKEN)
198198
print(f"Secret '{HF_SECRET_NAME}' created successfully")
199199
else:
@@ -264,14 +264,14 @@ def main() -> None:
264264
)
265265

266266
# Create the deployment
267-
created_deployment = datacrunch.containers.create(deployment)
267+
created_deployment = datacrunch_client.containers.create(deployment)
268268
print(f"Created deployment: {created_deployment.name}")
269269
print("This will take several minutes while the model is downloaded and the server starts...")
270270

271271
# Wait for deployment to be healthy
272-
if not wait_for_deployment_health(datacrunch, DEPLOYMENT_NAME):
272+
if not wait_for_deployment_health(datacrunch_client, DEPLOYMENT_NAME):
273273
print("Deployment health check failed")
274-
cleanup_resources(datacrunch)
274+
cleanup_resources(datacrunch_client)
275275
return
276276

277277
# Get the deployment endpoint URL and inference API key
@@ -301,7 +301,7 @@ def main() -> None:
301301
keep_running = input(
302302
"\nDo you want to keep the deployment running? (y/n): ")
303303
if keep_running.lower() != 'y':
304-
cleanup_resources(datacrunch)
304+
cleanup_resources(datacrunch_client)
305305
else:
306306
print(
307307
f"Deployment {DEPLOYMENT_NAME} is running. Don't forget to delete it when finished.")
@@ -312,7 +312,7 @@ def main() -> None:
312312
print(f"Unexpected error: {e}")
313313
# Attempt cleanup even if there was an error
314314
try:
315-
cleanup_resources(datacrunch)
315+
cleanup_resources(datacrunch_client)
316316
except Exception as cleanup_error:
317317
print(f"Error during cleanup after failure: {cleanup_error}")
318318

examples/containers/update_deployment_scaling.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,19 +94,19 @@ def main() -> None:
9494
return
9595

9696
# Initialize client
97-
client = DataCrunchClient(
97+
datacrunch_client = DataCrunchClient(
9898
DATACRUNCH_CLIENT_ID, DATACRUNCH_CLIENT_SECRET)
9999

100100
# Verify deployment exists
101-
if not check_deployment_exists(client, DEPLOYMENT_NAME):
101+
if not check_deployment_exists(datacrunch_client, DEPLOYMENT_NAME):
102102
print(f"Deployment {DEPLOYMENT_NAME} does not exist.")
103103
return
104104

105105
# Update scaling options using the API
106-
update_deployment_scaling(client, DEPLOYMENT_NAME)
106+
update_deployment_scaling(datacrunch_client, DEPLOYMENT_NAME)
107107

108108
# Get current scaling options
109-
scaling_options = client.containers.get_scaling_options(
109+
scaling_options = datacrunch_client.containers.get_scaling_options(
110110
DEPLOYMENT_NAME)
111111
print(f"\nCurrent scaling configuration:")
112112
print(f"Min replicas: {scaling_options.min_replica_count}")

tests/unit_tests/containers/test_containers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,7 @@ def test_delete_secret(self, containers_service, secrets_endpoint):
684684
# assert
685685
assert responses.assert_call_count(url, 1) is True
686686
request = responses.calls[0].request
687-
assert "force=False" in request.url
687+
assert "force=false" in request.url
688688

689689
@responses.activate
690690
def test_delete_secret_with_force(self, containers_service, secrets_endpoint):
@@ -702,7 +702,7 @@ def test_delete_secret_with_force(self, containers_service, secrets_endpoint):
702702
# assert
703703
assert responses.assert_call_count(url, 1) is True
704704
request = responses.calls[0].request
705-
assert "force=True" in request.url
705+
assert "force=true" in request.url
706706

707707
@responses.activate
708708
def test_get_registry_credentials(self, containers_service, registry_credentials_endpoint):

0 commit comments

Comments
 (0)