-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Parsing GPU tags on kubeapiserver collector
- Loading branch information
Showing
5 changed files
with
112 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
// 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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} |