1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
16 "git.curoverse.com/arvados.git/sdk/go/arvados"
17 "github.com/ghodss/yaml"
20 type logger interface {
21 Warnf(string, ...interface{})
24 type deprecatedConfig struct {
25 Clusters map[string]struct {
26 NodeProfiles map[string]arvados.NodeProfile
30 func Load(rdr io.Reader, log logger) (*arvados.Config, error) {
31 var cfg arvados.Config
32 buf, err := ioutil.ReadAll(rdr)
37 // Load the config into a dummy map to get the cluster ID
38 // keys, discarding the values; then set up defaults for each
39 // cluster ID; then load the real config on top of the
42 Clusters map[string]struct{}
44 err = yaml.Unmarshal(buf, &dummy)
48 if len(dummy.Clusters) == 0 {
49 return nil, errors.New("config does not define any clusters")
51 for id := range dummy.Clusters {
52 err = yaml.Unmarshal(bytes.Replace(DefaultYAML, []byte("xxxxx"), []byte(id), -1), &cfg)
54 return nil, fmt.Errorf("loading defaults for %s: %s", id, err)
57 err = yaml.Unmarshal(buf, &cfg)
62 // Check for deprecated config values, and apply them to cfg.
63 var dc deprecatedConfig
64 err = yaml.Unmarshal(buf, &dc)
68 err = applyDeprecatedConfig(&cfg, &dc, log)
75 func applyDeprecatedConfig(cfg *arvados.Config, dc *deprecatedConfig, log logger) error {
76 hostname, err := os.Hostname()
80 for id, dcluster := range dc.Clusters {
81 cluster, ok := cfg.Clusters[id]
83 return fmt.Errorf("can't load legacy config %q that is not present in current config", id)
85 for name, np := range dcluster.NodeProfiles {
86 if name == "*" || name == os.Getenv("ARVADOS_NODE_PROFILE") || name == hostname {
87 applyDeprecatedNodeProfile(hostname, np.RailsAPI, &cluster.Services.RailsAPI)
88 applyDeprecatedNodeProfile(hostname, np.Controller, &cluster.Services.Controller)
89 applyDeprecatedNodeProfile(hostname, np.DispatchCloud, &cluster.Services.DispatchCloud)
92 cfg.Clusters[id] = cluster
97 func applyDeprecatedNodeProfile(hostname string, ssi arvados.SystemServiceInstance, svc *arvados.Service) {
102 if svc.InternalURLs == nil {
103 svc.InternalURLs = map[arvados.URL]arvados.ServiceInstance{}
109 if strings.HasPrefix(host, ":") {
110 host = hostname + host
112 svc.InternalURLs[arvados.URL{Scheme: scheme, Host: host}] = arvados.ServiceInstance{}