|
| 1 | +/* |
| 2 | +SPDX-FileCopyrightText: Copyright 2025 SAP SE or an SAP affiliate company and cobaltcore-dev contributors |
| 3 | +SPDX-License-Identifier: Apache-2.0 |
| 4 | +
|
| 5 | +Licensed under the Apache License, LibVirtVersion 2.0 (the "License"); |
| 6 | +you may not use this file except in compliance with the License. |
| 7 | +You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | +Unless required by applicable law or agreed to in writing, software |
| 12 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +See the License for the specific language governing permissions and |
| 15 | +limitations under the License. |
| 16 | +*/ |
| 17 | + |
| 18 | +package capabilities |
| 19 | + |
| 20 | +import ( |
| 21 | + "encoding/xml" |
| 22 | + "fmt" |
| 23 | + |
| 24 | + "github.com/cobaltcore-dev/kvm-node-agent/api/v1alpha1" |
| 25 | + libvirt "github.com/digitalocean/go-libvirt" |
| 26 | + "k8s.io/apimachinery/pkg/api/resource" |
| 27 | + "sigs.k8s.io/controller-runtime/pkg/log" |
| 28 | +) |
| 29 | + |
| 30 | +// Client that returns the capabilities of the host we are mounted on. |
| 31 | +type Client interface { |
| 32 | + // Return the capabilities status of the host we are mounted on. |
| 33 | + Get(virt *libvirt.Libvirt) (v1alpha1.CapabilitiesStatus, error) |
| 34 | +} |
| 35 | + |
| 36 | +// Implementation of the CapabilitiesClient interface. |
| 37 | +type client struct{} |
| 38 | + |
| 39 | +// Create a new capabilities client. |
| 40 | +func NewClient() Client { |
| 41 | + return &client{} |
| 42 | +} |
| 43 | + |
| 44 | +// Return the capabilities of the host we are mounted on. |
| 45 | +func (m *client) Get(virt *libvirt.Libvirt) (v1alpha1.CapabilitiesStatus, error) { |
| 46 | + capabilitiesXMLBytes, err := virt.Capabilities() |
| 47 | + if err != nil { |
| 48 | + log.Log.Error(err, "failed to get libvirt capabilities") |
| 49 | + return v1alpha1.CapabilitiesStatus{}, err |
| 50 | + } |
| 51 | + var capabilities Capabilities |
| 52 | + if err := xml.Unmarshal(capabilitiesXMLBytes, &capabilities); err != nil { |
| 53 | + log.Log.Error(err, "failed to unmarshal libvirt capabilities") |
| 54 | + return v1alpha1.CapabilitiesStatus{}, err |
| 55 | + } |
| 56 | + return convert(capabilities) |
| 57 | +} |
| 58 | + |
| 59 | +// Emulated capabilities client returning an embedded capabilities xml. |
| 60 | +type clientEmulator struct{} |
| 61 | + |
| 62 | +// Create a new emulated capabilities client. |
| 63 | +func NewClientEmulator() Client { |
| 64 | + return &clientEmulator{} |
| 65 | +} |
| 66 | + |
| 67 | +// Get the capabilities of the host we are mounted on. |
| 68 | +func (c *clientEmulator) Get(virt *libvirt.Libvirt) (v1alpha1.CapabilitiesStatus, error) { |
| 69 | + var capabilities Capabilities |
| 70 | + if err := xml.Unmarshal(exampleXML, &capabilities); err != nil { |
| 71 | + log.Log.Error(err, "failed to unmarshal example capabilities") |
| 72 | + return v1alpha1.CapabilitiesStatus{}, err |
| 73 | + } |
| 74 | + return convert(capabilities) |
| 75 | +} |
| 76 | + |
| 77 | +// Convert the libvirt capabilities to the API format. |
| 78 | +func convert(in Capabilities) (out v1alpha1.CapabilitiesStatus, err error) { |
| 79 | + out.HostCpuArch = in.Host.CPU.Arch |
| 80 | + // Loop over all numa cells to get the total memory + vcpus. |
| 81 | + totalMemory := resource.NewQuantity(0, resource.BinarySI) |
| 82 | + totalCpus := resource.NewQuantity(0, resource.DecimalSI) |
| 83 | + for _, cell := range in.Host.Topology.CellSpec.Cells { |
| 84 | + mem, err := cell.Memory.AsQuantity() |
| 85 | + if err != nil { |
| 86 | + return v1alpha1.CapabilitiesStatus{}, err |
| 87 | + } |
| 88 | + totalMemory.Add(mem) |
| 89 | + cpu := resource.NewQuantity(cell.CPUs.Num, resource.DecimalSI) |
| 90 | + if cpu == nil { |
| 91 | + return v1alpha1.CapabilitiesStatus{}, |
| 92 | + fmt.Errorf("invalid CPU count for cell %d", cell.ID) |
| 93 | + } |
| 94 | + totalCpus.Add(*cpu) |
| 95 | + } |
| 96 | + out.HostMemory = *totalMemory |
| 97 | + out.HostCpus = *totalCpus |
| 98 | + return out, nil |
| 99 | +} |
0 commit comments