|
| 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 | + "os" |
| 24 | + |
| 25 | + "github.com/cobaltcode-dev/kvm-node-agent/api/v1alpha1" |
| 26 | + libvirt "github.com/digitalocean/go-libvirt" |
| 27 | + "github.com/digitalocean/go-libvirt/socket/dialers" |
| 28 | + "k8s.io/apimachinery/pkg/api/resource" |
| 29 | + "sigs.k8s.io/controller-runtime/pkg/log" |
| 30 | +) |
| 31 | + |
| 32 | +// Client that returns the capabilities of the host we are mounted on. |
| 33 | +type Client interface { |
| 34 | + // Return the capabilities status of the host we are mounted on. |
| 35 | + Get() (v1alpha1.CapabilitiesStatus, error) |
| 36 | +} |
| 37 | + |
| 38 | +// Implementation of the CapabilitiesClient interface. |
| 39 | +type client struct { |
| 40 | + // Libvirt instance to connect to. |
| 41 | + virt *libvirt.Libvirt |
| 42 | +} |
| 43 | + |
| 44 | +// Create a new capabilities client using the provided LIBVIRT_SOCKET env variable. |
| 45 | +func NewClient() Client { |
| 46 | + socketPath := os.Getenv("LIBVIRT_SOCKET") |
| 47 | + if socketPath == "" { |
| 48 | + socketPath = "/run/libvirt/libvirt-sock" |
| 49 | + } |
| 50 | + log.Log.Info("capabilities client uses libvirt socket", "socket", socketPath) |
| 51 | + dialer := dialers.NewLocal(dialers.WithSocket(socketPath)) |
| 52 | + virt := libvirt.NewWithDialer(dialer) |
| 53 | + return &client{virt: virt} |
| 54 | +} |
| 55 | + |
| 56 | +// Return the capabilities of the host we are mounted on. |
| 57 | +func (m *client) Get() (v1alpha1.CapabilitiesStatus, error) { |
| 58 | + if !m.virt.IsConnected() { |
| 59 | + if err := m.virt.Connect(); err != nil { |
| 60 | + log.Log.Error(err, "failed to connect to libvirt") |
| 61 | + return v1alpha1.CapabilitiesStatus{}, err |
| 62 | + } |
| 63 | + } |
| 64 | + capabilitiesXMLBytes, err := m.virt.Capabilities() |
| 65 | + if err != nil { |
| 66 | + log.Log.Error(err, "failed to get libvirt capabilities") |
| 67 | + return v1alpha1.CapabilitiesStatus{}, err |
| 68 | + } |
| 69 | + var capabilities Capabilities |
| 70 | + if err := xml.Unmarshal(capabilitiesXMLBytes, &capabilities); err != nil { |
| 71 | + log.Log.Error(err, "failed to unmarshal libvirt capabilities") |
| 72 | + return v1alpha1.CapabilitiesStatus{}, err |
| 73 | + } |
| 74 | + return convert(capabilities) |
| 75 | +} |
| 76 | + |
| 77 | +// Emulated capabilities client returning an embedded capabilities xml. |
| 78 | +type clientEmulator struct{} |
| 79 | + |
| 80 | +// Create a new emulated capabilities client. |
| 81 | +func NewClientEmulator() Client { |
| 82 | + return &clientEmulator{} |
| 83 | +} |
| 84 | + |
| 85 | +// Get the capabilities of the host we are mounted on. |
| 86 | +func (c *clientEmulator) Get() (v1alpha1.CapabilitiesStatus, error) { |
| 87 | + var capabilities Capabilities |
| 88 | + if err := xml.Unmarshal(exampleXML, &capabilities); err != nil { |
| 89 | + log.Log.Error(err, "failed to unmarshal example capabilities") |
| 90 | + return v1alpha1.CapabilitiesStatus{}, err |
| 91 | + } |
| 92 | + return convert(capabilities) |
| 93 | +} |
| 94 | + |
| 95 | +// Convert the libvirt capabilities to the API format. |
| 96 | +func convert(in Capabilities) (out v1alpha1.CapabilitiesStatus, err error) { |
| 97 | + out.HostCpuArch = in.Host.CPU.Arch |
| 98 | + // Loop over all numa cells to get the total memory + vcpus. |
| 99 | + totalMemory := resource.NewQuantity(0, resource.BinarySI) |
| 100 | + totalCpus := resource.NewQuantity(0, resource.DecimalSI) |
| 101 | + for _, cell := range in.Host.Topology.CellSpec.Cells { |
| 102 | + mem, err := cell.Memory.AsQuantity() |
| 103 | + if err != nil { |
| 104 | + return v1alpha1.CapabilitiesStatus{}, err |
| 105 | + } |
| 106 | + totalMemory.Add(mem) |
| 107 | + cpu := resource.NewQuantity(cell.CPUs.Num, resource.DecimalSI) |
| 108 | + if cpu == nil { |
| 109 | + return v1alpha1.CapabilitiesStatus{}, |
| 110 | + fmt.Errorf("invalid CPU count for cell %d", cell.ID) |
| 111 | + } |
| 112 | + totalCpus.Add(*cpu) |
| 113 | + } |
| 114 | + out.HostMemory = *totalMemory |
| 115 | + out.HostCpus = *totalCpus |
| 116 | + return out, nil |
| 117 | +} |
0 commit comments