Skip to content

Commit f199c8e

Browse files
committed
🏃 added demo script
Add demo script to run quick demo of Kubebuilder. This is also useful for recording demo videos using asciinema.
1 parent 5f27a72 commit f199c8e

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed

scripts/demo/README.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
This directory contains scripts to run a quick demo of KubeBuilder.
2+
3+
Steps to run demo:
4+
5+
```sh
6+
mkdir /tmp/kb-demo
7+
cd /tmp/kb-demo
8+
DEMO_AUTO_RUN=1 ./run.sh
9+
10+
```

scripts/demo/run.sh

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/bin/bash
2+
# Copyright 2016 The Kubernetes Authors.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
clear
17+
. $(dirname ${BASH_SOURCE})/util.sh
18+
19+
desc "Initialize Go modules"
20+
run "go mod init demo.kubebuilder.io"
21+
22+
desc "Let's initialize the project"
23+
run "kubebuilder init --domain tutorial.kubebuilder.io"
24+
clear
25+
26+
desc "Examine scaffolded files..."
27+
run "tree ."
28+
clear
29+
30+
desc "Let's create cronjob api"
31+
run "kubebuilder create api --group batch --version v1 --kind CronJob"
32+
clear
33+
34+
desc "Let's take a look at the API and Controller files"
35+
run "tree ./api ./controllers"
36+
clear
37+
38+
desc "Install CRD in Kubernetes cluster"
39+
run "make install"
40+
clear
41+
42+
desc "Run controller manager locally"
43+
run "make run"

scripts/demo/util.sh

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#!/bin/bash
2+
# Copyright 2016 The Kubernetes Authors.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
# This file is copied from @thockin's micro-demos repository:
17+
# https://github.com/thockin/micro-demos/blob/master/util.sh
18+
19+
readonly reset=$(tput sgr0)
20+
readonly green=$(tput bold; tput setaf 2)
21+
readonly yellow=$(tput bold; tput setaf 3)
22+
readonly blue=$(tput bold; tput setaf 6)
23+
readonly timeout=$(if [ "$(uname)" == "Darwin" ]; then echo "1"; else echo "0.1"; fi)
24+
25+
function desc() {
26+
maybe_first_prompt
27+
rate=25
28+
if [ -n "$DEMO_RUN_FAST" ]; then
29+
rate=1000
30+
fi
31+
echo "$blue# $@$reset" | pv -qL $rate
32+
prompt
33+
}
34+
35+
function prompt() {
36+
echo -n "$yellow\$ $reset"
37+
}
38+
39+
started=""
40+
function maybe_first_prompt() {
41+
if [ -z "$started" ]; then
42+
prompt
43+
started=true
44+
fi
45+
}
46+
47+
# After a `run` this variable will hold the stdout of the command that was run.
48+
# If the command was interactive, this will likely be garbage.
49+
DEMO_RUN_STDOUT=""
50+
51+
function run() {
52+
maybe_first_prompt
53+
rate=25
54+
if [ -n "$DEMO_RUN_FAST" ]; then
55+
rate=1000
56+
fi
57+
echo "$green$1$reset" | pv -qL $rate
58+
if [ -n "$DEMO_RUN_FAST" ]; then
59+
sleep 0.5
60+
fi
61+
OFILE="$(mktemp -t $(basename $0).XXXXXX)"
62+
if [ "$(uname)" == "Darwin" ]; then
63+
script -q "$OFILE" $1
64+
else
65+
script -eq -c "$1" -f "$OFILE"
66+
fi
67+
r=$?
68+
read -d '' -t "${timeout}" -n 10000 # clear stdin
69+
prompt
70+
if [ -z "$DEMO_AUTO_RUN" ]; then
71+
read -s
72+
fi
73+
DEMO_RUN_STDOUT="$(tail -n +2 $OFILE | sed 's/\r//g')"
74+
return $r
75+
}
76+
77+
function relative() {
78+
for arg; do
79+
echo "$(realpath $(dirname $(which $0)))/$arg" | sed "s|$(realpath $(pwd))|.|"
80+
done
81+
}
82+
83+
trap "echo" EXIT

0 commit comments

Comments
 (0)