Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ kubevirt:
server:
host: "0.0.0.0" # Host for the Redfish server (0.0.0.0 is default (typically unchanged unless testing)
port: 8443 # Port for the Redfish server (8443 is default (typically unchanged unless testing)
workers: 8 # Background task pool size for virtual media and other async tasks (8 is default)
tls:
enabled: false # Disabled for development/testing (not required with OpenShift because of default route TLS)
cert_file: "/etc/kubevirt-redfish/tls/tls.crt" # Path to the certificate file (not required with OpenShift, but may be leveraged for other platforms)
Expand Down
3 changes: 3 additions & 0 deletions helm/templates/secret.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ stringData:
server:
host: "{{ .Values.server.host }}"
port: {{ .Values.server.port }}
{{- if .Values.server.workers }}
workers: {{ .Values.server.workers }}
{{- end }}
tls:
enabled: {{ .Values.server.tls.enabled }}

Expand Down
1 change: 1 addition & 0 deletions helm/values-example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ kubevirt:
server:
host: "0.0.0.0"
port: 8443
workers: 8
tls:
enabled: false

Expand Down
6 changes: 6 additions & 0 deletions helm/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,12 @@ server:
# Server port
port: 8443

# Background task pool size (virtual media insert/eject and other async
# Redfish tasks). Each job is a short burst of Kubernetes API calls, so this
# bounds how many inserts start at once, not how many ISOs copy at once.
# Read at startup only; changing it needs a restart.
workers: 8

# System ID convention for Redfish compliance
# Options: "legacy" (vm-name only) or "enhanced" (namespace.vm-name)
# Default: "legacy" for backward compatibility with existing Metal3/Ironic deployments
Expand Down
17 changes: 17 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,20 @@ type Config struct {
SystemIDConvention string `mapstructure:"system_id_convention"` // "legacy" or "enhanced"
}

// DefaultWorkers is the size of the background task pool when server.workers
// is left unset.
const DefaultWorkers = 8

// ServerConfig holds HTTP server configuration including host, port, and TLS settings.
// The server configuration determines how the Redfish API server listens for requests.
type ServerConfig struct {
Host string `mapstructure:"host"`
Port int `mapstructure:"port"`
TLS TLSConfig `mapstructure:"tls"`
TestMode bool `mapstructure:"test_mode"` // Disables background processes for testing
// Workers is the size of the background task pool that runs virtual media
// inserts/ejects and other async Redfish tasks. Read once at startup.
Workers int `mapstructure:"workers"`
}

// TLSConfig holds TLS configuration for secure HTTPS connections.
Expand Down Expand Up @@ -293,6 +300,7 @@ func LoadConfig(configPath string) (*Config, error) {
func setDefaults() {
viper.SetDefault("server.host", "0.0.0.0")
viper.SetDefault("server.port", 8443)
viper.SetDefault("server.workers", DefaultWorkers)
viper.SetDefault("server.tls.enabled", true)
viper.SetDefault("kubevirt.api_version", "v1")
viper.SetDefault("kubevirt.timeout", 30)
Expand Down Expand Up @@ -368,6 +376,15 @@ func validateServerConfig(server *ServerConfig) error {
return errors.NewValidationError("Invalid server port", fmt.Sprintf("server.port must be between 1 and 65535, got %d", server.Port))
}

// Zero means "not set": apply the default so a Config built without viper
// (tests, programmatic use) validates the same way a loaded one does.
if server.Workers == 0 {
server.Workers = DefaultWorkers
}
if server.Workers < 1 || server.Workers > 64 {
return errors.NewValidationError("Invalid worker count", fmt.Sprintf("server.workers must be between 1 and 64, got %d", server.Workers))
}

if server.TLS.Enabled {
if server.TLS.CertFile == "" {
return errors.NewValidationError("TLS certificate file is required when TLS is enabled", "server.tls.cert_file cannot be empty when server.tls.enabled is true")
Expand Down
91 changes: 91 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package config
import (
"os"
"path/filepath"
"strings"
"testing"

"github.com/kubevirt/redfish-controller/pkg/errors"
Expand Down Expand Up @@ -104,6 +105,96 @@ kubevirt:
}
}

// minimalConfig is the smallest config that passes validation, with the
// kubevirt section supplied by the caller.
func minimalConfig(kubevirtSection string) string {
return `
server:
host: "127.0.0.1"
port: 8443
tls:
enabled: false

chassis:
- name: "test-cluster"
namespace: "test-namespace"
service_account: "test-sa"

authentication:
users:
- username: "testuser"
password: "testpass"
chassis: ["test-cluster"]

kubevirt:
api_version: "v1"
timeout: 30
` + kubevirtSection
}

func writeConfig(t *testing.T, content string) string {
t.Helper()
configPath := filepath.Join(t.TempDir(), "config.yaml")
if err := os.WriteFile(configPath, []byte(content), 0600); err != nil {
t.Fatalf("Failed to write test config: %v", err)
}
return configPath
}

func TestWorkersConfig(t *testing.T) {
t.Run("default", func(t *testing.T) {
config, err := LoadConfig(writeConfig(t, minimalConfig("")))
if err != nil {
t.Fatalf("Failed to load config: %v", err)
}
if config.Server.Workers != 8 {
t.Errorf("Expected default workers 8, got %d", config.Server.Workers)
}
})

t.Run("from config file", func(t *testing.T) {
content := strings.Replace(minimalConfig(""), " port: 8443\n", " port: 8443\n workers: 3\n", 1)
config, err := LoadConfig(writeConfig(t, content))
if err != nil {
t.Fatalf("Failed to load config: %v", err)
}
if config.Server.Workers != 3 {
t.Errorf("Expected workers 3 from config file, got %d", config.Server.Workers)
}
})

t.Run("from environment", func(t *testing.T) {
t.Setenv("KUBEVIRT_REDFISH_SERVER_WORKERS", "16")
config, err := LoadConfig(writeConfig(t, minimalConfig("")))
if err != nil {
t.Fatalf("Failed to load config: %v", err)
}
if config.Server.Workers != 16 {
t.Errorf("Expected workers 16 from env, got %d", config.Server.Workers)
}
})

t.Run("rejects out-of-range values", func(t *testing.T) {
for _, v := range []string{"-1", "65"} {
t.Setenv("KUBEVIRT_REDFISH_SERVER_WORKERS", v)
if _, err := LoadConfig(writeConfig(t, minimalConfig(""))); err == nil {
t.Errorf("Expected validation error for workers=%s, got nil", v)
}
}
})

t.Run("zero means default", func(t *testing.T) {
t.Setenv("KUBEVIRT_REDFISH_SERVER_WORKERS", "0")
config, err := LoadConfig(writeConfig(t, minimalConfig("")))
if err != nil {
t.Fatalf("Failed to load config: %v", err)
}
if config.Server.Workers != DefaultWorkers {
t.Errorf("Expected default workers %d for zero, got %d", DefaultWorkers, config.Server.Workers)
}
})
}

func TestLoadConfigWithDefaults(t *testing.T) {
// Create a temporary directory and config file
tempDir := t.TempDir()
Expand Down
7 changes: 6 additions & 1 deletion pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,12 @@ func NewServer(config *config.Config, kubevirtClient *kubevirt.Client) *Server {
// Initialize background components only if not in test mode
if !config.Server.TestMode {
// Initialize enhanced task manager
taskManager = NewTaskManager(4, kubevirtClient) // 4 workers for background processing
workers := config.Server.Workers
if workers < 1 {
workers = 4 // config validation normally guarantees >= 1; keep the historical default otherwise
}
taskManager = NewTaskManager(workers, kubevirtClient)
logger.Info("Task manager started with %d workers", workers)

// Initialize job scheduler
jobScheduler = NewJobScheduler()
Expand Down
14 changes: 13 additions & 1 deletion pkg/server/task_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"context"
"fmt"
"sync"
"sync/atomic"
"time"

"github.com/kubevirt/redfish-controller/pkg/kubevirt"
Expand Down Expand Up @@ -77,6 +78,7 @@ type TaskManager struct {
// Worker pool
workers []*Worker
workerCount int
nextWorker uint64 // round-robin cursor for worker selection
workerMutex sync.RWMutex

// KubeVirt client for actual operations
Expand Down Expand Up @@ -503,7 +505,17 @@ func (tm *TaskManager) getAvailableWorker() *Worker {
tm.workerMutex.RLock()
defer tm.workerMutex.RUnlock()

for _, worker := range tm.workers {
if len(tm.workers) == 0 {
return nil
}

// Advance the round-robin cursor so consecutive jobs land on different
// workers. Without this the dispatcher always returns the first worker
// with channel capacity, which serializes all jobs onto worker 1 while
// the remaining workers stay idle.
start := int(atomic.AddUint64(&tm.nextWorker, 1) % uint64(len(tm.workers)))
for i := range len(tm.workers) {
worker := tm.workers[(start+i)%len(tm.workers)]
// Check if worker's job channel has capacity
if len(worker.jobChan) < cap(worker.jobChan) {
return worker
Expand Down
46 changes: 46 additions & 0 deletions pkg/server/task_manager_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* This file is part of the KubeVirt Redfish project
*
* 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.
*
* Copyright 2025 KubeVirt Redfish project and its authors.
*
*/

package server

import "testing"

// With every worker idle, consecutive jobs must be spread across the pool.
// A dispatcher that always returns the first worker with queue capacity
// serializes every job onto worker 1 while the others stay idle.
func TestGetAvailableWorkerRoundRobin(t *testing.T) {
const workers = 4
tm := NewTaskManager(workers, nil)
defer tm.cancel()

picks := make(map[int]int)
for i := 0; i < 2*workers; i++ {
w := tm.getAvailableWorker()
if w == nil {
t.Fatalf("pick %d: no worker available although all queues are empty", i)
}
picks[w.ID]++
}

for id := 1; id <= workers; id++ {
if picks[id] != 2 {
t.Errorf("worker %d was picked %d times, want 2 (picks per worker: %v)", id, picks[id], picks)
}
}
}
Loading