Skip to content

Commit a93cbe6

Browse files
authored
feat: macos support for dev cluster scripts (knative#2897)
1 parent 84d2ad0 commit a93cbe6

File tree

4 files changed

+119
-37
lines changed

4 files changed

+119
-37
lines changed

hack/allocate.sh

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ set -o errexit
2020
set -o nounset
2121
set -o pipefail
2222

23-
source "$(dirname "$(realpath "$0")")/common.sh"
23+
source "$(cd "$(dirname "$0")" && pwd)/common.sh"
2424
# this is where versions of common components are (like knative)
25-
source "$(dirname "$(realpath "$0")")/component-versions.sh"
25+
source "$(cd "$(dirname "$0")" && pwd)/component-versions.sh"
2626

2727
main() {
2828
echo "${blue}Allocating${reset}"
@@ -442,7 +442,10 @@ next_steps() {
442442
echo -e "${grey}REGISTRY"
443443
echo -e "Before using the cluster for integration and E2E tests, please run \"${reset}registry.sh${grey}\" (Linux systems) which will configure podman or docker to communicate with the standalone container registry without TLS."
444444
echo -e ""
445-
echo -e "For other operating systems, or to do this manually, edit the docker daemon config (/etc/docker/daemon.json on linux and ~/.docker/daemon.json on OSX), add:"
445+
echo -e "For other operating systems, or to do this manually, edit the docker daemon config:"
446+
echo -e " - Linux: /etc/docker/daemon.json"
447+
echo -e " - macOS: ~/.docker/daemon.json (or via Docker Desktop settings)"
448+
echo -e "Add the following configuration:"
446449
echo -e "${reset}{ \"insecure-registries\": [ \"localhost:50000\" ] }"
447450
echo -e ""
448451
echo -e "${grey}For podman, edit /etc/container/registries.conf to include:"

hack/common.sh

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,25 @@ find_executables() {
2929
}
3030

3131
populate_environment() {
32-
export ARCH="${ARCH:-amd64}"
32+
# User's KUBECOFNIG and that used by these scripts should be isolated:
33+
export KUBECONFIG="$(cd "$(dirname "$0")" && pwd)/bin/kubeconfig.yaml"
34+
# Detect architecture, default to amd64 if unable to detect
35+
if [[ -z "${ARCH:-}" ]]; then
36+
local machine_arch=$(uname -m)
37+
case $machine_arch in
38+
x86_64) export ARCH="amd64" ;;
39+
aarch64|arm64) export ARCH="arm64" ;;
40+
*) export ARCH="amd64" ;;
41+
esac
42+
else
43+
export ARCH="$ARCH"
44+
fi
3345
export CONTAINER_ENGINE=${CONTAINER_ENGINE:-docker}
34-
export KUBECONFIG="${KUBECONFIG:-$(dirname "$(realpath "$0")")/bin/kubeconfig.yaml}"
3546
export TERM="${TERM:-dumb}"
47+
3648
echo "KUBECONFIG=${KUBECONFIG}"
49+
echo "CONTAINER_ENGINE=${CONTAINER_ENGINE}"
50+
echo "TERM=${TERM}"
3751
}
3852

3953
define_colors() {
@@ -75,7 +89,7 @@ find_executable() {
7589

7690
# Use the binary installed into hack/bin/ by allocate.sh if
7791
# it exists.
78-
path=$(dirname "$(realpath "$0")")"/bin/$name"
92+
path=$(cd "$(dirname "$0")" && pwd)"/bin/$name"
7993
if [[ -x "$path" ]]; then
8094
echo "$path" & return 0
8195
fi

hack/install-binaries.sh

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,23 @@
1919
source "$(dirname "$(realpath "$0")")/common.sh"
2020

2121
install_binaries() {
22-
assert_linux
22+
assert_supported_os
23+
set_os_arch_vars
2324
warn_architecture
2425

2526
local root="$(dirname "$(realpath "$0")")"
2627
local bin="${root}/bin"
2728

28-
local kubectl_version=1.32.0
29-
local kind_version=0.26.0
30-
local dapr_version=1.11.0
31-
local helm_version=3.12.0
32-
local stern_version=1.25.0
33-
local kn_version=1.13.0
29+
local kubectl_version=1.33.1
30+
local kind_version=0.29.0
31+
local dapr_version=1.14.1
32+
local helm_version=3.18.0
33+
local stern_version=1.32.0
34+
local kn_version=1.18.0
3435
local jq_version=1.7.1
3536

3637
echo "${blue}Installing binaries${reset}"
38+
echo " OS: ${OS}"
3739
echo " Architecture: ${ARCH}"
3840
echo " Destination: ${bin}"
3941

@@ -51,71 +53,97 @@ install_binaries() {
5153

5254
}
5355

54-
assert_linux() {
56+
assert_supported_os() {
5557
os_name=$(uname -s)
56-
if [ "$os_name" != "Linux" ]; then
58+
if [ "$os_name" != "Linux" ] && [ "$os_name" != "Darwin" ]; then
5759
echo "${yellow}----------------------------------------------------------------------${reset}"
58-
echo "${yellow}This script currently only supports Linux${reset}"
60+
echo "${yellow}This script only supports Linux and Darwin (macOS)${reset}"
5961
echo "Please install the dependencies manually"
6062
echo "${yellow}----------------------------------------------------------------------${reset}"
6163
exit 1
6264
fi
6365
}
6466

67+
set_os_arch_vars() {
68+
OS=$(uname -s | tr '[:upper:]' '[:lower:]')
69+
ARCH_RAW=$(uname -m)
70+
71+
# Map architecture names
72+
case "${ARCH_RAW}" in
73+
x86_64)
74+
ARCH="amd64"
75+
;;
76+
aarch64|arm64)
77+
ARCH="arm64"
78+
;;
79+
*)
80+
ARCH="${ARCH_RAW}"
81+
;;
82+
esac
83+
84+
# Override with environment variable if set
85+
ARCH="${ARCH:-$ARCH}"
86+
}
87+
6588
warn_architecture() {
6689
arch=$(uname -m)
67-
if [ "$arch" != "x86_64" ]; then
68-
echo -e "${yellow}Detected untested architecture ${arch}.${reset}\n This script is only tested with amd64, but you can use the ARCH env variable to specify an architecture to be interpolated in download links."
90+
if [ "$arch" != "x86_64" ] && [ "$arch" != "arm64" ] && [ "$arch" != "aarch64" ]; then
91+
echo -e "${yellow}Detected untested architecture ${arch}.${reset}\n This script is tested with amd64 and arm64, but you can use the ARCH env variable to specify an architecture to be interpolated in download links."
6992
fi
7093
}
7194

7295
install_kubectl() {
7396
echo '=== kubectl'
74-
curl -sSLo "${bin}"/kubectl "https://dl.k8s.io/v${kubectl_version}/bin/linux/${ARCH}/kubectl"
97+
curl -sSLo "${bin}"/kubectl "https://dl.k8s.io/v${kubectl_version}/bin/${OS}/${ARCH}/kubectl"
7598
chmod +x "${bin}"/kubectl
7699
"${bin}"/kubectl version --client=true
77100
}
78101

79102
install_kind() {
80103
echo '=== kind'
81-
curl -sSLo "${bin}"/kind "https://github.com/kubernetes-sigs/kind/releases/download/v$kind_version/kind-linux-${ARCH}"
104+
curl -sSLo "${bin}"/kind "https://github.com/kubernetes-sigs/kind/releases/download/v$kind_version/kind-${OS}-${ARCH}"
82105
chmod +x "${bin}"/kind
83106
"${bin}"/kind version
84107
}
85108

86109
install_dapr() {
87110
echo '=== dapr'
88-
curl -sSL "https://github.com/dapr/cli/releases/download/v$dapr_version/dapr_linux_${ARCH}.tar.gz" | \
111+
curl -sSL "https://github.com/dapr/cli/releases/download/v$dapr_version/dapr_${OS}_${ARCH}.tar.gz" | \
89112
tar fxz - -C "${bin}" dapr
90113
"${bin}"/dapr version
91114
}
92115

93116
install_helm() {
94117
echo '=== helm'
95-
curl -sSL "https://get.helm.sh/helm-v$helm_version-linux-${ARCH}.tar.gz" | \
96-
tar fxz - -C "${bin}" linux-"${ARCH}"/helm
97-
mv "${bin}/linux-${ARCH}"/helm "${bin}" && rmdir "${bin}/linux-${ARCH}"
118+
curl -sSL "https://get.helm.sh/helm-v$helm_version-${OS}-${ARCH}.tar.gz" | \
119+
tar fxz - -C "${bin}" ${OS}-"${ARCH}"/helm
120+
mv "${bin}/${OS}-${ARCH}"/helm "${bin}" && rmdir "${bin}/${OS}-${ARCH}"
98121
"${bin}"/helm version
99122
}
100123

101124
install_stern() {
102125
echo '=== stern'
103-
curl -sSL "https://github.com/stern/stern/releases/download/v${stern_version}/stern_${stern_version}_linux_${ARCH}.tar.gz" | \
126+
curl -sSL "https://github.com/stern/stern/releases/download/v${stern_version}/stern_${stern_version}_${OS}_${ARCH}.tar.gz" | \
104127
tar fxz - -C "${bin}" stern
105128
"${bin}"/stern -v
106129
}
107130

108131
install_kn() {
109132
echo '=== kn'
110-
curl -sSLo "${bin}"/kn "https://github.com/knative/client/releases/download/knative-v${kn_version}/kn-linux-${ARCH}"
133+
curl -sSLo "${bin}"/kn "https://github.com/knative/client/releases/download/knative-v${kn_version}/kn-${OS}-${ARCH}"
111134
chmod +x "${bin}"/kn
112135
"${bin}"/kn version
113136
}
114137

115138
install_jq() {
116139
echo '=== jq'
117-
# "https://github.com/jqlang/jq/releases/download/jq-1.7.1/jq-linux-amd64"
118-
curl -sSLo "${bin}"/jq "https://github.com/jqlang/jq/releases/download/jq-${jq_version}/jq-linux-${ARCH}"
140+
# jq uses different naming conventions for macOS
141+
if [ "$OS" = "darwin" ]; then
142+
JQ_OS="macos"
143+
else
144+
JQ_OS="linux"
145+
fi
146+
curl -sSLo "${bin}"/jq "https://github.com/jqlang/jq/releases/download/jq-${jq_version}/jq-${JQ_OS}-${ARCH}"
119147
chmod +x "${bin}"/jq
120148
"${bin}"/jq --version
121149
}

hack/registry.sh

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414

1515
#
16-
# - Registers registry with Docker as trusted (linux only)
16+
# - Registers registry with Docker as trusted (Linux and macOS)
1717
#
1818

1919
set -o errexit
@@ -42,11 +42,16 @@ warn_nix() {
4242
if [[ -x $(command -v "nix") || -x $(command -v "nixos-rebuild") ]]; then
4343
if [ "$CONTAINER_ENGINE" == "docker" ]; then
4444
echo "${yellow}Warning: Nix detected${reset}"
45-
echo "If Docker was configured using nix, this command will fail to find daemon.json. please configure the insecure registry by modifying your nix config:"
46-
echo " virtualisation.docker = {"
47-
echo " enable = true;"
48-
echo " daemon.settings.insecure-registries = [ \"localhost:50000\" ];"
49-
echo " };"
45+
if [[ "$(uname)" == "Darwin" ]]; then
46+
echo "If Docker Desktop was installed via Nix on macOS, you may need to manually configure the insecure registry."
47+
echo "Please confirm \"localhost:50000\" is specified as an insecure registry in the docker config file."
48+
else
49+
echo "If Docker was configured using nix, this command will fail to find daemon.json. please configure the insecure registry by modifying your nix config:"
50+
echo " virtualisation.docker = {"
51+
echo " enable = true;"
52+
echo " daemon.settings.insecure-registries = [ \"localhost:50000\" ];"
53+
echo " };"
54+
fi
5055
elif [ "$CONTAINER_ENGINE" == "podman" ]; then
5156
echo "${yellow}Warning: Nix detected${reset}"
5257
echo "If podman was configured via Nix, this command will likely fail. At time of this writing, podman configured via the nix option 'virtualisation.podman' does not have an option for configuring insecure registries."
@@ -57,9 +62,35 @@ warn_nix() {
5762
}
5863

5964
set_registry_insecure() {
60-
patch=".\"insecure-registries\" = [\"localhost:50000\""]
61-
sudo jq "$patch" /etc/docker/daemon.json > /tmp/daemon.json.tmp && sudo mv /tmp/daemon.json.tmp /etc/docker/daemon.json
62-
sudo service docker restart
65+
# Determine the daemon.json location based on OS
66+
if [[ "$(uname)" == "Darwin" ]]; then
67+
# macOS: Docker Desktop stores daemon.json in ~/.docker/
68+
DAEMON_JSON="$HOME/.docker/daemon.json"
69+
USE_SUDO=""
70+
else
71+
# Linux: daemon.json is in /etc/docker/
72+
DAEMON_JSON="/etc/docker/daemon.json"
73+
USE_SUDO="sudo"
74+
fi
75+
76+
# Create daemon.json if it doesn't exist
77+
if [ ! -f "$DAEMON_JSON" ]; then
78+
echo "{}" | $USE_SUDO tee "$DAEMON_JSON" > /dev/null
79+
fi
80+
81+
# Update daemon.json with insecure registry
82+
patch=".\"insecure-registries\" = [\"localhost:50000\"]"
83+
$USE_SUDO jq "$patch" "$DAEMON_JSON" > /tmp/daemon.json.tmp && $USE_SUDO mv /tmp/daemon.json.tmp "$DAEMON_JSON"
84+
echo "OK $DAEMON_JSON"
85+
86+
# Restart Docker based on OS
87+
if [[ "$(uname)" == "Darwin" ]]; then
88+
# macOS: Restart Docker Desktop
89+
echo "${yellow}*** If Docker Desktop is running, please restart it via the menu bar icon ***${reset}"
90+
else
91+
# Linux: Use service command
92+
sudo service docker restart
93+
fi
6394
}
6495

6596
set_registry_insecure_podman() {
@@ -70,6 +101,12 @@ set_registry_insecure_podman() {
70101
# Append the new section to the file
71102
echo -e "\n[[registry-insecure-local]]\nlocation = \"localhost:50000\"\ninsecure = true" | sudo tee -a "$FILE" > /dev/null
72103
fi
104+
105+
# On macOS, set up SSH port forwarding so Podman VM can access host's localhost:50000
106+
if [[ "$(uname)" == "Darwin" ]]; then
107+
echo "Setting up port forwarding for Podman VM to access registry..."
108+
podman machine ssh -- -L 50000:localhost:50000 -N -f
109+
fi
73110
}
74111

75112
if [ "$0" = "${BASH_SOURCE[0]}" ]; then

0 commit comments

Comments
 (0)