Skip to content

Commit a21dad4

Browse files
authored
Merge pull request #40 from DataCrunch-io/feature/filesecret-management
support for fileset management and example
2 parents 35512e6 + 728180d commit a21dad4

File tree

7 files changed

+101
-1
lines changed

7 files changed

+101
-1
lines changed

CHANGELOG.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
Changelog
22
=========
33

4+
* Added support for fileset secrets
5+
46
v1.11.0 (2025-04-28)
57
--------------------
68

datacrunch/containers/containers.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
creation, updates, deletion, and monitoring of containerized applications.
55
"""
66

7+
import base64
8+
import os
79
from dataclasses import dataclass, field
810
from dataclasses_json import dataclass_json, Undefined # type: ignore
911
from typing import List, Optional, Dict, Any
@@ -18,6 +20,7 @@
1820
SERVERLESS_COMPUTE_RESOURCES_ENDPOINT = '/serverless-compute-resources'
1921
CONTAINER_REGISTRY_CREDENTIALS_ENDPOINT = '/container-registry-credentials'
2022
SECRETS_ENDPOINT = '/secrets'
23+
FILESET_SECRETS_ENDPOINT = '/file-secrets'
2124

2225

2326
class EnvVarType(str, Enum):
@@ -27,6 +30,13 @@ class EnvVarType(str, Enum):
2730
SECRET = "secret"
2831

2932

33+
class SecretType(str, Enum):
34+
"""Types of secrets that can be set in containers."""
35+
36+
GENERIC = "generic" # Regular secret, can be used in env vars
37+
FILESET = "file-secret" # A file secret that can be mounted into the container
38+
39+
3040
class VolumeMountType(str, Enum):
3141
"""Types of volume mounts that can be configured for containers."""
3242

@@ -446,10 +456,12 @@ class Secret:
446456
Attributes:
447457
name: Name of the secret.
448458
created_at: Timestamp when the secret was created.
459+
secret_type: Type of the secret.
449460
"""
450461

451462
name: str
452463
created_at: str
464+
secret_type: SecretType
453465

454466

455467
@dataclass_json
@@ -909,6 +921,7 @@ def get_secrets(self) -> List[Secret]:
909921
List[Secret]: List of all secrets.
910922
"""
911923
response = self.client.get(SECRETS_ENDPOINT)
924+
print(response.json())
912925
return [Secret.from_dict(secret) for secret in response.json()]
913926

914927
def create_secret(self, name: str, value: str) -> None:
@@ -956,3 +969,42 @@ def delete_registry_credentials(self, credentials_name: str) -> None:
956969
"""
957970
self.client.delete(
958971
f"{CONTAINER_REGISTRY_CREDENTIALS_ENDPOINT}/{credentials_name}")
972+
973+
def get_fileset_secrets(self) -> List[Secret]:
974+
"""Retrieves all fileset secrets.
975+
976+
Returns:
977+
List of all fileset secrets.
978+
"""
979+
response = self.client.get(FILESET_SECRETS_ENDPOINT)
980+
return [Secret.from_dict(secret) for secret in response.json()]
981+
982+
def delete_fileset_secret(self, secret_name: str) -> None:
983+
"""Deletes a fileset secret.
984+
985+
Args:
986+
secret_name: Name of the secret to delete.
987+
"""
988+
self.client.delete(f"{FILESET_SECRETS_ENDPOINT}/{secret_name}")
989+
990+
def create_fileset_secret_from_file_paths(self, secret_name: str, file_paths: List[str]) -> None:
991+
"""Creates a new fileset secret.
992+
A fileset secret is a secret that contains several files,
993+
and can be used to mount a directory with the files in a container.
994+
995+
Args:
996+
secret_name: Name of the secret.
997+
file_paths: List of file paths to include in the secret.
998+
"""
999+
processed_files = []
1000+
for file_path in file_paths:
1001+
with open(file_path, "rb") as f:
1002+
base64_content = base64.b64encode(f.read()).decode("utf-8")
1003+
processed_files.append({
1004+
"file_name": os.path.basename(file_path),
1005+
"base64_content": base64_content
1006+
})
1007+
self.client.post(FILESET_SECRETS_ENDPOINT, {
1008+
"name": secret_name,
1009+
"files": processed_files
1010+
})
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Fileset Secrets
2+
===============
3+
4+
This example shows how to manage fileset secrets for container deployments in DataCrunch.
5+
Fileset secrets are a way to mount a directory with files into a container.
6+
7+
.. literalinclude:: ../../../../examples/containers/fileset_secret_example.py
8+
:language: python
9+
:caption: Fileset Secrets

docs/source/examples/containers/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ This section contains examples demonstrating how to work with containers in Data
1212
environment_variables
1313
registry_credentials
1414
secrets
15+
fileset_secrets
1516
sglang
1617
scaling
1718
inference_async

examples/containers/container_deployments_example.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,16 @@ def main() -> None:
9696
path="/health"
9797
),
9898
volume_mounts=[
99+
# Shared memory volume
99100
VolumeMount(
100101
type=VolumeMountType.SCRATCH,
101102
mount_path="/data"
103+
),
104+
# Fileset secret
105+
VolumeMount(
106+
type=VolumeMountType.SECRET,
107+
mount_path="/path/to/mount",
108+
name="my-fileset-secret" # This fileset secret must be created beforehand
102109
)
103110
],
104111
env=[
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import os
2+
from datacrunch import DataCrunchClient
3+
4+
# Fileset secrets are a way to mount sensitive files like API keys, certs, and credentials securely inside a container, without hardcoding them in the image or env vars.
5+
# This example demonstrates how to create a fileset secret containing two files from your local filesystem
6+
7+
# Get client secret and id from environment variables
8+
DATACRUNCH_CLIENT_ID = os.environ.get('DATACRUNCH_CLIENT_ID')
9+
DATACRUNCH_CLIENT_SECRET = os.environ.get('DATACRUNCH_CLIENT_SECRET')
10+
11+
# Initialize the client with your credentials
12+
datacrunch = DataCrunchClient(DATACRUNCH_CLIENT_ID, DATACRUNCH_CLIENT_SECRET)
13+
14+
# Define the secret name and the file paths from your local filesystem where this script is running
15+
SECRET_NAME = "my-fileset-secret"
16+
RELATIVE_FILE_PATH = "./relative-path/file1.txt"
17+
ABSOLUTE_FILE_PATH = "/home/username/absolute-path/file2.json"
18+
19+
# Create the fileset secret that has 2 files
20+
fileset_secret = datacrunch.containers.create_fileset_secret_from_file_paths(
21+
secret_name=SECRET_NAME, file_paths=[RELATIVE_FILE_PATH, ABSOLUTE_FILE_PATH])
22+
23+
# Get the secret
24+
secrets = datacrunch.containers.get_fileset_secrets()
25+
print(secrets)
26+
27+
# Delete the secret
28+
datacrunch.containers.delete_fileset_secret(secret_name=SECRET_NAME)

tests/unit_tests/containers/test_containers.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@
134134
SECRETS_DATA = [
135135
{
136136
"name": SECRET_NAME,
137-
"created_at": "2023-01-01T00:00:00+00:00"
137+
"created_at": "2023-01-01T00:00:00+00:00",
138+
"secret_type": "generic"
138139
}
139140
]
140141

0 commit comments

Comments
 (0)