Merge branch '15167-unlogged-attrs-api-docs'
[arvados.git] / lib / config / deprecated.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package config
6
7 import (
8         "fmt"
9         "os"
10         "strings"
11
12         "git.curoverse.com/arvados.git/sdk/go/arvados"
13         "github.com/ghodss/yaml"
14 )
15
16 type deprRequestLimits struct {
17         MaxItemsPerResponse            *int
18         MultiClusterRequestConcurrency *int
19 }
20
21 type deprCluster struct {
22         RequestLimits deprRequestLimits
23         NodeProfiles  map[string]arvados.NodeProfile
24 }
25
26 type deprecatedConfig struct {
27         Clusters map[string]deprCluster
28 }
29
30 func applyDeprecatedConfig(cfg *arvados.Config, configdata []byte, log logger) error {
31         var dc deprecatedConfig
32         err := yaml.Unmarshal(configdata, &dc)
33         if err != nil {
34                 return err
35         }
36         hostname, err := os.Hostname()
37         if err != nil {
38                 return err
39         }
40         for id, dcluster := range dc.Clusters {
41                 cluster, ok := cfg.Clusters[id]
42                 if !ok {
43                         return fmt.Errorf("can't load legacy config %q that is not present in current config", id)
44                 }
45                 for name, np := range dcluster.NodeProfiles {
46                         if name == "*" || name == os.Getenv("ARVADOS_NODE_PROFILE") || name == hostname {
47                                 name = "localhost"
48                         } else if log != nil {
49                                 log.Warnf("overriding Clusters.%s.Services using Clusters.%s.NodeProfiles.%s (guessing %q is a hostname)", id, id, name, name)
50                         }
51                         applyDeprecatedNodeProfile(name, np.RailsAPI, &cluster.Services.RailsAPI)
52                         applyDeprecatedNodeProfile(name, np.Controller, &cluster.Services.Controller)
53                         applyDeprecatedNodeProfile(name, np.DispatchCloud, &cluster.Services.DispatchCloud)
54                 }
55                 if dst, n := &cluster.API.MaxItemsPerResponse, dcluster.RequestLimits.MaxItemsPerResponse; n != nil && *n != *dst {
56                         *dst = *n
57                 }
58                 if dst, n := &cluster.API.MaxRequestAmplification, dcluster.RequestLimits.MultiClusterRequestConcurrency; n != nil && *n != *dst {
59                         *dst = *n
60                 }
61                 cfg.Clusters[id] = cluster
62         }
63         return nil
64 }
65
66 func applyDeprecatedNodeProfile(hostname string, ssi arvados.SystemServiceInstance, svc *arvados.Service) {
67         scheme := "https"
68         if !ssi.TLS {
69                 scheme = "http"
70         }
71         if svc.InternalURLs == nil {
72                 svc.InternalURLs = map[arvados.URL]arvados.ServiceInstance{}
73         }
74         host := ssi.Listen
75         if host == "" {
76                 return
77         }
78         if strings.HasPrefix(host, ":") {
79                 host = hostname + host
80         }
81         svc.InternalURLs[arvados.URL{Scheme: scheme, Host: host}] = arvados.ServiceInstance{}
82 }