-
Notifications
You must be signed in to change notification settings - Fork 640
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
Add support for CDI devices to device flag #2280
Open
elezar
wants to merge
3
commits into
containerd:main
Choose a base branch
from
elezar:add-cdi-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,7 @@ | ||
cdiVersion: "0.3.0" | ||
kind: "vendor1.com/device" | ||
devices: | ||
- name: foo | ||
containerEdits: | ||
env: | ||
- FOO=injected |
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
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,64 @@ | ||
/* | ||
Copyright The containerd 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 container | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/containerd/containerd/containers" | ||
"github.com/containerd/containerd/oci" | ||
"github.com/containerd/log" | ||
"tags.cncf.io/container-device-interface/pkg/cdi" | ||
) | ||
|
||
// withCDIDevices creates the OCI runtime spec options for injecting CDI devices. | ||
// Two options are returned: The first ensures that the CDI registry is initialized with | ||
// refresh disabled, and the second injects the devices into the container. | ||
func withCDIDevices(cdiSpecDirs []string, devices ...string) oci.SpecOpts { | ||
return func(ctx context.Context, _ oci.Client, c *containers.Container, s *oci.Spec) error { | ||
if len(devices) == 0 { | ||
return nil | ||
} | ||
|
||
// We configure the CDI registry with the configured spec dirs and disable refresh. | ||
cdi.Configure( | ||
cdi.WithSpecDirs(cdiSpecDirs...), | ||
cdi.WithAutoRefresh(false), | ||
) | ||
|
||
// TODO: Call oci.WithCDIDevices(devices...) here once the dependencies have been updated. | ||
if err := cdi.Refresh(); err != nil { | ||
// We don't consider registry refresh failure a fatal error. | ||
// For instance, a dynamically generated invalid CDI Spec file for | ||
// any particular vendor shouldn't prevent injection of devices of | ||
// different vendors. CDI itself knows better and it will fail the | ||
// injection if necessary. | ||
log.G(ctx).Warnf("CDI registry refresh failed: %v", err) | ||
} | ||
|
||
if _, err := cdi.InjectDevices(s, devices...); err != nil { | ||
return fmt.Errorf("CDI device injection failed: %w", err) | ||
} | ||
|
||
// One crucial thing to keep in mind is that CDI device injection | ||
// might add OCI Spec environment variables, hooks, and mounts as | ||
// well. Therefore it is important that none of the corresponding | ||
// OCI Spec fields are reset up in the call stack once we return. | ||
return nil | ||
} | ||
} |
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 |
---|---|---|
|
@@ -41,6 +41,8 @@ type Config struct { | |
HostGatewayIP string `toml:"host_gateway_ip"` | ||
BridgeIP string `toml:"bridge_ip, omitempty"` | ||
KubeHideDupe bool `toml:"kube_hide_dupe"` | ||
// CDISpecDirs is a list of directories in which CDI specifications can be found. | ||
CDISpecDirs []string `toml:"cdi_spec_dirs,omitempty"` | ||
} | ||
|
||
// New creates a default Config object statically, | ||
|
@@ -61,5 +63,6 @@ func New() *Config { | |
Experimental: true, | ||
HostGatewayIP: ncdefaults.HostGatewayIP(), | ||
KubeHideDupe: false, | ||
CDISpecDirs: []string{"/etc/cdi", "/var/run/cdi"}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should respect XDG dirs for rootless |
||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bike shedding: "Dirs" vs "Path"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"CDIPath" might be more consistent with the existing "CNIPath"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The point is that these are specifically directories and not files. Furthermore, the naming is consistent with this setting in other projects: containerd, cri-o, docker, singularity.
I'm happy to change if you feel strongly about it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍