Commit 0ccfece1 authored by songlinfeng's avatar songlinfeng
Browse files

support docker swarm

parent d2da0c28
...@@ -8,7 +8,8 @@ import ( ...@@ -8,7 +8,8 @@ import (
"fmt" "fmt"
"path/filepath" "path/filepath"
"strings" "strings"
"dtk-container-toolkit/internal/hydcu"
"strconv"
"github.com/opencontainers/runtime-spec/specs-go" "github.com/opencontainers/runtime-spec/specs-go"
"tags.cncf.io/container-device-interface/pkg/parser" "tags.cncf.io/container-device-interface/pkg/parser"
) )
...@@ -57,8 +58,29 @@ func (i DTK) Getenv(key string) string { ...@@ -57,8 +58,29 @@ func (i DTK) Getenv(key string) string {
return i.env[key] return i.env[key]
} }
func Contains[T comparable](slice []T, val T) bool {
for _, item := range slice {
if item == val {
return true
}
}
return false
}
// DevicesFromEnvvars returns the devices requested by the image through environment variables // DevicesFromEnvvars returns the devices requested by the image through environment variables
func (i DTK) DevicesFromEnvvars(envVars ...string) VisibleDevices { func (i DTK) DevicesFromEnvvars(envVars ...string) VisibleDevices {
isHexString := func(s string) bool {
if len(s) == 0 {
return false
}
for _, c := range s {
if !((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')) {
return false
}
}
return true
}
// We concantenate all the devices from the specified env. // We concantenate all the devices from the specified env.
var isSet bool var isSet bool
var devices []string var devices []string
...@@ -77,6 +99,35 @@ func (i DTK) DevicesFromEnvvars(envVars ...string) VisibleDevices { ...@@ -77,6 +99,35 @@ func (i DTK) DevicesFromEnvvars(envVars ...string) VisibleDevices {
} }
} }
uuidToDCUIdMap, err := hydcu.GetUniqueIdToDeviceIndexMap()
if err != nil {
uuidToDCUIdMap = make(map[string][]int)
}
for key, value := range i.env {
if strings.HasPrefix(key, "DOCKER_RESOURCE_") {
for _, c := range strings.Split(value, ",") {
if strings.HasPrefix(c, "0x") || strings.HasPrefix(c, "0X") ||
(len(c) > 8 && isHexString(c)) {
uuid := strings.ToLower(c)
if !strings.HasPrefix(uuid, "0x") {
uuid = "0x" + uuid
}
if dcuId, exists := uuidToDCUIdMap[uuid]; exists {
if !Contains(devices, strconv.Itoa(dcuId[0])) {
devices = append(devices, strconv.Itoa(dcuId[0]))
}
} else {
uuid = strings.TrimPrefix(uuid, "0x")
if dcuId, exists := uuidToDCUIdMap[uuid]; exists {
devices = append(devices, strconv.Itoa(dcuId[0]))
}
}
}
}
break
}
}
// Environment variable unset with legacy image: default to "all". // Environment variable unset with legacy image: default to "all".
if !isSet && len(devices) == 0 && i.IsLegacy() { if !isSet && len(devices) == 0 && i.IsLegacy() {
return NewVisibleDevices("all") return NewVisibleDevices("all")
......
...@@ -121,12 +121,22 @@ func NewCommonHCUDiscoverer(logger logger.Interface, dtkCDIHookPath string, driv ...@@ -121,12 +121,22 @@ func NewCommonHCUDiscoverer(logger logger.Interface, dtkCDIHookPath string, driv
trackHook = CreateTrackHook(dtkCDIHookPath, containerImage.ContainerId) trackHook = CreateTrackHook(dtkCDIHookPath, containerImage.ContainerId)
} }
d := Merge( var d Discover
metaDevices, if trackHook.Lifecycle == "" {
libraries, d = Merge(
NewUserGroupDiscover(logger), metaDevices,
linkHook, libraries,
trackHook, NewUserGroupDiscover(logger),
) linkHook,
)
} else {
d = Merge(
metaDevices,
libraries,
NewUserGroupDiscover(logger),
linkHook,
trackHook,
)
}
return d, nil return d, nil
} }
...@@ -7,7 +7,6 @@ package oci ...@@ -7,7 +7,6 @@ package oci
import ( import (
"dtk-container-toolkit/internal/logger" "dtk-container-toolkit/internal/logger"
"fmt" "fmt"
"github.com/opencontainers/runtime-spec/specs-go" "github.com/opencontainers/runtime-spec/specs-go"
) )
...@@ -39,7 +38,6 @@ func NewSpec(logger logger.Interface, args []string) (Spec, error) { ...@@ -39,7 +38,6 @@ func NewSpec(logger logger.Interface, args []string) (Spec, error) {
ociSpecPath := GetSpecFilePath(bundleDir) ociSpecPath := GetSpecFilePath(bundleDir)
logger.Infof("Using OCI specification file path: %v", ociSpecPath) logger.Infof("Using OCI specification file path: %v", ociSpecPath)
ociSpec := NewFileSpec(ociSpecPath) ociSpec := NewFileSpec(ociSpecPath)
return ociSpec, nil return ociSpec, nil
} }
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