Skip to content

Commit e7da424

Browse files
authored
Merge pull request #60 from n2h9/chore-enhance-local-build-experience
chore: update local build experience, so that there is no manual steps
2 parents ee4bcc6 + bacb1bb commit e7da424

File tree

7 files changed

+174
-30
lines changed

7 files changed

+174
-30
lines changed

Makefile

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,17 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
build:
16-
./scripts/build.sh
15+
check-dependencies:
16+
./scripts/main.sh check_dependencies
17+
18+
build: check-dependencies
19+
./scripts/main.sh build
1720

1821
uninstall:
19-
./scripts/uninstall.sh || true
22+
./scripts/main.sh uninstall || true
2023

21-
install: uninstall
22-
./scripts/install.sh
24+
install: check-dependencies uninstall
25+
./scripts/main.sh install
2326

24-
install-local: uninstall
25-
./scripts/install-local.sh
27+
install-local: check-dependencies uninstall
28+
./scripts/main.sh install_local

meshsync-snapshot-local.yaml renamed to meshsync-snapshot-local-template.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,26 @@ spec:
1515
os: linux
1616
arch: amd64
1717
uri: not needed for local
18-
sha256: <put your hash here>
18+
sha256: <sha256 hash placeholder, populated from make, DO NOT UPDATE>
1919
bin: bin/meshsync-snapshot
2020
- selector:
2121
matchLabels:
2222
os: linux
2323
arch: arm64
2424
uri: not needed for local
25-
sha256: <put your hash here>
25+
sha256: <sha256 hash placeholder, populated from make, DO NOT UPDATE>
2626
bin: bin/meshsync-snapshot
2727
- selector:
2828
matchLabels:
2929
os: darwin
3030
arch: amd64
3131
uri: not needed for local
32-
sha256: <put your hash here>
32+
sha256: <sha256 hash placeholder, populated from make, DO NOT UPDATE>
3333
bin: bin/meshsync-snapshot
3434
- selector:
3535
matchLabels:
3636
os: darwin
3737
arch: arm64
3838
uri: not needed for local
39-
sha256: <put your hash here>
39+
sha256: <sha256 hash placeholder, populated from make, DO NOT UPDATE>
4040
bin: bin/meshsync-snapshot

scripts/build.sh

Lines changed: 0 additions & 10 deletions
This file was deleted.

scripts/install-local.sh

Lines changed: 0 additions & 3 deletions
This file was deleted.

scripts/install.sh

Lines changed: 0 additions & 3 deletions
This file was deleted.

scripts/main.sh

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
#!/bin/bash
2+
3+
# Shared variables
4+
BINARY_NAME="meshsync-snapshot"
5+
TAR_NAME="meshsync-snapshot.tar.gz"
6+
BINARY_PATH="bin/${BINARY_NAME}"
7+
TAR_PATH="bin/${TAR_NAME}"
8+
SHA256_PATH="bin/${TAR_NAME}.sha256"
9+
TEMPLATE_FILE="meshsync-snapshot-local-template.yaml"
10+
11+
# Check dependencies function
12+
check_dependencies() {
13+
echo "Checking dependencies..."
14+
local missing_deps=()
15+
16+
# Check for required commands
17+
if ! command -v go &> /dev/null; then
18+
missing_deps+=("go")
19+
fi
20+
21+
if ! command -v kubectl &> /dev/null; then
22+
missing_deps+=("kubectl")
23+
fi
24+
25+
if ! command -v tar &> /dev/null; then
26+
missing_deps+=("tar")
27+
fi
28+
29+
if ! command -v sha256sum &> /dev/null; then
30+
missing_deps+=("sha256sum")
31+
fi
32+
33+
# Check for kubectl krew plugin
34+
if ! kubectl krew version &> /dev/null; then
35+
missing_deps+=("kubectl-krew")
36+
fi
37+
38+
# Report results
39+
if [ ${#missing_deps[@]} -eq 0 ]; then
40+
echo "✓ All dependencies are available"
41+
return 0
42+
else
43+
echo "✗ Missing dependencies:"
44+
for dep in "${missing_deps[@]}"; do
45+
echo " - $dep"
46+
done
47+
echo ""
48+
echo "Please install the missing dependencies before proceeding."
49+
echo "For kubectl krew installation: https://krew.sigs.k8s.io/docs/user-guide/setup/install/"
50+
return 1
51+
fi
52+
}
53+
54+
# Build function
55+
build() {
56+
echo "build"
57+
go build -o ${BINARY_PATH} cmd/meshsync/*.go
58+
59+
echo "tar"
60+
tar -czvf ${TAR_PATH} ${BINARY_PATH}
61+
62+
echo "sha256"
63+
sha256sum ${TAR_PATH} > ${SHA256_PATH}
64+
cat ${SHA256_PATH}
65+
}
66+
67+
# Install function
68+
install() {
69+
kubectl krew install --manifest=meshsync-snapshot.yaml
70+
}
71+
72+
# Install local function
73+
install_local() {
74+
# Check if tar file exists
75+
if [ ! -f "${TAR_PATH}" ]; then
76+
echo "⚠️ Warning: ${TAR_PATH} not found. Building first..."
77+
build
78+
elif [ ! -f "${SHA256_PATH}" ]; then
79+
echo "⚠️ Warning: ${SHA256_PATH} not found. Building first..."
80+
build
81+
else
82+
# Check if current hash matches stored hash
83+
current_hash=$(sha256sum "${TAR_PATH}" | cut -d' ' -f1)
84+
stored_hash=$(cut -d' ' -f1 "${SHA256_PATH}")
85+
86+
if [ "${current_hash}" != "${stored_hash}" ]; then
87+
echo "⚠️ Warning: Hash mismatch detected!"
88+
echo " Stored hash: ${stored_hash}"
89+
echo " Real hash: ${current_hash}"
90+
echo " Rebuilding..."
91+
build
92+
fi
93+
fi
94+
95+
# Get the current hash for template replacement
96+
current_hash=$(sha256sum "${TAR_PATH}" | cut -d' ' -f1)
97+
98+
# Check if template file exists
99+
if [ ! -f "${TEMPLATE_FILE}" ]; then
100+
echo "❌ Error: Template file ${TEMPLATE_FILE} not found"
101+
exit 1
102+
fi
103+
104+
# Create temporary manifest file (cross-platform)
105+
local_manifest=$(mktemp -t meshsync-manifest.XXXXXX.yaml)
106+
107+
# Ensure cleanup on exit
108+
trap "rm -f '${local_manifest}'" EXIT
109+
110+
# Create manifest from template with real hash
111+
echo "📝 Generating manifest from template..."
112+
sed "s/<sha256 hash placeholder, populated from make, DO NOT UPDATE>/${current_hash}/g" "${TEMPLATE_FILE}" > "${local_manifest}"
113+
114+
echo "🚀 Installing with generated manifest..."
115+
kubectl krew install --manifest="${local_manifest}" --archive="${TAR_PATH}"
116+
}
117+
118+
# Uninstall function
119+
uninstall() {
120+
kubectl krew uninstall meshsync-snapshot 2>/dev/null
121+
}
122+
123+
# Help function
124+
help() {
125+
echo "Usage: $0 {check_dependencies|build|install|install_local|uninstall}"
126+
echo ""
127+
echo "Commands:"
128+
echo " check_dependencies Check if all required dependencies are installed"
129+
echo " build Build binary and create tar archive"
130+
echo " install Install using krew with remote manifest"
131+
echo " install_local Install using krew with local manifest and archive"
132+
echo " uninstall Uninstall the plugin"
133+
}
134+
135+
# Main script logic
136+
case "$1" in
137+
check_dependencies)
138+
check_dependencies
139+
;;
140+
build)
141+
build
142+
;;
143+
install)
144+
install
145+
;;
146+
install_local)
147+
install_local
148+
;;
149+
uninstall)
150+
uninstall
151+
;;
152+
help|--help|-h)
153+
help
154+
;;
155+
*)
156+
echo "Error: Unknown command '$1'"
157+
help
158+
exit 1
159+
;;
160+
esac

scripts/uninstall.sh

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)