Commit d7e13eb9 authored by songlinfeng's avatar songlinfeng
Browse files

add dtk-container-toolkit

parent fcdba4f3
/**
# Copyright (c) 2024, HCUOpt CORPORATION. All rights reserved.
**/
package runtime
import (
"dtk-container-toolkit/internal/config"
"dtk-container-toolkit/internal/config/image"
"dtk-container-toolkit/internal/info"
"dtk-container-toolkit/internal/logger"
"dtk-container-toolkit/internal/lookup/root"
"dtk-container-toolkit/internal/modifier"
"dtk-container-toolkit/internal/oci"
"fmt"
)
const (
loadHyhalMethodEnvvar = "DTK_LOAD_HYHAL_METHOD"
)
// newDTKContainerRuntime is a factory method that constructs a runtime based on the selected configuration and specified logger
func newDTKContainerRuntime(logger logger.Interface, cfg *config.Config, argv []string, driver *root.Driver) (oci.Runtime, error) {
lowLevelRuntime, err := oci.NewLowLevelRuntime(logger, cfg.DTKContainerRuntimeConfig.Runtimes)
if err != nil {
return nil, fmt.Errorf("error constructing low-level runtime: %v", err)
}
if !oci.HasCreateSubcommand(argv) {
logger.Debugf("Skipping modifier for non-create subcommand")
return lowLevelRuntime, nil
}
ociSpec, err := oci.NewSpec(logger, argv)
if err != nil {
return nil, fmt.Errorf("error constructing OCI specification: %v", err)
}
specModifier, err := newSpecModifier(logger, cfg, ociSpec, driver)
if err != nil {
return nil, fmt.Errorf("failed to construct OCI spec modifier: %v", err)
}
// Create the wrapping runtime with the specified modifier
r := oci.NewModifyingRuntimeWrapper(
logger,
lowLevelRuntime,
ociSpec,
specModifier,
)
return r, nil
}
// newSpecModifier is a factory method that creates constructs an OCI spec modifer based on the provided config.
func newSpecModifier(logger logger.Interface, cfg *config.Config, ociSpec oci.Spec, driver *root.Driver) (oci.SpecModifier, error) {
rawSpec, err := ociSpec.Load()
if err != nil {
return nil, fmt.Errorf("failed to load OCI spec: %v", err)
}
image, err := image.NewDTKImageFromSpec(rawSpec)
if err != nil {
return nil, err
}
mode := info.ResolveAutoMode(logger, cfg.DTKContainerRuntimeConfig.Mode, image)
// We update the mode here so that we can continue passing just the config to other functions.
cfg.DTKContainerRuntimeConfig.Mode = mode
modeModifier, err := newModeModifier(logger, mode, cfg, ociSpec, image)
if err != nil {
return nil, err
}
isMount := image.LoadHyhalMethod(loadHyhalMethodEnvvar)
var modifiers modifier.List
for _, modifierType := range supportedModifierTypes(mode) {
switch modifierType {
case "mode":
modifiers = append(modifiers, modeModifier)
case "graphics":
graphicsModifier, err := modifier.NewGraphicsModifier(logger, cfg, image, driver, isMount)
if err != nil {
return nil, err
}
modifiers = append(modifiers, graphicsModifier)
case "feature-gated":
featureGatedModifier, err := modifier.NewFeatureGatedModifier(logger, cfg, image, driver)
if err != nil {
return nil, err
}
modifiers = append(modifiers, featureGatedModifier)
}
}
var copyModifier oci.SpecModifier
if !isMount {
copyModifier, err = modifier.NewCopyModifier(logger)
if err != nil {
return nil, fmt.Errorf("failed to create copy modifier: %v", err)
}
modifiers = append(modifiers, copyModifier)
}
return modifiers, nil
}
func newModeModifier(logger logger.Interface, mode string, cfg *config.Config, ociSpec oci.Spec, image image.DTK) (oci.SpecModifier, error) {
switch mode {
case "legacy":
return modifier.NewStableModifier(logger, cfg, image)
case "cdi":
return modifier.NewCDIModifier(logger, cfg, ociSpec)
}
return nil, fmt.Errorf("invalid runtime mode: %v", cfg.DTKContainerRuntimeConfig.Mode)
}
// supportedModifierTypes returns the modifiers supported for a specific runtime mode.
func supportedModifierTypes(mode string) []string {
switch mode {
case "cdi":
// For CDI mode we make no additional modifications.
return []string{"mode"}
default:
return []string{"feature-gated", "graphics", "mode"}
}
}
// newStable
Source: dtk-container-toolkit
Section: @SECTION@utils
Priority: optional
Maintainer: DTK HCU Opt Devops
Standards-Version: 1.1.0
Build-Depends: debhelper (>= 9)
Package: dtk-container-toolkit
Architecture: any
Description: DTK Container toolkit
Provides tools and utilities to enable HCU support in containers.
\ No newline at end of file
dtk-container-runtime /usr/bin
dtk-ctk /usr/bin
dtk-cdi-hook /usr/bin
#!/bin/sh
set -e
NVIDIA_CONTAINER_RUNTIME_HOOK=/usr/bin/nvidia-container-runtime-hook
case "$1" in
configure)
/usr/bin/dtk-ctk --quiet config --config-file=/etc/dtk-container-runtime/config.toml --in-place
if [ ! -e "${NVIDIA_CONTAINER_RUNTIME_HOOK}" ]; then
ln -s /usr/bin/dtk-ctk $NVIDIA_CONTAINER_RUNTIME_HOOK
fi
/usr/bin/dtk-ctk runtime configure --runtime=docker --set-as-default
echo -e "\e[032m ================================== \e[0m"
echo -e "\e[033m Please restart Docker service: \e[0m"
echo -e "\e[033m sudo systemctl restart docker \e[0m"
echo -e "\e[032m ================================== \e[0m"
;;
abort-upgrade|abort-remove|abort-deconfigure)
;;
*)
echo "postinst called with unknown argument \`$1'" >&2
exit 1
;;
esac
#DEBHELPER#
exit 0
#!/bin/sh
set -e
NVIDIA_CONTAINER_RUNTIME_HOOK=/usr/bin/nvidia-container-runtime-hook
case "$1" in
purge)
[ -L "${NVIDIA_CONTAINER_RUNTIME_HOOK}" ] && rm ${NVIDIA_CONTAINER_RUNTIME_HOOK}
;;
upgrade|failed-upgrade|remove|abort-install|abort-upgrade|disappear)
;;
*)
echo "postrm called with unknown argument \`$1'" >&2
exit 1
;;
esac
#DEBHELPER#
exit 0
#! /bin/bash
set -e
sed -i "s;@SECTION@;${SECTION:+$SECTION/};g" debian/control
sed -i "s;@VERSION@;${VERSION:+$VERSION};g" debian/control
#!/usr/bin/make -f
# -*- makefile -*-
#export DH_VERBOSE=1
%:
dh $@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
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.
Name: dtk-container-toolkit
Version: %{version}
Release: %{release}
Group: Development Tools
Vendor: HCUOpt CORPORATION
Packager: HCUOpt CORPORATION
Summary: DTK Container Toolkit
License: Apache-2.0
Source0: dtk-ctk
Source1: dtk-container-runtime
Source2: dtk-cdi-hook
Source3: LICENSE
%description
Provides tools and utilities to enable HCU support in containers.
%prep
cp %{SOURCE0} %{SOURCE1} %{SOURCE2} %{SOURCE3} .
%install
mkdir -p %{buildroot}%{_bindir}
install -m 755 -t %{buildroot}%{_bindir} dtk-container-runtime
install -m 755 -t %{buildroot}%{_bindir} dtk-ctk
install -m 755 -t %{buildroot}%{_bindir} dtk-cdi-hook
%posttrans
if [ ! -e %{_bindir}/nvidia-container-runtime-hook ]; then
# repairing lost file nvidia-container-runtime-hook
ln -sf %{_bindir}/dtk-container-runtime %{_bindir}/nvidia-container-runtime-hook
fi
# Generate the default config; If this file already exists no changes are made.
%{_bindir}/dtk-ctk --quiet config --config-file=%{_sysconfdir}/dtk-container-runtime/config.toml --in-place
%{_bindir}/dtk-ctk runtime configure --runtime=docker --set-as-default
echo -e "\e[032m ================================== \e[0m"
echo -e "\e[033m Please restart Docker service: \e[0m"
echo -e "\e[033m sudo systemctl restart docker \e[0m"
echo -e "\e[032m ================================== \e[0m"
%postun
if [ "$1" = 0 ]; then # package is uninstalled, not upgraded
if [ -L %{_bindir}/nvidia-container-runtime-hook ]; then rm -f %{_bindir}/nvidia-container-runtime-hook; fi
fi
%files
%license LICENSE
%{_bindir}/dtk-ctk
%{_bindir}/dtk-container-runtime
%{_bindir}/dtk-cdi-hook
%changelog
* %{release_date} HCUOpt CORPORATION %{version}-%{release}
- initialize version
\ No newline at end of file
/**
# Copyright (c) 2024, HCUOpt CORPORATION. All rights reserved.
**/
package c3000cdi
import (
"dtk-container-toolkit/pkg/c3000cdi/spec"
"dtk-container-toolkit/pkg/go-c3000lib/pkg/device"
"tags.cncf.io/container-device-interface/pkg/cdi"
"tags.cncf.io/container-device-interface/specs-go"
)
// Interface defines the API for the c3000cdi package
type Interface interface {
GetSpec() (spec.Interface, error)
GetCommonEdits() (*cdi.ContainerEdits, error)
GetAllDeviceSpecs() ([]specs.Device, error)
GetHCUDeviceEdits(device.Device) (*cdi.ContainerEdits, error)
GetHCUDeviceSpecs(string, device.Device) ([]specs.Device, error)
GetMIGDeviceEdits(device.Device, device.MigDevice) (*cdi.ContainerEdits, error)
GetMIGDeviceSpecs(int, device.Device, int, device.MigDevice) ([]specs.Device, error)
GetDeviceSpecsByID(...string) ([]specs.Device, error)
}
/**
# Copyright (c) 2024, HCUOpt CORPORATION. All rights reserved.
**/
package c3000cdi
import (
"dtk-container-toolkit/internal/discover"
"dtk-container-toolkit/internal/edits"
"dtk-container-toolkit/internal/platform-support/dhcu"
"dtk-container-toolkit/pkg/c3000cdi/spec"
"dtk-container-toolkit/pkg/go-c3000lib/pkg/device"
"fmt"
"tags.cncf.io/container-device-interface/pkg/cdi"
"tags.cncf.io/container-device-interface/specs-go"
)
type c3000smilib c3000cdilib
var _ Interface = (*c3000smilib)(nil)
// GetSpec should not be called for c3000smilib
func (l *c3000smilib) GetSpec() (spec.Interface, error) {
return nil, fmt.Errorf("unexpected call to c3000smilib.GetSpec()")
}
// GetAllDeviceSpecs returns the device specs for all available devices.
func (l *c3000smilib) GetAllDeviceSpecs() ([]specs.Device, error) {
var deviceSpecs []specs.Device
hcuDeviceSpecs, err := l.getHCUDeviceSpecs()
if err != nil {
return nil, err
}
deviceSpecs = append(deviceSpecs, hcuDeviceSpecs...)
// todo: MIG
// migDeviceSpecs, err := l.getMigDeviceSpecs()
// if err != nil {
// return nil, err
// }
// deviceSpecs = append(deviceSpecs, migDeviceSpecs...)
return deviceSpecs, nil
}
// GetCommonEdits generates a CDI specification that can be used for ANY devices
func (l *c3000smilib) GetCommonEdits() (*cdi.ContainerEdits, error) {
common, err := l.newCommonC3000SmiDiscoverer()
if err != nil {
return nil, fmt.Errorf("failed to create discoverer for common entities: %v", err)
}
return edits.FromDiscoverer(l.logger, common)
}
// GetDeviceSpecsByID returns the CDI device specs for the HCU(s) represented by
// the provided identifiers, where an identifier is an index or UUID of a valid
// HCU device.
// Deprecated: Use GetDeviceSpecsBy instead.
func (l *c3000smilib) GetDeviceSpecsByID(ids ...string) ([]specs.Device, error) {
var identifiers []device.Identifier
for _, id := range ids {
identifiers = append(identifiers, device.Identifier(id))
}
return l.GetDeviceSpecsBy(identifiers...)
}
// GetDeviceSpecsBy returns the device specs for devices with the specified identifiers.
func (l *c3000smilib) GetDeviceSpecsBy(identifiers ...device.Identifier) ([]specs.Device, error) {
for _, id := range identifiers {
if id == "all" {
return l.GetAllDeviceSpecs()
}
}
var deviceSpecs []specs.Device
return deviceSpecs, nil
}
// GetHCUDeviceEdits returns the CDI edits for the full HCU represented by 'device'.
func (l *c3000smilib) GetHCUDeviceEdits(d device.Device) (*cdi.ContainerEdits, error) {
device, err := l.newFullHCUDiscoverer(d)
if err != nil {
return nil, fmt.Errorf("failed to create device discoverer: %v", err)
}
editsForDevice, err := edits.FromDiscoverer(l.logger, device)
if err != nil {
return nil, fmt.Errorf("failed to create container edits for device: %v", err)
}
return editsForDevice, nil
}
// GetHCUDeviceSpecs returns the CDI device specs for the full HCU represented by 'device'.
func (l *c3000smilib) GetHCUDeviceSpecs(i string, d device.Device) ([]specs.Device, error) {
edits, err := l.GetHCUDeviceEdits(d)
if err != nil {
return nil, fmt.Errorf("failed to get edits for device: %v", err)
}
var deviceSpecs []specs.Device
names, err := l.deviceNamers.GetDeviceNames(i, convert{d})
if err != nil {
return nil, fmt.Errorf("failed to get device name: %v", err)
}
for _, name := range names {
spec := specs.Device{
Name: name,
ContainerEdits: *edits.ContainerEdits,
}
deviceSpecs = append(deviceSpecs, spec)
}
return deviceSpecs, nil
}
func (l *c3000smilib) getHCUDeviceSpecs() ([]specs.Device, error) {
var deviceSpecs []specs.Device
err := l.devicelib.VisitDevices(func(i string, d device.Device) error {
specsForDevice, err := l.GetHCUDeviceSpecs(i, d)
if err != nil {
return err
}
deviceSpecs = append(deviceSpecs, specsForDevice...)
return nil
})
if err != nil {
return nil, fmt.Errorf("failed to generate CDI edits for HCU devices: %v", err)
}
return deviceSpecs, err
}
// GetMIGDeviceEdits returns the CDI edits for the MIG device represented by 'mig' on 'parent'.
func (l *c3000smilib) GetMIGDeviceEdits(parent device.Device, mig device.MigDevice) (*cdi.ContainerEdits, error) {
// todo
return nil, nil
}
// GetMIGDeviceSpecs returns the CDI device specs for the full GPU represented by 'device'.
func (l *c3000smilib) GetMIGDeviceSpecs(i int, d device.Device, j int, mig device.MigDevice) ([]specs.Device, error) {
// todo
return nil, nil
}
// newCommonNVMLDiscoverer returns a discoverer for entities that are not associated with a specific CDI device.
// This includes driver libraries and meta devices, for example.
func (l *c3000smilib) newCommonC3000SmiDiscoverer() (discover.Discover, error) {
d, err := discover.NewCommonHCUDiscoverer(
l.logger,
l.dtkCDIHookPath,
l.driver,
true,
)
return d, err
}
// newFullHCUDiscoverer creates a discoverer for the full HCU defined by the specified device.
func (l *c3000smilib) newFullHCUDiscoverer(d device.Device) (discover.Discover, error) {
deviceNodes, err := dhcu.NewForDevice(
d,
dhcu.WithDevRoot(l.devRoot),
dhcu.WithLogger(l.logger),
dhcu.WithDTKCDIHookPath(l.dtkCDIHookPath),
)
if err != nil {
return nil, fmt.Errorf("failed to create device discoverer: %v", err)
}
deviceFolderPermissionHooks := newDeviceFolderPermissionHookDiscoverer(
l.logger,
l.devRoot,
l.dtkCDIHookPath,
deviceNodes,
)
dd := discover.Merge(
deviceNodes,
deviceFolderPermissionHooks,
)
return dd, nil
}
/**
# Copyright (c) 2024, HCUOpt CORPORATION. All rights reserved.
**/
package c3000cdi
import (
"dtk-container-toolkit/internal/logger"
"dtk-container-toolkit/internal/lookup/root"
"dtk-container-toolkit/pkg/c3000cdi/transform"
"dtk-container-toolkit/pkg/go-c3000lib/pkg/device"
"dtk-container-toolkit/pkg/go-c3000smi/pkg/c3000smi"
"fmt"
)
type c3000cdilib struct {
logger logger.Interface
c3000smicmd c3000smi.Interface
mode Mode
devicelib device.Interface
deviceNamers DeviceNamers
driverRoot string
devRoot string
dtkCDIHookPath string
ldconfigPath string
configSearchPaths []string
librarySearchPaths []string
vendor string
class string
driver *root.Driver
mergedDeviceOptions []transform.MergedDeviceOption
}
// New creates a new c3000cdi library
func New(opts ...Option) (Interface, error) {
l := &c3000cdilib{}
for _, opt := range opts {
opt(l)
}
if l.mode == "" {
l.mode = ModeAuto
}
if l.logger == nil {
l.logger = logger.New()
}
if len(l.deviceNamers) == 0 {
indexNamer, _ := NewDeviceNamer(DeviceNameStrategyIndex)
l.deviceNamers = []DeviceNamer{indexNamer}
}
if l.dtkCDIHookPath == "" {
l.dtkCDIHookPath = "/usr/bin/dtk-cdi-hook"
}
if l.driverRoot == "" {
l.driverRoot = "/"
}
if l.devRoot == "" {
l.devRoot = "/"
}
l.driver = root.New(
root.WithLogger(l.logger),
root.WithDriverRoot(l.driverRoot),
root.WithLibrarySearchPaths(l.librarySearchPaths...),
root.WithConfigSearchPaths(l.configSearchPaths...),
)
if l.c3000smicmd == nil {
smicmd, err := c3000smi.NewSmiCommand(l.logger)
if err != nil {
return nil, fmt.Errorf("failed to new smi commd: %v", err)
}
l.c3000smicmd = smicmd
}
if l.devicelib == nil {
l.devicelib = device.New(l.c3000smicmd)
}
var lib Interface
switch l.resolveMode() {
case ModeSmi:
lib = (*c3000smilib)(l)
default:
return nil, fmt.Errorf("unknown mode %q", l.mode)
}
w := wrapper{
Interface: lib,
vendor: l.vendor,
class: l.class,
mergedDeviceOptions: l.mergedDeviceOptions,
}
return &w, nil
}
/**
# Copyright (c) 2024, HCUOpt CORPORATION. All rights reserved.
**/
package c3000cdi
import "sync"
type Mode string
const (
// ModeAuto configures the CDI spec generator to automatically detect the system configuration
ModeAuto = Mode("auto")
// ModeSmi configures the CDI spec generator to use c3000 smi.
ModeSmi = Mode("smi")
)
type modeConstraint interface {
string | Mode
}
type modes struct {
lookup map[Mode]bool
all []Mode
}
var validModes modes
var validModesOnce sync.Once
func getModes() modes {
validModesOnce.Do(func() {
all := []Mode{
ModeAuto,
ModeSmi,
}
lookup := make(map[Mode]bool)
for _, m := range all {
lookup[m] = true
}
validModes = modes{
lookup: lookup,
all: all,
}
})
return validModes
}
// AllModes returns the set of valid modes.
func AllModes[T modeConstraint]() []T {
var output []T
for _, m := range getModes().all {
output = append(output, T(m))
}
return output
}
// IsValidMode checks whether a specified mode is valid.
func IsValidMode[T modeConstraint](mode T) bool {
return getModes().lookup[Mode(mode)]
}
// resolveMode resolves the mode for CDI spec generation based on the current system.
func (l *c3000cdilib) resolveMode() (rmode Mode) {
if l.mode != ModeAuto {
return l.mode
}
defer func() {
l.logger.Infof("Auto-detected mode as '%v'", rmode)
}()
// todo: 更合理的方式决定用什么mode
if l.c3000smicmd.IsValid() {
return ModeSmi
}
return ModeSmi
}
/**
# Copyright (c) 2024, HCUOpt CORPORATION. All rights reserved.
**/
package c3000cdi
import (
"errors"
"fmt"
)
// UUIDer is an interface for getting UUIDs.
type UUIDer interface {
GetUUID() string
}
// DeviceNamers represents a list of device namers
type DeviceNamers []DeviceNamer
// DeviceNamer is an interface for getting device names
type DeviceNamer interface {
GetDeviceName(string, UUIDer) (string, error)
GetMigDeviceName(string, UUIDer, int, UUIDer) (string, error)
}
// Supported device naming strategies
const (
// DeviceNameStrategyIndex generates devices names such as 0 or 1:0
DeviceNameStrategyIndex = "index"
// DeviceNameStrategyTypeIndex generates devices names such as hcu0 or mig1:0
DeviceNameStrategyTypeIndex = "type-index"
// DeviceNameStrategyUUID uses the device UUID as the name
DeviceNameStrategyUUID = "uuid"
)
type deviceNameIndex struct {
hcuPrefix string
migPrefix string
}
type deviceNameUUID struct{}
// NewDeviceNamer creates a Device Namer based on the supplied strategy.
// This namer can be used to construct the names for MIG and HCU devices when generating the CDI spec.
func NewDeviceNamer(strategy string) (DeviceNamer, error) {
switch strategy {
case DeviceNameStrategyIndex:
return deviceNameIndex{}, nil
case DeviceNameStrategyTypeIndex:
return deviceNameIndex{hcuPrefix: "hcu", migPrefix: "mig"}, nil
case DeviceNameStrategyUUID:
return deviceNameUUID{}, nil
}
return nil, fmt.Errorf("invalid device name strategy: %v", strategy)
}
// GetDeviceName returns the name for the specified device based on the naming strategy
func (s deviceNameIndex) GetDeviceName(i string, _ UUIDer) (string, error) {
return fmt.Sprintf("%s%s", s.hcuPrefix, i), nil
}
// GetMigDeviceName returns the name for the specified device based on the naming strategy
func (s deviceNameIndex) GetMigDeviceName(i string, _ UUIDer, j int, _ UUIDer) (string, error) {
return fmt.Sprintf("%s%s:%d", s.migPrefix, i, j), nil
}
// GetDeviceName returns the name for the specified device based on the naming strategy
func (s deviceNameUUID) GetDeviceName(i string, d UUIDer) (string, error) {
return fmt.Sprintf("hcu-%s", d.GetUUID()), nil
}
// GetMigDeviceName returns the name for the specified device based on the naming strategy
func (s deviceNameUUID) GetMigDeviceName(i string, _ UUIDer, j int, mig UUIDer) (string, error) {
return mig.GetUUID(), nil
}
type c3000smiUUIDer interface {
GetUUID() string
}
type convert struct {
c3000smiUUIDer
}
func (l DeviceNamers) GetDeviceNames(i string, d UUIDer) ([]string, error) {
var names []string
for _, namer := range l {
name, err := namer.GetDeviceName(i, d)
if err != nil {
return nil, err
}
if name == "" {
continue
}
names = append(names, name)
}
if len(names) == 0 {
return nil, errors.New("no names defined")
}
return names, nil
}
/**
# Copyright (c) 2024, HCUOpt CORPORATION. All rights reserved.
**/
package c3000cdi
import (
"dtk-container-toolkit/internal/logger"
"dtk-container-toolkit/pkg/c3000cdi/transform"
"dtk-container-toolkit/pkg/go-c3000lib/pkg/device"
"dtk-container-toolkit/pkg/go-c3000smi/pkg/c3000smi"
)
// Option is a function that configures the c3000cdilib
type Option func(*c3000cdilib)
// WithDeviceLib sets the device library for the library
func WithDeviceLib(devicelib device.Interface) Option {
return func(l *c3000cdilib) {
l.devicelib = devicelib
}
}
// WithDeviceNamers sets the device namer for the library
func WithDeviceNamers(namers ...DeviceNamer) Option {
return func(l *c3000cdilib) {
l.deviceNamers = namers
}
}
// WithDriverRoot sets the driver root for the library
func WithDriverRoot(root string) Option {
return func(l *c3000cdilib) {
l.driverRoot = root
}
}
// WithDevRoot sets the root where /dev is located.
func WithDevRoot(root string) Option {
return func(l *c3000cdilib) {
l.devRoot = root
}
}
// WithLogger sets the logger for the library
func WithLogger(logger logger.Interface) Option {
return func(l *c3000cdilib) {
l.logger = logger
}
}
// WithDTKCDIHookPath sets the path to the DTK Container Toolkit CLI path for the library
func WithDTKCDIHookPath(path string) Option {
return func(l *c3000cdilib) {
l.dtkCDIHookPath = path
}
}
// WithLdconfigPath sets the path to the ldconfig program
func WithLdconfigPath(path string) Option {
return func(l *c3000cdilib) {
l.ldconfigPath = path
}
}
// WithNvmlLib sets the nvml library for the library
func WithNvmlLib(c3000smicmd c3000smi.Interface) Option {
return func(l *c3000cdilib) {
l.c3000smicmd = c3000smicmd
}
}
// WithMode sets the discovery mode for the library
func WithMode[m modeConstraint](mode m) Option {
return func(l *c3000cdilib) {
l.mode = Mode(mode)
}
}
// WithVendor sets the vendor for the library
func WithVendor(vendor string) Option {
return func(o *c3000cdilib) {
o.vendor = vendor
}
}
// WithClass sets the class for the library
func WithClass(class string) Option {
return func(o *c3000cdilib) {
o.class = class
}
}
// WithMergedDeviceOptions sets the merged device options for the library
// If these are not set, no merged device will be generated.
func WithMergedDeviceOptions(opts ...transform.MergedDeviceOption) Option {
return func(o *c3000cdilib) {
o.mergedDeviceOptions = opts
}
}
// WithConfigSearchPaths sets the search paths for config files.
func WithConfigSearchPaths(paths []string) Option {
return func(o *c3000cdilib) {
o.configSearchPaths = paths
}
}
// WithLibrarySearchPaths sets the library search paths.
// This is currently only used for CSV-mode.
func WithLibrarySearchPaths(paths []string) Option {
return func(o *c3000cdilib) {
o.librarySearchPaths = paths
}
}
/**
# Copyright (c) 2024, HCUOpt CORPORATION. All rights reserved.
**/
package spec
import (
"io"
"tags.cncf.io/container-device-interface/specs-go"
)
const (
// FormatJSON indicates a JSON output format
FormatJSON = "json"
// FormatYAML indicates a YAML output format
FormatYAML = "yaml"
)
// Interface is the interface for the spec API
type Interface interface {
io.WriterTo
Save(string) error
Raw() *specs.Spec
}
/**
# Copyright (c) 2024, HCUOpt CORPORATION. All rights reserved.
**/
package spec
import (
"dtk-container-toolkit/pkg/c3000cdi/transform"
"fmt"
"os"
"tags.cncf.io/container-device-interface/pkg/cdi"
"tags.cncf.io/container-device-interface/pkg/parser"
"tags.cncf.io/container-device-interface/specs-go"
)
type builder struct {
raw *specs.Spec
version string
vendor string
class string
deviceSpecs []specs.Device
edits specs.ContainerEdits
format string
mergedDeviceOptions []transform.MergedDeviceOption
noSimplify bool
permissions os.FileMode
transformOnSave transform.Transformer
}
// newBuilder creates a new spec builder with the supplied options
func newBuilder(opts ...Option) *builder {
s := &builder{}
for _, opt := range opts {
opt(s)
}
if s.raw != nil {
s.noSimplify = true
vendor, class := parser.ParseQualifier(s.raw.Kind)
if s.vendor == "" {
s.vendor = vendor
}
if s.class == "" {
s.class = class
}
if s.version == "" {
s.version = s.raw.Version
}
}
if s.version == "" {
s.transformOnSave = &setMinimumRequiredVersion{}
s.version = cdi.CurrentVersion
}
if s.vendor == "" {
s.vendor = "c-3000.com"
}
if s.class == "" {
s.class = "hcu"
}
if s.format == "" {
s.format = FormatYAML
}
if s.permissions == 0 {
s.permissions = 0644
}
return s
}
// Build builds a CDI spec form the spec builder.
func (o *builder) Build() (*spec, error) {
raw := o.raw
if raw == nil {
raw = &specs.Spec{
Version: o.version,
Kind: fmt.Sprintf("%s/%s", o.vendor, o.class),
Devices: o.deviceSpecs,
ContainerEdits: o.edits,
}
}
if raw.Version == "" {
raw.Version = o.version
}
if !o.noSimplify {
err := transform.NewSimplifier().Transform(raw)
if err != nil {
return nil, fmt.Errorf("failed to simplify spec: %v", err)
}
}
if len(o.mergedDeviceOptions) > 0 {
merge, err := transform.NewMergedDevice(o.mergedDeviceOptions...)
if err != nil {
return nil, fmt.Errorf("failed to create merged device transformer: %v", err)
}
if err := merge.Transform(raw); err != nil {
return nil, fmt.Errorf("failed to merge devices: %v", err)
}
}
s := spec{
Spec: raw,
format: o.format,
permissions: o.permissions,
transformOnSave: o.transformOnSave,
}
return &s, nil
}
// Option defines a function that can be used to configure the spec builder.
type Option func(*builder)
// WithDeviceSpecs sets the device specs for the spec builder
func WithDeviceSpecs(deviceSpecs []specs.Device) Option {
return func(o *builder) {
o.deviceSpecs = deviceSpecs
}
}
// WithEdits sets the container edits for the spec builder
func WithEdits(edits specs.ContainerEdits) Option {
return func(o *builder) {
o.edits = edits
}
}
// WithVersion sets the version for the spec builder
func WithVersion(version string) Option {
return func(o *builder) {
o.version = version
}
}
// WithVendor sets the vendor for the spec builder
func WithVendor(vendor string) Option {
return func(o *builder) {
o.vendor = vendor
}
}
// WithClass sets the class for the spec builder
func WithClass(class string) Option {
return func(o *builder) {
o.class = class
}
}
// WithFormat sets the output file format
func WithFormat(format string) Option {
return func(o *builder) {
o.format = format
}
}
// WithNoSimplify sets whether the spec must be simplified
func WithNoSimplify(noSimplify bool) Option {
return func(o *builder) {
o.noSimplify = noSimplify
}
}
// WithRawSpec sets the raw spec for the spec builder
func WithRawSpec(raw *specs.Spec) Option {
return func(o *builder) {
o.raw = raw
}
}
// WithPermissions sets the permissions for the generated spec file
func WithPermissions(permissions os.FileMode) Option {
return func(o *builder) {
o.permissions = permissions
}
}
// WithMergedDeviceOptions sets the options for generating a merged device.
func WithMergedDeviceOptions(opts ...transform.MergedDeviceOption) Option {
return func(o *builder) {
o.mergedDeviceOptions = opts
}
}
/**
# Copyright (c) 2024, HCUOpt CORPORATION. All rights reserved.
**/
package spec
import (
"fmt"
"tags.cncf.io/container-device-interface/pkg/cdi"
"tags.cncf.io/container-device-interface/specs-go"
)
type setMinimumRequiredVersion struct{}
func (d setMinimumRequiredVersion) Transform(spec *specs.Spec) error {
minVersion, err := cdi.MinimumRequiredVersion(spec)
if err != nil {
return fmt.Errorf("failed to get minimum required CDI spec version: %v", err)
}
spec.Version = minVersion
return nil
}
/**
# Copyright (c) 2024, HCUOpt CORPORATION. All rights reserved.
**/
package spec
import (
"dtk-container-toolkit/pkg/c3000cdi/transform"
"fmt"
"io"
"os"
"path/filepath"
"tags.cncf.io/container-device-interface/pkg/cdi"
"tags.cncf.io/container-device-interface/specs-go"
)
type spec struct {
*specs.Spec
format string
permissions os.FileMode
transformOnSave transform.Transformer
}
var _ Interface = (*spec)(nil)
// New creates a new spec with the specified options.
func New(opts ...Option) (Interface, error) {
return newBuilder(opts...).Build()
}
// Save writes the spec to the specified path and overwrites the file if it exists.
func (s *spec) Save(path string) error {
if s.transformOnSave != nil {
err := s.transformOnSave.Transform(s.Raw())
if err != nil {
return fmt.Errorf("error applying transform: %w", err)
}
}
path, err := s.normalizePath(path)
if err != nil {
return fmt.Errorf("failed to normalize path: %w", err)
}
specDir := filepath.Dir(path)
cache, _ := cdi.NewCache(
cdi.WithAutoRefresh(false),
cdi.WithSpecDirs(specDir),
)
if err := cache.WriteSpec(s.Raw(), filepath.Base(path)); err != nil {
return fmt.Errorf("failed to write spec: %w", err)
}
if err := os.Chmod(path, s.permissions); err != nil {
return fmt.Errorf("failed to set permissions on spec file: %w", err)
}
return nil
}
// WriteTo writes the spec to the specified writer.
func (s *spec) WriteTo(w io.Writer) (int64, error) {
name, err := cdi.GenerateNameForSpec(s.Raw())
if err != nil {
return 0, err
}
path, _ := s.normalizePath(name)
tmpFile, err := os.CreateTemp("", "*"+filepath.Base(path))
if err != nil {
return 0, err
}
defer os.Remove(tmpFile.Name())
if err := s.Save(tmpFile.Name()); err != nil {
return 0, err
}
err = tmpFile.Close()
if err != nil {
return 0, fmt.Errorf("failed to close temporary file: %w", err)
}
r, err := os.Open(tmpFile.Name())
if err != nil {
return 0, fmt.Errorf("failed to open temporary file: %w", err)
}
defer r.Close()
return io.Copy(w, r)
}
// Raw returns a pointer to the raw spec.
func (s *spec) Raw() *specs.Spec {
return s.Spec
}
// normalizePath ensures that the specified path has a supported extension
func (s *spec) normalizePath(path string) (string, error) {
if ext := filepath.Ext(path); ext != ".yaml" && ext != ".json" {
path += s.extension()
}
if filepath.Clean(filepath.Dir(path)) == "." {
pwd, err := os.Getwd()
if err != nil {
return path, fmt.Errorf("failed to get current working directory: %v", err)
}
path = filepath.Join(pwd, path)
}
return path, nil
}
func (s *spec) extension() string {
switch s.format {
case FormatJSON:
return ".json"
case FormatYAML:
return ".yaml"
}
return ".yaml"
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment