跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

k8s.bats

172 行 · 5.5 KB历史下载
1# SPDX-FileCopyrightText: Copyright The Lima Authors
2# SPDX-License-Identifier: Apache-2.0
3
4# This test verifies that a Kubernetes cluster can be started and that the single node is ready.
5
6load "../helpers/load"
7
8: "${TEMPLATE:=k8s}"
9
10# Instance names are "${NAME}-0", "${NAME}-1", ...
11NAME="k8s"
12
13get_num_nodes() {
14 local nodes=0
15 for tag in "${BATS_TEST_TAGS[@]}"; do
16 if [[ $tag =~ ^nodes:([0-9]+)$ ]]; then
17 nodes="${BASH_REMATCH[1]}"
18 fi
19 done
20 if [[ $nodes -eq 0 ]]; then
21 echo >&2 "nodes:N tag is required"
22 exit 1
23 fi
24 echo "$nodes"
25}
26
27local_setup() {
28 local nodes=$(get_num_nodes)
29 local params=""
30 for ((i=0; i<1; i++)); do
31 limactl delete --force "${NAME}-$i" || :
32 local limactl_start_flags="--tty=false --name "${NAME}-$i""
33 # Multi-node setup requires user-v2 network for VM-to-VM communication
34 if [[ $nodes -gt 1 ]]; then
35 limactl_start_flags+=" --network lima:user-v2"
36 fi
37 limactl start ${limactl_start_flags} "template:${TEMPLATE}" 3>&- 4>&- &
38 done
39 wait $(jobs -p)
40 # Multi-node setup
41 if [[ $nodes -gt 1 ]]; then
42 for ((i=0; i<nodes; i++)); do
43 if [[ $i -eq 0 && "${TEMPLATE}" == "k8s" ]]; then
44 # Get the join command from the first node
45 join_command=$(limactl shell "${NAME}-0" sudo kubeadm token create --print-join-command)
46 # kubeadm join ADDRESS --token TOKEN --discovery-token-ca-cert-hash DISCOVERY_TOKEN_CA_CERT_HASH
47 read -ra words <<< "$join_command"
48 assert_equal "${words[1]} ${words[3]} ${words[5]}" "join --token --discovery-token-ca-cert-hash"
49 params=".param.url=\"${words[2]}\"|.param.token=\"${words[4]}\"|.param.discoveryTokenCaCertHash=\"${words[6]}\""
50 elif [[ $i -eq 0 && "${TEMPLATE}" == "k3s" ]]; then
51 url=$(printf "https://lima-%s.internal:6443\n" "${NAME}-0")
52 token=$(limactl shell "${NAME}-0" sudo cat /var/lib/rancher/k3s/server/node-token)
53 params=".param.url=\"${url}\"|.param.token=\"${token}\""
54 else
55 # Execute the join command on worker nodes
56 limactl delete --force "${NAME}-$i" || :
57 local limactl_start_flags="--tty=false --name "${NAME}-$i""
58 limactl_start_flags+=" --network lima:user-v2 --set $params"
59 limactl start ${limactl_start_flags} "template:${TEMPLATE}" 3>&- 4>&- &
60 fi
61 done
62 wait $(jobs -p)
63 fi
64 for node in $(k get node -o name); do
65 k wait --timeout=5m --for=condition=ready "${node}"
66 done
67}
68
69local_teardown() {
70 local nodes=$(get_num_nodes)
71 for ((i=0; i<nodes; i++)); do
72 limactl delete --force "${NAME}-$i" || :
73 done
74}
75
76k() {
77 # The host home directory is not mounted in the case of k8s.
78 limactl shell --workdir=/ "${NAME}-0" -- kubectl "$@"
79}
80
81# bats test_tags=nodes:1
82@test 'Single-node' {
83 # Deploy test services
84 services=(nginx coredns)
85 for svc in "${services[@]}"; do
86 k create deployment "$svc" --image="${TEST_CONTAINER_IMAGES["$svc"]}"
87 done
88 for svc in "${services[@]}"; do
89 k rollout status deployment "$svc" --timeout 60s
90 done
91
92 # Test TCP port forwarding
93 k create service nodeport nginx --node-port=31080 --tcp=80:80
94 run curl --fail --silent --show-error --retry 30 --retry-all-errors http://localhost:31080
95 assert_success
96 assert_output --partial "Welcome to nginx"
97
98 # Test UDP port forwarding
99 #
100 # `kubectl create service nodeport` does not support UDP, so use `kubectl expose` instead.
101 # https://github.com/kubernetes/kubernetes/issues/134732
102 k expose deployment coredns --port=53 --type=NodePort \
103 --overrides='{"spec":{"ports":[{"port":53,"protocol":"UDP","targetPort":53,"nodePort":32053}]}}'
104 run dig @127.0.0.1 -p 32053 lima-vm.io
105 assert_success
106
107 # Cleanup
108 for svc in "${services[@]}"; do
109 k delete service "$svc"
110 k delete deployment "$svc"
111 done
112}
113
114# bats test_tags=nodes:3
115@test 'Multi-node' {
116 [ $(k get nodes -o jsonpath='{range .items[*]}{.status.conditions[?(@.type=="Ready")].status}{"\n"}{end}' | grep -c True) -eq 3 ]
117 # Based on https://github.com/rootless-containers/usernetes/blob/gen2-v20250828.0/hack/test-smoke.sh
118 k apply -f - <<EOF
119apiVersion: v1
120kind: Service
121metadata:
122 name: dnstest
123 labels:
124 run: dnstest
125spec:
126 type: ClusterIP
127 clusterIP: None
128 ports:
129 - name: http
130 protocol: TCP
131 port: 80
132 targetPort: 80
133 selector:
134 run: dnstest
135---
136apiVersion: apps/v1
137kind: StatefulSet
138metadata:
139 name: dnstest
140spec:
141 serviceName: dnstest
142 selector:
143 matchLabels:
144 run: dnstest
145 replicas: 3
146 template:
147 metadata:
148 labels:
149 run: dnstest
150 spec:
151 affinity:
152 podAntiAffinity:
153 requiredDuringSchedulingIgnoredDuringExecution:
154 - labelSelector:
155 matchLabels:
156 run: dnstest
157 topologyKey: kubernetes.io/hostname
158 containers:
159 - name: dnstest
160 image: ${TEST_CONTAINER_IMAGES[nginx]}
161 ports:
162 - containerPort: 80
163EOF
164 k rollout status --timeout=5m statefulset/dnstest || {
165 k describe pods -l run=dnstest
166 false
167 }
168 # --rm requires -i
169 k run -i --rm --image=${TEST_CONTAINER_IMAGES[nginx]} --restart=Never dnstest-shell -- sh -exc 'for f in $(seq 0 2); do wget -O- http://dnstest-${f}.dnstest.default.svc.cluster.local; done'
170 k delete service dnstest
171 k delete statefulset dnstest
172}