From f199c8e424affd1844c2e09b9c74f242c3fb7bf7 Mon Sep 17 00:00:00 2001 From: Sunil Arora Date: Wed, 25 Sep 2019 17:00:08 -0700 Subject: [PATCH] :running: added demo script Add demo script to run quick demo of Kubebuilder. This is also useful for recording demo videos using asciinema. --- scripts/demo/README.md | 10 +++++ scripts/demo/run.sh | 43 ++++++++++++++++++++++ scripts/demo/util.sh | 83 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 136 insertions(+) create mode 100644 scripts/demo/README.md create mode 100755 scripts/demo/run.sh create mode 100644 scripts/demo/util.sh diff --git a/scripts/demo/README.md b/scripts/demo/README.md new file mode 100644 index 00000000000..50ce820a1d1 --- /dev/null +++ b/scripts/demo/README.md @@ -0,0 +1,10 @@ +This directory contains scripts to run a quick demo of KubeBuilder. + +Steps to run demo: + +```sh +mkdir /tmp/kb-demo +cd /tmp/kb-demo +DEMO_AUTO_RUN=1 ./run.sh + +``` diff --git a/scripts/demo/run.sh b/scripts/demo/run.sh new file mode 100755 index 00000000000..d6d60597da0 --- /dev/null +++ b/scripts/demo/run.sh @@ -0,0 +1,43 @@ +#!/bin/bash +# Copyright 2016 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +clear +. $(dirname ${BASH_SOURCE})/util.sh + +desc "Initialize Go modules" +run "go mod init demo.kubebuilder.io" + +desc "Let's initialize the project" +run "kubebuilder init --domain tutorial.kubebuilder.io" +clear + +desc "Examine scaffolded files..." +run "tree ." +clear + +desc "Let's create cronjob api" +run "kubebuilder create api --group batch --version v1 --kind CronJob" +clear + +desc "Let's take a look at the API and Controller files" +run "tree ./api ./controllers" +clear + +desc "Install CRD in Kubernetes cluster" +run "make install" +clear + +desc "Run controller manager locally" +run "make run" diff --git a/scripts/demo/util.sh b/scripts/demo/util.sh new file mode 100644 index 00000000000..374c497440b --- /dev/null +++ b/scripts/demo/util.sh @@ -0,0 +1,83 @@ +#!/bin/bash +# Copyright 2016 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# This file is copied from @thockin's micro-demos repository: +# https://github.com/thockin/micro-demos/blob/master/util.sh + +readonly reset=$(tput sgr0) +readonly green=$(tput bold; tput setaf 2) +readonly yellow=$(tput bold; tput setaf 3) +readonly blue=$(tput bold; tput setaf 6) +readonly timeout=$(if [ "$(uname)" == "Darwin" ]; then echo "1"; else echo "0.1"; fi) + +function desc() { + maybe_first_prompt + rate=25 + if [ -n "$DEMO_RUN_FAST" ]; then + rate=1000 + fi + echo "$blue# $@$reset" | pv -qL $rate + prompt +} + +function prompt() { + echo -n "$yellow\$ $reset" +} + +started="" +function maybe_first_prompt() { + if [ -z "$started" ]; then + prompt + started=true + fi +} + +# After a `run` this variable will hold the stdout of the command that was run. +# If the command was interactive, this will likely be garbage. +DEMO_RUN_STDOUT="" + +function run() { + maybe_first_prompt + rate=25 + if [ -n "$DEMO_RUN_FAST" ]; then + rate=1000 + fi + echo "$green$1$reset" | pv -qL $rate + if [ -n "$DEMO_RUN_FAST" ]; then + sleep 0.5 + fi + OFILE="$(mktemp -t $(basename $0).XXXXXX)" + if [ "$(uname)" == "Darwin" ]; then + script -q "$OFILE" $1 + else + script -eq -c "$1" -f "$OFILE" + fi + r=$? + read -d '' -t "${timeout}" -n 10000 # clear stdin + prompt + if [ -z "$DEMO_AUTO_RUN" ]; then + read -s + fi + DEMO_RUN_STDOUT="$(tail -n +2 $OFILE | sed 's/\r//g')" + return $r +} + +function relative() { + for arg; do + echo "$(realpath $(dirname $(which $0)))/$arg" | sed "s|$(realpath $(pwd))|.|" + done +} + +trap "echo" EXIT