15003: Split out deprecated config stuff.
[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                                 applyDeprecatedNodeProfile(hostname, np.RailsAPI, &cluster.Services.RailsAPI)
48                                 applyDeprecatedNodeProfile(hostname, np.Controller, &cluster.Services.Controller)
49                                 applyDeprecatedNodeProfile(hostname, np.DispatchCloud, &cluster.Services.DispatchCloud)
50                         }
51                 }
52                 if dst, n := &cluster.API.MaxItemsPerResponse, dcluster.RequestLimits.MaxItemsPerResponse; n != nil && *n != *dst {
53                         log.Warnf("overriding Clusters.%s.API.MaxItemsPerResponse with deprecated config RequestLimits.MultiClusterRequestConcurrency = %d", id, *n)
54                         *dst = *n
55                 }
56                 if dst, n := &cluster.API.MaxRequestAmplification, dcluster.RequestLimits.MultiClusterRequestConcurrency; n != nil && *n != *dst {
57                         log.Warnf("overriding Clusters.%s.API.MaxRequestAmplification with deprecated config RequestLimits.MultiClusterRequestConcurrency = %d", id, *n)
58                         *dst = *n
59                 }
60                 cfg.Clusters[id] = cluster
61         }
62         return nil
63 }
64
65 func applyDeprecatedNodeProfile(hostname string, ssi arvados.SystemServiceInstance, svc *arvados.Service) {
66         scheme := "https"
67         if !ssi.TLS {
68                 scheme = "http"
69         }
70         if svc.InternalURLs == nil {
71                 svc.InternalURLs = map[arvados.URL]arvados.ServiceInstance{}
72         }
73         host := ssi.Listen
74         if host == "" {
75                 return
76         }
77         if strings.HasPrefix(host, ":") {
78                 host = hostname + host
79         }
80         svc.InternalURLs[arvados.URL{Scheme: scheme, Host: host}] = arvados.ServiceInstance{}
81 }