diff --git a/Dockerfile b/Dockerfile index 88b23e7..414e610 100644 --- a/Dockerfile +++ b/Dockerfile @@ -85,8 +85,14 @@ RUN set -eux; \ rm -f "/tmp/${f}" # ---- 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