|
| 1 | +/** |
| 2 | +# Copyright 2024 NVIDIA CORPORATION |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +**/ |
| 16 | + |
| 17 | +package engine |
| 18 | + |
| 19 | +import "strings" |
| 20 | + |
| 21 | +// GetBinaryPathsForRuntimes returns the list of binary paths for common rutimes. |
| 22 | +// The following list of runtimes is considered: |
| 23 | +// |
| 24 | +// the default runtime, "runc", and "crun" |
| 25 | +// |
| 26 | +// If an nvidia* runtime is set as the default runtime, this is ignored. |
| 27 | +func GetBinaryPathsForRuntimes(cfg Interface) []string { |
| 28 | + |
| 29 | + var binaryPaths []string |
| 30 | + seen := make(map[string]bool) |
| 31 | + for _, runtime := range GetLowLevelRuntimes(cfg) { |
| 32 | + runtimeConfig, err := cfg.GetRuntimeConfig(runtime) |
| 33 | + if err != nil { |
| 34 | + // TODO: It will be useful to log the error when GetRuntimeConfig fails for a runtime |
| 35 | + continue |
| 36 | + } |
| 37 | + binaryPath := runtimeConfig.GetBinaryPath() |
| 38 | + if binaryPath == "" || seen[binaryPath] { |
| 39 | + continue |
| 40 | + } |
| 41 | + seen[binaryPath] = true |
| 42 | + binaryPaths = append(binaryPaths, binaryPath) |
| 43 | + } |
| 44 | + |
| 45 | + return binaryPaths |
| 46 | +} |
| 47 | + |
| 48 | +// GetLowLevelRuntimes returns a predefined list low-level runtimes from the specified config. |
| 49 | +// nvidia* runtimes are ignored. |
| 50 | +func GetLowLevelRuntimes(cfg Interface) []string { |
| 51 | + var runtimes []string |
| 52 | + isValidDefault := func(s string) bool { |
| 53 | + if s == "" { |
| 54 | + return false |
| 55 | + } |
| 56 | + // ignore nvidia* runtimes. |
| 57 | + return !strings.HasPrefix(s, "nvidia") |
| 58 | + } |
| 59 | + if defaultRuntime := cfg.DefaultRuntime(); isValidDefault(defaultRuntime) { |
| 60 | + runtimes = append(runtimes, defaultRuntime) |
| 61 | + } |
| 62 | + return append(runtimes, "runc", "crun") |
| 63 | +} |
0 commit comments