Refactor NodeConfig handling to use a map

This commit is contained in:
2025-10-08 19:24:24 +02:00
parent 3418735204
commit 7f6725da96
8 changed files with 25 additions and 52 deletions

View File

@@ -41,7 +41,7 @@ type AppConfig struct {
Backends BackendConfig `yaml:"backends"` Backends BackendConfig `yaml:"backends"`
Instances InstancesConfig `yaml:"instances"` Instances InstancesConfig `yaml:"instances"`
Auth AuthConfig `yaml:"auth"` Auth AuthConfig `yaml:"auth"`
Nodes []NodeConfig `yaml:"nodes,omitempty"` Nodes map[string]NodeConfig `yaml:"nodes,omitempty"`
Version string `yaml:"-"` Version string `yaml:"-"`
CommitHash string `yaml:"-"` CommitHash string `yaml:"-"`
BuildTime string `yaml:"-"` BuildTime string `yaml:"-"`
@@ -130,7 +130,6 @@ type AuthConfig struct {
} }
type NodeConfig struct { type NodeConfig struct {
Name string `yaml:"name"`
Address string `yaml:"address"` Address string `yaml:"address"`
APIKey string `yaml:"api_key,omitempty"` APIKey string `yaml:"api_key,omitempty"`
} }

View File

@@ -63,15 +63,16 @@ type instanceManager struct {
} }
// NewInstanceManager creates a new instance of InstanceManager. // NewInstanceManager creates a new instance of InstanceManager.
func NewInstanceManager(backendsConfig config.BackendConfig, instancesConfig config.InstancesConfig, nodesConfig []config.NodeConfig) InstanceManager { func NewInstanceManager(backendsConfig config.BackendConfig, instancesConfig config.InstancesConfig, nodesConfig map[string]config.NodeConfig) InstanceManager {
if instancesConfig.TimeoutCheckInterval <= 0 { if instancesConfig.TimeoutCheckInterval <= 0 {
instancesConfig.TimeoutCheckInterval = 5 // Default to 5 minutes if not set instancesConfig.TimeoutCheckInterval = 5 // Default to 5 minutes if not set
} }
// Build node config map for quick lookup // Build node config map for quick lookup
nodeConfigMap := make(map[string]*config.NodeConfig) nodeConfigMap := make(map[string]*config.NodeConfig)
for i := range nodesConfig { for name := range nodesConfig {
nodeConfigMap[nodesConfig[i].Name] = &nodesConfig[i] nodeCopy := nodesConfig[name]
nodeConfigMap[name] = &nodeCopy
} }
im := &instanceManager{ im := &instanceManager{

View File

@@ -34,7 +34,7 @@ func TestNewInstanceManager(t *testing.T) {
TimeoutCheckInterval: 5, TimeoutCheckInterval: 5,
} }
mgr := manager.NewInstanceManager(backendConfig, cfg, []config.NodeConfig{}) mgr := manager.NewInstanceManager(backendConfig, cfg, map[string]config.NodeConfig{})
if mgr == nil { if mgr == nil {
t.Fatal("NewInstanceManager returned nil") t.Fatal("NewInstanceManager returned nil")
} }
@@ -69,7 +69,7 @@ func TestPersistence(t *testing.T) {
} }
// Test instance persistence on creation // Test instance persistence on creation
manager1 := manager.NewInstanceManager(backendConfig, cfg, []config.NodeConfig{}) manager1 := manager.NewInstanceManager(backendConfig, cfg, map[string]config.NodeConfig{})
options := &instance.CreateInstanceOptions{ options := &instance.CreateInstanceOptions{
BackendType: backends.BackendTypeLlamaCpp, BackendType: backends.BackendTypeLlamaCpp,
LlamaServerOptions: &llamacpp.LlamaServerOptions{ LlamaServerOptions: &llamacpp.LlamaServerOptions{
@@ -90,7 +90,7 @@ func TestPersistence(t *testing.T) {
} }
// Test loading instances from disk // Test loading instances from disk
manager2 := manager.NewInstanceManager(backendConfig, cfg, []config.NodeConfig{}) manager2 := manager.NewInstanceManager(backendConfig, cfg, map[string]config.NodeConfig{})
instances, err := manager2.ListInstances() instances, err := manager2.ListInstances()
if err != nil { if err != nil {
t.Fatalf("ListInstances failed: %v", err) t.Fatalf("ListInstances failed: %v", err)
@@ -207,7 +207,7 @@ func createTestManager() manager.InstanceManager {
DefaultRestartDelay: 5, DefaultRestartDelay: 5,
TimeoutCheckInterval: 5, TimeoutCheckInterval: 5,
} }
return manager.NewInstanceManager(backendConfig, cfg, []config.NodeConfig{}) return manager.NewInstanceManager(backendConfig, cfg, map[string]config.NodeConfig{})
} }
func TestAutoRestartDisabledInstanceStatus(t *testing.T) { func TestAutoRestartDisabledInstanceStatus(t *testing.T) {
@@ -227,7 +227,7 @@ func TestAutoRestartDisabledInstanceStatus(t *testing.T) {
} }
// Create first manager and instance with auto-restart disabled // Create first manager and instance with auto-restart disabled
manager1 := manager.NewInstanceManager(backendConfig, cfg, []config.NodeConfig{}) manager1 := manager.NewInstanceManager(backendConfig, cfg, map[string]config.NodeConfig{})
autoRestart := false autoRestart := false
options := &instance.CreateInstanceOptions{ options := &instance.CreateInstanceOptions{
@@ -252,7 +252,7 @@ func TestAutoRestartDisabledInstanceStatus(t *testing.T) {
manager1.Shutdown() manager1.Shutdown()
// Create second manager (simulating restart of llamactl) // Create second manager (simulating restart of llamactl)
manager2 := manager.NewInstanceManager(backendConfig, cfg, []config.NodeConfig{}) manager2 := manager.NewInstanceManager(backendConfig, cfg, map[string]config.NodeConfig{})
// Get the loaded instance // Get the loaded instance
loadedInst, err := manager2.GetInstance("test-instance") loadedInst, err := manager2.GetInstance("test-instance")

View File

@@ -75,7 +75,7 @@ func TestCreateInstance_ValidationAndLimits(t *testing.T) {
MaxInstances: 1, // Very low limit for testing MaxInstances: 1, // Very low limit for testing
TimeoutCheckInterval: 5, TimeoutCheckInterval: 5,
} }
limitedManager := manager.NewInstanceManager(backendConfig, cfg, nil) limitedManager := manager.NewInstanceManager(backendConfig, cfg, map[string]config.NodeConfig{})
_, err = limitedManager.CreateInstance("instance1", options) _, err = limitedManager.CreateInstance("instance1", options)
if err != nil { if err != nil {

View File

@@ -23,7 +23,7 @@ func TestTimeoutFunctionality(t *testing.T) {
MaxInstances: 5, MaxInstances: 5,
} }
manager := manager.NewInstanceManager(backendConfig, cfg, nil) manager := manager.NewInstanceManager(backendConfig, cfg, map[string]config.NodeConfig{})
if manager == nil { if manager == nil {
t.Fatal("Manager should be initialized with timeout checker") t.Fatal("Manager should be initialized with timeout checker")
} }

View File

@@ -3,7 +3,6 @@ package server
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"llamactl/pkg/config"
"llamactl/pkg/instance" "llamactl/pkg/instance"
"llamactl/pkg/manager" "llamactl/pkg/manager"
"net/http" "net/http"
@@ -414,15 +413,8 @@ func (h *Handler) RemoteInstanceProxy(w http.ResponseWriter, r *http.Request, na
if !exists { if !exists {
// Find node configuration // Find node configuration
var nodeConfig *config.NodeConfig nodeConfig, exists := h.cfg.Nodes[nodeName]
for i := range h.cfg.Nodes { if !exists {
if h.cfg.Nodes[i].Name == nodeName {
nodeConfig = &h.cfg.Nodes[i]
break
}
}
if nodeConfig == nil {
http.Error(w, fmt.Sprintf("Node %s not found", nodeName), http.StatusInternalServerError) http.Error(w, fmt.Sprintf("Node %s not found", nodeName), http.StatusInternalServerError)
return return
} }

View File

@@ -2,7 +2,6 @@ package server
import ( import (
"encoding/json" "encoding/json"
"llamactl/pkg/config"
"net/http" "net/http"
"github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5"
@@ -10,26 +9,24 @@ import (
// NodeResponse represents a sanitized node configuration for API responses // NodeResponse represents a sanitized node configuration for API responses
type NodeResponse struct { type NodeResponse struct {
Name string `json:"name"`
Address string `json:"address"` Address string `json:"address"`
} }
// ListNodes godoc // ListNodes godoc
// @Summary List all configured nodes // @Summary List all configured nodes
// @Description Returns a list of all nodes configured in the server // @Description Returns a map of all nodes configured in the server (node name -> node config)
// @Tags nodes // @Tags nodes
// @Security ApiKeyAuth // @Security ApiKeyAuth
// @Produces json // @Produces json
// @Success 200 {array} NodeResponse "List of nodes" // @Success 200 {object} map[string]NodeResponse "Map of nodes"
// @Failure 500 {string} string "Internal Server Error" // @Failure 500 {string} string "Internal Server Error"
// @Router /nodes [get] // @Router /nodes [get]
func (h *Handler) ListNodes() http.HandlerFunc { func (h *Handler) ListNodes() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
// Convert to sanitized response format // Convert to sanitized response format (map of name -> NodeResponse)
nodeResponses := make([]NodeResponse, len(h.cfg.Nodes)) nodeResponses := make(map[string]NodeResponse, len(h.cfg.Nodes))
for i, node := range h.cfg.Nodes { for name, node := range h.cfg.Nodes {
nodeResponses[i] = NodeResponse{ nodeResponses[name] = NodeResponse{
Name: node.Name,
Address: node.Address, Address: node.Address,
} }
} }
@@ -62,22 +59,14 @@ func (h *Handler) GetNode() http.HandlerFunc {
return return
} }
var nodeConfig *config.NodeConfig nodeConfig, exists := h.cfg.Nodes[name]
for i := range h.cfg.Nodes { if !exists {
if h.cfg.Nodes[i].Name == name {
nodeConfig = &h.cfg.Nodes[i]
break
}
}
if nodeConfig == nil {
http.Error(w, "Node not found", http.StatusNotFound) http.Error(w, "Node not found", http.StatusNotFound)
return return
} }
// Convert to sanitized response format // Convert to sanitized response format
nodeResponse := NodeResponse{ nodeResponse := NodeResponse{
Name: nodeConfig.Name,
Address: nodeConfig.Address, Address: nodeConfig.Address,
} }

View File

@@ -5,7 +5,6 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"llamactl/pkg/config"
"llamactl/pkg/instance" "llamactl/pkg/instance"
"net/http" "net/http"
"net/http/httputil" "net/http/httputil"
@@ -170,15 +169,8 @@ func (h *Handler) RemoteOpenAIProxy(w http.ResponseWriter, r *http.Request, mode
if !exists { if !exists {
// Find node configuration // Find node configuration
var nodeConfig *config.NodeConfig nodeConfig, exists := h.cfg.Nodes[nodeName]
for i := range h.cfg.Nodes { if !exists {
if h.cfg.Nodes[i].Name == nodeName {
nodeConfig = &h.cfg.Nodes[i]
break
}
}
if nodeConfig == nil {
http.Error(w, fmt.Sprintf("Node %s not found", nodeName), http.StatusInternalServerError) http.Error(w, fmt.Sprintf("Node %s not found", nodeName), http.StatusInternalServerError)
return return
} }