移除深圳节点及中继拓扑,新增第二台宁波 K3s agent 的全互联 WireGuard 接入和严格 UFW 门禁。\n\nWorker Deployment 与容量控制器仅选择 easyai.io/worker=true 节点,使原宁波混部节点退出 Worker 资源预算,生产基线恢复为宁波专用节点与香港节点各一实例。\n\n已通过 Go 全量测试、go vet、gofmt、迁移安全检查、bash -n、ShellCheck、发布脚本测试和 Kubernetes 清单渲染。
99 lines
3.8 KiB
Go
99 lines
3.8 KiB
Go
package capacitycontroller
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestKubernetesClientReadsSiteAndMutatesOnlyWorkerScale(t *testing.T) {
|
|
var patches []string
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, request *http.Request) {
|
|
if request.Header.Get("Authorization") != "Bearer test-token" {
|
|
http.Error(w, "unauthorized", http.StatusUnauthorized)
|
|
return
|
|
}
|
|
switch {
|
|
case request.Method == http.MethodGet && strings.Contains(request.URL.Path, "/deployments/easyai-worker-hongkong"):
|
|
_, _ = w.Write([]byte(`{
|
|
"spec":{"replicas":1,"template":{"spec":{"containers":[{
|
|
"name":"worker","image":"registry.invalid/gateway@sha256:abc",
|
|
"env":[{"name":"AI_GATEWAY_REVISION","value":"release-sha"}],
|
|
"resources":{"requests":{"memory":"512Mi","cpu":"250m"}}
|
|
}]}}}
|
|
}`))
|
|
case request.Method == http.MethodGet && request.URL.Path == "/api/v1/nodes":
|
|
if request.URL.Query().Get("labelSelector") != "easyai.io/site=hongkong,easyai.io/worker=true" {
|
|
http.Error(w, "unexpected Worker node selector", http.StatusBadRequest)
|
|
return
|
|
}
|
|
_, _ = w.Write([]byte(`{"items":[{"metadata":{"name":"easyai-hongkong"},"status":{
|
|
"allocatable":{"memory":"8Gi","cpu":"4"},
|
|
"conditions":[{"type":"MemoryPressure","status":"False"}]
|
|
}}]}`))
|
|
case request.Method == http.MethodGet && strings.Contains(request.URL.Path, "/api/v1/namespaces/easyai/pods"):
|
|
_, _ = w.Write([]byte(`{"items":[{"metadata":{"name":"worker-hongkong-1"},"spec":{"nodeName":"easyai-hongkong"}}]}`))
|
|
case request.Method == http.MethodGet && strings.Contains(request.URL.Path, "/metrics.k8s.io/") &&
|
|
strings.HasSuffix(request.URL.Path, "/nodes/easyai-hongkong"):
|
|
_, _ = w.Write([]byte(`{"usage":{"memory":"3Gi","cpu":"500m"}}`))
|
|
case request.Method == http.MethodGet && strings.Contains(request.URL.Path, "/metrics.k8s.io/") &&
|
|
strings.HasSuffix(request.URL.Path, "/pods"):
|
|
_, _ = w.Write([]byte(`{"items":[{
|
|
"metadata":{"name":"worker-hongkong-1"},
|
|
"containers":[{"usage":{"memory":"384Mi","cpu":"125m"}}]
|
|
}]}`))
|
|
case request.Method == http.MethodPatch:
|
|
patches = append(patches, request.URL.Path)
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte(`{}`))
|
|
default:
|
|
http.NotFound(w, request)
|
|
}
|
|
}))
|
|
defer server.Close()
|
|
tokenFile := filepath.Join(t.TempDir(), "token")
|
|
if err := os.WriteFile(tokenFile, []byte("test-token"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
client, err := NewKubernetesClient(KubernetesConfig{
|
|
Namespace: "easyai", APIServer: server.URL, TokenFile: tokenFile, HTTPClient: server.Client(),
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
state, err := client.SiteState(context.Background(), "hongkong")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if state.CurrentReplicas != 1 || state.NodeName != "easyai-hongkong" ||
|
|
state.WorkerRequestMemoryBytes != 512<<20 || state.WorkerRequestMilliCPU != 250 ||
|
|
state.AllocatableMemoryBytes != 8<<30 || state.UsedMemoryBytes != 3<<30 ||
|
|
state.Nodes[0].WorkerUsedMemoryBytes != 384<<20 || state.Nodes[0].WorkerUsedMilliCPU != 125 {
|
|
t.Fatalf("site state=%+v", state)
|
|
}
|
|
if err := client.SetPodDeletionCost(context.Background(), "worker-pod", -1000); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := client.ScaleWorkerDeployment(context.Background(), "hongkong", 2); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(patches) != 2 ||
|
|
!strings.Contains(patches[0], "/pods/worker-pod") ||
|
|
!strings.Contains(patches[1], "/deployments/easyai-worker-hongkong/scale") {
|
|
t.Fatalf("patches=%v", patches)
|
|
}
|
|
}
|
|
|
|
func TestQuantityParsing(t *testing.T) {
|
|
if value, err := parseBinaryQuantity("1536Mi"); err != nil || value != 1536<<20 {
|
|
t.Fatalf("memory=%d err=%v", value, err)
|
|
}
|
|
if value, err := parseCPUQuantity("250000000n"); err != nil || value != 250 {
|
|
t.Fatalf("cpu=%d err=%v", value, err)
|
|
}
|
|
}
|