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

[CONTP-499] Parsing GPU tags on kubeapiserver collector #31465

Merged
merged 6 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 6 additions & 30 deletions comp/core/workloadmeta/collectors/internal/kubelet/kubelet.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/DataDog/datadog-agent/pkg/errors"
"github.com/DataDog/datadog-agent/pkg/util/containers"
pkgcontainersimage "github.com/DataDog/datadog-agent/pkg/util/containers/image"
"github.com/DataDog/datadog-agent/pkg/util/gpu"
"github.com/DataDog/datadog-agent/pkg/util/kubernetes"
"github.com/DataDog/datadog-agent/pkg/util/kubernetes/kubelet"
"github.com/DataDog/datadog-agent/pkg/util/log"
Expand Down Expand Up @@ -418,21 +419,6 @@ func extractEnvFromSpec(envSpec []kubelet.EnvVar) map[string]string {
return env
}

func extractGPUVendor(gpuNamePrefix kubelet.ResourceName) string {
gpuVendor := ""
switch gpuNamePrefix {
case kubelet.ResourcePrefixNvidiaMIG, kubelet.ResourceGenericNvidiaGPU:
gpuVendor = "nvidia"
case kubelet.ResourcePrefixAMDGPU:
gpuVendor = "amd"
case kubelet.ResourcePrefixIntelGPU:
gpuVendor = "intel"
default:
gpuVendor = string(gpuNamePrefix)
}
return gpuVendor
}

func extractResources(spec *kubelet.ContainerSpec) workloadmeta.ContainerResources {
resources := workloadmeta.ContainerResources{}
if cpuReq, found := spec.Resources.Requests[kubelet.ResourceCPU]; found {
Expand All @@ -444,24 +430,14 @@ func extractResources(spec *kubelet.ContainerSpec) workloadmeta.ContainerResourc
}

// extract GPU resource info from the possible GPU sources
uniqueGPUVendor := make(map[string]bool)

resourceKeys := make([]kubelet.ResourceName, 0, len(spec.Resources.Requests))
uniqueGPUVendor := make(map[string]struct{})
for resourceName := range spec.Resources.Requests {
resourceKeys = append(resourceKeys, resourceName)
}

for _, gpuResourceName := range kubelet.GetGPUResourceNames() {
for _, resourceKey := range resourceKeys {
if strings.HasPrefix(string(resourceKey), string(gpuResourceName)) {
if gpuReq, found := spec.Resources.Requests[resourceKey]; found {
resources.GPURequest = pointer.Ptr(uint64(gpuReq.Value()))
uniqueGPUVendor[extractGPUVendor(gpuResourceName)] = true
break
}
}
gpuName, found := gpu.ExtractSimpleGPUName(gpu.ResourceGPU(resourceName))
if found {
uniqueGPUVendor[gpuName] = struct{}{}
}
}

Comment on lines +433 to +440
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we wrap this logic in one function to be reused in comp/core/workloadmeta/collectors/util/kubernetes_resource_parsers/pod.go ?
I feel like the code is very similar

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tried implementing a function such as the following:

func GetGPUResourcesFromResourceList[T corev1.ResourceList | kubelet.ResourceList](gpuSet *map[string]struct{}, resourceList T) {
	for resourceName := range resourceList {
		gpuName, found := ExtractSimpleGPUName(ResourceGPU(resourceName))
		if found {
			(*gpuSet)[gpuName] = struct{}{}
		}
	}
}

However, I get an error stating "cannot range over resourceList: no core type". It seems that Go's type system cannot infer the underlying map structure directly from T corev1.ResourceList | kubelet.ResourceList.
I've tried directly setting it to [T ~map[corev1.ResourceName]resource.Quantity | ~map[kubelet.ResourceName]resource.Quantity] and get the same issue. It's interesting that corev1.ResourceName and kubelet.ResourceName are both simply strings, so these types are essentially the same. I tried type casting the input map[string]resource.Quantity(container.Resources.Requests) but it doesn't allow me either.

I can't think of a lightweight way for wrapping this logic into one function that can be reused. Do you have any ideas? It would be very appreciated. Thanks!

gpuVendorList := make([]string, 0, len(uniqueGPUVendor))
gabedos marked this conversation as resolved.
Show resolved Hide resolved
for GPUVendor := range uniqueGPUVendor {
gpuVendorList = append(gpuVendorList, GPUVendor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
corev1 "k8s.io/api/core/v1"

workloadmeta "github.com/DataDog/datadog-agent/comp/core/workloadmeta/def"
"github.com/DataDog/datadog-agent/pkg/util/gpu"
)

type podParser struct {
Expand Down Expand Up @@ -62,6 +63,20 @@ func (p podParser) Parse(obj interface{}) workloadmeta.Entity {
rtcName = *pod.Spec.RuntimeClassName
}

var gpuVendorList []string
uniqueGPUVendor := make(map[string]struct{})
for _, container := range pod.Spec.Containers {
for resourceName := range container.Resources.Limits {
gpuName, found := gpu.ExtractSimpleGPUName(gpu.ResourceGPU(resourceName))
if found {
uniqueGPUVendor[gpuName] = struct{}{}
}
}
}
for gpuVendor := range uniqueGPUVendor {
gpuVendorList = append(gpuVendorList, gpuVendor)
}
gabedos marked this conversation as resolved.
Show resolved Hide resolved

return &workloadmeta.KubernetesPod{
EntityID: workloadmeta.EntityID{
Kind: workloadmeta.KindKubernetesPod,
Expand All @@ -81,6 +96,7 @@ func (p podParser) Parse(obj interface{}) workloadmeta.Entity {
PriorityClass: pod.Spec.PriorityClassName,
QOSClass: string(pod.Status.QOSClass),
RuntimeClass: rtcName,
GPUVendorList: gpuVendorList,

// Containers could be generated by this collector, but
// currently it's not to save on memory, since this is supposed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

workloadmeta "github.com/DataDog/datadog-agent/comp/core/workloadmeta/def"
Expand Down Expand Up @@ -54,6 +55,28 @@ func TestPodParser_Parse(t *testing.T) {
},
},
},
Containers: []corev1.Container{
{
Name: "gpuContainer1",
Resources: corev1.ResourceRequirements{
Limits: corev1.ResourceList{
"nvidia.com/gpu": resource.Quantity{
Format: "1",
},
},
},
},
{
Name: "gpuContainer2",
Resources: corev1.ResourceRequirements{
Limits: corev1.ResourceList{
"gpu.intel.com/xe": resource.Quantity{
Format: "2",
},
},
},
},
},
},
Status: corev1.PodStatus{
Phase: corev1.PodRunning,
Expand Down Expand Up @@ -97,6 +120,7 @@ func TestPodParser_Parse(t *testing.T) {
Ready: true,
IP: "127.0.0.1",
PriorityClass: "priorityClass",
GPUVendorList: []string{"nvidia", "intel"},
QOSClass: "Guaranteed",
}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ replace (
github.com/DataDog/datadog-agent/pkg/util/filesystem => ./pkg/util/filesystem
github.com/DataDog/datadog-agent/pkg/util/flavor => ./pkg/util/flavor
github.com/DataDog/datadog-agent/pkg/util/fxutil => ./pkg/util/fxutil/
github.com/DataDog/datadog-agent/pkg/util/gpu => ./pkg/util/gpu
gabedos marked this conversation as resolved.
Show resolved Hide resolved
github.com/DataDog/datadog-agent/pkg/util/grpc => ./pkg/util/grpc/
github.com/DataDog/datadog-agent/pkg/util/hostname/validate => ./pkg/util/hostname/validate/
github.com/DataDog/datadog-agent/pkg/util/http => ./pkg/util/http/
Expand Down
48 changes: 48 additions & 0 deletions pkg/util/gpu/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Unless explicitly stated otherwise all files in this repository are licensed
gabedos marked this conversation as resolved.
Show resolved Hide resolved
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.

// Package gpu provides utilities for interacting with GPU resources.
package gpu

import "strings"

// ResourceGPU represents a GPU resource
type ResourceGPU string

// Resource name prefixes
const (
gpuNvidiaGeneric ResourceGPU = "nvidia.com/gpu"
gpuAMD ResourceGPU = "amd.com/gpu"
gpuIntelXe ResourceGPU = "gpu.intel.com/xe"
gpuInteli915 ResourceGPU = "gpu.intel.com/i915"

gpuNvidiaMigPrefix ResourceGPU = "nvidia.com/mig"
)

// longToShortGPUName maps a GPU resource to a simplified name
var longToShortGPUName = map[ResourceGPU]string{
gpuNvidiaGeneric: "nvidia",
gpuAMD: "amd",
gpuIntelXe: "intel",
gpuInteli915: "intel",
}

// ExtractSimpleGPUName returns a simplified GPU name.
// If the resource is not recognized, the second return value is false.
func ExtractSimpleGPUName(gpuName ResourceGPU) (string, bool) {
val, ok := longToShortGPUName[gpuName]
if ok {
return val, true
}

// More complex cases (eg. nvidia.com/mig-3g.20gb => nvidia)
switch {
case strings.HasPrefix(string(gpuName), string(gpuNvidiaMigPrefix)):
return "nvidia", true
}

// Not a GPU resource (or not recognized)
return "", false
}
41 changes: 41 additions & 0 deletions pkg/util/gpu/common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.

package gpu

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestExtractSimpleGPUName(t *testing.T) {
tests := []struct {
name string
gpuName ResourceGPU
found bool
expected string
}{
{
name: "known gpu resource",
gpuName: gpuNvidiaGeneric,
found: true,
expected: "nvidia",
},
{
name: "unknown gpu resource",
gpuName: ResourceGPU("cpu"),
found: false,
expected: "",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
actual, found := ExtractSimpleGPUName(test.gpuName)
assert.Equal(t, test.found, found)
assert.Equal(t, test.expected, actual)
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Each section from every release note are combined when the
# CHANGELOG.rst is rendered. So the text needs to be worded so that
# it does not depend on any information only available in another
# section. This may mean repeating some details, but each section
# must be readable independently of the other.
#
# Each section note must be formatted as reStructuredText.
---
fixes:
- |
Include `gpu_vendor` pod tags on the Datadog Cluster Agent when
enabling datadog.clusterTagger.collectKubernetesTags.
Loading