14931: Tag and filter instances by SetID, so driver doesn't need to.
[arvados.git] / lib / dispatchcloud / driver.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package dispatchcloud
6
7 import (
8         "fmt"
9         "time"
10
11         "git.curoverse.com/arvados.git/lib/cloud"
12         "git.curoverse.com/arvados.git/lib/cloud/azure"
13         "git.curoverse.com/arvados.git/lib/cloud/ec2"
14         "git.curoverse.com/arvados.git/sdk/go/arvados"
15         "github.com/sirupsen/logrus"
16         "golang.org/x/crypto/ssh"
17 )
18
19 var drivers = map[string]cloud.Driver{
20         "azure": azure.Driver,
21         "ec2":   ec2.Driver,
22 }
23
24 func newInstanceSet(cluster *arvados.Cluster, setID cloud.InstanceSetID, logger logrus.FieldLogger) (cloud.InstanceSet, error) {
25         driver, ok := drivers[cluster.Containers.CloudVMs.Driver]
26         if !ok {
27                 return nil, fmt.Errorf("unsupported cloud driver %q", cluster.Containers.CloudVMs.Driver)
28         }
29         is, err := driver.InstanceSet(cluster.Containers.CloudVMs.DriverParameters, setID, logger)
30         if maxops := cluster.Containers.CloudVMs.MaxCloudOpsPerSecond; maxops > 0 {
31                 is = rateLimitedInstanceSet{
32                         InstanceSet: is,
33                         ticker:      time.NewTicker(time.Second / time.Duration(maxops)),
34                 }
35         }
36         is = defaultTaggingInstanceSet{
37                 InstanceSet: is,
38                 defaultTags: cloud.InstanceTags(cluster.Containers.CloudVMs.ResourceTags),
39         }
40         is = filteringInstanceSet{
41                 InstanceSet: is,
42                 logger:      logger,
43         }
44         return is, err
45 }
46
47 type rateLimitedInstanceSet struct {
48         cloud.InstanceSet
49         ticker *time.Ticker
50 }
51
52 func (is rateLimitedInstanceSet) Create(it arvados.InstanceType, image cloud.ImageID, tags cloud.InstanceTags, init cloud.InitCommand, pk ssh.PublicKey) (cloud.Instance, error) {
53         <-is.ticker.C
54         inst, err := is.InstanceSet.Create(it, image, tags, init, pk)
55         return &rateLimitedInstance{inst, is.ticker}, err
56 }
57
58 type rateLimitedInstance struct {
59         cloud.Instance
60         ticker *time.Ticker
61 }
62
63 func (inst *rateLimitedInstance) Destroy() error {
64         <-inst.ticker.C
65         return inst.Instance.Destroy()
66 }
67
68 // Adds the specified defaultTags to every Create() call.
69 type defaultTaggingInstanceSet struct {
70         cloud.InstanceSet
71         defaultTags cloud.InstanceTags
72 }
73
74 func (is defaultTaggingInstanceSet) Create(it arvados.InstanceType, image cloud.ImageID, tags cloud.InstanceTags, init cloud.InitCommand, pk ssh.PublicKey) (cloud.Instance, error) {
75         allTags := cloud.InstanceTags{}
76         for k, v := range is.defaultTags {
77                 allTags[k] = v
78         }
79         for k, v := range tags {
80                 allTags[k] = v
81         }
82         return is.InstanceSet.Create(it, image, allTags, init, pk)
83 }
84
85 // Filters the instances returned by the wrapped InstanceSet's
86 // Instances() method (in case the wrapped InstanceSet didn't do this
87 // itself).
88 type filteringInstanceSet struct {
89         cloud.InstanceSet
90         logger logrus.FieldLogger
91 }
92
93 func (is filteringInstanceSet) Instances(tags cloud.InstanceTags) ([]cloud.Instance, error) {
94         instances, err := is.InstanceSet.Instances(tags)
95
96         skipped := 0
97         var returning []cloud.Instance
98 nextInstance:
99         for _, inst := range instances {
100                 instTags := inst.Tags()
101                 for k, v := range tags {
102                         if instTags[k] != v {
103                                 skipped++
104                                 continue nextInstance
105                         }
106                 }
107                 returning = append(returning, inst)
108         }
109         is.logger.WithFields(logrus.Fields{
110                 "returning": len(returning),
111                 "skipped":   skipped,
112         }).WithError(err).Debugf("filteringInstanceSet returning instances")
113         return returning, err
114 }