From a8eaabca953abc8cb493c369aa1c88041fb72b09 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 12 Nov 2026 21:59:51 +0000 Subject: [PATCH] Fix bashrc interactive guard, bake kubectl completion, load for any HOME - The banner guard tested $PS1, which the file exports two lines earlier, so it was always true. Use [[ $- == *i* ]] and return early for non-interactive shells. - `source <(kubectl completion bash)` forked kubectl on every shell start. Generate the completion file once at image build time and source it. - Install the config as /etc/bash.bashrc instead of /root/.bashrc so it applies when the pod runs the debug container with a different UID/HOME; stub out /root/.bashrc so Ubuntu's skel copy cannot reset PS1. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_014iDTyB5N8EdZyyZo4ymUVR --- Dockerfile | 8 +++++++- bashrc | 21 +++++++++++++++++---- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/Dockerfile b/Dockerfile index 4eedb03..6cff0c1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -65,8 +65,14 @@ RUN set -eux; \ | tar -xz -C /usr/local/bin stern # ---- Shell experience ------------------------------------------------------- +# kubectl completion is generated once at build time (not on every shell start). +RUN kubectl completion bash > /usr/share/bash-completion/completions/kubectl + COPY motd.txt /etc/motd -COPY bashrc /root/.bashrc +# /etc/bash.bashrc applies to any HOME/UID; /root/.bashrc (Ubuntu's skel copy, +# which would reset PS1 afterwards) is replaced with a stub. +COPY bashrc /etc/bash.bashrc +RUN echo '# see /etc/bash.bashrc' > /root/.bashrc WORKDIR /root ENTRYPOINT ["/bin/bash"] diff --git a/bashrc b/bashrc index 07ff675..0c352a7 100644 --- a/bashrc +++ b/bashrc @@ -1,4 +1,10 @@ -# k8s-debug shell config +# k8s-debug shell config. Installed as /etc/bash.bashrc so it applies whatever +# HOME / UID the pod runs the debug container with (not just root's ~/.bashrc). + +# Interactive shells only (previously the guard tested $PS1, which was always +# set by this very file, so it never guarded anything). +[[ $- == *i* ]] || return 0 + [ -f /etc/bash_completion ] && . /etc/bash_completion export PS1='\[\e[1;36m\][debug]\[\e[0m\] \h:\w \$ ' @@ -10,11 +16,18 @@ alias ll='ls -alF --color=auto' alias ls='ls --color=auto' alias grep='grep --color=auto' alias k=kubectl -command -v kubectl >/dev/null && source <(kubectl completion bash) && complete -o default -F __start_kubectl k +# Completion is generated at image build time (see Dockerfile); running +# `kubectl completion bash` here would fork kubectl on every shell start. +_kc=/usr/share/bash-completion/completions/kubectl +[ -r "$_kc" ] && . "$_kc" && complete -o default -F __start_kubectl k +unset _kc # Helper: list the target container's filesystem root. # troot -> /proc//root (use with --target / shared PID namespace) troot() { echo "/proc/${1:?usage: troot }/root"; } -# Show the banner once, for interactive shells only. -[ -n "$PS1" ] && [ -z "$DEBUG_MOTD_SHOWN" ] && export DEBUG_MOTD_SHOWN=1 && cat /etc/motd +# Show the banner once per shell tree. +if [ -z "$DEBUG_MOTD_SHOWN" ]; then + export DEBUG_MOTD_SHOWN=1 + cat /etc/motd +fi