forked from kubeflow/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase.py
More file actions
214 lines (174 loc) · 6.1 KB
/
base.py
File metadata and controls
214 lines (174 loc) · 6.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# Copyright 2025 The Kubeflow Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Container client adapters for Docker and Podman.
This module implements the adapter pattern to abstract away differences between
Docker and Podman APIs, allowing the backend to work with either runtime through
a common interface.
"""
from __future__ import annotations
import abc
from collections.abc import Iterator
from typing import Optional
class BaseContainerClientAdapter(abc.ABC):
"""
Abstract adapter interface for container clients.
This adapter abstracts the container runtime API, allowing the backend
to work with Docker and Podman through a unified interface.
"""
@abc.abstractmethod
def ping(self):
"""Test the connection to the container runtime."""
raise NotImplementedError()
@abc.abstractmethod
def create_network(
self,
name: str,
labels: dict[str, str],
) -> str:
"""
Create a container network.
Args:
name: Network name
labels: Labels to attach to the network
Returns:
Network ID or name
"""
raise NotImplementedError()
@abc.abstractmethod
def delete_network(self, network_id: str):
"""Delete a network."""
raise NotImplementedError()
@abc.abstractmethod
def create_and_start_container(
self,
image: str,
command: list[str],
name: str,
network_id: str,
environment: dict[str, str],
labels: dict[str, str],
volumes: dict[str, dict[str, str]],
working_dir: str,
) -> str:
"""
Create and start a container.
Args:
image: Container image
command: Command to run
name: Container name
network_id: Network to attach to
environment: Environment variables
labels: Container labels
volumes: Volume mounts
working_dir: Working directory
Returns:
Container ID
"""
raise NotImplementedError()
@abc.abstractmethod
def get_container(self, container_id: str):
"""Get container object by ID."""
raise NotImplementedError()
@abc.abstractmethod
def container_logs(self, container_id: str, follow: bool) -> Iterator[str]:
"""Stream logs from a container."""
raise NotImplementedError()
@abc.abstractmethod
def stop_container(self, container_id: str, timeout: int = 10):
"""Stop a container."""
raise NotImplementedError()
@abc.abstractmethod
def remove_container(self, container_id: str, force: bool = True):
"""Remove a container."""
raise NotImplementedError()
@abc.abstractmethod
def pull_image(self, image: str):
"""Pull an image."""
raise NotImplementedError()
@abc.abstractmethod
def image_exists(self, image: str) -> bool:
"""Check if an image exists locally."""
raise NotImplementedError()
@abc.abstractmethod
def run_oneoff_container(self, image: str, command: list[str]) -> str:
"""
Run a short-lived container and return its output.
Args:
image: Container image
command: Command to run
Returns:
Container output as string
"""
raise NotImplementedError()
@abc.abstractmethod
def container_status(self, container_id: str) -> tuple[str, Optional[int]]:
"""
Get container status.
Returns:
Tuple of (status_string, exit_code)
Status strings: "running", "created", "exited", etc.
Exit code is None if container hasn't exited
"""
raise NotImplementedError()
@abc.abstractmethod
def get_container_ip(self, container_id: str, network_id: str) -> Optional[str]:
"""
Get container's IP address on a specific network.
Args:
container_id: Container ID
network_id: Network name or ID
Returns:
IP address string or None if not found
"""
raise NotImplementedError()
@abc.abstractmethod
def list_containers(self, filters: Optional[dict[str, list[str]]] = None) -> list[dict]:
"""
List containers, optionally filtered by labels.
Args:
filters: Dictionary of filters (e.g., {"label": ["key=value"]})
Returns:
List of container info dictionaries with keys:
- id: Container ID
- name: Container name
- labels: Dictionary of labels
- status: Container status
- created: Creation timestamp
"""
raise NotImplementedError()
@abc.abstractmethod
def get_network(self, network_id: str) -> Optional[dict]:
"""
Get network information by ID or name.
Args:
network_id: Network ID or name
Returns:
Dictionary with network info including labels, or None if not found
"""
raise NotImplementedError()
@abc.abstractmethod
def wait_for_container(self, container_id: str, timeout: Optional[int] = None) -> int:
"""
Wait for a container to exit and return its exit code.
This is a blocking call that waits until the container stops.
Args:
container_id: Container ID
timeout: Maximum time to wait in seconds, or None to wait indefinitely
Returns:
Container exit code
Raises:
TimeoutError: If timeout is reached before container exits
"""
raise NotImplementedError()