From d0d221fae659ccce73df5684fca53e0719dab814 Mon Sep 17 00:00:00 2001 From: Zhanghao Wu Date: Thu, 10 Oct 2024 22:38:25 -0700 Subject: [PATCH] [k8s] Fix rsync for context name with `:` and `/` (#4065) * [kubernetes] Fix context name with colon * comment * remove additional / * Move the encoding to kubernetes only --- sky/utils/command_runner.py | 9 ++++++++- sky/utils/kubernetes/rsync_helper.sh | 8 +++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/sky/utils/command_runner.py b/sky/utils/command_runner.py index 2936e7c5e62..c94970ce764 100644 --- a/sky/utils/command_runner.py +++ b/sky/utils/command_runner.py @@ -831,10 +831,17 @@ def get_remote_home_dir() -> str: # Build command. helper_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'kubernetes', 'rsync_helper.sh') + namespace_context = f'{self.namespace}+{self.context}' + # Avoid rsync interpreting :, /, and + in namespace_context as the + # default delimiter for options and arguments. + # rsync_helper.sh will parse the namespace_context by reverting the + # encoding and pass it to kubectl exec. + encoded_namespace_context = namespace_context.replace( + ':', '%3A').replace('/', '%2F').replace('+', '%2B') self._rsync( source, target, - node_destination=f'{self.pod_name}@{self.namespace}+{self.context}', + node_destination=f'{self.pod_name}@{encoded_namespace_context}', up=up, rsh_option=helper_path, log_path=log_path, diff --git a/sky/utils/kubernetes/rsync_helper.sh b/sky/utils/kubernetes/rsync_helper.sh index 30b63fe6a15..0ee93d8521a 100755 --- a/sky/utils/kubernetes/rsync_helper.sh +++ b/sky/utils/kubernetes/rsync_helper.sh @@ -4,9 +4,15 @@ shift pod=$1 shift -namespace_context=$1 +echo "pod: $pod" >&2 +encoded_namespace_context=$1 +# Revert the encoded namespace+context to the original string. +namespace_context=$(echo "$encoded_namespace_context" | sed 's|%3A|:|g' | sed 's|%2B|+|g' | sed 's|%2F|/|g') +echo "namespace_context: $namespace_context" >&2 namespace=$(echo $namespace_context | cut -d+ -f1) +echo "namespace: $namespace" >&2 context=$(echo $namespace_context | grep '+' >/dev/null && echo $namespace_context | cut -d+ -f2- || echo "") +echo "context: $context" >&2 context_lower=$(echo "$context" | tr '[:upper:]' '[:lower:]') shift if [ -z "$context" ] || [ "$context_lower" = "none" ]; then