Skip to content

Commit f61c474

Browse files
committed
feat(components/ingress): replace pod with batch sandbox cr
1 parent 7e5cdcf commit f61c474

File tree

18 files changed

+855
-434
lines changed

18 files changed

+855
-434
lines changed

components/ingress/DEVELOPMENT.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,26 @@
33
## Prerequisites
44
- Go 1.24+
55
- Docker (optional, for image build)
6-
- Access to a Kubernetes cluster if you want to exercise proxy behavior.
6+
- Access to a Kubernetes cluster with BatchSandbox CRD installed.
77

88
## Install deps
99
```bash
10-
cd components/router
10+
cd components/ingress
1111
go mod tidy && go mod vendor
1212
```
1313

1414
## Build & Run
1515
```bash
16-
make build # binary at bin/router with ldflags version info
17-
./bin/router \
18-
--namespace <ns> \
19-
--ingress-label-key <label-key> \
16+
make build # binary at bin/ingress with ldflags version info
17+
./bin/ingress \
18+
--namespace <target-namespace> \
2019
--port 28888 \
2120
--log-level info
2221
```
2322

2423
## Tests & Lint
2524
```bash
26-
make test # go test ./pkg/...
25+
make test # go test ./...
2726
go vet ./... # included in make build
2827
```
2928

@@ -33,15 +32,18 @@ docker build \
3332
--build-arg VERSION=$(git describe --tags --always --dirty) \
3433
--build-arg GIT_COMMIT=$(git rev-parse HEAD) \
3534
--build-arg BUILD_TIME=$(date -u +"%Y-%m-%dT%H:%M:%SZ") \
36-
-t opensandbox/router:dev .
35+
-t opensandbox/ingress:dev .
3736
```
3837

3938
## Key Paths
40-
- `main.go` — entrypoint, HTTP routes.
41-
- `pkg/proxy/` — HTTP/WebSocket proxy logic and pod watching.
39+
- `main.go` — entrypoint, HTTP routes, provider initialization.
40+
- `pkg/proxy/` — HTTP/WebSocket reverse proxy logic.
41+
- `pkg/sandbox/` — Sandbox provider abstraction and BatchSandbox implementation.
4242
- `version/` — build metadata (ldflags).
4343

4444
## Tips
4545
- Health check: `/status.ok`
46+
- Proxy endpoint: `/` (routes based on `OPEN-SANDBOX-INGRESS` header or Host)
4647
- Env overrides: `VERSION/GIT_COMMIT/BUILD_TIME` usable via Makefile and build.sh.
48+
- BatchSandbox must have `sandbox.opensandbox.io/endpoints` annotation with JSON array of IPs.
4749

components/ingress/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,7 @@ LDFLAGS := -X 'github.com/alibaba/opensandbox/router/version.Version=$(VERSION)'
3737
build: vet ## Build the binary.
3838
@mkdir -p bin
3939
go build -ldflags "$(LDFLAGS)" -o bin/router main.go
40+
41+
.PHONY: clean
42+
clean: ## Clean build artifacts.
43+
rm -rf bin/ vendor/

components/ingress/README.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33
English | [中文](README_zh.md)
44

55
## Overview
6-
- HTTP/WebSocket reverse proxy that routes to sandbox Pods by `OPEN-SANDBOX-INGRESS` header or Host.
7-
- Watches Pods in a target Namespace filtered by an ingress label, and routes only when exactly one ready Pod matches.
6+
- HTTP/WebSocket reverse proxy that routes to sandbox instances by `OPEN-SANDBOX-INGRESS` header or Host.
7+
- Watches BatchSandbox CRs in a target Namespace, and routes to sandbox endpoints from annotations.
88
- Exposes `/status.ok` health check; prints build metadata (version, commit, time, Go/platform) at startup.
99

1010
## Quick Start
1111
```bash
1212
go run main.go \
1313
--namespace <target-namespace> \
14-
--ingress-label-key <label-key> \
1514
--port 28888 \
1615
--log-level info
1716
```
@@ -44,11 +43,15 @@ TAG=local VERSION=1.2.3 GIT_COMMIT=abc BUILD_TIME=2025-01-01T00:00:00Z bash buil
4443

4544
## Runtime Requirements
4645
- Access to Kubernetes API (in-cluster or via KUBECONFIG).
47-
- Pods in the specified Namespace labeled with the configured ingress label; Pod IPs must be reachable.
46+
- BatchSandbox CRs in the specified Namespace with `sandbox.opensandbox.io/endpoints` annotation containing Pod IPs.
4847

4948
## Behavior Notes
50-
- Routing key priority: `OPEN-SANDBOX-INGRESS` header first, otherwise Host parsing `<ingress>-<port>.*`.
51-
- Multiple matching Pods → HTTP 409; no matching Pod → HTTP 404.
49+
- Routing key priority: `OPEN-SANDBOX-INGRESS` header first, otherwise Host parsing `<sandbox-name>-<port>.*`.
50+
- Sandbox name extracted from request is used to query BatchSandbox CR for endpoint IP.
51+
- Error handling:
52+
- `ErrSandboxNotFound` (sandbox resource not exists) → HTTP 404
53+
- `ErrSandboxNotReady` (not enough replicas, missing endpoints, invalid config) → HTTP 503
54+
- Other errors (K8s API errors, etc.) → HTTP 502
5255
- WebSocket path forwards essential headers and X-Forwarded-*; HTTP path strips `OPEN-SANDBOX-INGRESS` before proxying.
5356

5457
## Development & Tests
@@ -58,6 +61,7 @@ go test ./...
5861
```
5962
Key code:
6063
- `main.go`: entrypoint and handlers.
61-
- `pkg/proxy/`: HTTP/WebSocket proxy logic, pod watching, health check.
64+
- `pkg/proxy/`: HTTP/WebSocket proxy logic, sandbox endpoint resolution.
65+
- `pkg/sandbox/`: Sandbox provider abstraction and BatchSandbox implementation.
6266
- `version/`: build metadata output (populated via ldflags).
6367

components/ingress/README_zh.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
# OpenSandbox Ingress
22

33
## 功能概览
4-
- 基于 Kubernetes Pod 标签的 HTTP / WebSocket 反向代理,按 `OPEN-SANDBOX-INGRESS` 或 Host 解析目标沙箱。
5-
- 自动监听目标 Namespace 内带有 `ingress-label-key` 标签的 Pod 列表,动态路由到单个可用实例,避免多实例冲突
4+
- 基于 Kubernetes BatchSandbox CR 的 HTTP / WebSocket 反向代理,按 `OPEN-SANDBOX-INGRESS` 或 Host 解析目标沙箱。
5+
- 自动监听目标 Namespace 内的 BatchSandbox 资源,从 annotation 中获取端点 IP
66
- 提供 `/status.ok` 健康探针,启动时打印编译版本、时间、提交、Go/平台信息。
77

88
## 启动与参数
99
```bash
1010
go run main.go \
1111
--namespace <目标命名空间> \
12-
--ingress-label-key <标签键> \
1312
--port 28888 \
1413
--log-level info
1514
```
1615
- `--namespace`:监听的 Kubernetes 命名空间。
17-
- `--ingress-label-key`:用于匹配沙箱 Pod 的标签键。
1816
- `--port`:监听端口(默认 28888)。
1917
- `--log-level`:日志级别,遵循 zap 定义。
2018

@@ -48,7 +46,7 @@ TAG=local VERSION=1.2.3 GIT_COMMIT=abc BUILD_TIME=2025-01-01T00:00:00Z bash buil
4846

4947
## 运行时依赖
5048
- 可访问的 Kubernetes API(集群内或 KUBECONFIG)。
51-
- 目标命名空间中按 `ingress-label-key` 标记的运行中 Pod,并确保 Pod IP 可直连
49+
- 目标命名空间中的 BatchSandbox CR 必须包含 `sandbox.opensandbox.io/endpoints` annotation
5250

5351
## 开发与测试
5452
```bash
@@ -57,11 +55,16 @@ go test ./...
5755
```
5856
主要代码位置:
5957
- `main.go`:入口与 HTTP 路由注册。
60-
- `pkg/proxy/`:HTTP/WebSocket 代理逻辑、Pod 监听、健康检查。
58+
- `pkg/proxy/`:HTTP/WebSocket 代理逻辑、沙箱端点解析。
59+
- `pkg/sandbox/`:沙箱 Provider 抽象和 BatchSandbox 实现。
6160
- `version/`:版本信息输出(ldflags 注入)。
6261

6362
## 常见行为说明
64-
- Header 优先:`OPEN-SANDBOX-INGRESS`,否则回退 Host 解析 `<ingress>-<port>.*`
65-
- 多实例同 ingress 时返回 409;无可用 Pod 返回 404。
63+
- Header 优先:`OPEN-SANDBOX-INGRESS`,否则回退 Host 解析 `<sandbox-name>-<port>.*`
64+
- 从请求中提取沙箱名称,查询 BatchSandbox CR 获取端点 IP。
65+
- 错误处理:
66+
- `ErrSandboxNotFound`(沙箱资源不存在)→ HTTP 404
67+
- `ErrSandboxNotReady`(副本数不足、缺少端点、配置无效)→ HTTP 503
68+
- 其他错误(K8s API 错误等)→ HTTP 502
6669
- WebSocket 保留关键头并透传 X-Forwarded-*,HTTP 会移除 `OPEN-SANDBOX-INGRESS` 后再转发。
6770

components/ingress/go.mod

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@ module github.com/alibaba/opensandbox/ingress
33
go 1.24.0
44

55
require (
6-
github.com/google/uuid v1.6.0
6+
github.com/alibaba/OpenSandbox/sandbox-k8s v0.0.0
77
github.com/gorilla/websocket v1.5.4-0.20250319132907-e064f32e3674
88
github.com/stretchr/testify v1.11.1
99
go.uber.org/zap v1.27.1
10-
k8s.io/api v0.34.3
1110
k8s.io/apimachinery v0.34.3
1211
k8s.io/client-go v0.34.3
13-
k8s.io/utils v0.0.0-20250604170112-4c0f3b243397
14-
knative.dev/pkg v0.0.0-20251217214024-80c8bc434670
12+
knative.dev/pkg v0.0.0-20260120122510-4a022ed9999a
1513
)
1614

1715
require (
@@ -29,6 +27,7 @@ require (
2927
github.com/gogo/protobuf v1.3.2 // indirect
3028
github.com/google/gnostic-models v0.7.0 // indirect
3129
github.com/google/go-cmp v0.7.0 // indirect
30+
github.com/google/uuid v1.6.0 // indirect
3231
github.com/hashicorp/golang-lru v1.0.2 // indirect
3332
github.com/josharian/intern v1.0.0 // indirect
3433
github.com/json-iterator/go v1.1.12 // indirect
@@ -48,22 +47,27 @@ require (
4847
go.uber.org/multierr v1.11.0 // indirect
4948
go.yaml.in/yaml/v2 v2.4.3 // indirect
5049
go.yaml.in/yaml/v3 v3.0.4 // indirect
51-
golang.org/x/net v0.48.0 // indirect
50+
golang.org/x/net v0.49.0 // indirect
5251
golang.org/x/oauth2 v0.32.0 // indirect
5352
golang.org/x/sync v0.19.0 // indirect
54-
golang.org/x/sys v0.39.0 // indirect
55-
golang.org/x/term v0.38.0 // indirect
56-
golang.org/x/text v0.32.0 // indirect
53+
golang.org/x/sys v0.40.0 // indirect
54+
golang.org/x/term v0.39.0 // indirect
55+
golang.org/x/text v0.33.0 // indirect
5756
golang.org/x/time v0.10.0 // indirect
5857
gomodules.xyz/jsonpatch/v2 v2.5.0 // indirect
5958
google.golang.org/protobuf v1.36.10 // indirect
6059
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
6160
gopkg.in/inf.v0 v0.9.1 // indirect
6261
gopkg.in/yaml.v3 v3.0.1 // indirect
62+
k8s.io/api v0.34.3 // indirect
6363
k8s.io/klog/v2 v2.130.1 // indirect
6464
k8s.io/kube-openapi v0.0.0-20250710124328-f3f2b991d03b // indirect
65+
k8s.io/utils v0.0.0-20250604170112-4c0f3b243397 // indirect
66+
sigs.k8s.io/controller-runtime v0.21.0 // indirect
6567
sigs.k8s.io/json v0.0.0-20241014173422-cfa47c3a1cc8 // indirect
6668
sigs.k8s.io/randfill v1.0.0 // indirect
6769
sigs.k8s.io/structured-merge-diff/v6 v6.3.0 // indirect
6870
sigs.k8s.io/yaml v1.6.0 // indirect
6971
)
72+
73+
replace github.com/alibaba/OpenSandbox/sandbox-k8s => ../../kubernetes

components/ingress/go.sum

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ github.com/modern-go/reflect2 v1.0.3-0.20250322232337-35a7c28c31ee h1:W5t00kpgFd
6464
github.com/modern-go/reflect2 v1.0.3-0.20250322232337-35a7c28c31ee/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
6565
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
6666
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
67-
github.com/onsi/ginkgo/v2 v2.21.0 h1:7rg/4f3rB88pb5obDgNZrNHrQ4e6WpjonchcpuBRnZM=
68-
github.com/onsi/ginkgo/v2 v2.21.0/go.mod h1:7Du3c42kxCUegi0IImZ1wUQzMBVecgIHjR1C+NkhLQo=
69-
github.com/onsi/gomega v1.35.1 h1:Cwbd75ZBPxFSuZ6T+rN/WCb/gOc6YgFBXLlZLhC7Ds4=
70-
github.com/onsi/gomega v1.35.1/go.mod h1:PvZbdDc8J6XJEpDK4HCuRBm8a6Fzp9/DmhC9C7yFlog=
67+
github.com/onsi/ginkgo/v2 v2.22.0 h1:Yed107/8DjTr0lKCNt7Dn8yQ6ybuDRQoMGrNFKzMfHg=
68+
github.com/onsi/ginkgo/v2 v2.22.0/go.mod h1:7Du3c42kxCUegi0IImZ1wUQzMBVecgIHjR1C+NkhLQo=
69+
github.com/onsi/gomega v1.36.1 h1:bJDPBO7ibjxcbHMgSCoo4Yj18UWbKDlLwX1x9sybDcw=
70+
github.com/onsi/gomega v1.36.1/go.mod h1:PvZbdDc8J6XJEpDK4HCuRBm8a6Fzp9/DmhC9C7yFlog=
7171
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
7272
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
7373
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
@@ -117,14 +117,14 @@ golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8U
117117
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
118118
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
119119
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
120-
golang.org/x/mod v0.31.0 h1:HaW9xtz0+kOcWKwli0ZXy79Ix+UW/vOfmWI5QVd2tgI=
121-
golang.org/x/mod v0.31.0/go.mod h1:43JraMp9cGx1Rx3AqioxrbrhNsLl2l/iNAvuBkrezpg=
120+
golang.org/x/mod v0.32.0 h1:9F4d3PHLljb6x//jOyokMv3eX+YDeepZSEo3mFJy93c=
121+
golang.org/x/mod v0.32.0/go.mod h1:SgipZ/3h2Ci89DlEtEXWUk/HteuRin+HHhN+WbNhguU=
122122
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
123123
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
124124
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
125125
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
126-
golang.org/x/net v0.48.0 h1:zyQRTTrjc33Lhh0fBgT/H3oZq9WuvRR5gPC70xpDiQU=
127-
golang.org/x/net v0.48.0/go.mod h1:+ndRgGjkh8FGtu1w1FGbEC31if4VrNVMuKTgcAAnQRY=
126+
golang.org/x/net v0.49.0 h1:eeHFmOGUTtaaPSGNmjBKpbng9MulQsJURQUAfUwY++o=
127+
golang.org/x/net v0.49.0/go.mod h1:/ysNB2EvaqvesRkuLAyjI1ycPZlQHM3q01F02UY/MV8=
128128
golang.org/x/oauth2 v0.32.0 h1:jsCblLleRMDrxMN29H3z/k1KliIvpLgCkE6R8FXXNgY=
129129
golang.org/x/oauth2 v0.32.0/go.mod h1:lzm5WQJQwKZ3nwavOZ3IS5Aulzxi68dUSgRHujetwEA=
130130
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
@@ -135,22 +135,22 @@ golang.org/x/sync v0.19.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
135135
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
136136
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
137137
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
138-
golang.org/x/sys v0.39.0 h1:CvCKL8MeisomCi6qNZ+wbb0DN9E5AATixKsvNtMoMFk=
139-
golang.org/x/sys v0.39.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
140-
golang.org/x/term v0.38.0 h1:PQ5pkm/rLO6HnxFR7N2lJHOZX6Kez5Y1gDSJla6jo7Q=
141-
golang.org/x/term v0.38.0/go.mod h1:bSEAKrOT1W+VSu9TSCMtoGEOUcKxOKgl3LE5QEF/xVg=
138+
golang.org/x/sys v0.40.0 h1:DBZZqJ2Rkml6QMQsZywtnjnnGvHza6BTfYFWY9kjEWQ=
139+
golang.org/x/sys v0.40.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
140+
golang.org/x/term v0.39.0 h1:RclSuaJf32jOqZz74CkPA9qFuVTX7vhLlpfj/IGWlqY=
141+
golang.org/x/term v0.39.0/go.mod h1:yxzUCTP/U+FzoxfdKmLaA0RV1WgE0VY7hXBwKtY/4ww=
142142
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
143143
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
144-
golang.org/x/text v0.32.0 h1:ZD01bjUt1FQ9WJ0ClOL5vxgxOI/sVCNgX1YtKwcY0mU=
145-
golang.org/x/text v0.32.0/go.mod h1:o/rUWzghvpD5TXrTIBuJU77MTaN0ljMWE47kxGJQ7jY=
144+
golang.org/x/text v0.33.0 h1:B3njUFyqtHDUI5jMn1YIr5B0IE2U0qck04r6d4KPAxE=
145+
golang.org/x/text v0.33.0/go.mod h1:LuMebE6+rBincTi9+xWTY8TztLzKHc/9C1uBCG27+q8=
146146
golang.org/x/time v0.10.0 h1:3usCWA8tQn0L8+hFJQNgzpWbd89begxN66o1Ojdn5L4=
147147
golang.org/x/time v0.10.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
148148
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
149149
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
150150
golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
151151
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
152-
golang.org/x/tools v0.40.0 h1:yLkxfA+Qnul4cs9QA3KnlFu0lVmd8JJfoq+E41uSutA=
153-
golang.org/x/tools v0.40.0/go.mod h1:Ik/tzLRlbscWpqqMRjyWYDisX8bG13FrdXp3o4Sr9lc=
152+
golang.org/x/tools v0.41.0 h1:a9b8iMweWG+S0OBnlU36rzLp20z1Rp10w+IY2czHTQc=
153+
golang.org/x/tools v0.41.0/go.mod h1:XSY6eDqxVNiYgezAVqqCeihT4j1U2CCsqvH3WhQpnlg=
154154
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
155155
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
156156
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
@@ -180,8 +180,10 @@ k8s.io/kube-openapi v0.0.0-20250710124328-f3f2b991d03b h1:MloQ9/bdJyIu9lb1PzujOP
180180
k8s.io/kube-openapi v0.0.0-20250710124328-f3f2b991d03b/go.mod h1:UZ2yyWbFTpuhSbFhv24aGNOdoRdJZgsIObGBUaYVsts=
181181
k8s.io/utils v0.0.0-20250604170112-4c0f3b243397 h1:hwvWFiBzdWw1FhfY1FooPn3kzWuJ8tmbZBHi4zVsl1Y=
182182
k8s.io/utils v0.0.0-20250604170112-4c0f3b243397/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
183-
knative.dev/pkg v0.0.0-20251217214024-80c8bc434670 h1:MKgHnTvNprMn+Tr73CRB088PqR22q4KuVFIBTLFltwA=
184-
knative.dev/pkg v0.0.0-20251217214024-80c8bc434670/go.mod h1:jU9OxeX3zL4W6aHpdMjMA/B7kgkm5JQv6PGMod2Qu/M=
183+
knative.dev/pkg v0.0.0-20260120122510-4a022ed9999a h1:9f29OTA7w/iVIX6PS6yveVVzNbcUS74eQfchVe8o2/4=
184+
knative.dev/pkg v0.0.0-20260120122510-4a022ed9999a/go.mod h1:Tz3GoxcNC5vH3Zo//cW3mnHL474u+Y1wbsUIZ11p8No=
185+
sigs.k8s.io/controller-runtime v0.21.0 h1:CYfjpEuicjUecRk+KAeyYh+ouUBn4llGyDYytIGcJS8=
186+
sigs.k8s.io/controller-runtime v0.21.0/go.mod h1:OSg14+F65eWqIu4DceX7k/+QRAbTTvxeQSNSOQpukWM=
185187
sigs.k8s.io/json v0.0.0-20241014173422-cfa47c3a1cc8 h1:gBQPwqORJ8d8/YNZWEjoZs7npUVDpVXUUOFfW6CgAqE=
186188
sigs.k8s.io/json v0.0.0-20241014173422-cfa47c3a1cc8/go.mod h1:mdzfpAEoE6DHQEN0uh9ZbOCuHbLK5wOm7dK4ctXE9Tg=
187189
sigs.k8s.io/randfill v1.0.0 h1:JfjMILfT8A6RbawdsK2JXGBR5AQVfd+9TbzrlneTyrU=

components/ingress/main.go

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,44 +19,59 @@ import (
1919
"fmt"
2020
"log"
2121
"net/http"
22+
"time"
2223

2324
"go.uber.org/zap/zapcore"
2425
"k8s.io/apimachinery/pkg/runtime"
25-
"k8s.io/client-go/kubernetes"
26-
kubeclient "knative.dev/pkg/client/injection/kube/client"
2726
"knative.dev/pkg/injection"
2827
"knative.dev/pkg/logging"
2928
"knative.dev/pkg/signals"
3029

3130
"github.com/alibaba/opensandbox/ingress/pkg/flag"
3231
"github.com/alibaba/opensandbox/ingress/pkg/proxy"
32+
"github.com/alibaba/opensandbox/ingress/pkg/sandbox"
3333
"github.com/alibaba/opensandbox/ingress/version"
3434
)
3535

3636
func main() {
3737
version.EchoVersion()
3838

3939
flag.InitFlags()
40-
if flag.IngressLabelKey == "" || flag.Namespace == "" {
41-
log.Panicf("'-ingress-label-key' and/or '-namespace' not set.")
40+
if flag.Namespace == "" {
41+
log.Panicf("'-namespace' not set.")
4242
}
4343

4444
cfg := injection.ParseAndGetRESTConfigOrDie()
4545
cfg.ContentType = runtime.ContentTypeProtobuf
4646
cfg.UserAgent = "opensandbox-ingress/" + version.GitCommit
4747

48-
ctx := injection.WithNamespaceScope(signals.NewContext(), flag.Namespace)
48+
ctx := signals.NewContext()
4949
ctx = withLogger(ctx, flag.LogLevel)
5050

51-
clientset := kubernetes.NewForConfigOrDie(cfg)
52-
ctx = context.WithValue(ctx, kubeclient.Key{}, clientset)
51+
// Create sandbox provider factory
52+
providerFactory := sandbox.NewProviderFactory(
53+
cfg,
54+
flag.Namespace,
55+
time.Second*30, // resync period
56+
)
5357

54-
reverseProxy := proxy.NewProxy(ctx)
58+
// Create sandbox provider based on provider type
59+
sandboxProvider, err := providerFactory.CreateProvider(flag.ProviderType)
60+
if err != nil {
61+
log.Panicf("Failed to create sandbox provider: %v", err)
62+
}
63+
64+
// Start provider (includes cache sync)
65+
if err := sandboxProvider.Start(ctx); err != nil {
66+
log.Panicf("Failed to start sandbox provider: %v", err)
67+
}
68+
69+
// Create reverse proxy with sandbox provider
70+
reverseProxy := proxy.NewProxy(ctx, sandboxProvider)
5571
http.Handle("/", reverseProxy)
5672
http.HandleFunc("/status.ok", proxy.Healthz)
5773

58-
err := http.ListenAndServe(fmt.Sprintf(":%v", flag.Port), nil)
59-
if err != nil {
74+
if err := http.ListenAndServe(fmt.Sprintf(":%v", flag.Port), nil); err != nil {
6075
log.Panicf("Error starting http server: %v", err)
6176
}
6277

components/ingress/pkg/flag/flags.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ var (
2121
// Port controls the HTTP listener port.
2222
Port int
2323

24-
// IngressLabelKey filters the target sandbox instances.
25-
IngressLabelKey string
26-
2724
// Namespace filters the target sandbox instances.
2825
Namespace string
26+
27+
// ProviderType specifies the sandbox provider type (e.g., batchsandbox).
28+
ProviderType string
2929
)

components/ingress/pkg/flag/parser.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ import (
2222
func InitFlags() {
2323
flag.StringVar(&LogLevel, "log-level", "info", "Server log level")
2424
flag.IntVar(&Port, "port", 28888, "Server listening port (default: 28888)")
25-
flag.StringVar(&IngressLabelKey, "ingress-label-key", "", "The Kubernetes label key used to identify sandbox instances for routing")
26-
flag.StringVar(&Namespace, "namespace", "", "The Kubernetes namespace to watch for sandbox pods")
25+
flag.StringVar(&Namespace, "namespace", "opensandbox", "The Kubernetes namespace to watch for sandbox resources")
26+
flag.StringVar(&ProviderType, "provider-type", "batchsandbox", "The sandbox provider type (default: batchsandbox)")
2727

2828
// Parse flags - these will override environment variables if provided
2929
flag.Parse()

0 commit comments

Comments
 (0)