Files
ai/controllers/zap_interservice.go
zeekayandHanzo Dev f9b966079d Move the Kubernetes plane out of the object model
github.com/hanzoai/ai/object is the record layer. Seven of its files were not
records: they built client-go clients, ran an informer cache, polled the metrics
API, rendered manifests through kustomize and applied them. Importing an
Application row therefore linked a Kubernetes control plane.

They move to github.com/hanzoai/ai/cluster, which owns everything the product
does against a cluster: render, apply, observe. object keeps the values —
ApplicationView and its nested types are plain data with no k8s types in them, so
Application.Details keeps its exact static type and the /v1/get-application wire
shape is unchanged. The Status constants become their own string literals rather
than aliases of core/v1 pod phases.

Two braids come apart with the move:

UpdateApplication recomputed the manifest from the template on every write, so a
status or URL update re-ran kustomize. It now writes the record; cluster.Manifest
renders, and the four callers that want a fresh manifest render first.

DeleteApplication raced a fire-and-forget undeploy goroutine against its own row
delete. The controllers now tear down first, best-effort, then drop the row — an
org with no Kubernetes provider still deletes its applications.

RegisterServedModel, which writes the Provider and ModelRoute for a served
fine-tune, is a record write and stays in object; it was the one place reaching
adapter.db from the moved files.

No route, opcode, swagger path or admin page changes. Names lose the stutter the
package qualifier now carries: GetK8sStatus is cluster.Status, AddDetails is
cluster.Describe, DeployApplicationSync is cluster.DeploySync,
GetFinetuneTrainJobStatus is cluster.TrainJobStatus.

object drops 538 packages, 1799 to 1261: all 412 k8s.io, all 84 sigs.k8s.io, and
with kustomize gone the `plugin` package, which forces --export-dynamic on every
binary that links it.

Co-authored-by: Hanzo Dev <[email protected]>
2026-07-26 18:18:47 -07:00

176 lines
5.3 KiB
Go

// Copyright 2023-2026 Hanzo AI Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ZAP inter-service transport for cloud operations.
//
// Listens on CLOUD_ZAP_PORT (default 9320) and serves the deploy, undeploy
// and status opcodes over ZAP rather than HTTP.
//
// Message type 110 (cloud ops):
// Request: method(0:Text) + auth(8:Text) + body(16:Bytes)
// Response: status(0:Uint32) + body(4:Bytes) + error(12:Text)
package controllers
import (
"context"
"encoding/json"
"fmt"
"log/slog"
"os"
"github.com/hanzoai/ai/log"
"github.com/luxfi/zap"
"github.com/hanzoai/ai/cluster"
"github.com/hanzoai/ai/object"
)
// MsgTypeCloudOps is the ZAP message type for inter-service cloud operations.
const MsgTypeCloudOps uint16 = 110
var interserviceNode *zap.Node
// InitInterserviceZap starts a dedicated ZAP node for inter-service operations.
// Separate from the main inference ZAP node (port 9999).
func InitInterserviceZap() {
port := 9320
if p := os.Getenv("CLOUD_ZAP_PORT"); p != "" {
fmt.Sscanf(p, "%d", &port)
}
node := zap.NewNode(zap.NodeConfig{
NodeID: "cloud-ops",
Port: port,
NoDiscovery: true,
Logger: slog.Default(),
})
if err := node.Start(); err != nil {
log.Error("ZAP ops: failed to start on :%d: %v", port, err)
return
}
node.Handle(MsgTypeCloudOps, handleCloudOps)
interserviceNode = node
log.Info("ZAP ops: listening on :%d (msg_type=%d)", port, MsgTypeCloudOps)
}
// StopInterserviceZap gracefully shuts down the inter-service ZAP node.
func StopInterserviceZap() {
if interserviceNode != nil {
interserviceNode.Stop()
interserviceNode = nil
log.Info("ZAP ops: stopped")
}
}
func handleCloudOps(ctx context.Context, from string, msg *zap.Message) (*zap.Message, error) {
root := msg.Root()
method := root.Text(object.CloudReqMethod)
body := root.Bytes(object.CloudReqBody)
switch method {
case "deploy":
return opsDeployHandler(body)
case "undeploy":
return opsUndeployHandler(body)
case "status":
return opsStatusHandler()
default:
return buildOpsResponse(404, nil, "unknown op: "+method)
}
}
func buildOpsResponse(status uint32, body []byte, errMsg string) (*zap.Message, error) {
return object.BuildCloudResponse(status, body, errMsg)
}
// ── deploy ──────────────────────────────────────────────────────────────
func opsDeployHandler(body []byte) (*zap.Message, error) {
var params struct {
ID string `json:"id"`
}
if err := json.Unmarshal(body, &params); err != nil {
return buildOpsResponse(400, nil, "invalid body: "+err.Error())
}
if params.ID == "" {
return buildOpsResponse(400, nil, "id required")
}
app, err := object.GetApplication(params.ID)
if err != nil {
return buildOpsResponse(500, nil, err.Error())
}
if app == nil {
return buildOpsResponse(404, nil, "application not found: "+params.ID)
}
ok, err := cluster.DeploySync(app, "en")
if err != nil {
return buildOpsResponse(500, nil, "deploy failed: "+err.Error())
}
if !ok {
return buildOpsResponse(500, nil, "deploy returned false")
}
updated, err := object.GetApplication(params.ID)
if err != nil {
return buildOpsResponse(500, nil, err.Error())
}
data, _ := json.Marshal(updated)
return buildOpsResponse(200, data, "")
}
// ── undeploy ────────────────────────────────────────────────────────────
func opsUndeployHandler(body []byte) (*zap.Message, error) {
var params struct {
Owner string `json:"owner"`
Name string `json:"name"`
Namespace string `json:"namespace"`
}
if err := json.Unmarshal(body, &params); err != nil {
return buildOpsResponse(400, nil, "invalid body: "+err.Error())
}
if params.Owner == "" || params.Name == "" {
return buildOpsResponse(400, nil, "owner and name required")
}
ok, err := cluster.UndeploySync(params.Owner, params.Name, params.Namespace, "en")
if err != nil {
return buildOpsResponse(500, nil, "undeploy failed: "+err.Error())
}
if !ok {
return buildOpsResponse(500, nil, "undeploy returned false")
}
data, _ := json.Marshal(map[string]string{"status": "undeployed", "owner": params.Owner, "name": params.Name})
return buildOpsResponse(200, data, "")
}
// ── status ──────────────────────────────────────────────────────────────
func opsStatusHandler() (*zap.Message, error) {
status, err := cluster.Status("en")
if err != nil {
return buildOpsResponse(500, nil, err.Error())
}
data, _ := json.Marshal(map[string]interface{}{"k8s": status})
return buildOpsResponse(200, data, "")
}