Files
ai/object/application.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

160 lines
5.3 KiB
Go

// Copyright 2023-2025 Hanzo AI Inc. All Rights Reserved.
// Portions Copyright 2025 The OpenAgent Authors. 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.
package object
import (
"fmt"
"strings"
"github.com/hanzoai/ai/util"
"github.com/hanzoai/dbx"
)
// Application.Status values. The four middle ones are the pod phases a
// deployed application reports back.
const (
StatusNotDeployed = "Not Deployed"
StatusPending = "Pending"
StatusRunning = "Running"
StatusUnknown = "Unknown"
StatusFailed = "Failed"
StatusTerminating = "Terminating"
NamespaceFormat = "hanzo-cloud-%s"
)
type Application struct {
Owner string `db:"pk" json:"owner"`
Name string `db:"pk" json:"name"`
CreatedTime string `json:"createdTime"`
UpdatedTime string `json:"updatedTime"`
DisplayName string `json:"displayName"`
Description string `json:"description"`
Template string `json:"template"` // Reference to Template.Name
Parameters string `json:"parameters"`
Manifest string `json:"manifest"` // Deployment manifest
Status string `json:"status"` // Running, Pending, Failed, Not Deployed
Namespace string `json:"namespace"` // Kubernetes namespace (auto-generated)
URL string `json:"url"` // Available service URL
Details *ApplicationView `db:"-" json:"details,omitempty"`
BasicConfigOptions []applicationConfigOption `json:"basicConfigOptions"`
}
type applicationConfigOption struct {
Parameter string `json:"parameter"`
Setting string `json:"setting"`
}
func GetApplications(owner string) ([]*Application, error) {
applications := []*Application{}
err := findAll(adapter.db, "application", &applications, dbx.HashExp{"owner": owner}, "created_time DESC")
if err != nil {
return applications, err
}
return applications, nil
}
func GetApplicationCount(owner, field, value string) (int64, error) {
session := GetDbQuery(owner, -1, -1, field, value, "", "")
return queryCount(session, "application")
}
func GetPaginationApplications(owner string, offset, limit int, field, value, sortField, sortOrder string) ([]*Application, error) {
applications := []*Application{}
session := GetDbQuery(owner, offset, limit, field, value, sortField, sortOrder)
err := queryFind(session, "application", &applications)
if err != nil {
return applications, err
}
return applications, nil
}
func getApplication(owner, name string) (*Application, error) {
application := Application{Owner: owner, Name: name}
existed, err := getOne(adapter.db, "application", &application, pk2(application.Owner, application.Name))
if err != nil {
return &application, err
}
if existed {
return &application, nil
} else {
return nil, nil
}
}
func GetApplication(id string) (*Application, error) {
owner, name, err := util.GetOwnerAndNameFromIdWithError(id)
if err != nil {
return nil, err
}
return getApplication(owner, name)
}
// UpdateApplication writes the record. The Manifest field is rendered from the
// template by cluster.Manifest, which the caller applies before saving.
func UpdateApplication(id string, application *Application) (bool, error) {
owner, name, err := util.GetOwnerAndNameFromIdWithError(id)
if err != nil {
return false, err
}
if application == nil {
return false, nil
}
application.UpdatedTime = util.GetCurrentTime()
_, err = getApplication(owner, name)
if err != nil {
return false, err
}
application.Owner = owner
application.Name = name
err = adapter.db.Model(application).Update()
if err != nil {
return false, err
}
return true, nil
}
func AddApplication(application *Application) (bool, error) {
if application.CreatedTime == "" {
application.CreatedTime = util.GetCurrentTime()
}
if application.UpdatedTime == "" {
application.UpdatedTime = util.GetCurrentTime()
}
// Generate namespace name based on application owner and name
application.Namespace = fmt.Sprintf(NamespaceFormat, strings.ReplaceAll(application.Name, "_", "-"))
// Set initial status
if application.Status == "" {
application.Status = StatusNotDeployed
}
err := insertRow(adapter.db, application)
affected := int64(1)
if err != nil {
affected = 0
}
if err != nil {
return false, err
}
return affected != 0, nil
}
// DeleteApplication removes the record. Tearing down what the record deployed is
// cluster.Undeploy, which the caller runs first.
func DeleteApplication(application *Application) (bool, error) {
affected, err := deleteByPK(adapter.db, "application", pk2(application.Owner, application.Name))
if err != nil {
return false, err
}
return affected != 0, nil
}