-
Notifications
You must be signed in to change notification settings - Fork 346
/
root.go
302 lines (260 loc) · 8.24 KB
/
root.go
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"github.com/ghodss/yaml"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/determined-ai/determined/master/internal"
"github.com/determined-ai/determined/master/internal/config"
"github.com/determined-ai/determined/master/pkg/check"
"github.com/determined-ai/determined/master/pkg/logger"
)
const defaultConfigPath = "/etc/determined/master.yaml"
// logStoreSize is how many log events to keep in memory.
const logStoreSize = 25000
func newRootCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "determined-master",
Run: func(cmd *cobra.Command, args []string) {
if err := runRoot(); err != nil {
log.Error(fmt.Sprintf("%+v", err))
os.Exit(1)
}
},
}
cmd.AddCommand(newMigrateCmd())
cmd.AddCommand(newPopulateCmd())
return cmd
}
var rootCmd = newRootCmd()
func runRoot() error {
logStore := logger.NewLogBuffer(logStoreSize)
log.AddHook(logStore)
err := initializeConfig()
if err != nil {
return err
}
config := config.GetMasterConfig()
printableConfig, err := config.Printable()
if err != nil {
return err
}
log.Infof("master configuration: %s", printableConfig)
err = os.MkdirAll(config.Cache.CacheDir, 0o700)
if err != nil {
log.WithError(err).Errorf("Failed to make cache directory (%s)", config.Cache.CacheDir)
return err
}
m := internal.New(logStore, config)
return m.Run(context.TODO(), nil)
}
// initializeConfig initializes master config with the validated configuration populated from config
// file, environment variables, and command line flags) and also initializes
// global logging state based on those options.
func initializeConfig() error {
// Fetch an initial config to get the config file path and read its settings into Viper.
initialConfig, err := getConfig(v.AllSettings())
if err != nil {
return err
}
bs, err := readConfigFile(initialConfig.ConfigFile)
if err != nil {
return err
}
conf, err := mergeConfigIntoViper(bs)
if err != nil {
return err
}
if err := check.Validate(conf); err != nil {
return err
}
config.SetMasterConfig(conf)
for _, deprecation := range conf.Deprecations() {
log.Warn(deprecation.Error())
}
return nil
}
func mergeConfigIntoViper(bs []byte) (*config.Config, error) {
// Write a configMap from the config file, and create a copy (cpMap) to
// deepcopy values needed to override viper's merge auto-lowercasing.
var configMap, cpMap map[string]interface{}
if err := yaml.Unmarshal(bs, &configMap); err != nil {
return nil, fmt.Errorf("unmarshalling yaml configuration file: %w", err)
}
if err := yaml.Unmarshal(bs, &cpMap); err != nil {
return nil, fmt.Errorf("unmarshalling yaml configuration file: %w", err)
}
// Now merge the config file values into viper.
if err := v.MergeConfigMap(configMap); err != nil {
return nil, fmt.Errorf("can't merge configuration to viper: %w", err)
}
// Now call viper.AllSettings() again to get the full config, containing all values from CLI flags,
// environment variables, and the configuration file. Override the task_container_defaults value
// using the map you copied.
customConfig := mergeCustomSpecs(v.AllSettings(), cpMap)
return getConfig(customConfig)
}
func mergeCustomSpecs(
config map[string]interface{},
cp map[string]interface{},
) map[string]interface{} {
if conf, ok := cp["task_container_defaults"].(map[string]interface{}); ok {
if cpu, ok := conf["cpu_pod_spec"]; ok {
config["task_container_defaults"].(map[string]interface{})["cpu_pod_spec"] = cpu
}
if gpu, ok := conf["gpu_pod_spec"]; ok {
config["task_container_defaults"].(map[string]interface{})["gpu_pod_spec"] = gpu
}
}
return config
}
func readConfigFile(configPath string) ([]byte, error) {
isDefault := configPath == ""
if isDefault {
configPath = defaultConfigPath
}
var err error
if _, err = os.Stat(configPath); err != nil {
if isDefault && os.IsNotExist(err) {
log.Warnf("no configuration file at %s, skipping", configPath)
return nil, nil
}
return nil, errors.Wrap(err, "error finding configuration file")
}
bs, err := os.ReadFile(configPath) // #nosec G304
if err != nil {
return nil, errors.Wrap(err, "error reading configuration file")
}
return bs, nil
}
func getConfig(configMap map[string]interface{}) (*config.Config, error) {
configMap, err := applyBackwardsCompatibility(configMap)
if err != nil {
return nil, errors.Wrap(err, "cannot apply backwards compatibility")
}
config := config.DefaultConfig()
bs, err := json.Marshal(configMap)
if err != nil {
return nil, errors.Wrap(err, "cannot marshal configuration map into json bytes")
}
if err = yaml.Unmarshal(bs, &config, yaml.DisallowUnknownFields); err != nil {
return nil, errors.Wrap(err, "cannot unmarshal configuration")
}
if err := config.Resolve(); err != nil {
return nil, err
}
return config, nil
}
func applyBackwardsCompatibility(configMap map[string]interface{}) (map[string]interface{}, error) {
const (
defaultVal = "default"
agentVal = "agent"
kubernetesVal = "kubernetes"
)
_, rmExisted := configMap["resource_manager"]
_, rpsExisted := configMap["resource_pools"]
vScheduler, schedulerExisted := configMap["scheduler"]
vProvisioner, provisionerExisted := configMap["provisioner"]
// Ensure we use either the old schema or the new one.
if (rmExisted || rpsExisted) && (schedulerExisted || provisionerExisted) {
return nil, errors.New(
"cannot use the old and the new configuration schema at the same time",
)
}
if rmExisted || rpsExisted {
return configMap, nil
}
// If use the old schema, convert it to the new one.
newScheduler := map[string]interface{}{
"type": "fair_share",
"fitting_policy": "best",
}
newRM := map[string]interface{}{
"type": agentVal,
}
if schedulerExisted {
schedulerMap, ok := vScheduler.(map[string]interface{})
if !ok {
return nil, errors.New("wrong type for scheduler field")
}
if vFit, ok := schedulerMap["fit"]; ok {
newScheduler["fitting_policy"], ok = vFit.(string)
if !ok {
return nil, errors.New("wrong type for scheduler.fit field")
}
}
if vType, ok := schedulerMap["type"]; ok {
newScheduler["type"], ok = vType.(string)
if !ok {
return nil, errors.New("wrong type for scheduler.type field")
}
}
if vRP, ok := schedulerMap["resource_provider"]; ok {
rpMap, ok := vRP.(map[string]interface{})
if !ok {
return nil, errors.New("wrong type for scheduler.resource_provider field")
}
vRPType, ok := rpMap["type"]
if ok {
switch vRPTypeStr, ok := vRPType.(string); {
case !ok:
return nil, errors.New("wrong type for scheduler.resource_provider.type field")
case vRPTypeStr == defaultVal:
newRM["type"] = agentVal
case vRPTypeStr == kubernetesVal:
newRM["type"] = kubernetesVal
default:
return nil, errors.New("wrong value for scheduler.resource_provider.type field")
}
} else {
newRM["type"] = agentVal
}
if newRM["type"] == kubernetesVal {
for key, value := range rpMap {
if key == "type" {
continue
}
newRM[key] = value
}
} else {
newRM["default_cpu_resource_pool"] = defaultVal
newRM["default_gpu_resource_pool"] = defaultVal
}
} else {
newRM["default_cpu_resource_pool"] = defaultVal
newRM["default_gpu_resource_pool"] = defaultVal
}
}
newRM["scheduler"] = newScheduler
configMap["resource_manager"] = newRM
if newRM["type"] == agentVal {
newRP := make(map[string]interface{})
newRP["pool_name"] = defaultVal
if provisionerExisted {
provisionerMap, ok := vProvisioner.(map[string]interface{})
if !ok {
return nil, errors.New("wrong type for provisioner field")
}
newRP["provider"] = provisionerMap
if vProvider, ok := provisionerMap["provider"]; ok {
vProviderStr, ok := vProvider.(string)
if !ok {
return nil, errors.New("wrong type for provisioner.provider field")
}
if vProviderStr != "aws" && vProviderStr != "gcp" {
return nil, errors.New("wrong value for provisioner.provider field")
}
provisionerMap["type"] = provisionerMap["provider"]
delete(provisionerMap, "provider")
}
}
configMap["resource_pools"] = []map[string]interface{}{newRP}
}
delete(configMap, "scheduler")
delete(configMap, "provisioner")
return configMap, nil
}