Skip to content

Commit e9a06b1

Browse files
committed
chore: enhance the asciinema demo creation script
1 parent 72d4edb commit e9a06b1

File tree

5 files changed

+425
-14
lines changed

5 files changed

+425
-14
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-kind.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: 97 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,116 @@
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+
```
14+
15+
## Automated Demo Recording
16+
17+
To automatically record and update the demo using Asciinema:
918

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+
### Generate Custom Demos
33+
34+
The script supports generating multiple demo variations:
1335

1436
```sh
37+
# Generate the default kb-demo
38+
./scripts/demo/generate-demo.sh
39+
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
45+
46+
# Show help
47+
./scripts/demo/generate-demo.sh --help
48+
```
49+
50+
Custom demos will be saved to `docs/gif/${DEMO_NAME}.${VERSION}.svg` and won't automatically update the README (you'll need to reference them manually).
51+
52+
## Setup Demo Cluster Only
53+
54+
If you just want to set up the Kind cluster for testing:
55+
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
66+
```
67+
68+
## Prerequisites for Recording
69+
70+
- `kind`: For creating local Kubernetes clusters
71+
- `kubectl`: For interacting with Kubernetes
72+
- `asciinema`: For recording terminal sessions
73+
- `svg-term`: For converting recordings to SVG (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
91+
92+
## Manual Recording Instructions (Legacy)
93+
94+
If you prefer to record manually:
95+
96+
```sh
97+
# Set up Kind cluster first
98+
./scripts/demo/setup-kind.sh
1599

16100
# Create temporary directory
17101
mkdir /tmp/kb-demo
18102
cd /tmp/kb-demo
19103

20-
asciinema rec
21-
<path-to-KB-repo>/scripts/demo/run.sh
22-
23-
# After each step, press <Enter> to proceed to the next step
104+
# Start recording
105+
asciinema rec --title "Kubebuilder Demo" kb-demo.cast
24106

25-
<CTRL-C> to terminate the script
26-
<CTRL-D> to terminate the asciinema recording
27-
<CTRL-C> to save the recording locally
107+
# Run the demo script
108+
DEMO_AUTO_RUN=1 /path/to/kubebuilder/scripts/demo/run.sh
28109

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
110+
# Stop recording with Ctrl+C when done
111+
# Convert to SVG
112+
svg-term --in=kb-demo.cast --out=kb-demo.svg --window --width=120 --height=30
31113

32-
svg-term --in=<cast-file-path> --out demo.svg --window
114+
# Clean up when done
115+
kind delete cluster --name kubebuilder-demo
33116
```

scripts/demo/generate-demo.sh

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2023 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+
# Default demo name
23+
DEMO_NAME="${1:-kb-demo}"
24+
DEMO_SCRIPT="${2:-${SCRIPT_ROOT}/run.sh}"
25+
26+
# Colors for output
27+
RED='\033[0;31m'
28+
GREEN='\033[0;32m'
29+
YELLOW='\033[1;33m'
30+
NC='\033[0m' # No Color
31+
32+
log() {
33+
echo -e "${GREEN}[INFO]${NC} $1"
34+
}
35+
36+
warn() {
37+
echo -e "${YELLOW}[WARN]${NC} $1"
38+
}
39+
40+
error() {
41+
echo -e "${RED}[ERROR]${NC} $1"
42+
exit 1
43+
}
44+
45+
usage() {
46+
cat << EOF
47+
Usage: $0 [DEMO_NAME] [DEMO_SCRIPT]
48+
49+
Generate an asciinema demo recording and convert it to SVG.
50+
51+
Arguments:
52+
DEMO_NAME Name of the demo (default: kb-demo)
53+
DEMO_SCRIPT Path to the demo script to run (default: ${SCRIPT_ROOT}/run.sh)
54+
55+
Examples:
56+
$0 # Generate default kb-demo
57+
$0 my-custom-demo # Generate custom demo with default script
58+
$0 advanced-demo ./my-demo.sh # Generate custom demo with custom script
59+
60+
EOF
61+
exit 0
62+
}
63+
64+
check_prerequisites() {
65+
log "Checking prerequisites..."
66+
67+
# Check for help flag
68+
if [[ "$1" == "-h" || "$1" == "--help" ]]; then
69+
usage
70+
fi
71+
72+
# Validate demo script exists
73+
if [[ ! -f "$DEMO_SCRIPT" ]]; then
74+
error "Demo script not found: $DEMO_SCRIPT"
75+
fi
76+
77+
local missing_tools=()
78+
79+
if ! command -v asciinema &> /dev/null; then
80+
missing_tools+=("asciinema")
81+
fi
82+
83+
if ! command -v svg-term &> /dev/null; then
84+
missing_tools+=("svg-term")
85+
fi
86+
87+
if ! command -v kind &> /dev/null; then
88+
missing_tools+=("kind")
89+
fi
90+
91+
if ! command -v kubectl &> /dev/null; then
92+
missing_tools+=("kubectl")
93+
fi
94+
95+
if [ ${#missing_tools[@]} -ne 0 ]; then
96+
error "Missing required tools:\n$(printf ' - %s\n' "${missing_tools[@]}")"
97+
fi
98+
99+
log "All prerequisites are installed ✓"
100+
}
101+
102+
setup_cluster() {
103+
log "Setting up Kind cluster for demo..."
104+
"${SCRIPT_ROOT}/setup-kind.sh"
105+
106+
log "Verifying cluster connection..."
107+
kubectl cluster-info --context kind-kubebuilder-demo > /dev/null
108+
log "Cluster connection verified ✓"
109+
}
110+
111+
record_demo() {
112+
local recording_dir="/tmp/kb-demo-recording"
113+
114+
log "Cleaning up any previous recording files..."
115+
rm -rf "$recording_dir"
116+
mkdir -p "$recording_dir"
117+
118+
log "Starting demo recording for '${DEMO_NAME}' in 3 seconds..."
119+
sleep 3
120+
121+
cd "$recording_dir"
122+
asciinema rec \
123+
--command "$DEMO_SCRIPT" \
124+
--env "DEMO_AUTO_RUN=1" \
125+
--title "Kubebuilder Demo: ${DEMO_NAME}" \
126+
--idle-time-limit 2 \
127+
"${DEMO_NAME}.cast"
128+
}
129+
130+
convert_to_svg() {
131+
local recording_dir="/tmp/kb-demo-recording"
132+
local version="$1"
133+
local svg_file="${PROJECT_ROOT}/docs/gif/${DEMO_NAME}.${version}.svg"
134+
135+
log "Converting recording to SVG..."
136+
svg-term \
137+
--in="${recording_dir}/${DEMO_NAME}.cast" \
138+
--out="$svg_file" \
139+
--window \
140+
--width=120 \
141+
--height=30
142+
143+
log "Demo updated! New file: docs/gif/${DEMO_NAME}.${version}.svg"
144+
return 0
145+
}
146+
147+
update_readme() {
148+
local version="$1"
149+
150+
# Only update README for the default kb-demo
151+
if [[ "$DEMO_NAME" != "kb-demo" ]]; then
152+
log "Skipping README update for custom demo '${DEMO_NAME}'"
153+
return 0
154+
fi
155+
156+
log "Updating README.md with new demo..."
157+
if [[ "$OSTYPE" == "darwin"* ]]; then
158+
# macOS
159+
sed -i '' "s|docs/gif/kb-demo\.v[^)]*\.svg|docs/gif/kb-demo.${version}.svg|g" "${PROJECT_ROOT}/README.md"
160+
else
161+
# Linux
162+
sed -i "s|docs/gif/kb-demo\.v[^)]*\.svg|docs/gif/kb-demo.${version}.svg|g" "${PROJECT_ROOT}/README.md"
163+
fi
164+
165+
log "README.md updated with new demo file ✓"
166+
}
167+
168+
cleanup() {
169+
log "Cleaning up temporary files..."
170+
rm -rf /tmp/kb-demo-recording
171+
log "To clean up the demo cluster, run: make clean-demo"
172+
}
173+
174+
main() {
175+
# Check for help flag first
176+
if [[ "$1" == "-h" || "$1" == "--help" ]]; then
177+
usage
178+
fi
179+
180+
log "Starting Kubebuilder demo generation for '${DEMO_NAME}'..."
181+
log "Using demo script: ${DEMO_SCRIPT}"
182+
183+
# Extract version once to avoid duplication
184+
local version
185+
version=$(git -C "$PROJECT_ROOT" describe --tags --abbrev=0 2>/dev/null || echo "v4.0.0")
186+
187+
check_prerequisites
188+
setup_cluster
189+
record_demo
190+
convert_to_svg "$version"
191+
update_readme "$version"
192+
cleanup
193+
194+
log "Demo generation completed successfully! 🎉"
195+
log "Generated: docs/gif/${DEMO_NAME}.${version}.svg"
196+
}
197+
198+
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)