-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
if [[ -n "$ZSH_VERSION" ]]; then | ||
source "$(dirname "$0")/../shared.sh" | ||
else | ||
source "$(dirname "${BASH_SOURCE[0]}")/../shared.sh" | ||
fi | ||
prevent_to_execute_directly | ||
|
||
kube_run_pod_python() { | ||
local python_version="3.11" | ||
local pod_name="python-${python_version//./}-pod" | ||
local run_as_root=false | ||
local force=false | ||
local user_id=1000 | ||
local group_id=1000 | ||
local username="pyuser" | ||
|
||
while [[ "$#" -gt 0 ]]; do | ||
case $1 in | ||
--root) | ||
run_as_root=true | ||
shift | ||
;; | ||
-f | --force) | ||
force=true | ||
shift | ||
;; | ||
-u | --user) | ||
user_id="$2" | ||
shift 2 | ||
;; | ||
-g | --group) | ||
group_id="$2" | ||
shift 2 | ||
;; | ||
-v | --version) | ||
python_version="$2" | ||
pod_name="python-${python_version//./}-pod" | ||
shift 2 | ||
;; | ||
-h | --help) | ||
echo "Usage: kube_run_pod_python [--root] [-f|--force] [-u|--user <user_id>] [-g|--group <group_id>] [-v|--version <python_version>]" | ||
echo " --root Run as root user (default is non-root)" | ||
echo " -f, --force Force creation of new pod if it already exists" | ||
echo " -u, --user Specify user ID (default: 1000)" | ||
echo " -g, --group Specify group ID (default: 1000)" | ||
echo " -v, --version Specify Python version (default: 3.11)" | ||
return 0 | ||
;; | ||
*) | ||
echo "Unknown parameter passed: $1" | ||
return 1 | ||
;; | ||
esac | ||
done | ||
|
||
if kubectl get pod "$pod_name" &>/dev/null; then | ||
if [ "$force" = true ]; then | ||
echo "Pod $pod_name already exists. Deleting it..." | ||
kubectl delete pod "$pod_name" | ||
sleep 5 | ||
else | ||
echo "Pod $pod_name already exists. Use -f or --force to replace it." | ||
return 1 | ||
fi | ||
fi | ||
|
||
echo "Creating new Python $python_version environment pod..." | ||
|
||
local yaml_template | ||
yaml_template=$( | ||
cat <<EOF | ||
apiVersion: v1 | ||
kind: Pod | ||
metadata: | ||
name: $pod_name | ||
spec: | ||
containers: | ||
- name: python | ||
image: python:$python_version-slim | ||
command: ["/bin/bash", "-c"] | ||
args: | ||
- | | ||
set -e | ||
groupadd -g $group_id $username | ||
useradd -u $user_id -g $group_id -m -s /bin/bash $username | ||
chown $user_id:$group_id /home/$username | ||
echo 'export PATH="/home/$username/.local/bin:$PATH"' >> /home/$username/.bashrc | ||
echo "PS1='\u@\h:\w\$ '" >> /home/$username/.bashrc | ||
mkdir -p /home/$username/.cache/pip | ||
chown -R $user_id:$group_id /home/$username | ||
if [ $user_id -ne 0 ]; then | ||
su - $username -c "pip install --user --upgrade pip" | ||
else | ||
pip install --upgrade pip | ||
fi | ||
echo "Environment setup complete. Running sleep loop..." | ||
while true; do sleep 30; done | ||
env: | ||
- name: HOME | ||
value: /home/$username | ||
- name: PATH | ||
value: /home/$username/.local/bin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin | ||
EOF | ||
) | ||
|
||
if [ "$run_as_root" = false ]; then | ||
yaml_template+=$( | ||
cat <<EOF | ||
securityContext: | ||
runAsUser: 0 | ||
runAsGroup: 0 | ||
EOF | ||
) | ||
fi | ||
|
||
echo "$yaml_template" | kubectl apply -f - | ||
|
||
echo "Waiting for pod to be ready..." | ||
if kubectl wait --for=condition=ready pod/"$pod_name" --timeout=60s; then | ||
echo "Python $python_version environment is ready." | ||
if [ "$run_as_root" = true ]; then | ||
echo "Pod is running as root user." | ||
else | ||
echo "Pod is running as user ID: $user_id, group ID: $group_id" | ||
echo "Use 'kubectl exec -it $pod_name -- su - $username' to start a shell as the user." | ||
fi | ||
echo "Use 'kubectl delete pod $pod_name' to remove the pod when you're done." | ||
else | ||
echo "Failed to create the pod. Please check for any error messages above." | ||
fi | ||
} |