Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Understanding Double Colon Function Naming in Kubernetes Shell Scripts

Tech 1

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.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.