Skip to content

Commit 601ef2f

Browse files
committed
chore: enhance the asciinema demo creation script
1 parent cded1fd commit 601ef2f

File tree

5 files changed

+360
-16
lines changed

5 files changed

+360
-16
lines changed

Makefile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,22 @@ generate-charts: build ## Re-generate the helm chart testdata and docs samples
109109
(cd docs/book/src/cronjob-tutorial/testdata/project && make build-installer && ../../../../../../bin/kubebuilder edit --plugins=helm/v2-alpha)
110110
(cd docs/book/src/multiversion-tutorial/testdata/project && make build-installer && ../../../../../../bin/kubebuilder edit --plugins=helm/v2-alpha)
111111

112+
.PHONY: update-demo
113+
update-demo: ## Record and update the Kubebuilder demo using Asciinema
114+
@./scripts/demo/generate-demo.sh
115+
116+
.PHONY: setup-demo-cluster
117+
setup-demo-cluster: ## Set up Kind cluster for Kubebuilder demo
118+
@./scripts/demo/setup-demo-cluster.sh
119+
120+
.PHONY: clean-demo
121+
clean-demo: ## Clean up the demo Kind cluster and temporary directories
122+
@echo "Cleaning up demo cluster..."
123+
@kind delete cluster --name kubebuilder-demo || echo "Demo cluster was not found or already deleted"
124+
@echo "Cleaning up temporary demo directories..."
125+
@rm -rf /tmp/kubebuilder-demo-project /tmp/kb-demo-recording
126+
@echo "Demo cleanup completed"
127+
112128
.PHONY: check-docs
113129
check-docs: ## Run the script to ensure that the docs are updated
114130
./hack/docs/check.sh

scripts/demo/README.md

Lines changed: 73 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,90 @@
1-
This directory contains scripts to run a quick demo of Kubebuilder.
1+
# Kubebuilder Demo
22

3-
Steps to run demo:
3+
This directory contains scripts to run a comprehensive demo of Kubebuilder features with a local Kind cluster.
4+
5+
## Quick Demo (Manual)
6+
7+
To run the demo manually:
48

59
```sh
610
mkdir /tmp/kb-demo
711
cd /tmp/kb-demo
8-
DEMO_AUTO_RUN=1 ./run.sh
12+
DEMO_AUTO_RUN=1 /path/to/kubebuilder/scripts/demo/run.sh
13+
```
914

15+
## Automated Demo Recording
16+
17+
To automatically record and update the demo using Asciinema:
18+
19+
```sh
20+
# From the root of the Kubebuilder repository
21+
make update-demo
1022
```
1123

12-
Instructions for producing the demo movie:
24+
This will:
25+
1. Check for required dependencies (asciinema, svg-term, kind, kubectl)
26+
2. Set up a Kind cluster for the demo
27+
3. Record the demo session automatically
28+
4. Convert the recording to SVG format
29+
5. Update the demo file in `docs/gif/kb-demo.${VERSION}.svg`
30+
6. Clean up temporary files
31+
32+
## Using the Script Utilities
33+
34+
The demo generation script (`generate-demo.sh`) supports flexible usage:
1335

1436
```sh
37+
# Generate the default kb-demo
38+
./scripts/demo/generate-demo.sh
1539

16-
# Create temporary directory
17-
mkdir /tmp/kb-demo
18-
cd /tmp/kb-demo
40+
# Generate a custom demo with a different name
41+
./scripts/demo/generate-demo.sh my-custom-demo
42+
43+
# Generate a custom demo using a different script
44+
./scripts/demo/generate-demo.sh advanced-demo ./path/to/custom-script.sh
1945

20-
asciinema rec
21-
<path-to-KB-repo>/scripts/demo/run.sh
46+
# Show help
47+
./scripts/demo/generate-demo.sh --help
48+
```
2249

23-
# After each step, press <Enter> to proceed to the next step
50+
**Note**: Custom demos (non-default names) won't automatically update the README. You'll need to reference them manually in documentation.
2451

25-
<CTRL-C> to terminate the script
26-
<CTRL-D> to terminate the asciinema recording
27-
<CTRL-C> to save the recording locally
52+
## Setup Demo Cluster Only
2853

29-
# Edit the recorded file by editing the controller-gen path
30-
# Once you are happy with the recording, use svg-term program to generate the svg
54+
If you just want to set up the Kind cluster for testing:
3155

32-
svg-term --in=<cast-file-path> --out demo.svg --window
56+
```sh
57+
make setup-demo-cluster
58+
```
59+
60+
## Clean Up Demo Cluster
61+
62+
To remove the demo Kind cluster when done:
63+
64+
```sh
65+
make clean-demo
3366
```
67+
68+
## Prerequisites
69+
70+
- `kind`: For creating local Kubernetes clusters
71+
- `kubectl`: For interacting with Kubernetes
72+
- `asciinema`: For recording terminal sessions (recording only)
73+
- `svg-term`: For converting recordings to SVG (recording only, requires Node.js/npm)
74+
75+
## What the Demo Shows
76+
77+
The current demo showcases:
78+
79+
1. **Cluster Setup**: Creates a local Kind cluster for testing
80+
2. **Installation**: Installing Kubebuilder from scratch
81+
3. **Project Initialization**: Creating a new operator project
82+
4. **API Creation**: Creating APIs with validation markers
83+
5. **Plugin System**: Using the deploy-image plugin
84+
6. **Modern Features**:
85+
- Validation markers (`+kubebuilder:validation`)
86+
- Multiple APIs in one project
87+
- Generated CRDs with OpenAPI schemas
88+
- Sample resource management
89+
7. **Development Workflow**: Install CRDs, apply samples, run controller
90+
8. **Cluster Integration**: Full integration with Kubernetes cluster

scripts/demo/generate-demo.sh

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2025 The Kubernetes Authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
set -e
18+
19+
SCRIPT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
20+
PROJECT_ROOT="$(cd "${SCRIPT_ROOT}/../.." && pwd)"
21+
22+
# Source the e2e test infrastructure for kind cluster management
23+
source "${PROJECT_ROOT}/test/common.sh"
24+
source "${PROJECT_ROOT}/test/e2e/setup.sh"
25+
26+
# Default demo name
27+
DEMO_NAME="${1:-kb-demo}"
28+
DEMO_SCRIPT="${2:-${SCRIPT_ROOT}/run.sh}"
29+
30+
# Use color output from common.sh or define our own
31+
RED='\033[0;31m'
32+
GREEN='\033[0;32m'
33+
YELLOW='\033[1;33m'
34+
NC='\033[0m' # No Color
35+
36+
log() {
37+
echo -e "${GREEN}[INFO]${NC} $1"
38+
}
39+
40+
warn() {
41+
echo -e "${YELLOW}[WARN]${NC} $1"
42+
}
43+
44+
error() {
45+
echo -e "${RED}[ERROR]${NC} $1"
46+
exit 1
47+
}
48+
49+
usage() {
50+
cat << EOF
51+
Usage: $0 [DEMO_NAME] [DEMO_SCRIPT]
52+
53+
Generate an asciinema demo recording and convert it to SVG.
54+
55+
Arguments:
56+
DEMO_NAME Name of the demo (default: kb-demo)
57+
DEMO_SCRIPT Path to the demo script to run (default: ${SCRIPT_ROOT}/run.sh)
58+
59+
Examples:
60+
$0 # Generate default kb-demo
61+
$0 my-custom-demo # Generate custom demo with default script
62+
$0 advanced-demo ./my-demo.sh # Generate custom demo with custom script
63+
64+
EOF
65+
exit 0
66+
}
67+
68+
check_prerequisites() {
69+
log "Checking prerequisites..."
70+
71+
# Check for help flag
72+
if [[ "$1" == "-h" || "$1" == "--help" ]]; then
73+
usage
74+
fi
75+
76+
# Validate demo script exists
77+
if [[ ! -f "$DEMO_SCRIPT" ]]; then
78+
error "Demo script not found: $DEMO_SCRIPT"
79+
fi
80+
81+
local missing_tools=()
82+
83+
if ! command -v asciinema &> /dev/null; then
84+
missing_tools+=("asciinema")
85+
fi
86+
87+
if ! command -v svg-term &> /dev/null; then
88+
missing_tools+=("svg-term")
89+
fi
90+
91+
if ! command -v kind &> /dev/null; then
92+
missing_tools+=("kind")
93+
fi
94+
95+
if ! command -v kubectl &> /dev/null; then
96+
missing_tools+=("kubectl")
97+
fi
98+
99+
if [ ${#missing_tools[@]} -ne 0 ]; then
100+
error "Missing required tools:\n$(printf ' - %s\n' "${missing_tools[@]}")"
101+
fi
102+
103+
log "All prerequisites are installed ✓"
104+
}
105+
106+
setup_cluster() {
107+
log "Setting up Kind cluster for demo..."
108+
109+
# Use the e2e infrastructure to set up the cluster
110+
export KIND_CLUSTER="kubebuilder-demo"
111+
112+
# Build kubebuilder, fetch tools, and install kind if needed
113+
build_kb
114+
fetch_tools
115+
install_kind
116+
117+
# Create the cluster using the e2e setup function
118+
create_cluster ${KIND_K8S_VERSION}
119+
120+
log "Cluster connection verified ✓"
121+
}
122+
123+
record_demo() {
124+
local recording_dir="/tmp/kb-demo-recording"
125+
126+
log "Cleaning up any previous recording files..."
127+
rm -rf "$recording_dir"
128+
mkdir -p "$recording_dir"
129+
130+
log "Starting demo recording for '${DEMO_NAME}' in 3 seconds..."
131+
sleep 3
132+
133+
cd "$recording_dir"
134+
asciinema rec \
135+
--command "$DEMO_SCRIPT" \
136+
--env "DEMO_AUTO_RUN=1" \
137+
--title "Kubebuilder Demo: ${DEMO_NAME}" \
138+
--idle-time-limit 2 \
139+
"${DEMO_NAME}.cast"
140+
}
141+
142+
convert_to_svg() {
143+
local recording_dir="/tmp/kb-demo-recording"
144+
local version="$1"
145+
local svg_file="${PROJECT_ROOT}/docs/gif/${DEMO_NAME}.${version}.svg"
146+
147+
log "Converting recording to SVG..."
148+
svg-term \
149+
--in="${recording_dir}/${DEMO_NAME}.cast" \
150+
--out="$svg_file" \
151+
--window \
152+
--width=120 \
153+
--height=30
154+
155+
log "Demo updated! New file: docs/gif/${DEMO_NAME}.${version}.svg"
156+
return 0
157+
}
158+
159+
update_readme() {
160+
local version="$1"
161+
162+
# Only update README for the default kb-demo
163+
if [[ "$DEMO_NAME" != "kb-demo" ]]; then
164+
log "Skipping README update for custom demo '${DEMO_NAME}'"
165+
return 0
166+
fi
167+
168+
log "Updating README.md with new demo..."
169+
if [[ "$OSTYPE" == "darwin"* ]]; then
170+
# macOS
171+
sed -i '' "s|docs/gif/kb-demo\.v[^)]*\.svg|docs/gif/kb-demo.${version}.svg|g" "${PROJECT_ROOT}/README.md"
172+
else
173+
# Linux
174+
sed -i "s|docs/gif/kb-demo\.v[^)]*\.svg|docs/gif/kb-demo.${version}.svg|g" "${PROJECT_ROOT}/README.md"
175+
fi
176+
177+
log "README.md updated with new demo file ✓"
178+
}
179+
180+
cleanup() {
181+
log "Cleaning up temporary files..."
182+
rm -rf /tmp/kb-demo-recording
183+
log "To clean up the demo cluster, run: make clean-demo"
184+
}
185+
186+
cleanup_cluster() {
187+
log "Cleaning up demo cluster..."
188+
export KIND_CLUSTER="kubebuilder-demo"
189+
delete_cluster
190+
log "Demo cluster removed ✓"
191+
}
192+
193+
main() {
194+
# Check for help flag first
195+
if [[ "$1" == "-h" || "$1" == "--help" ]]; then
196+
usage
197+
fi
198+
199+
log "Starting Kubebuilder demo generation for '${DEMO_NAME}'..."
200+
log "Using demo script: ${DEMO_SCRIPT}"
201+
202+
# Extract version once to avoid duplication
203+
local version
204+
version=$(git -C "$PROJECT_ROOT" describe --tags --abbrev=0 2>/dev/null || echo "v4.0.0")
205+
206+
check_prerequisites
207+
setup_cluster
208+
record_demo
209+
convert_to_svg "$version"
210+
update_readme "$version"
211+
cleanup
212+
213+
log "Demo generation completed successfully! 🎉"
214+
log "Generated: docs/gif/${DEMO_NAME}.${version}.svg"
215+
}
216+
217+
main "$@"

scripts/demo/run.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16+
# Set up working directory in /tmp for clean demo
17+
cd /tmp
18+
rm -rf kubebuilder-demo-project
19+
mkdir kubebuilder-demo-project
20+
cd kubebuilder-demo-project
21+
1622
clear
1723
. $(dirname ${BASH_SOURCE})/util.sh
1824

0 commit comments

Comments
 (0)