mirror of
https://github.com/dsx-ai-factory/nvke.git
synced 2026-12-07 04:35:58 +08:00
* feat(controller): expose observed node pool counts (poc) Signed-off-by: Anisha Gupta <anisgupta@nvidia.com> * refactor(controller): keep node pool counts with pool queries Signed-off-by: Anisha Gupta <anisgupta@nvidia.com> * refactor(controller): name compared node set current Signed-off-by: Anisha Gupta <anisgupta@nvidia.com> * refactor(controller): use a generic json equality helper name Signed-off-by: Anisha Gupta <anisgupta@nvidia.com> * refactor(controller): use inner join for node set counts Signed-off-by: Anisha Gupta <anisgupta@nvidia.com> * refactor(controller): use node set names in count aggregation Signed-off-by: Anisha Gupta <anisgupta@nvidia.com> * refactor(controller): consolidate node pool response mapping Signed-off-by: Anisha Gupta <anisgupta@nvidia.com> * docs: keep node pool field definitions in the api reference Signed-off-by: Anisha Gupta <anisgupta@nvidia.com> * refactor(controller): restore single node pool response conversion Signed-off-by: Anisha Gupta <anisgupta@nvidia.com> * test(controller): colocate node pool count tests Signed-off-by: Anisha Gupta <anisgupta@nvidia.com> * test(controller): colocate node pool response count tests Signed-off-by: Anisha Gupta <anisgupta@nvidia.com> * refactor(api): name observed node pool count explicitly Signed-off-by: Anisha Gupta <anisgupta@nvidia.com> * fix(controller): compare template JSON numbers by exact value Signed-off-by: Anisha Gupta <anisgupta@nvidia.com> * refactor(controller): limit JSON comparison to node pool counts Signed-off-by: Anisha Gupta <anisgupta@nvidia.com> * refactor(controller): restore shared pool configuration comparison Signed-off-by: Anisha Gupta <anisgupta@nvidia.com> * refactor(controller): move json equality to shared utils Signed-off-by: Anisha Gupta <anisgupta@nvidia.com> * test(controller): use unambiguous numeric precision fixtures Signed-off-by: Anisha Gupta <anisgupta@nvidia.com> * refactor(controller): simplify node pool count responses Signed-off-by: Anisha Gupta <anisgupta@nvidia.com> * perf(controller): index nodes by node pool Signed-off-by: Anisha Gupta <anisgupta@nvidia.com> --------- Signed-off-by: Anisha Gupta <anisgupta@nvidia.com>
200 lines
6.4 KiB
Go
200 lines
6.4 KiB
Go
// SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package db
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type DiskSelectionStrategy string
|
|
|
|
const (
|
|
DiskSelectionStrategySmallest DiskSelectionStrategy = "smallest"
|
|
DiskSelectionStrategyLargest DiskSelectionStrategy = "largest"
|
|
)
|
|
|
|
type NodeSet struct {
|
|
ID uuid.UUID `bun:"type:uuid,pk"`
|
|
NodePoolID uuid.UUID `bun:"type:uuid,notnull"`
|
|
|
|
// DesiredCount is the desired count of nodes in the NodeSet.
|
|
// The desired count is only for this NodeSet, the parent NodePool
|
|
// contains the desired count for the entire NodePool.
|
|
DesiredCount int `bun:"type:integer,notnull,default:0"`
|
|
|
|
KubernetesVersion string `bun:"type:text,notnull,default:''"`
|
|
SystemReservedMemory string `bun:"type:text,notnull,default:'16Gi'"`
|
|
KubeReservedMemory string `bun:"type:text,notnull,default:''"`
|
|
|
|
CreatedAt time.Time `bun:"type:timestamptz,notnull,default:current_timestamp"`
|
|
UpdatedAt time.Time `bun:"type:timestamptz,notnull,default:current_timestamp"`
|
|
TeleportEnabled bool `bun:"type:boolean,notnull,default:false"`
|
|
|
|
Nico *NicoNodePoolProvider `bun:"nico,type:jsonb"`
|
|
Capi *CAPINodePoolProvider `bun:"capi,type:jsonb"`
|
|
LaunchLayer *LaunchLayerNodePoolProvider `bun:"launch_layer,type:jsonb"`
|
|
}
|
|
|
|
// NodePoolNeedsNewNodeSet reports whether reconciling nodePool requires a replacement node set.
|
|
func NodePoolNeedsNewNodeSet(nodePool NodePool, current NodeSet) bool {
|
|
return nodePool.CurrentNodeSetID == nil || !NodePoolMatchesNodeSet(nodePool, current)
|
|
}
|
|
|
|
// NodePoolMatchesNodeSet reports whether the supplied NodeSet matches the pool's requested
|
|
// rollout configuration. It does not depend on the active generation's ID, so a
|
|
// previous generation can match again after a rollback.
|
|
func NodePoolMatchesNodeSet(nodePool NodePool, current NodeSet) bool {
|
|
if nodePool.KubernetesVersion != current.KubernetesVersion ||
|
|
nodePool.SystemReservedMemory != current.SystemReservedMemory ||
|
|
nodePool.KubeReservedMemory != current.KubeReservedMemory ||
|
|
nodePool.TeleportEnabled != current.TeleportEnabled {
|
|
return false
|
|
}
|
|
|
|
switch {
|
|
case nodePool.Nico != nil:
|
|
return current.Nico != nil && nodePool.Nico.Equal(*current.Nico)
|
|
case nodePool.Capi != nil:
|
|
return current.Capi != nil && nodePool.Capi.Equal(*current.Capi)
|
|
case nodePool.LaunchLayer != nil:
|
|
return current.LaunchLayer != nil && nodePool.LaunchLayer.Equal(*current.LaunchLayer)
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
|
|
func (n *NodeSet) NicoProvider() (NicoNodePoolProvider, error) {
|
|
if n.Nico == nil {
|
|
return NicoNodePoolProvider{}, fmt.Errorf("%w: node set %s nico", ErrMissingProviderPayload, n.ID)
|
|
}
|
|
return *cloneNicoNodePoolProvider(*n.Nico), nil
|
|
}
|
|
|
|
func (n *NodeSet) CAPIProvider() (CAPINodePoolProvider, error) {
|
|
if n.Capi == nil {
|
|
return CAPINodePoolProvider{}, fmt.Errorf("%w: node set %s capi", ErrMissingProviderPayload, n.ID)
|
|
}
|
|
return *cloneCAPINodePoolProvider(*n.Capi), nil
|
|
}
|
|
|
|
func (n *NodeSet) LaunchLayerProvider() (LaunchLayerNodePoolProvider, error) {
|
|
if n.LaunchLayer == nil {
|
|
return LaunchLayerNodePoolProvider{}, fmt.Errorf("%w: node set %s launch_layer", ErrMissingProviderPayload, n.ID)
|
|
}
|
|
return *cloneLaunchLayerNodePoolProvider(*n.LaunchLayer), nil
|
|
}
|
|
|
|
// ResolveProviderType infers the node set's provider type from which
|
|
// provider payload is set. NodeSet has no persisted ProviderType field the
|
|
// way Cluster does, so this is the node-set equivalent of
|
|
// Cluster.ResolveProviderType.
|
|
func (n *NodeSet) ResolveProviderType() (ProviderType, error) {
|
|
switch {
|
|
case n.Nico != nil:
|
|
return ProviderTypeNico, nil
|
|
case n.Capi != nil:
|
|
return ProviderTypeCAPI, nil
|
|
case n.LaunchLayer != nil:
|
|
return ProviderTypeLaunchLayer, nil
|
|
default:
|
|
return "", fmt.Errorf("%w: node set %s has no provider payload set", ErrMissingProviderPayload, n.ID)
|
|
}
|
|
}
|
|
|
|
func (n *NodeSet) SetNico(provider NicoNodePoolProvider) {
|
|
n.Nico = cloneNicoNodePoolProvider(provider)
|
|
n.Capi = nil
|
|
n.LaunchLayer = nil
|
|
}
|
|
|
|
func (n *NodeSet) SetCapi(provider CAPINodePoolProvider) {
|
|
n.Capi = cloneCAPINodePoolProvider(provider)
|
|
n.Nico = nil
|
|
n.LaunchLayer = nil
|
|
}
|
|
|
|
func (n *NodeSet) SetLaunchLayer(provider LaunchLayerNodePoolProvider) {
|
|
n.LaunchLayer = cloneLaunchLayerNodePoolProvider(provider)
|
|
n.Nico = nil
|
|
n.Capi = nil
|
|
}
|
|
|
|
type NodeSetFilters struct {
|
|
NodeSetID *uuid.UUID
|
|
NodePoolID *uuid.UUID
|
|
SiteID *uuid.UUID
|
|
}
|
|
|
|
type NodeSetQueries interface {
|
|
ListNodeSets(ctx context.Context, filters NodeSetFilters) ([]NodeSet, error)
|
|
GetNodeSet(ctx context.Context, id uuid.UUID) (NodeSet, error)
|
|
CreateNodeSet(ctx context.Context, nodeSet *NodeSet) (NodeSet, error)
|
|
UpdateNodeSet(ctx context.Context, nodeSet *NodeSet) (NodeSet, error)
|
|
DeleteNodeSet(ctx context.Context, id uuid.UUID) error
|
|
}
|
|
|
|
func (q *queriesImpl) ListNodeSets(ctx context.Context, filters NodeSetFilters) ([]NodeSet, error) {
|
|
var nodeSets []NodeSet
|
|
query := q.db.NewSelect().Model(&nodeSets).
|
|
Join("JOIN node_pools np ON np.id = node_set.node_pool_id").
|
|
Join("JOIN clusters c ON c.id = np.cluster_id")
|
|
|
|
if filters.NodePoolID != nil {
|
|
query = query.Where("node_set.node_pool_id = ?", filters.NodePoolID)
|
|
}
|
|
if filters.NodeSetID != nil {
|
|
query = query.Where("node_set.id = ?", filters.NodeSetID)
|
|
}
|
|
|
|
if filters.SiteID != nil {
|
|
query = query.Where("c.nico->>'site_id' = ?", filters.SiteID.String())
|
|
}
|
|
|
|
err := query.Scan(ctx)
|
|
return nodeSets, err
|
|
}
|
|
|
|
func (q *queriesImpl) GetNodeSet(ctx context.Context, id uuid.UUID) (NodeSet, error) {
|
|
var config NodeSet
|
|
err := q.db.NewSelect().
|
|
Model(&config).
|
|
Where("id = ?", id).
|
|
Scan(ctx)
|
|
if err != nil {
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return NodeSet{}, ErrNotFound
|
|
}
|
|
return NodeSet{}, err
|
|
}
|
|
return config, nil
|
|
}
|
|
|
|
func (q *queriesImpl) CreateNodeSet(ctx context.Context, nodeSet *NodeSet) (NodeSet, error) {
|
|
_, err := q.db.NewInsert().Model(nodeSet).Exec(ctx)
|
|
if err != nil {
|
|
return NodeSet{}, err
|
|
}
|
|
return *nodeSet, nil
|
|
}
|
|
|
|
func (q *queriesImpl) UpdateNodeSet(ctx context.Context, nodeSet *NodeSet) (NodeSet, error) {
|
|
nodeSet.UpdatedAt = time.Now()
|
|
res, err := q.db.NewUpdate().Model(nodeSet).WherePK().Exec(ctx)
|
|
if err != nil {
|
|
return NodeSet{}, err
|
|
}
|
|
return *nodeSet, notFoundErrIfNoRowsAffected(res)
|
|
}
|
|
|
|
func (q *queriesImpl) DeleteNodeSet(ctx context.Context, id uuid.UUID) error {
|
|
_, err := q.db.NewDelete().Model(&NodeSet{}).Where("id = ?", id).Exec(ctx)
|
|
return err
|
|
}
|