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

PCI DRA driver #1

Merged
merged 1 commit into from
Aug 21, 2024
Merged
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
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Ignore .cache directory
.cache/

# Ignore vendor directory
vendor/

# Ignore swap files
[._]*.sw[a-p]

# Ignore .bak and .idea directories
.bak/
.idea/
.del/
174 changes: 174 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
# Copyright 2024 The KubeVirt 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.

RUNTIME ?= docker
MKDIR ?= mkdir
TR ?= tr
DIST_DIR ?= $(CURDIR)/dist

include $(CURDIR)/common.mk

BUILDIMAGE_TAG ?= golang$(GOLANG_VERSION)
BUILDIMAGE ?= $(IMAGE_NAME)-build:$(BUILDIMAGE_TAG)

CMDS := $(patsubst ./cmd/%/,%,$(sort $(dir $(wildcard ./cmd/*/))))
CMD_TARGETS := $(patsubst %,cmd-%, $(CMDS))

CHECK_TARGETS := assert-fmt vet lint ineffassign misspell
MAKE_TARGETS := binaries build check vendor fmt test examples cmds coverage generate $(CHECK_TARGETS)

TARGETS := $(MAKE_TARGETS) $(CMD_TARGETS)

RUNTIME_TARGETS := $(patsubst %,$(RUNTIME)-%, $(TARGETS))
.PHONY: $(TARGETS) $(RUNTIME_TARGETS)

GOOS ?= linux

binaries: cmds
ifneq ($(PREFIX),)
cmd-%: COMMAND_BUILD_OPTIONS = -o $(PREFIX)/$(*)
endif
cmds: $(CMD_TARGETS)
$(CMD_TARGETS): cmd-%:
CGO_LDFLAGS_ALLOW='-Wl,--unresolved-symbols=ignore-in-object-files' GOOS=$(GOOS) \
go build -ldflags "-s -w -X main.version=$(VERSION)" $(COMMAND_BUILD_OPTIONS) $(MODULE)/cmd/$(*)

build:
GOOS=$(GOOS) go build ./...

examples: $(EXAMPLE_TARGETS)
$(EXAMPLE_TARGETS): example-%:
GOOS=$(GOOS) go build ./examples/$(*)

all: check test build binary
check: $(CHECK_TARGETS)

# Update the vendor folder
vendor:
go mod vendor

# Apply go fmt to the codebase
fmt:
go list -f '{{.Dir}}' $(MODULE)/... \
| xargs gofmt -s -l -w

assert-fmt:
go list -f '{{.Dir}}' $(MODULE)/... \
| xargs gofmt -s -l > fmt.out
@if [ -s fmt.out ]; then \
echo "\nERROR: The following files are not formatted:\n"; \
cat fmt.out; \
rm fmt.out; \
exit 1; \
else \
rm fmt.out; \
fi

ineffassign:
ineffassign $(MODULE)/...

lint:
golangci-lint run ./...

misspell:
misspell $(MODULE)/...

vet:
go vet $(MODULE)/...

# Ensure that all log calls support contextual logging.
test: logcheck
.PHONY: logcheck
logcheck:
(cd hack/tools && GOBIN=$(PWD) go install sigs.k8s.io/logtools/logcheck)
./logcheck -check-contextual -check-deprecations ./...

COVERAGE_FILE := coverage.out
test: build cmds
go test -v -coverprofile=$(COVERAGE_FILE) $(MODULE)/...

coverage: test
cat $(COVERAGE_FILE) | grep -v "_mock.go" > $(COVERAGE_FILE).no-mocks
go tool cover -func=$(COVERAGE_FILE).no-mocks

generate: generate-clientset

generate-clientset: generate-crds
mkdir -p $(CURDIR)/pkg/$(VENDOR)/resource
rm -rf $(CURDIR)/pkg/$(VENDOR)/resource/clientset
client-gen \
--go-header-file=$(CURDIR)/hack/boilerplate.go.txt \
--clientset-name "versioned" \
--build-tag "ignore_autogenerated" \
--output-package "$(MODULE)/pkg/$(VENDOR)/resource/clientset" \
--input-base "$(MODULE)/api/$(VENDOR)/resource" \
--output-base "$(CURDIR)/pkg/tmp_clientset" \
--input "$(shell echo $(APIS) | tr ' ' ',')" \
--plural-exceptions "$(shell echo $(PLURAL_EXCEPTIONS) | tr ' ' ',')"
mv $(CURDIR)/pkg/tmp_clientset/$(MODULE)/pkg/$(VENDOR)/resource/clientset \
$(CURDIR)/pkg/$(VENDOR)/resource/clientset
rm -rf $(CURDIR)/pkg/tmp_clientset

generate-crds: vendor
rm -rf $(CURDIR)/deployments/pci/static/$(DRIVER_NAME)/crds
for api in $(APIS); do \
rm -f $(CURDIR)/api/$(VENDOR)/resource/$${api}/zz_generated.deepcopy.go; \
controller-gen \
object:headerFile=$(CURDIR)/hack/boilerplate.go.txt,year=$(shell date +"%Y") \
paths=$(CURDIR)/api/$(VENDOR)/resource/$${api}/ \
output:object:dir=$(CURDIR)/api/$(VENDOR)/resource/$${api}; \
controller-gen crd:crdVersions=v1 \
paths=$(CURDIR)/api/$(VENDOR)/resource/$${api}/ \
output:crd:dir=$(CURDIR)/deployments/native/$(DRIVER_NAME)/crds; \
done

# Generate an image for containerized builds
# Note: This image is local only
.PHONY: .build-image
.build-image: docker/Dockerfile.devel
if [ x"$(SKIP_IMAGE_BUILD)" = x"" ]; then \
$(RUNTIME) build \
--progress=plain \
--build-arg GOLANG_VERSION="$(GOLANG_VERSION)" \
--tag $(BUILDIMAGE) \
-f $(^) \
docker; \
fi

$(RUNTIME_TARGETS): docker-%: .build-image
@echo "Running 'make $(*)' in docker container $(BUILDIMAGE)"
$(RUNTIME) run \
--rm \
-e HOME=$(PWD) \
-e GOCACHE=$(PWD)/.cache/go \
-e GOPATH=$(PWD)/.cache/gopath \
-v $(PWD):$(PWD) \
-w $(PWD) \
--user $$(id -u):$$(id -g) \
$(BUILDIMAGE) \
make $(*)

# Start an interactive shell using the development image.
PHONY: .shell
.shell:
$(RUNTIME) run \
--rm \
-ti \
-e HOME=$(PWD) \
-e GOCACHE=$(PWD)/.cache/go \
-e GOPATH=$(PWD)/.cache/gopath \
-v $(PWD):$(PWD) \
-w $(PWD) \
--user $$(id -u):$$(id -g) \
$(BUILDIMAGE)
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# KubeVirt DRA PCI Driver

<p align="center">
<img src="https://github.com/kubevirt/community/raw/main/logo/KubeVirt_icon.png" width="100">
</p>

This repository contains a Dynamic Resource Allocation (DRA) device driver for PCI passthrough devices to be used with KubeVirt.

## About the Resource Driver

The KubeVirt DRA Driver is designed as an alternative to the device-plugin framework-based host device management in KubeVirt's `virt-handler`. It provides better control over devices on KubeVirt VMs by leveraging the [Dynamic Resource Allocation (DRA)](https://kubernetes.io/docs/concepts/scheduling-eviction/dynamic-resource-allocation/) framework in Kubernetes.

### Prerequisites

* [GNU Make 3.81+](https://www.gnu.org/software/make/)
* [GNU Tar 1.34+](https://www.gnu.org/software/tar/)
* [Docker v20.10+ (including buildx)](https://docs.docker.com/engine/install/)

### Documentation

- [Install and Use KubeVirt with the DRA PCI Driver](doc/KV_SETUP.md)
- [How to Change and Rebuild the Driver](doc/BUILD.md)

### Prior Art

- [kubernetes-sigs/dra-example-driver](https://github.com/kubernetes-sigs/dra-example-driver) provides a practical implementation of how a resource entity should be handled to ensure proper scheduling of Pods according to resources present in a Node.
- [Device Manager Implementation](https://github.com/kubevirt/kubevirt/tree/main/pkg/virt-handler/device-manager) from KubeVirt provides an implementation of handling several devices, including PCI devices for VMIs, using the device-plugin framework as part of `virt-handler`.
53 changes: 53 additions & 0 deletions api/kubevirt.io/resource/pci/nas/v1alpha1/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2024 The KubeVirt 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"
)

const (
GroupName = "nas.pci.resource.kubevirt.io"
Version = "v1alpha1"

PciDeviceType = "pci"
UnknownDeviceType = "unknown"

NodeAllocationStateStatusReady = "Ready"
NodeAllocationStateStatusNotReady = "NotReady"
)

type NodeAllocationStateConfig struct {
Name string
Namespace string
Owner *metav1.OwnerReference
}

func NewNodeAllocationState(config *NodeAllocationStateConfig) *NodeAllocationState {
nascrd := &NodeAllocationState{
ObjectMeta: metav1.ObjectMeta{
Name: config.Name,
Namespace: config.Namespace,
},
}

if config.Owner != nil {
nascrd.OwnerReferences = []metav1.OwnerReference{*config.Owner}
}

return nascrd
}
101 changes: 101 additions & 0 deletions api/kubevirt.io/resource/pci/nas/v1alpha1/client/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Copyright 2024 The KubeVirt 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 client

import (
"context"

"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

nascrd "kubevirt.io/dra-pci-driver/api/kubevirt.io/resource/pci/nas/v1alpha1"
nasclient "kubevirt.io/dra-pci-driver/pkg/kubevirt.io/resource/clientset/versioned/typed/nas/v1alpha1"
)

type Client struct {
nas *nascrd.NodeAllocationState
client nasclient.NasV1alpha1Interface
}

func New(nas *nascrd.NodeAllocationState, client nasclient.NasV1alpha1Interface) *Client {
return &Client{
nas,
client,
}
}

func (c *Client) GetOrCreate(ctx context.Context) error {
err := c.Get(ctx)
if err == nil {
return nil
}
if errors.IsNotFound(err) {
return c.Create(ctx)
}
return err
}

func (c *Client) Create(ctx context.Context) error {
crd := c.nas.DeepCopy()
crd, err := c.client.NodeAllocationStates(c.nas.Namespace).Create(ctx, crd, metav1.CreateOptions{})
if err != nil {
return err
}
*c.nas = *crd
return nil
}

func (c *Client) Delete(ctx context.Context) error {
deletePolicy := metav1.DeletePropagationForeground
deleteOptions := metav1.DeleteOptions{PropagationPolicy: &deletePolicy}
err := c.client.NodeAllocationStates(c.nas.Namespace).Delete(ctx, c.nas.Name, deleteOptions)
if err != nil && !errors.IsNotFound(err) {
return err
}
return nil
}

func (c *Client) Update(ctx context.Context, spec *nascrd.NodeAllocationStateSpec) error {
crd := c.nas.DeepCopy()
crd.Spec = *spec
crd, err := c.client.NodeAllocationStates(c.nas.Namespace).Update(ctx, crd, metav1.UpdateOptions{})
if err != nil {
return err
}
*c.nas = *crd
return nil
}

func (c *Client) UpdateStatus(ctx context.Context, status string) error {
crd := c.nas.DeepCopy()
crd.Status = status
crd, err := c.client.NodeAllocationStates(c.nas.Namespace).Update(ctx, crd, metav1.UpdateOptions{})
if err != nil {
return err
}
*c.nas = *crd
return nil
}

func (c *Client) Get(ctx context.Context) error {
crd, err := c.client.NodeAllocationStates(c.nas.Namespace).Get(ctx, c.nas.Name, metav1.GetOptions{})
if err != nil {
return err
}
*c.nas = *crd
return nil
}
20 changes: 20 additions & 0 deletions api/kubevirt.io/resource/pci/nas/v1alpha1/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright 2024 The KubeVirt 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.
*/

// +k8s:deepcopy-gen=package
// +groupName=nas.pci.resource.kubevirt.io

package v1alpha1
Loading