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