Understanding Double Colon Function Naming in Kubernetes Shell Scripts
Shell scripts in Kubernetes projects often use double colons (::) in function names for namespacing purposes. This convantion helps organize functions into logical groups, similar to packages in other langauges.
# Example from Kubernetes logging script
kube::log::status() {
local V="${V:-0}"
if (( KUBE_VERBOSE < V )); then
return
fi
timestamp=$(date +"[%m%d %H:%M:%S]")
echo "+++ ${timestamp} ${1}"
shift
for msg; do
echo " ${msg}"
done
}
The for message; do construct iterates through all remaining arguments after the first one. Each argument is temporarily assigned to the message variable and processed in the loop body.
When testing this pattern, you might encounter errors when using sh:
$ sh script.sh
script.sh: line X: `kube::log::status': not a valid identifier
This occurs because sh (typically linked to dash on many systems) has stricter naming requirements than bash. The script works when eexcuted directly with bash or as an executable:
$ chmod +x script.sh
$ ./script.sh
+++ [0715 16:21:28] Message
additional
lines
The Google Shell Style Guide recommends this namespacing approach for functions that belong to logical packages, while suggesting simpler names for interactive functions to avoid conflicts with shell completion.