Skip to content

Commit 5729275

Browse files
committed
refactor: optional print conversion result
Signed-off-by: thxCode <thxcode0824@gmail.com>
1 parent 68d8e7c commit 5729275

File tree

4 files changed

+84
-29
lines changed

4 files changed

+84
-29
lines changed

gpustack_runtime/deployer/docker.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,13 @@
4242
WorkloadStatusOperation,
4343
WorkloadStatusStateEnum,
4444
)
45-
from .__utils__ import _MiB, bytes_to_human_readable
45+
from .__utils__ import _MiB, bytes_to_human_readable, safe_json
4646

4747
if TYPE_CHECKING:
4848
from collections.abc import Callable, Generator
4949

5050
logger = logging.getLogger(__name__)
51+
clogger = logger.getChild("conversion")
5152

5253
_LABEL_WORKLOAD = f"{envs.GPUSTACK_RUNTIME_DEPLOY_LABEL_PREFIX}/workload"
5354
_LABEL_COMPONENT = f"{envs.GPUSTACK_RUNTIME_DEPLOY_LABEL_PREFIX}/component"
@@ -633,6 +634,13 @@ def _create_pause_container(
633634
elif workload.shm_size:
634635
create_options["shm_size"] = workload.shm_size
635636

637+
if envs.GPUSTACK_RUNTIME_DEPLOY_PRINT_CONVERSION:
638+
clogger.info(
639+
f"Creating pause container %s with options:{os.linesep}%s",
640+
container_name,
641+
safe_json(create_options, indent=2),
642+
)
643+
636644
try:
637645
d_container = self._client.containers.create(
638646
image=self._get_image(workload.pause_image),
@@ -697,6 +705,13 @@ def _create_unhealthy_restart_container(
697705
],
698706
}
699707

708+
if envs.GPUSTACK_RUNTIME_DEPLOY_PRINT_CONVERSION:
709+
clogger.info(
710+
f"Creating unhealthy restart container %s with options:{os.linesep}%s",
711+
container_name,
712+
safe_json(create_options, indent=2),
713+
)
714+
700715
try:
701716
d_container = self._client.containers.create(
702717
image=self._get_image(workload.unhealthy_restart_image),
@@ -1074,6 +1089,13 @@ def _create_containers(
10741089
try:
10751090
if c.profile == ContainerProfileEnum.RUN:
10761091
create_options = self._mutate_create_options(create_options)
1092+
if envs.GPUSTACK_RUNTIME_DEPLOY_PRINT_CONVERSION:
1093+
clogger.info(
1094+
f"Creating container %s with options:{os.linesep}%s",
1095+
container_name,
1096+
safe_json(create_options, indent=2),
1097+
)
1098+
10771099
d_container = self._client.containers.create(
10781100
image=self._get_image(c.image, c.image_pull_policy),
10791101
detach=detach,

gpustack_runtime/deployer/kuberentes.py

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,15 @@
3939
base64_encode,
4040
fnv1a_32_hex,
4141
fnv1a_64_hex,
42+
safe_yaml,
4243
validate_rfc1123_domain_name,
4344
)
4445

4546
if TYPE_CHECKING:
4647
from collections.abc import Callable, Generator
4748

4849
logger = logging.getLogger(__name__)
50+
clogger = logger.getChild("conversion")
4951

5052
_LABEL_WORKLOAD = f"{envs.GPUSTACK_RUNTIME_DEPLOY_LABEL_PREFIX}/workload"
5153
_LABEL_COMPONENT = f"{envs.GPUSTACK_RUNTIME_DEPLOY_LABEL_PREFIX}/component"
@@ -408,6 +410,13 @@ def _create_ephemeral_configmaps(
408410
),
409411
data={"content": content},
410412
)
413+
if envs.GPUSTACK_RUNTIME_DEPLOY_PRINT_CONVERSION:
414+
clogger.info(
415+
f"Creating configmap %s/%s:{os.linesep}%s",
416+
workload.namespace,
417+
config_map_name,
418+
safe_yaml(config_map, indent=2, sort_keys=False),
419+
)
411420

412421
actual_config_map = None
413422
with contextlib.suppress(kubernetes.client.exceptions.ApiException):
@@ -421,9 +430,9 @@ def _create_ephemeral_configmaps(
421430
body=config_map,
422431
)
423432
logger.debug(
424-
"Created configmap %s in namespace %s",
425-
config_map_name,
433+
"Created configmap %s/%s",
426434
workload.namespace,
435+
config_map_name,
427436
)
428437
elif not equal_config_maps(actual_config_map, config_map):
429438
core_api.patch_namespaced_config_map(
@@ -434,9 +443,9 @@ def _create_ephemeral_configmaps(
434443
},
435444
)
436445
logger.debug(
437-
"Updated configmap %s in namespace %s",
438-
config_map_name,
446+
"Updated configmap %s/%s",
439447
workload.namespace,
448+
config_map_name,
440449
)
441450
except kubernetes.client.exceptions.ApiException as e:
442451
msg = f"Failed to create configmap for workload {workload.name}{_detail_api_call_error(e)}"
@@ -727,9 +736,9 @@ def _apply_image_pull_secret(
727736
body=secret,
728737
)
729738
logger.debug(
730-
"Created image pull secret %s in namespace %s",
731-
secret_name,
739+
"Created image pull secret %s/%s",
732740
secret_namespace,
741+
secret_name,
733742
)
734743
elif not equal_secrets(actual_secret, secret):
735744
core_api.patch_namespaced_secret(
@@ -740,9 +749,9 @@ def _apply_image_pull_secret(
740749
},
741750
)
742751
logger.debug(
743-
"Updated image pull secret %s in namespace %s",
744-
secret_name,
752+
"Updated image pull secret %s/%s",
745753
secret_namespace,
754+
secret_name,
746755
)
747756
except kubernetes.client.exceptions.ApiException as e:
748757
msg = f"Failed to create image pull secret{_detail_api_call_error(e)}"
@@ -797,6 +806,13 @@ def _create_service(
797806
],
798807
),
799808
)
809+
if envs.GPUSTACK_RUNTIME_DEPLOY_PRINT_CONVERSION:
810+
clogger.info(
811+
f"Creating service %s/%s:{os.linesep}%s",
812+
workload.namespace,
813+
service_name,
814+
safe_yaml(service, indent=2, sort_keys=False),
815+
)
800816

801817
core_api = kubernetes.client.CoreV1Api(self._client)
802818
try:
@@ -812,9 +828,9 @@ def _create_service(
812828
body=service,
813829
)
814830
logger.debug(
815-
"Created service %s in namespace %s",
816-
service_name,
831+
"Created service %s/%s",
817832
workload.namespace,
833+
service_name,
818834
)
819835
elif not equal_services(actual_service, service):
820836
service = core_api.patch_namespaced_service(
@@ -825,9 +841,9 @@ def _create_service(
825841
},
826842
)
827843
logger.debug(
828-
"Updated service %s in namespace %s",
829-
service_name,
844+
"Updated service %s/%s",
830845
workload.namespace,
846+
service_name,
831847
)
832848
except kubernetes.client.exceptions.ApiException as e:
833849
msg = f"Failed to create service for workload {workload.name}{_detail_api_call_error(e)}"
@@ -1114,10 +1130,17 @@ def _create_pod(
11141130
else:
11151131
pod.spec.containers.append(container)
11161132

1117-
pod = self._mutate_create_pod(pod)
1118-
11191133
core_api = kubernetes.client.CoreV1Api(self._client)
11201134
try:
1135+
pod = self._mutate_create_pod(pod)
1136+
if envs.GPUSTACK_RUNTIME_DEPLOY_PRINT_CONVERSION:
1137+
clogger.info(
1138+
f"Creating pod %s/%s:{os.linesep}%s",
1139+
workload.namespace,
1140+
pod_name,
1141+
safe_yaml(pod, indent=2, sort_keys=False),
1142+
)
1143+
11211144
actual_pod = None
11221145
with contextlib.suppress(kubernetes.client.exceptions.ApiException):
11231146
actual_pod = core_api.read_namespaced_pod(
@@ -1130,9 +1153,9 @@ def _create_pod(
11301153
body=pod,
11311154
)
11321155
logger.debug(
1133-
"Created pod %s in namespace %s",
1134-
pod_name,
1156+
"Created pod %s/%s",
11351157
workload.namespace,
1158+
pod_name,
11361159
)
11371160
elif not equal_pods(actual_pod, pod):
11381161
# Delete the existing Pod first, then create a new one.
@@ -1158,9 +1181,9 @@ def _create_pod(
11581181
body=pod,
11591182
)
11601183
logger.debug(
1161-
"Updated pod %s in namespace %s",
1162-
pod_name,
1184+
"Updated pod %s/%s",
11631185
workload.namespace,
1186+
pod_name,
11641187
)
11651188
except kubernetes.client.exceptions.ApiException as e:
11661189
msg = f"Failed to create pod for workload {workload.name}{_detail_api_call_error(e)}"

gpustack_runtime/envs.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@
7676
"""
7777
Enable detailing the API call error during deployment.
7878
"""
79+
GPUSTACK_RUNTIME_DEPLOY_PRINT_CONVERSION: bool = False
80+
"""
81+
Enable printing the conversion during deployment.
82+
GPUStack Runtime provides a unified Workload definition API,
83+
which will be converted to the specific Container Runtime API calls(e.g., Docker SDK, Kubernetes API).
84+
Enabling this option will print the final converted API calls in INFO log for debugging purposes.
85+
"""
7986
GPUSTACK_RUNTIME_DEPLOY_ASYNC: bool = True
8087
"""
8188
Enable asynchronous deployment.
@@ -275,6 +282,9 @@
275282
"GPUSTACK_RUNTIME_DEPLOY_API_CALL_ERROR_DETAIL": lambda: to_bool(
276283
getenv("GPUSTACK_RUNTIME_DEPLOY_API_CALL_ERROR_DETAIL", "1"),
277284
),
285+
"GPUSTACK_RUNTIME_DEPLOY_PRINT_CONVERSION": lambda: to_bool(
286+
getenv("GPUSTACK_RUNTIME_DEPLOY_PRINT_CONVERSION", "0"),
287+
),
278288
"GPUSTACK_RUNTIME_DEPLOY_ASYNC": lambda: to_bool(
279289
getenv("GPUSTACK_RUNTIME_DEPLOY_ASYNC", "1"),
280290
),

gpustack_runtime/logging.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -157,31 +157,31 @@ def cleanup_logging() -> None:
157157
_LOG_LISTENER = None
158158

159159

160-
def debug_log_exception(logger: logging.Logger, msg: str, *args: Any):
160+
def debug_log_warning(logger: logging.Logger, msg: str, *args: Any):
161161
"""
162-
Log an exception message,
163-
if the logger is enabled for DEBUG and GPUSTACK_RUNTIME_LOG_EXCEPTION is enabled.
162+
Log a warning message,
163+
if the logger is enabled for DEBUG and GPUSTACK_RUNTIME_LOG_WARNING is enabled.
164164
165165
Args:
166166
logger: The logger instance to use.
167167
msg: The message format string.
168168
*args: Arguments to be formatted into the message.
169169
170170
"""
171-
if logger.isEnabledFor(logging.DEBUG) and envs.GPUSTACK_RUNTIME_LOG_EXCEPTION:
172-
logger.exception(msg, *args)
171+
if logger.isEnabledFor(logging.DEBUG) and envs.GPUSTACK_RUNTIME_LOG_WARNING:
172+
logger.warning(msg, *args)
173173

174174

175-
def debug_log_warning(logger: logging.Logger, msg: str, *args: Any):
175+
def debug_log_exception(logger: logging.Logger, msg: str, *args: Any):
176176
"""
177-
Log a warning message,
178-
if the logger is enabled for DEBUG and GPUSTACK_RUNTIME_LOG_WARNING is enabled.
177+
Log an exception message,
178+
if the logger is enabled for DEBUG and GPUSTACK_RUNTIME_LOG_EXCEPTION is enabled.
179179
180180
Args:
181181
logger: The logger instance to use.
182182
msg: The message format string.
183183
*args: Arguments to be formatted into the message.
184184
185185
"""
186-
if logger.isEnabledFor(logging.DEBUG) and envs.GPUSTACK_RUNTIME_LOG_WARNING:
187-
logger.warning(msg, *args)
186+
if logger.isEnabledFor(logging.DEBUG) and envs.GPUSTACK_RUNTIME_LOG_EXCEPTION:
187+
logger.exception(msg, *args)

0 commit comments

Comments
 (0)