Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add downwardMetrics volume to KubeVirt to allow exposing host metrics to guests #5502

Merged
merged 14 commits into from
May 21, 2021
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -448,9 +448,9 @@ container_pull(
# TODO build fedora_with_test_tooling for multi-arch
container_pull(
name = "fedora_with_test_tooling",
digest = "sha256:9ec3e137bff093597d192f5a4e346f25b614c3a94216b857de0e3d75b68bfb17",
digest = "sha256:24bac3f1653ef0bc918c8aa5ee1043ad01ac2b7bee75137194fd0c8db06b73c6",
registry = "quay.io",
repository = "kubevirt/fedora-with-test-tooling",
repository = "kubevirtci/fedora-with-test-tooling",
)

container_pull(
Expand Down
8 changes: 8 additions & 0 deletions api/openapi-spec/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -9640,6 +9640,10 @@
}
}
},
"v1.DownwardMetricsVolumeSource": {
"description": "DownwardMetricsVolumeSource adds a very small disk to VMIs which contains a limited view of host and guest metrics. The disk content is compatible with vhostmd (https://github.com/vhostmd/vhostmd) and vm-dump-metrics.",
"type": "object"
},
"v1.EFI": {
"description": "If set, EFI will be used instead of BIOS.",
"type": "object",
Expand Down Expand Up @@ -12206,6 +12210,10 @@
"description": "DownwardAPI represents downward API about the pod that should populate this volume",
"$ref": "#/definitions/v1.DownwardAPIVolumeSource"
},
"downwardMetrics": {
"description": "DownwardMetrics adds a very small disk to VMIs which contains a limited view of host and guest metrics. The disk content is compatible with vhostmd (https://github.com/vhostmd/vhostmd) and vm-dump-metrics.",
"$ref": "#/definitions/v1.DownwardMetricsVolumeSource"
},
"emptyDisk": {
"description": "EmptyDisk represents a temporary disk which shares the vmis lifecycle. More info: https://kubevirt.gitbooks.io/user-guide/disks-and-volumes.html",
"$ref": "#/definitions/v1.EmptyDiskSource"
Expand Down
1 change: 1 addition & 0 deletions cmd/virt-handler/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ go_library(
"//pkg/inotify-informer:go_default_library",
"//pkg/monitoring/client/prometheus:go_default_library",
"//pkg/monitoring/reflector/prometheus:go_default_library",
"//pkg/monitoring/vms/downwardmetrics:go_default_library",
"//pkg/monitoring/vms/prometheus:go_default_library",
"//pkg/monitoring/workqueue/prometheus:go_default_library",
"//pkg/service:go_default_library",
Expand Down
5 changes: 5 additions & 0 deletions cmd/virt-handler/virt-handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ import (
"k8s.io/client-go/tools/record"
"k8s.io/client-go/util/certificate"

"kubevirt.io/kubevirt/pkg/monitoring/vms/downwardmetrics"

"kubevirt.io/kubevirt/pkg/healthz"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -321,6 +323,9 @@ func (app *virtHandlerApp) Run() {
)

promvm.SetupCollector(app.virtCli, app.VirtShareDir, app.HostOverride, app.MaxRequestsInFlight, vmiSourceInformer)
if err := downwardmetrics.RunDownwardMetricsCollector(context.Background(), app.HostOverride, vmiSourceInformer, podIsolationDetector); err != nil {
panic(fmt.Errorf("failed to set up the downwardMetrics collector: %v", err))
}

go app.clientcertmanager.Start()
go app.servercertmanager.Start()
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module kubevirt.io/kubevirt
require (
github.com/Masterminds/semver v1.5.0
github.com/blang/semver v3.5.1+incompatible
github.com/c9s/goprocinfo v0.0.0-20210130143923-c95fcf8c64a8
github.com/containernetworking/plugins v0.8.2
github.com/coreos/go-iptables v0.4.3
github.com/coreos/go-semver v0.3.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ github.com/blang/semver v3.5.1+incompatible h1:cQNTCjp13qL8KC3Nbxr/y2Bqb63oX6wdn
github.com/blang/semver v3.5.1+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnwebNt5EWlYSAyrTnjyyk=
github.com/brancz/gojsontoyaml v0.0.0-20190425155809-e8bd32d46b3d/go.mod h1:IyUJYN1gvWjtLF5ZuygmxbnsAyP3aJS6cHzIuZY50B0=
github.com/buger/jsonparser v0.0.0-20180808090653-f4dd9f5a6b44/go.mod h1:bbYlZJ7hK1yFx9hf58LP0zeX7UjIGs20ufpu3evjr+s=
github.com/c9s/goprocinfo v0.0.0-20210130143923-c95fcf8c64a8 h1:SjZ2GvvOononHOpK84APFuMvxqsk3tEIaKH/z4Rpu3g=
github.com/c9s/goprocinfo v0.0.0-20210130143923-c95fcf8c64a8/go.mod h1:uEyr4WpAH4hio6LFriaPkL938XnrvLpNPmQHBdrmbIE=
github.com/campoy/embedmd v1.0.0/go.mod h1:oxyr9RCiSXg0M3VJ3ks0UGfp98BpSSGr0kpiX3MzVl8=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/certifi/gocertifi v0.0.0-20180905225744-ee1a9a0726d2/go.mod h1:GJKEexRPVJrBSOjoqN5VNOIKJ5Q3RViH6eu3puDRwx4=
Expand Down
2 changes: 1 addition & 1 deletion hack/check-for-binaries.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash

if diff <(git grep -c '') <(git grep -cI '') |
grep -E -v -e 'docs/.*\.png|swagger-ui' -e 'vendor/*' -e 'assets/*' |
grep -E -v -e 'docs/.*\.png|swagger-ui' -e 'vendor/*' -e 'assets/*' -e 'pkg/downwardmetrics/vhostmd/testdata/vhostmd0' |
grep '^<'; then
echo "Binary files are present in git repostory."
exit 1
Expand Down
4 changes: 4 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ var (
SysprepDisksDir = mountBaseDir + "/sysprep-disks"
// DownwardAPIDisksDir represents a path to DownwardAPI iso images
DownwardAPIDisksDir = mountBaseDir + "/downwardapi-disks"
// DownwardMetricDisksDir represents a path to DownwardMetric block disk
DownwardMetricDisksDir = mountBaseDir + "/downwardmetric-disk"
// DownwardMetricDisks represents the disk location for the DownwardMetric disk
DownwardMetricDisk = filepath.Join(DownwardAPIDisksDir, "vhostmd0")
// ServiceAccountDiskDir represents a path to the ServiceAccount iso image
ServiceAccountDiskDir = mountBaseDir + "/service-account-disk"
// ServiceAccountDiskName represents the name of the ServiceAccount iso image
Expand Down
13 changes: 13 additions & 0 deletions pkg/downwardmetrics/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "go_default_library",
srcs = ["downwardmetrics.go"],
importpath = "kubevirt.io/kubevirt/pkg/downwardmetrics",
visibility = ["//visibility:public"],
deps = [
"//pkg/config:go_default_library",
"//pkg/downwardmetrics/vhostmd:go_default_library",
"//staging/src/kubevirt.io/client-go/api/v1:go_default_library",
],
)
32 changes: 32 additions & 0 deletions pkg/downwardmetrics/downwardmetrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package downwardmetrics

import (
"path/filepath"
"strconv"

v1 "kubevirt.io/client-go/api/v1"
"kubevirt.io/kubevirt/pkg/config"
"kubevirt.io/kubevirt/pkg/downwardmetrics/vhostmd"
)

func CreateDownwardMetricDisk(vmi *v1.VirtualMachineInstance) error {
for _, volume := range vmi.Spec.Volumes {
if volume.DownwardMetrics != nil {
return vhostmd.NewMetricsIODisk(config.DownwardMetricDisk).Create()
}
}
return nil
}

func FormatDownwardMetricPath(pid int) string {
return filepath.Join("/proc", strconv.Itoa(pid), "/root", config.DownwardMetricDisk)
}

func HasDownwardMetricDisk(vmi *v1.VirtualMachineInstance) bool {
for _, volume := range vmi.Spec.Volumes {
if volume.DownwardMetrics != nil {
return true
}
}
return false
}
29 changes: 29 additions & 0 deletions pkg/downwardmetrics/vhostmd/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "go_default_library",
srcs = [
"disk.go",
"vhostmd.go",
],
importpath = "kubevirt.io/kubevirt/pkg/downwardmetrics/vhostmd",
visibility = ["//visibility:public"],
deps = ["//pkg/downwardmetrics/vhostmd/api:go_default_library"],
)

go_test(
name = "go_default_test",
srcs = [
"disk_test.go",
"vhostmd_suite_test.go",
],
data = glob(["testdata/**"]),
embed = [":go_default_library"],
deps = [
"//pkg/downwardmetrics/vhostmd/api:go_default_library",
"//pkg/downwardmetrics/vhostmd/metrics:go_default_library",
"//staging/src/kubevirt.io/client-go/testutils:go_default_library",
"//vendor/github.com/onsi/ginkgo:go_default_library",
"//vendor/github.com/onsi/gomega:go_default_library",
],
)
8 changes: 8 additions & 0 deletions pkg/downwardmetrics/vhostmd/api/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "go_default_library",
srcs = ["api.go"],
importpath = "kubevirt.io/kubevirt/pkg/downwardmetrics/vhostmd/api",
visibility = ["//visibility:public"],
)
36 changes: 36 additions & 0 deletions pkg/downwardmetrics/vhostmd/api/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package api

import "encoding/xml"

type MetricContext string
type MetricType string

const (
MetricContextHost MetricContext = "host"
MetricContextVM MetricContext = "vm"
)

const (
MetricTypeReal64 MetricType = "real64"
MetricTypeReal32 MetricType = "real32"
MetricTypeInt64 MetricType = "int64"
MetricTypeInt32 MetricType = "int32"
MetricTypeUInt64 MetricType = "uint64"
MetricTypeUInt32 MetricType = "uint32"
MetricTypeString MetricType = "string"
)

type Metrics struct {
XMLName xml.Name `xml:"metrics"`
Text string `xml:",chardata"`
Metrics []Metric `xml:"metric"`
}

type Metric struct {
Text string `xml:",chardata"`
Type MetricType `xml:"type,attr"`
Context MetricContext `xml:"context,attr"`
Name string `xml:"name"`
Value string `xml:"value"`
Unit string `xml:"unit,attr,omitempty"`
}