diff --git a/go.mod b/go.mod index ee9d8a3..f943f75 100644 --- a/go.mod +++ b/go.mod @@ -26,6 +26,7 @@ require ( github.com/go-openapi/swag v0.23.0 // indirect github.com/gogo/protobuf v1.3.2 // indirect github.com/google/gnostic-models v0.7.0 // indirect + github.com/google/go-cmp v0.7.0 // indirect github.com/google/uuid v1.6.0 // indirect github.com/hashicorp/hcl v1.0.0 // indirect github.com/josharian/intern v1.0.0 // indirect diff --git a/go.sum b/go.sum index f9896c8..549c176 100644 --- a/go.sum +++ b/go.sum @@ -310,6 +310,8 @@ go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.opencensus.io v0.22.3/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.opencensus.io v0.22.4/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.opencensus.io v0.22.5/go.mod h1:5pWMHQbX5EPX2/62yrJeAkowc+lfs/XD7Uxpq3pI6kk= +go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= +go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= go.yaml.in/yaml/v2 v2.4.3 h1:6gvOSjQoTB3vt1l+CU+tSyi/HOjfOjRLJ4YwYZGwRO0= go.yaml.in/yaml/v2 v2.4.3/go.mod h1:zSxWcmIDjOzPXpjlTTbAsKokqkDNAVtZO0WOMiT90s8= go.yaml.in/yaml/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc= diff --git a/pkg/kubevirt/cache.go b/pkg/kubevirt/cache.go new file mode 100644 index 0000000..bd1cd7c --- /dev/null +++ b/pkg/kubevirt/cache.go @@ -0,0 +1,281 @@ +/* + * 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 kubevirt + +// A read-only cache of VirtualMachine and VirtualMachineInstance objects. +// +// Every Redfish request fans out to several Kubernetes API calls, and nearly +// all of them are reads of the same VM/VMI objects (a GET System is five GETs). +// With many BareMetalHosts polling at once, those reads are what saturate the +// client-side rate limiter. For every configured namespace the client keeps +// one client-go informer for VMs and one for VMIs, so the read-only Redfish +// paths are served from memory. +// +// Two rules keep cached reads correct: +// +// - Only read-only inventory lookups use the cache (getVMForRead and its +// callers). Every path that reads an object in order to write it, and the +// handler that reads an object back right after writing it, reads from +// the API server (GetVM, GetVMBootOptionsLive). A write is therefore +// never computed from, or reported from, a copy the watch has not caught +// up with. +// - A cache that has not finished its initial list is not consulted, and +// once it has, its answer is final, including "not found". Falling back +// to a GET on every miss would cost a GET on every power poll of a +// stopped VM, which has no VMI, and that is the dominant state of a BMH +// during inspection and provisioning. +// +// A write by anyone, this process included, reaches the cache with its watch +// event, usually within milliseconds. Until then a cached read returns the +// previous version. No write path reads from the cache, so a stale read can +// make a response out of date but can never be written back over a change. + +import ( + "fmt" + "sort" + "time" + + apierrors "k8s.io/apimachinery/pkg/api/errors" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "k8s.io/apimachinery/pkg/labels" + "k8s.io/apimachinery/pkg/runtime/schema" + "k8s.io/client-go/dynamic" + "k8s.io/client-go/dynamic/dynamicinformer" + "k8s.io/client-go/informers" + kubevirtv1 "kubevirt.io/api/core/v1" + + "github.com/kubevirt/redfish-controller/pkg/logger" +) + +var ( + vmGVR = schema.GroupVersionResource{Group: "kubevirt.io", Version: "v1", Resource: "virtualmachines"} + vmiGVR = schema.GroupVersionResource{Group: "kubevirt.io", Version: "v1", Resource: "virtualmachineinstances"} +) + +// vmCache holds one VM informer and one VMI informer per configured namespace. +type vmCache struct { + stopCh chan struct{} + factories []dynamicinformer.DynamicSharedInformerFactory + vms map[string]informers.GenericInformer + vmis map[string]informers.GenericInformer +} + +// newVMCache starts informers for every distinct, non-empty namespace. It does +// not wait for them to sync; readers check HasSynced per call. +func newVMCache(client dynamic.Interface, namespaces []string) *vmCache { + vc := &vmCache{ + stopCh: make(chan struct{}), + vms: make(map[string]informers.GenericInformer), + vmis: make(map[string]informers.GenericInformer), + } + seen := make(map[string]bool) + for _, namespace := range namespaces { + if namespace == "" || seen[namespace] { + continue + } + seen[namespace] = true + + factory := dynamicinformer.NewFilteredDynamicSharedInformerFactory(client, 0, namespace, nil) + vmInformer := factory.ForResource(vmGVR) + vmiInformer := factory.ForResource(vmiGVR) + // Transforms must be set before the factory starts. + for _, inf := range []informers.GenericInformer{vmInformer, vmiInformer} { + if err := inf.Informer().SetTransform(stripManagedFields); err != nil { + logger.Error("Failed to set informer transform for namespace %s: %v", namespace, err) + } + } + vc.vms[namespace] = vmInformer + vc.vmis[namespace] = vmiInformer + vc.factories = append(vc.factories, factory) + factory.Start(vc.stopCh) + } + return vc +} + +// stop closes the stop channel first so the informer goroutines exit, then +// waits for them. The other order deadlocks. +func (vc *vmCache) stop() { + if vc == nil { + return + } + close(vc.stopCh) + for _, factory := range vc.factories { + factory.Shutdown() + } +} + +// informer returns the synced informer for gvr in namespace, or false when +// there is none or it has not finished its initial list. +func (vc *vmCache) informer(gvr schema.GroupVersionResource, namespace string) (informers.GenericInformer, bool) { + if vc == nil { + return nil, false + } + m := vc.vms + if gvr == vmiGVR { + m = vc.vmis + } + inf, ok := m[namespace] + if !ok || !inf.Informer().HasSynced() { + return nil, false + } + return inf, true +} + +// stripManagedFields drops the managedFields block before an object enters +// the store; it is the largest part of a VM object and nothing here reads it. +func stripManagedFields(obj interface{}) (interface{}, error) { + if u, ok := obj.(*unstructured.Unstructured); ok { + u.SetManagedFields(nil) + } + return obj, nil +} + +// restartVMCache replaces the running cache with one covering namespaces. +// Called with the watchers on startup and on config reload. +func (c *Client) restartVMCache(namespaces []string) { + c.cacheMu.Lock() + defer c.cacheMu.Unlock() + + if c.vmCache != nil { + c.vmCache.stop() + c.vmCache = nil + } + if c.dynamicClient == nil { + return + } + c.vmCache = newVMCache(c.dynamicClient, namespaces) + logger.Info("VM cache started for namespaces %v", namespaces) +} + +func (c *Client) stopVMCache() { + c.cacheMu.Lock() + defer c.cacheMu.Unlock() + if c.vmCache != nil { + c.vmCache.stop() + c.vmCache = nil + } +} + +func (c *Client) currentVMCache() *vmCache { + c.cacheMu.RLock() + defer c.cacheMu.RUnlock() + return c.vmCache +} + +// GetVMForRead returns a VM for a read-only purpose. It is served from the +// informer cache when the cache is synced for the namespace; otherwise it is +// a GetVM. Callers that go on to patch the VM must use GetVM. +func (c *Client) GetVMForRead(namespace, name string) (*kubevirtv1.VirtualMachine, error) { + return c.getVMForRead(namespace, name) +} + +func (c *Client) getVMForRead(namespace, name string) (*kubevirtv1.VirtualMachine, error) { + start := time.Now() + if u, served, err := c.cachedObject(vmGVR, namespace, name); served { + c.trackOperation("GetVM.cache", time.Since(start)) + if err != nil { + return nil, fmt.Errorf("failed to get VM: %w", err) + } + return unstructuredToVM(u) + } + c.trackOperation("GetVM.direct", time.Since(start)) + return c.GetVM(namespace, name) +} + +// getVMIForRead is GetVMI served from the cache under the same rules as +// getVMForRead. A synced cache's "not found" means the VM has no running +// instance, which is what callers use to report power state Off. +func (c *Client) getVMIForRead(namespace, name string) (*kubevirtv1.VirtualMachineInstance, error) { + start := time.Now() + if u, served, err := c.cachedObject(vmiGVR, namespace, name); served { + c.trackOperation("GetVMI.cache", time.Since(start)) + if err != nil { + return nil, fmt.Errorf("failed to get VMI %s: %w", name, err) + } + return unstructuredToVMI(u) + } + c.trackOperation("GetVMI.direct", time.Since(start)) + return c.GetVMI(namespace, name) +} + +// cachedObject looks the object up in the cache. served is false when the +// caller must ask the API server instead: the cache is not synced for the +// namespace or the lister failed. When served, err is a NotFound error if the +// object is absent, and the returned object is a copy the caller may modify. +func (c *Client) cachedObject(gvr schema.GroupVersionResource, namespace, name string) (*unstructured.Unstructured, bool, error) { + inf, ok := c.currentVMCache().informer(gvr, namespace) + if !ok { + return nil, false, nil + } + obj, err := inf.Lister().ByNamespace(namespace).Get(name) + if apierrors.IsNotFound(err) { + return nil, true, err + } + if err != nil { + return nil, false, nil + } + u, ok := obj.(*unstructured.Unstructured) + if !ok { + return nil, false, nil + } + return u.DeepCopy(), true, nil +} + +// listVMNamesFromCache returns the names of the VMs in namespace matching +// selector, or false when the cache is not synced for that namespace. +func (c *Client) listVMNamesFromCache(namespace string, selector labels.Selector) ([]string, bool) { + inf, ok := c.currentVMCache().informer(vmGVR, namespace) + if !ok { + return nil, false + } + objs, err := inf.Lister().ByNamespace(namespace).List(selector) + if err != nil { + return nil, false + } + names := make([]string, 0, len(objs)) + for _, obj := range objs { + if u, ok := obj.(*unstructured.Unstructured); ok { + names = append(names, u.GetName()) + } + } + c.trackOperation("ListVMs.cache", 0) + return names, true +} + +// filterVMNames applies the selector's explicit name list, if any, and sorts +// the result so collections render in a stable order regardless of source. +func filterVMNames(names []string, vmSelector *VMSelectorConfig) []string { + var out []string + if vmSelector != nil && len(vmSelector.Names) > 0 { + allowed := make(map[string]bool, len(vmSelector.Names)) + for _, n := range vmSelector.Names { + allowed[n] = true + } + for _, n := range names { + if allowed[n] { + out = append(out, n) + } + } + } else { + out = append(out, names...) + } + sort.Strings(out) + return out +} diff --git a/pkg/kubevirt/cache_test.go b/pkg/kubevirt/cache_test.go new file mode 100644 index 0000000..dbfe685 --- /dev/null +++ b/pkg/kubevirt/cache_test.go @@ -0,0 +1,224 @@ +/* + * 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 kubevirt + +import ( + "errors" + "reflect" + "strings" + "testing" + "time" + + apierrors "k8s.io/apimachinery/pkg/api/errors" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "k8s.io/apimachinery/pkg/labels" + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/runtime/schema" + dynamicfake "k8s.io/client-go/dynamic/fake" + k8stesting "k8s.io/client-go/testing" +) + +func testVM(namespace, name string, lbls map[string]string) *unstructured.Unstructured { + u := &unstructured.Unstructured{} + u.SetAPIVersion("kubevirt.io/v1") + u.SetKind("VirtualMachine") + u.SetNamespace(namespace) + u.SetName(name) + u.SetLabels(lbls) + return u +} + +func withBootTarget(u *unstructured.Unstructured, target string) *unstructured.Unstructured { + u.SetAnnotations(map[string]string{"redfish.boot.source.override.target": target}) + return u +} + +func newCacheTestClient(t *testing.T, objs ...runtime.Object) (*Client, *dynamicfake.FakeDynamicClient) { + t.Helper() + fake := dynamicfake.NewSimpleDynamicClientWithCustomListKinds(runtime.NewScheme(), map[schema.GroupVersionResource]string{ + vmGVR: "VirtualMachineList", + vmiGVR: "VirtualMachineInstanceList", + }, objs...) + c := &Client{ + dynamicClient: fake, + timeout: 5 * time.Second, + } + c.metrics.operationCounts = make(map[string]int64) + c.metrics.operationTimes = make(map[string]time.Duration) + return c, fake +} + +func waitSynced(t *testing.T, c *Client, namespace string) { + t.Helper() + deadline := time.Now().Add(5 * time.Second) + for time.Now().Before(deadline) { + _, vmOK := c.currentVMCache().informer(vmGVR, namespace) + _, vmiOK := c.currentVMCache().informer(vmiGVR, namespace) + if vmOK && vmiOK { + return + } + time.Sleep(10 * time.Millisecond) + } + t.Fatalf("cache for namespace %s did not sync", namespace) +} + +func failGets(fake *dynamicfake.FakeDynamicClient, msg string) { + fake.PrependReactor("get", "virtualmachines", func(k8stesting.Action) (bool, runtime.Object, error) { + return true, nil, errors.New(msg) + }) + fake.PrependReactor("get", "virtualmachineinstances", func(k8stesting.Action) (bool, runtime.Object, error) { + return true, nil, errors.New(msg) + }) +} + +func TestVMCacheServesReads(t *testing.T) { + c, fake := newCacheTestClient(t, + testVM("ns", "vm-a", map[string]string{"redfish-enabled": "true"}), + testVM("ns", "vm-b", nil), + ) + // Duplicate and empty namespaces must be tolerated. + c.restartVMCache([]string{"ns", "ns", ""}) + defer c.stopVMCache() + waitSynced(t, c, "ns") + + // From here on any GET that reaches the API server fails, so a + // successful read proves the cache is on the path. + failGets(fake, "unexpected API server GET") + + vm, err := c.getVMForRead("ns", "vm-a") + if err != nil { + t.Fatalf("cached read failed: %v", err) + } + if vm.Name != "vm-a" || vm.Namespace != "ns" { + t.Fatalf("unexpected VM from cache: %s/%s", vm.Namespace, vm.Name) + } + + // A synced cache's not-found is authoritative: no fallback GET. + if _, err := c.getVMForRead("ns", "missing"); !apierrors.IsNotFound(err) { + t.Fatalf("expected NotFound from cache, got %v", err) + } + + // No VMI exists for vm-a, which callers read as power state Off. + if _, err := c.getVMIForRead("ns", "vm-a"); !apierrors.IsNotFound(err) { + t.Fatalf("expected NotFound VMI from cache, got %v", err) + } + + // The direct path still goes to the API server. + if _, err := c.GetVM("ns", "vm-a"); err == nil { + t.Fatal("expected direct GetVM to reach the API server and fail") + } + + // A namespace the cache does not cover is not served. + if _, err := c.getVMForRead("other", "vm-a"); err == nil || !strings.Contains(err.Error(), "unexpected API server GET") { + t.Fatalf("expected fallback to API server for uncovered namespace, got %v", err) + } + + // List honours the label selector and reports coverage. + names, ok := c.listVMNamesFromCache("ns", labels.SelectorFromSet(labels.Set{"redfish-enabled": "true"})) + if !ok || !reflect.DeepEqual(names, []string{"vm-a"}) { + t.Fatalf("selector list: ok=%v names=%v", ok, names) + } + names, ok = c.listVMNamesFromCache("ns", labels.Everything()) + if !ok || len(names) != 2 { + t.Fatalf("everything list: ok=%v names=%v", ok, names) + } + if _, ok := c.listVMNamesFromCache("other", labels.Everything()); ok { + t.Fatal("uncovered namespace must not be served from cache") + } +} + +// The boot-device PATCH handler writes the VM and then reads it back to build +// its response. That read-back must come from the API server, because the +// watch event for the write may not have reached the cache yet. +func TestGetVMBootOptionsLiveReadsAPIServer(t *testing.T) { + c, fake := newCacheTestClient(t, withBootTarget(testVM("ns", "vm-a", nil), "Hdd")) + c.restartVMCache([]string{"ns"}) + defer c.stopVMCache() + waitSynced(t, c, "ns") + + // The API server holds a newer version the watch has not delivered. + live := withBootTarget(testVM("ns", "vm-a", nil), "Cd") + fake.PrependReactor("get", "virtualmachines", func(k8stesting.Action) (bool, runtime.Object, error) { + return true, live, nil + }) + + cached, err := c.GetVMBootOptions("ns", "vm-a") + if err != nil { + t.Fatalf("cached boot options: %v", err) + } + if cached["bootSourceOverrideTarget"] != "Hdd" { + t.Fatalf("cached read should still hold the old target, got %v", cached["bootSourceOverrideTarget"]) + } + + fresh, err := c.GetVMBootOptionsLive("ns", "vm-a") + if err != nil { + t.Fatalf("live boot options: %v", err) + } + if fresh["bootSourceOverrideTarget"] != "Cd" { + t.Fatalf("live read must come from the API server, got %v", fresh["bootSourceOverrideTarget"]) + } +} + +func TestVMCacheRestartAndStop(t *testing.T) { + c, _ := newCacheTestClient(t, testVM("ns1", "a", nil), testVM("ns2", "b", nil)) + c.restartVMCache([]string{"ns1"}) + waitSynced(t, c, "ns1") + if _, ok := c.currentVMCache().informer(vmGVR, "ns2"); ok { + t.Fatal("ns2 should not be covered yet") + } + // Config reload: a new namespace set replaces the old cache without deadlocking. + c.restartVMCache([]string{"ns2"}) + waitSynced(t, c, "ns2") + if _, ok := c.currentVMCache().informer(vmGVR, "ns1"); ok { + t.Fatal("ns1 should no longer be covered") + } + c.stopVMCache() + if c.currentVMCache() != nil { + t.Fatal("cache should be nil after stop") + } + // Reads after stop fall back to the API server. + if _, err := c.getVMForRead("ns2", "b"); err != nil { + t.Fatalf("direct read after stop: %v", err) + } +} + +// A client built without a dynamic client, as the server tests do, must not +// start a cache or panic. +func TestVMCacheWithoutDynamicClient(t *testing.T) { + c := &Client{} + c.restartVMCache([]string{"ns"}) + if c.currentVMCache() != nil { + t.Fatal("cache must not start without a dynamic client") + } + c.stopVMCache() +} + +func TestFilterVMNames(t *testing.T) { + in := []string{"c", "a", "b"} + if got := filterVMNames(in, nil); !reflect.DeepEqual(got, []string{"a", "b", "c"}) { + t.Fatalf("nil selector: %v", got) + } + if got := filterVMNames(in, &VMSelectorConfig{Names: []string{"b", "zzz"}}); !reflect.DeepEqual(got, []string{"b"}) { + t.Fatalf("names filter: %v", got) + } + if got := filterVMNames(nil, &VMSelectorConfig{Names: []string{"b"}}); got != nil { + t.Fatalf("empty input should stay nil, got %v", got) + } +} diff --git a/pkg/kubevirt/client.go b/pkg/kubevirt/client.go index 26ad6c2..4bb6f1c 100644 --- a/pkg/kubevirt/client.go +++ b/pkg/kubevirt/client.go @@ -83,6 +83,10 @@ type Client struct { operationTimes map[string]time.Duration } + // Read-only VM/VMI informer cache (cache.go) + cacheMu sync.RWMutex + vmCache *vmCache + // Boot-once watcher management watcherCtx context.Context watcherCancel context.CancelFunc @@ -310,6 +314,7 @@ func (c *Client) Close() error { c.stopBootOnceWatcher() c.stopImportPodWatcher() c.stopCDIPVCWatcher() + c.stopVMCache() if c.httpClient != nil && c.httpClient.Transport != nil { if transport, ok := c.httpClient.Transport.(*http.Transport); ok { @@ -328,6 +333,7 @@ func (c *Client) RestartWatchers(ctx context.Context, namespaces []string) { c.StartBootOnceWatcher(ctx, namespaces) c.StartImportPodWatcher(ctx, namespaces) c.StartCDIPVCWatcher(ctx, namespaces) + c.restartVMCache(namespaces) } // retryWithBackoff executes a function with exponential backoff retry logic @@ -462,9 +468,9 @@ func (c *Client) ListVMsWithSelector(namespace string, vmSelector *VMSelectorCon } // Build list options with label selector if provided - listOptions := metav1.ListOptions{} + selector := labels.Everything() if vmSelector != nil && len(vmSelector.Labels) > 0 { - selector := labels.NewSelector() + selector = labels.NewSelector() for key, value := range vmSelector.Labels { req, err := labels.NewRequirement(key, selection.Equals, []string{value}) if err != nil { @@ -473,34 +479,31 @@ func (c *Client) ListVMsWithSelector(namespace string, vmSelector *VMSelectorCon } selector = selector.Add(*req) } - listOptions.LabelSelector = selector.String() logger.Debug("Using label selector: %s", selector.String()) } - vms, err := c.dynamicClient.Resource(gvr).Namespace(namespace).List(ctx, listOptions) - if err != nil { - logger.Error("Failed to list VMs in namespace %s: %v", namespace, err) - return nil, fmt.Errorf("failed to list VMs: %w", err) - } - - var vmNames []string - for _, vm := range vms.Items { - vmName := vm.GetName() - - // If explicit names are specified, only include those VMs - if vmSelector != nil && len(vmSelector.Names) > 0 { - for _, allowedName := range vmSelector.Names { - if vmName == allowedName { - vmNames = append(vmNames, vmName) - break - } - } - } else { - // Include all VMs if no explicit names specified - vmNames = append(vmNames, vmName) + // Serve from the informer cache when it is synced for this namespace; + // otherwise list from the API server. + names, ok := c.listVMNamesFromCache(namespace, selector) + if !ok { + listOptions := metav1.ListOptions{} + if !selector.Empty() { + listOptions.LabelSelector = selector.String() + } + vms, err := c.dynamicClient.Resource(gvr).Namespace(namespace).List(ctx, listOptions) + if err != nil { + logger.Error("Failed to list VMs in namespace %s: %v", namespace, err) + return nil, fmt.Errorf("failed to list VMs: %w", err) + } + names = make([]string, 0, len(vms.Items)) + for _, vm := range vms.Items { + names = append(names, vm.GetName()) } } + // If explicit names are specified, only include those VMs + vmNames := filterVMNames(names, vmSelector) + logger.Info("Found %d VMs in namespace %s with selector: %v", len(vmNames), namespace, vmNames) return vmNames, nil } @@ -645,7 +648,7 @@ func (c *Client) GetVMPowerState(namespace, name string) (string, error) { } // Fetch the VM resource using typed API - vm, err := c.GetVM(namespace, name) + vm, err := c.getVMForRead(namespace, name) if err != nil { return "Unknown", fmt.Errorf("failed to get VM %s: %w", name, err) } @@ -685,7 +688,7 @@ func (c *Client) GetVMPowerState(namespace, name string) (string, error) { } // Fallback to VMI phase logic - vmi, err := c.GetVMI(namespace, name) + vmi, err := c.getVMIForRead(namespace, name) if err != nil { // If VMI doesn't exist, VM is stopped return "Off", nil @@ -1337,7 +1340,7 @@ func (c *Client) GetVMNetworkInterfaces(namespace, name string) ([]string, error return nil, fmt.Errorf("dynamic client is not initialized") } - vmi, err := c.GetVMI(namespace, name) + vmi, err := c.getVMIForRead(namespace, name) if err != nil { return nil, fmt.Errorf("failed to get VMI %s: %w", name, err) } @@ -1354,7 +1357,7 @@ func (c *Client) GetVMNetworkInterfaces(namespace, name string) ([]string, error // GetVMStorage gets storage information of a VirtualMachine func (c *Client) GetVMStorage(namespace, name string) ([]string, error) { - vm, err := c.GetVM(namespace, name) + vm, err := c.getVMForRead(namespace, name) if err != nil { return nil, err } @@ -1375,11 +1378,25 @@ func (c *Client) GetVMStorage(namespace, name string) ([]string, error) { // GetVMBootOptions gets boot options of a VirtualMachine func (c *Client) GetVMBootOptions(namespace, name string) (map[string]interface{}, error) { + vm, err := c.getVMForRead(namespace, name) + if err != nil { + return nil, err + } + return bootOptionsFromVM(vm), nil +} + +// GetVMBootOptionsLive is GetVMBootOptions read from the API server instead of +// the cache. The boot-device PATCH handler uses it to build its response from +// the VM it has just written, which the cache may not reflect yet. +func (c *Client) GetVMBootOptionsLive(namespace, name string) (map[string]interface{}, error) { vm, err := c.GetVM(namespace, name) if err != nil { return nil, err } + return bootOptionsFromVM(vm), nil +} +func bootOptionsFromVM(vm *kubevirtv1.VirtualMachine) map[string]interface{} { bootOptions := map[string]interface{}{ "bootSourceOverrideEnabled": "Disabled", "bootSourceOverrideTarget": "None", @@ -1408,7 +1425,7 @@ func (c *Client) GetVMBootOptions(namespace, name string) (map[string]interface{ } } - return bootOptions, nil + return bootOptions } // RecordVMBootOptionsAsAnnotations stores the boot options of a VirtualMachine @@ -1477,7 +1494,7 @@ func (c *Client) RecordVMBootOptionsAsAnnotations(namespace, name string, option // GetVMVirtualMedia gets virtual media information of a VirtualMachine func (c *Client) GetVMVirtualMedia(namespace, name string) ([]string, error) { - vm, err := c.GetVM(namespace, name) + vm, err := c.getVMForRead(namespace, name) if err != nil { return nil, fmt.Errorf("failed to get VM %s: %w", name, err) } @@ -1503,7 +1520,7 @@ func (c *Client) GetVMVirtualMedia(namespace, name string) ([]string, error) { // IsVirtualMediaInserted checks if a specific virtual media device has media inserted and ready func (c *Client) IsVirtualMediaInserted(namespace, name, mediaID string) (bool, error) { - vm, err := c.GetVM(namespace, name) + vm, err := c.getVMForRead(namespace, name) if err != nil { return false, fmt.Errorf("failed to get VM %s: %w", name, err) } @@ -2627,7 +2644,7 @@ func (c *Client) GetNamespaceInfo(namespace string) (*corev1.Namespace, error) { // GetVMMemory gets memory information of a VirtualMachine func (c *Client) GetVMMemory(namespace, name string) (float64, error) { - vm, err := c.GetVM(namespace, name) + vm, err := c.getVMForRead(namespace, name) if err != nil { logger.Warning("Failed to get VM %s/%s for memory lookup: %v", namespace, name, err) return 2.0, nil // Low default fallback @@ -2665,7 +2682,7 @@ func (c *Client) GetVMMemory(namespace, name string) (float64, error) { // GetVMCPU gets CPU information of a VirtualMachine func (c *Client) GetVMCPU(namespace, name string) (int, error) { - vm, err := c.GetVM(namespace, name) + vm, err := c.getVMForRead(namespace, name) if err != nil { logger.Warning("Failed to get VM %s/%s for CPU lookup: %v", namespace, name, err) return 1, nil // Low default fallback @@ -2689,7 +2706,7 @@ func (c *Client) GetVMCPU(namespace, name string) (int, error) { // GetVMStorageDetails gets detailed storage information of a VirtualMachine func (c *Client) GetVMStorageDetails(namespace, name string) (map[string]interface{}, error) { - vm, err := c.GetVM(namespace, name) + vm, err := c.getVMForRead(namespace, name) if err != nil { return nil, err } @@ -2741,7 +2758,7 @@ func (c *Client) GetVMStorageDetails(namespace, name string) (map[string]interfa // GetVMNetworkDetails gets detailed network information of a VirtualMachine func (c *Client) GetVMNetworkDetails(namespace, name string) ([]map[string]interface{}, error) { - vm, err := c.GetVM(namespace, name) + vm, err := c.getVMForRead(namespace, name) if err != nil { return nil, err } diff --git a/pkg/server/server.go b/pkg/server/server.go index df224ce..1564964 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -1203,7 +1203,7 @@ func (s *Server) handleBootUpdate(w http.ResponseWriter, r *http.Request, system } // Get updated boot options to return in response - bootOptions, err := s.kubevirtClient.GetVMBootOptions(namespace, vmName) + bootOptions, err := s.kubevirtClient.GetVMBootOptionsLive(namespace, vmName) if err != nil { logger.Error("Failed to get updated boot options for VM %s: %v", vmName, err) bootOptions = map[string]interface{}{ @@ -1899,7 +1899,7 @@ func (s *Server) resolveSystemVMandCheckAccess(w http.ResponseWriter, r *http.Re continue } if cfg.Namespace == namespace { - vm, err := s.kubevirtClient.GetVM(namespace, vmName) + vm, err := s.kubevirtClient.GetVMForRead(namespace, vmName) if err == nil && kubevirt.VMMatchesSelector(vm, cfg.VMSelector) { vmFound = true namespace = cfg.Namespace @@ -1915,7 +1915,7 @@ func (s *Server) resolveSystemVMandCheckAccess(w http.ResponseWriter, r *http.Re continue } - vm, err := s.kubevirtClient.GetVM(cfg.Namespace, vmName) + vm, err := s.kubevirtClient.GetVMForRead(cfg.Namespace, vmName) if err == nil && kubevirt.VMMatchesSelector(vm, cfg.VMSelector) { vmFound = true namespace = cfg.Namespace