Skip to content

Commit

Permalink
Add a verbosity concept to kubernetes scripts
Browse files Browse the repository at this point in the history
The KUBE_VERBOSE environment variable sets the verbosity level to
use. Log messages can specify a verbosity by setting the V
variable. e.g.

    V=2 kube::log::info foo bar

Would only print "foo bar" if $KUBE_VERBOSE >= 2.
  • Loading branch information
tallclair committed Aug 5, 2016
1 parent 07b650e commit 9abdf71
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 12 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ META_DIR := .make
KUBE_GOFLAGS := $(GOFLAGS)
KUBE_GOLDFLAGS := $(GOLDFLAGS)

KUBE_VERBOSE ?= 1

GOGCFLAGS ?=
BRANCH ?=
KUBE_GOGCFLAGS = $(GOGCFLAGS)
Expand Down
2 changes: 2 additions & 0 deletions Makefile.generated_files
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ DEEPCOPY_FILES := $(addsuffix /$(DEEPCOPY_FILENAME), $(DEEPCOPY_DIRS))
gen_deepcopy: $(DEEPCOPY_FILES)
if [[ -f $(META_DIR)/$(DEEPCOPY_GEN).todo ]]; then \
./hack/run-in-gopath.sh $(DEEPCOPY_GEN) \
--v $(KUBE_VERBOSE) \
-i $$(cat $(META_DIR)/$(DEEPCOPY_GEN).todo | paste -sd, -) \
--bounding-dirs $(PRJ_SRC_PATH) \
-O $(DEEPCOPY_BASENAME); \
Expand Down Expand Up @@ -323,6 +324,7 @@ CONVERSION_FILES := $(addsuffix /$(CONVERSION_FILENAME), $(CONVERSION_DIRS))
gen_conversion: $(CONVERSION_FILES)
if [[ -f $(META_DIR)/$(CONVERSION_GEN).todo ]]; then \
./hack/run-in-gopath.sh $(CONVERSION_GEN) \
--v $(KUBE_VERBOSE) \
-i $$(cat $(META_DIR)/$(CONVERSION_GEN).todo | paste -sd, -) \
-O $(CONVERSION_BASENAME); \
fi
Expand Down
32 changes: 24 additions & 8 deletions cluster/lib/logging.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# Controls verbosity of the script output and logging.
KUBE_VERBOSE="${KUBE_VERBOSE:-5}"

# Handler for when we exit automatically on an error.
# Borrowed from https://gist.github.com/ahendrix/7030300
kube::log::errexit() {
Expand Down Expand Up @@ -70,16 +73,19 @@ kube::log::error_exit() {
local stack_skip="${3:-0}"
stack_skip=$((stack_skip + 1))

local source_file=${BASH_SOURCE[$stack_skip]}
local source_line=${BASH_LINENO[$((stack_skip - 1))]}
echo "!!! Error in ${source_file}:${source_line}" >&2
[[ -z ${1-} ]] || {
echo " ${1}" >&2
}
if [[ ${KUBE_VERBOSE} -ge 4 ]]; then
local source_file=${BASH_SOURCE[$stack_skip]}
local source_line=${BASH_LINENO[$((stack_skip - 1))]}
echo "!!! Error in ${source_file}:${source_line}" >&2
[[ -z ${1-} ]] || {
echo " ${1}" >&2
}

kube::log::stack $stack_skip

kube::log::stack $stack_skip
echo "Exiting with status ${code}" >&2
fi

echo "Exiting with status ${code}" >&2
exit "${code}"
}

Expand Down Expand Up @@ -114,6 +120,11 @@ kube::log::usage_from_stdin() {

# Print out some info that isn't a top level status line
kube::log::info() {
local V="${V:-0}"
if [[ $KUBE_VERBOSE < $V ]]; then
return
fi

for message; do
echo "$message"
done
Expand All @@ -137,6 +148,11 @@ kube::log::info_from_stdin() {

# Print a status line. Formatted to show up in a stream of output.
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
Expand Down
4 changes: 2 additions & 2 deletions hack/lib/golang.sh
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ kube::golang::place_bins() {
local host_platform
host_platform=$(kube::golang::host_platform)

kube::log::status "Placing binaries"
V=2 kube::log::status "Placing binaries"

local platform
for platform in "${KUBE_CLIENT_PLATFORMS[@]}"; do
Expand Down Expand Up @@ -598,7 +598,7 @@ kube::golang::build_binaries() {
(
# Check for `go` binary and set ${GOPATH}.
kube::golang::setup_env
echo "Go version: $(go version)"
V=2 kube::log::info "Go version: $(go version)"

local host_platform
host_platform=$(kube::golang::host_platform)
Expand Down
1 change: 1 addition & 0 deletions hack/make-rules/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ set -o nounset
set -o pipefail

KUBE_ROOT=$(dirname "${BASH_SOURCE}")/../..
KUBE_VERBOSE="${KUBE_VERBOSE:-1}"
source "${KUBE_ROOT}/hack/lib/init.sh"

kube::golang::build_binaries "$@"
Expand Down
4 changes: 3 additions & 1 deletion hack/update-bindata.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ if [[ -z "${KUBE_ROOT:-}" ]]; then
KUBE_ROOT="../../../"
fi

source "${KUBE_ROOT}/cluster/lib/logging.sh"

if [[ ! -d "${KUBE_ROOT}/examples" ]]; then
echo "${KUBE_ROOT}/examples not detected. This script should be run from a location where the source dirs are available."
exit 1
Expand All @@ -44,4 +46,4 @@ go-bindata -nometadata -prefix "${KUBE_ROOT}" -o ${BINDATA_OUTPUT} -pkg generate

gofmt -s -w ${BINDATA_OUTPUT}

echo "Generated bindata file : $(wc -l ${BINDATA_OUTPUT}) lines of lovely automated artifacts"
V=2 kube::log::info "Generated bindata file : $(wc -l ${BINDATA_OUTPUT}) lines of lovely automated artifacts"
2 changes: 1 addition & 1 deletion hack/verify-flags/exceptions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ cluster/juju/layers/kubernetes/reactive/k8s.py: client_key = '/srv/kubernetes
cluster/juju/layers/kubernetes/reactive/k8s.py: cluster_name = 'kubernetes'
cluster/juju/layers/kubernetes/reactive/k8s.py: tlslib.client_key(None, client_key, user='ubuntu', group='ubuntu')
cluster/lib/logging.sh: local source_file=${BASH_SOURCE[$frame_no]}
cluster/lib/logging.sh: local source_file=${BASH_SOURCE[$stack_skip]}
cluster/lib/logging.sh: local source_file=${BASH_SOURCE[$stack_skip]}
cluster/log-dump.sh: for node_name in "${NODE_NAMES[@]}"; do
cluster/log-dump.sh: local -r node_name="${1}"
cluster/log-dump.sh:readonly report_dir="${1:-_artifacts}"
Expand Down

0 comments on commit 9abdf71

Please sign in to comment.