Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
39 changes: 1 addition & 38 deletions api/example.com/resource/gpu/v1alpha1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,9 @@ import (
"fmt"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer/json"
)

const (
GroupName = "gpu.resource.example.com"
Version = "v1alpha1"

GpuConfigKind = "GpuConfig"
)

// Decoder implements a decoder for objects in this API group.
var Decoder runtime.Decoder
const GpuConfigKind = "GpuConfig"

// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
Expand Down Expand Up @@ -82,29 +71,3 @@ func (c *GpuConfig) Normalize() error {
}
return nil
}

func init() {
// Create a new scheme and add our types to it. If at some point in the
// future a new version of the configuration API becomes necessary, then
// conversion functions can be generated and registered to continue
// supporting older versions.
scheme := runtime.NewScheme()
schemeGroupVersion := schema.GroupVersion{
Group: GroupName,
Version: Version,
}
scheme.AddKnownTypes(schemeGroupVersion,
&GpuConfig{},
)
metav1.AddToGroupVersion(scheme, schemeGroupVersion)

// Set up a json serializer to decode our types.
Decoder = json.NewSerializerWithOptions(
json.DefaultMetaFactory,
scheme,
scheme,
json.SerializerOptions{
Pretty: true, Strict: true,
},
)
}
45 changes: 45 additions & 0 deletions api/example.com/resource/gpu/v1alpha1/register.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 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 v1alpha1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
)

const (
GroupName = "gpu.resource.example.com"
Version = "v1alpha1"
)

// SchemeGroupVersion is group version used to register these objects.
var SchemeGroupVersion = schema.GroupVersion{Group: GroupName, Version: Version}

var (
SchemeBuilder = runtime.NewSchemeBuilder(addKnownTypes)
AddToScheme = SchemeBuilder.AddToScheme
)

// Adds the list of known types to the given scheme.
func addKnownTypes(scheme *runtime.Scheme) error {
scheme.AddKnownTypes(SchemeGroupVersion,
&GpuConfig{},
)
metav1.AddToGroupVersion(scheme, SchemeGroupVersion)
return nil
}
53 changes: 32 additions & 21 deletions cmd/dra-example-kubeletplugin/cdi.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,50 +19,52 @@ package main
import (
"fmt"
"os"

"sigs.k8s.io/dra-example-driver/pkg/consts"
"regexp"
"strings"

cdiapi "tags.cncf.io/container-device-interface/pkg/cdi"
cdiparser "tags.cncf.io/container-device-interface/pkg/parser"
cdispec "tags.cncf.io/container-device-interface/specs-go"

"sigs.k8s.io/dra-example-driver/internal/profiles"
)

const (
cdiVendor = "k8s." + consts.DriverName
cdiClass = "gpu"
cdiKind = cdiVendor + "/" + cdiClass
const cdiCommonDeviceName = "common"

cdiCommonDeviceName = "common"
)
var nonWord = regexp.MustCompile(`[^a-zA-Z0-9]+`)

type CDIHandler struct {
cache *cdiapi.Cache
cache *cdiapi.Cache
driverName string
class string
}

func NewCDIHandler(config *Config) (*CDIHandler, error) {
func NewCDIHandler(root string, driverName, class string) (*CDIHandler, error) {
cache, err := cdiapi.NewCache(
cdiapi.WithSpecDirs(config.flags.cdiRoot),
cdiapi.WithSpecDirs(root),
)
if err != nil {
return nil, fmt.Errorf("unable to create a new CDI cache: %w", err)
}
handler := &CDIHandler{
cache: cache,
cache: cache,
driverName: driverName,
class: class,
}

return handler, nil
}

func (cdi *CDIHandler) CreateCommonSpecFile() error {
spec := &cdispec.Spec{
Kind: cdiKind,
Kind: cdi.kind(),
Devices: []cdispec.Device{
{
Name: cdiCommonDeviceName,
ContainerEdits: cdispec.ContainerEdits{
Env: []string{
fmt.Sprintf("KUBERNETES_NODE_NAME=%s", os.Getenv("NODE_NAME")),
fmt.Sprintf("DRA_RESOURCE_DRIVER_NAME=%s", consts.DriverName),
fmt.Sprintf("DRA_RESOURCE_DRIVER_NAME=%s", cdi.driverName),
},
},
},
Expand All @@ -83,19 +85,20 @@ func (cdi *CDIHandler) CreateCommonSpecFile() error {
return cdi.cache.WriteSpec(spec, specName)
}

func (cdi *CDIHandler) CreateClaimSpecFile(claimUID string, devices PreparedDevices) error {
specName := cdiapi.GenerateTransientSpecName(cdiVendor, cdiClass, claimUID)
func (cdi *CDIHandler) CreateClaimSpecFile(claimUID string, devices profiles.PreparedDevices) error {
specName := cdiapi.GenerateTransientSpecName(cdi.vendor(), cdi.class, claimUID)

spec := &cdispec.Spec{
Kind: cdiKind,
Kind: cdi.kind(),
Devices: []cdispec.Device{},
}

for _, device := range devices {
deviceEnvKey := strings.ToUpper(nonWord.ReplaceAllString(device.DeviceName, "_"))
claimEdits := cdiapi.ContainerEdits{
ContainerEdits: &cdispec.ContainerEdits{
Env: []string{
fmt.Sprintf("GPU_DEVICE_%s_RESOURCE_CLAIM=%s", device.DeviceName[4:], claimUID),
fmt.Sprintf("%s_DEVICE_%s_RESOURCE_CLAIM=%s", strings.ToUpper(cdi.class), deviceEnvKey, claimUID),
},
},
}
Expand All @@ -119,19 +122,27 @@ func (cdi *CDIHandler) CreateClaimSpecFile(claimUID string, devices PreparedDevi
}

func (cdi *CDIHandler) DeleteClaimSpecFile(claimUID string) error {
specName := cdiapi.GenerateTransientSpecName(cdiVendor, cdiClass, claimUID)
specName := cdiapi.GenerateTransientSpecName(cdi.vendor(), cdi.class, claimUID)
return cdi.cache.RemoveSpec(specName)
}

func (cdi *CDIHandler) GetClaimDevices(claimUID string, devices []string) []string {
cdiDevices := []string{
cdiparser.QualifiedName(cdiVendor, cdiClass, cdiCommonDeviceName),
cdiparser.QualifiedName(cdi.vendor(), cdi.class, cdiCommonDeviceName),
}

for _, device := range devices {
cdiDevice := cdiparser.QualifiedName(cdiVendor, cdiClass, fmt.Sprintf("%s-%s", claimUID, device))
cdiDevice := cdiparser.QualifiedName(cdi.vendor(), cdi.class, fmt.Sprintf("%s-%s", claimUID, device))
cdiDevices = append(cdiDevices, cdiDevice)
}

return cdiDevices
}

func (cdi *CDIHandler) kind() string {
return cdi.vendor() + "/" + cdi.class
}

func (cdi *CDIHandler) vendor() string {
return "k8s." + cdi.driverName
}
84 changes: 0 additions & 84 deletions cmd/dra-example-kubeletplugin/discovery.go

This file was deleted.

24 changes: 2 additions & 22 deletions cmd/dra-example-kubeletplugin/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,13 @@ import (
"context"
"errors"
"fmt"
"maps"

resourceapi "k8s.io/api/resource/v1"
"k8s.io/apimachinery/pkg/types"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
coreclientset "k8s.io/client-go/kubernetes"
"k8s.io/dynamic-resource-allocation/kubeletplugin"
"k8s.io/dynamic-resource-allocation/resourceslice"
"k8s.io/klog/v2"

"sigs.k8s.io/dra-example-driver/pkg/consts"
)

type driver struct {
Expand Down Expand Up @@ -58,7 +54,7 @@ func NewDriver(ctx context.Context, config *Config) (*driver, error) {
driver,
kubeletplugin.KubeClient(config.coreclient),
kubeletplugin.NodeName(config.flags.nodeName),
kubeletplugin.DriverName(consts.DriverName),
kubeletplugin.DriverName(config.flags.driverName),
kubeletplugin.RegistrarDirectoryPath(config.flags.kubeletRegistrarDirectoryPath),
kubeletplugin.PluginDataDirectoryPath(config.DriverPluginPath()),
)
Expand All @@ -67,28 +63,12 @@ func NewDriver(ctx context.Context, config *Config) (*driver, error) {
}
driver.helper = helper

devices := make([]resourceapi.Device, 0, len(state.allocatable))
for device := range maps.Values(state.allocatable) {
devices = append(devices, device)
}
resources := resourceslice.DriverResources{
Pools: map[string]resourceslice.Pool{
config.flags.nodeName: {
Slices: []resourceslice.Slice{
{
Devices: devices,
},
},
},
},
}

driver.healthcheck, err = startHealthcheck(ctx, config)
if err != nil {
return nil, fmt.Errorf("start healthcheck: %w", err)
}

if err := helper.PublishResources(ctx, resources); err != nil {
if err := helper.PublishResources(ctx, state.driverResources); err != nil {
return nil, err
}

Expand Down
4 changes: 1 addition & 3 deletions cmd/dra-example-kubeletplugin/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ import (
"k8s.io/klog/v2"
drapb "k8s.io/kubelet/pkg/apis/dra/v1"
registerapi "k8s.io/kubelet/pkg/apis/pluginregistration/v1"

"sigs.k8s.io/dra-example-driver/pkg/consts"
)

type healthcheck struct {
Expand Down Expand Up @@ -65,7 +63,7 @@ func startHealthcheck(ctx context.Context, config *Config) (*healthcheck, error)
Scheme: "unix",
// TODO: this needs to adapt when seamless upgrades
// are enabled and the filename includes a uid.
Path: path.Join(config.flags.kubeletRegistrarDirectoryPath, consts.DriverName+"-reg.sock"),
Path: path.Join(config.flags.kubeletRegistrarDirectoryPath, config.flags.driverName+"-reg.sock"),
}).String()
log.Info("connecting to registration socket", "path", regSockPath)
regConn, err := grpc.NewClient(
Expand Down
Loading
Loading