mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-13 13:55:41 +00:00
Move CDI annotation code to utils package
Signed-off-by: Evan Lezar <elezar@nvidia.com>
This commit is contained in:
parent
2d742bb8ab
commit
f0e3c32fe5
@ -23,6 +23,7 @@ import (
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
"k8s.io/apimachinery/pkg/util/sets"
|
||||
"k8s.io/kubernetes/pkg/kubelet/cm/dra/state"
|
||||
"k8s.io/kubernetes/pkg/kubelet/cm/util/cdi"
|
||||
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
|
||||
)
|
||||
|
||||
@ -57,7 +58,7 @@ func (info *ClaimInfo) addCDIDevices(pluginName string, cdiDevices []string) err
|
||||
// NOTE: Passing CDI device names as annotations is a temporary solution
|
||||
// It will be removed after all runtimes are updated
|
||||
// to get CDI device names from the ContainerConfig.CDIDevices field
|
||||
annotations, err := generateCDIAnnotations(info.ClaimUID, info.DriverName, cdiDevices)
|
||||
annotations, err := cdi.GenerateAnnotations(info.ClaimUID, info.DriverName, cdiDevices)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to generate container annotations, err: %+v", err)
|
||||
}
|
||||
|
@ -23,7 +23,7 @@ limitations under the License.
|
||||
// Long term it would be good to avoid this duplication:
|
||||
// https://github.com/container-orchestrated-devices/container-device-interface/issues/97
|
||||
|
||||
package dra
|
||||
package cdi
|
||||
|
||||
import (
|
||||
"errors"
|
||||
@ -39,8 +39,8 @@ const (
|
||||
annotationPrefix = "cdi.k8s.io/"
|
||||
)
|
||||
|
||||
// generate container annotations using CDI UpdateAnnotations API.
|
||||
func generateCDIAnnotations(
|
||||
// GenerateAnnotations generate container annotations using CDI UpdateAnnotations API.
|
||||
func GenerateAnnotations(
|
||||
claimUID types.UID,
|
||||
driverName string,
|
||||
cdiDevices []string,
|
52
pkg/kubelet/cm/util/cdi/cdi_test.go
Normal file
52
pkg/kubelet/cm/util/cdi/cdi_test.go
Normal file
@ -0,0 +1,52 @@
|
||||
/*
|
||||
Copyright 2023 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package cdi
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"k8s.io/kubernetes/pkg/kubelet/container"
|
||||
)
|
||||
|
||||
func TestGenerateAnnotations(t *testing.T) {
|
||||
testCases := []struct {
|
||||
description string
|
||||
deviceIDs []string
|
||||
expecteError error
|
||||
expectedAnnotations []container.Annotation
|
||||
}{
|
||||
{
|
||||
description: "no devices",
|
||||
deviceIDs: []string{},
|
||||
},
|
||||
{
|
||||
description: "one device",
|
||||
deviceIDs: []string{"vendor.com/class=device1"},
|
||||
expectedAnnotations: []container.Annotation{{Name: "cdi.k8s.io/test-driver-name_test-claim-uid", Value: "vendor.com/class=device1"}},
|
||||
},
|
||||
}
|
||||
|
||||
as := assert.New(t)
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.description, func(t *testing.T) {
|
||||
annotations, err := GenerateAnnotations("test-claim-uid", "test-driver-name", tc.deviceIDs)
|
||||
as.ErrorIs(err, tc.expecteError)
|
||||
as.Equal(tc.expectedAnnotations, annotations)
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user