|
| 1 | +/* |
| 2 | + * Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. |
| 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 v1 |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "io" |
| 22 | + "os" |
| 23 | + |
| 24 | + cli "github.com/urfave/cli/v2" |
| 25 | + "k8s.io/klog/v2" |
| 26 | + |
| 27 | + "sigs.k8s.io/yaml" |
| 28 | +) |
| 29 | + |
| 30 | +// Version indicates the version of the 'Config' struct used to hold configuration information. |
| 31 | +const Version = "v1" |
| 32 | + |
| 33 | +// Config is a versioned struct used to hold configuration information. |
| 34 | +type Config struct { |
| 35 | + Version string `json:"version" yaml:"version"` |
| 36 | + Flags Flags `json:"flags,omitempty" yaml:"flags,omitempty"` |
| 37 | + Resources Resources `json:"resources,omitempty" yaml:"resources,omitempty"` |
| 38 | + Sharing Sharing `json:"sharing,omitempty" yaml:"sharing,omitempty"` |
| 39 | + Imex Imex `json:"imex,omitempty" yaml:"imex,omitempty"` |
| 40 | +} |
| 41 | + |
| 42 | +// NewConfig builds out a Config struct from a config file (or command line flags). |
| 43 | +// The data stored in the config will be populated in order of precedence from |
| 44 | +// (1) command line, (2) environment variable, (3) config file. |
| 45 | +func NewConfig(c *cli.Context, flags []cli.Flag) (*Config, error) { |
| 46 | + config := &Config{Version: Version} |
| 47 | + |
| 48 | + if configFile := c.String("config-file"); configFile != "" { |
| 49 | + var err error |
| 50 | + config, err = parseConfig(configFile) |
| 51 | + if err != nil { |
| 52 | + return nil, fmt.Errorf("unable to parse config file: %v", err) |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + config.Flags.UpdateFromCLIFlags(c, flags) |
| 57 | + // TODO: This is currently not at the flags level? |
| 58 | + // Does this mean that we should move UpdateFromCLIFlags to function off Config? |
| 59 | + if c.IsSet("imex-channel-ids") { |
| 60 | + config.Imex.ChannelIDs = c.IntSlice("imex-channel-ids") |
| 61 | + } |
| 62 | + if c.IsSet("imex-required") { |
| 63 | + config.Imex.Required = c.Bool("imex-required") |
| 64 | + } |
| 65 | + |
| 66 | + // If nvidiaDevRoot (the path to the device nodes on the host) is not set, |
| 67 | + // we default to using the driver root on the host. |
| 68 | + if config.Flags.NvidiaDevRoot == nil || *config.Flags.NvidiaDevRoot == "" { |
| 69 | + config.Flags.NvidiaDevRoot = config.Flags.NvidiaDriverRoot |
| 70 | + } |
| 71 | + |
| 72 | + // We explicitly set sharing.mps.failRequestsGreaterThanOne = true |
| 73 | + // This can be relaxed in certain cases -- such as a single GPU -- but |
| 74 | + // requires additional logic around when it's OK to combine requests and |
| 75 | + // makes the semantics of a request unclear. |
| 76 | + if config.Sharing.MPS != nil { |
| 77 | + config.Sharing.MPS.FailRequestsGreaterThanOne = true |
| 78 | + } |
| 79 | + |
| 80 | + return config, nil |
| 81 | +} |
| 82 | + |
| 83 | +// DisableResourceNamingInConfig temporarily disable the resource renaming feature of the plugin. |
| 84 | +// This may be reenabled in a future release. |
| 85 | +func DisableResourceNamingInConfig(config *Config) { |
| 86 | + // Disable resource renaming through config.Resource |
| 87 | + if len(config.Resources.GPUs) > 0 || len(config.Resources.MIGs) > 0 { |
| 88 | + klog.Warning("Customizing the 'resources' field is not yet supported in the config. Ignoring...") |
| 89 | + } |
| 90 | + config.Resources.GPUs = nil |
| 91 | + config.Resources.MIGs = nil |
| 92 | + |
| 93 | + // Disable renaming / device selection in Sharing.TimeSlicing.Resources |
| 94 | + config.Sharing.TimeSlicing.disableResoureRenaming("timeSlicing") |
| 95 | + // Disable renaming / device selection in Sharing.MPS.Resources |
| 96 | + config.Sharing.MPS.disableResoureRenaming("mps") |
| 97 | +} |
| 98 | + |
| 99 | +// parseConfig parses a config file as either YAML of JSON and unmarshals it into a Config struct. |
| 100 | +func parseConfig(configFile string) (*Config, error) { |
| 101 | + reader, err := os.Open(configFile) |
| 102 | + if err != nil { |
| 103 | + return nil, fmt.Errorf("error opening config file: %v", err) |
| 104 | + } |
| 105 | + defer reader.Close() |
| 106 | + |
| 107 | + config, err := parseConfigFrom(reader) |
| 108 | + if err != nil { |
| 109 | + return nil, fmt.Errorf("error parsing config file: %v", err) |
| 110 | + } |
| 111 | + |
| 112 | + return config, nil |
| 113 | +} |
| 114 | + |
| 115 | +func parseConfigFrom(reader io.Reader) (*Config, error) { |
| 116 | + var err error |
| 117 | + var configYaml []byte |
| 118 | + |
| 119 | + configYaml, err = io.ReadAll(reader) |
| 120 | + if err != nil { |
| 121 | + return nil, fmt.Errorf("read error: %v", err) |
| 122 | + } |
| 123 | + |
| 124 | + var config Config |
| 125 | + err = yaml.Unmarshal(configYaml, &config) |
| 126 | + if err != nil { |
| 127 | + return nil, fmt.Errorf("unmarshal error: %v", err) |
| 128 | + } |
| 129 | + |
| 130 | + if config.Version == "" { |
| 131 | + config.Version = Version |
| 132 | + } |
| 133 | + |
| 134 | + if config.Version != Version { |
| 135 | + return nil, fmt.Errorf("unknown version: %v", config.Version) |
| 136 | + } |
| 137 | + |
| 138 | + return &config, nil |
| 139 | +} |
0 commit comments