Skip to content

Commit

Permalink
feat: customize vm_id with libvirt metadata
Browse files Browse the repository at this point in the history
By default the name chosen to define the Virtual Machine ID is the one
found in /proc/<pid>/cgroup/.

Many IaaS platform like Openstack use metadata in libvirt to customise
among other things the instance's ID.

Allow the user to use the metadata in libvirt to set the name of the
vm metrics and gives a better user experience.

Set "LIBVIRT_METADATA_URI" with the uri of the XML namespace identifier.
Set "LIBVIRT_METADATA_TOKEN" to choose the string used for VM ID. By
default, the token "name" is used.

Signed-off-by: Anthony Harivel <[email protected]>
  • Loading branch information
aharivel committed Aug 9, 2024
1 parent 73367d3 commit 5432a39
Show file tree
Hide file tree
Showing 144 changed files with 49,049 additions and 0 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ require (
github.com/HabanaAI/gohlml v1.16.0
github.com/NVIDIA/go-dcgm v0.0.0-20240118201113-3385e277e49f
github.com/NVIDIA/go-nvml v0.12.0-1
github.com/beevik/etree v1.4.0
github.com/cilium/ebpf v0.15.0
github.com/digitalocean/go-libvirt v0.0.0-20240709142323-d8406205c752
github.com/jaypipes/ghw v0.12.0
github.com/joho/godotenv v1.5.1
github.com/jszwec/csvutil v1.10.0
Expand Down Expand Up @@ -69,6 +71,7 @@ require (
github.com/prometheus/procfs v0.12.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.9.0 // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/oauth2 v0.21.0 // indirect
golang.org/x/term v0.22.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ github.com/StackExchange/wmi v1.2.1 h1:VIkavFPXSjcnS+O8yTq7NI32k0R5Aj+v39y29VYDO
github.com/StackExchange/wmi v1.2.1/go.mod h1:rcmrprowKIVzvc+NUiLncP2uuArMWLCbu9SBzvHz7e8=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
github.com/beevik/etree v1.4.0 h1:oz1UedHRepuY3p4N5OjE0nK1WLCqtzHf25bxplKOHLs=
github.com/beevik/etree v1.4.0/go.mod h1:cyWiXwGoasx60gHvtnEh5x8+uIjUVnjWqBvEnhnqKDA=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/bits-and-blooms/bitset v1.13.0 h1:bAQ9OPNFYbGHV6Nez0tmNI0RiEu7/hxlYJRUA0wFAVE=
Expand All @@ -22,6 +24,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/digitalocean/go-libvirt v0.0.0-20240709142323-d8406205c752 h1:NI7XEcHzWVvBfVjSVK6Qk4wmrUfoyQxCNpBjrHelZFk=
github.com/digitalocean/go-libvirt v0.0.0-20240709142323-d8406205c752/go.mod h1:/Ok8PA2qi/ve0Py38+oL+VxoYmlowigYRyLEODRYdgc=
github.com/emicklei/go-restful/v3 v3.11.0 h1:rAQeMHw1c7zTmncogyy8VvRZwtkmkZ4FxERmMY4rD+g=
github.com/emicklei/go-restful/v3 v3.11.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc=
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
Expand Down Expand Up @@ -149,6 +153,8 @@ go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30=
golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M=
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 h1:2dVuKD2vS7b0QIHQbpyTISPd0LeHDbnYEryqj5Q1ug8=
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56/go.mod h1:M4RDyNAINzryxdtnbRXRL/OHtkFuWGRjvuhBJpk2IlY=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
Expand Down
14 changes: 14 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ var (
// KubeConfig is used to start k8s client with the pod running outside the cluster
KubeConfig = ""
EnableAPIServer = false

////////////////////////////////////

// Libvirt Metadata config
LibvirtMetadataURI = getConfig("LIBVIRT_METADATA_URI", "")
LibvirtMetadataToken = getConfig("LIBVIRT_METADATA_TOKEN", "name")
)

// return local path to power model weight
Expand Down Expand Up @@ -423,3 +429,11 @@ func GetModelConfigMap() map[string]string {
func SetGpuUsageMetric(metric string) {
GpuUsageMetric = metric
}

func GetLibvirtMetadataURI() string {
return LibvirtMetadataURI
}

func GetLibvirtMetadataToken() string {
return LibvirtMetadataToken
}
59 changes: 59 additions & 0 deletions pkg/libvirt/resolve_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ package libvirt

import (
"fmt"
"net/url"
"os"
"regexp"
"strings"

"github.com/beevik/etree"
"github.com/digitalocean/go-libvirt"

"github.com/sustainable-computing-io/kepler/pkg/config"
"github.com/sustainable-computing-io/kepler/pkg/utils"
)

Expand Down Expand Up @@ -66,7 +71,14 @@ func getVMID(pid uint64, fileName string) (string, error) {
vmID = strings.ReplaceAll(vmID, "\\x2d", "-")
vmID = strings.ReplaceAll(vmID, ".scope", "")

if config.GetLibvirtMetadataURI() != "" {
// NOTE: use VM Metadata instead
if metaID, err := getMetadataVMID(vmID); metaID != "" && err == nil {
vmID = metaID
}
}
addVMIDToCache(pid, vmID)

return vmID, nil
}

Expand Down Expand Up @@ -99,3 +111,50 @@ func addToNotExistCache(pid uint64) {
}
cacheNotExist[pid] = false
}

func getMetadataVMID(vmid string) (metaVmid string, err error) {
metadataURI := config.GetLibvirtMetadataURI()
domainName := ""
metaVmid = ""

parts := strings.Split(vmid, "-")
if len(parts) < 4 {
return metaVmid, fmt.Errorf("the vmid provided does not match machine-qemu-number-name : %v", vmid)
}

domainName = strings.Join(parts[3:], "-")

uri, _ := url.Parse(string(libvirt.QEMUSystem))
l, err := libvirt.ConnectToURI(uri)
if err != nil {
return metaVmid, fmt.Errorf("libvirt failed to connect: %w", err)
}

defer func() {
e := l.Disconnect()
if err == nil {
err = e
}
}()

d, err := l.DomainLookupByName(domainName)
if err != nil {
return metaVmid, fmt.Errorf("libvirt Domainlookup fail %w", err)
}

metaURI := libvirt.OptString{metadataURI}
// Domain Metadata Element = 0x2
metaXML, _ := l.DomainGetMetadata(d, 0x2, metaURI, 0)

doc := etree.NewDocument()
if err := doc.ReadFromString(metaXML); err != nil {
return metaVmid, fmt.Errorf("failed to read metadata XML: %w", err)
}

token := config.GetLibvirtMetadataToken()
nameElement := doc.FindElement(fmt.Sprintf("//%s", token))
if nameElement == nil {
return metaVmid, fmt.Errorf("no <%s> element found", token)
}
return nameElement.Text(), nil
}
14 changes: 14 additions & 0 deletions vendor/github.com/beevik/etree/CONTRIBUTORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions vendor/github.com/beevik/etree/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

205 changes: 205 additions & 0 deletions vendor/github.com/beevik/etree/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5432a39

Please sign in to comment.