How to code shell like google style in k8s

hi, there

this is a question about how to coding shell like the style in k8s.
recently I read the google shell style guide, and there is an usage called package :

# Single function
my_func() {
  …
}

# Part of a package
mypackage::my_func() {
  …
}

and I found there are a lot of shell code in k8s like this style:

function kube::util::sourced_variable {
  # Call this function to tell shellcheck that a variable is supposed to
  # be used from other calling context. This helps quiet an "unused
  # variable" warning from shellcheck and also document your code.
  true
}

kube::util::sortable_date() {
  date "+%Y%m%d-%H%M%S"
}

# arguments: target, item1, item2, item3, ...
# returns 0 if target is in the given items, 1 otherwise.
kube::util::array_contains() {
  local search="$1"
  local element
  shift
  for element; do
    if [[ "${element}" == "${search}" ]]; then
      return 0
     fi
  done
  return 1
}

https://github.com/kubernetes/kubernetes/blob/master/hack/lib/util.sh

but when I try to use this kind of style

utils::curl() {
  echo "curl"
}

utils::curl

I got an error util.sh: line 11: utils::curl’: not a valid identifier`

I don’t know what I missed or if there is anythong wrong. I do like this shell style, and want to change all my shell code like this.

an help will be appreciated.

thank you

Hi taojintianxia:

I saw a talk were this “style” was mentioned (and maybe, explained) a long time ago, but I did not fully understood how it worked… It was in the talk Shell Ninja: Mastering the Art of Shell Scripting | Roland Huß.

Hope it helps.

Xavi

1 Like

thank you. watching now :grinning: