17529: Heed MaxCloudOpsPerSecond when calling SetTags.
[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.arvados.org/arvados.git/lib/cloud"
12         "git.arvados.org/arvados.git/lib/cloud/azure"
13         "git.arvados.org/arvados.git/lib/cloud/ec2"
14         "git.arvados.org/arvados.git/sdk/go/arvados"
15         "github.com/prometheus/client_golang/prometheus"
16         "github.com/sirupsen/logrus"
17         "golang.org/x/crypto/ssh"
18 )
19
20 // Drivers is a map of available cloud drivers.
21 // Clusters.*.Containers.CloudVMs.Driver configuration values
22 // correspond to keys in this map.
23 var Drivers = map[string]cloud.Driver{
24         "azure": azure.Driver,
25         "ec2":   ec2.Driver,
26 }
27
28 func newInstanceSet(cluster *arvados.Cluster, setID cloud.InstanceSetID, logger logrus.FieldLogger, reg *prometheus.Registry) (cloud.InstanceSet, error) {
29         driver, ok := Drivers[cluster.Containers.CloudVMs.Driver]
30         if !ok {
31                 return nil, fmt.Errorf("unsupported cloud driver %q", cluster.Containers.CloudVMs.Driver)
32         }
33         sharedResourceTags := cloud.SharedResourceTags(cluster.Containers.CloudVMs.ResourceTags)
34         is, err := driver.InstanceSet(cluster.Containers.CloudVMs.DriverParameters, setID, sharedResourceTags, logger)
35         is = newInstrumentedInstanceSet(is, reg)
36         if maxops := cluster.Containers.CloudVMs.MaxCloudOpsPerSecond; maxops > 0 {
37                 is = rateLimitedInstanceSet{
38                         InstanceSet: is,
39                         ticker:      time.NewTicker(time.Second / time.Duration(maxops)),
40                 }
41         }
42         is = defaultTaggingInstanceSet{
43                 InstanceSet: is,
44                 defaultTags: cloud.InstanceTags(cluster.Containers.CloudVMs.ResourceTags),
45         }
46         is = filteringInstanceSet{
47                 InstanceSet: is,
48                 logger:      logger,
49         }
50         return is, err
51 }
52
53 type rateLimitedInstanceSet struct {
54         cloud.InstanceSet
55         ticker *time.Ticker
56 }
57
58 func (is rateLimitedInstanceSet) Create(it arvados.InstanceType, image cloud.ImageID, tags cloud.InstanceTags, init cloud.InitCommand, pk ssh.PublicKey) (cloud.Instance, error) {
59         <-is.ticker.C
60         inst, err := is.InstanceSet.Create(it, image, tags, init, pk)
61         return &rateLimitedInstance{inst, is.ticker}, err
62 }
63
64 type rateLimitedInstance struct {
65         cloud.Instance
66         ticker *time.Ticker
67 }
68
69 func (inst *rateLimitedInstance) Destroy() error {
70         <-inst.ticker.C
71         return inst.Instance.Destroy()
72 }
73
74 func (inst *rateLimitedInstance) SetTags(tags cloud.InstanceTags) error {
75         <-inst.ticker.C
76         return inst.Instance.SetTags(tags)
77 }
78
79 // Adds the specified defaultTags to every Create() call.
80 type defaultTaggingInstanceSet struct {
81         cloud.InstanceSet
82         defaultTags cloud.InstanceTags
83 }
84
85 func (is defaultTaggingInstanceSet) Create(it arvados.InstanceType, image cloud.ImageID, tags cloud.InstanceTags, init cloud.InitCommand, pk ssh.PublicKey) (cloud.Instance, error) {
86         allTags := cloud.InstanceTags{}
87         for k, v := range is.defaultTags {
88                 allTags[k] = v
89         }
90         for k, v := range tags {
91                 allTags[k] = v
92         }
93         return is.InstanceSet.Create(it, image, allTags, init, pk)
94 }
95
96 // Filter the instances returned by the wrapped InstanceSet's
97 // Instances() method (in case the wrapped InstanceSet didn't do this
98 // itself).
99 type filteringInstanceSet struct {
100         cloud.InstanceSet
101         logger logrus.FieldLogger
102 }
103
104 func (is filteringInstanceSet) Instances(tags cloud.InstanceTags) ([]cloud.Instance, error) {
105         instances, err := is.InstanceSet.Instances(tags)
106
107         skipped := 0
108         var returning []cloud.Instance
109 nextInstance:
110         for _, inst := range instances {
111                 instTags := inst.Tags()
112                 for k, v := range tags {
113                         if instTags[k] != v {
114                                 skipped++
115                                 continue nextInstance
116                         }
117                 }
118                 returning = append(returning, inst)
119         }
120         is.logger.WithFields(logrus.Fields{
121                 "returning": len(returning),
122                 "skipped":   skipped,
123         }).WithError(err).Debugf("filteringInstanceSet returning instances")
124         return returning, err
125 }
126
127 func newInstrumentedInstanceSet(is cloud.InstanceSet, reg *prometheus.Registry) cloud.InstanceSet {
128         cv := prometheus.NewCounterVec(prometheus.CounterOpts{
129                 Namespace: "arvados",
130                 Subsystem: "dispatchcloud",
131                 Name:      "driver_operations",
132                 Help:      "Number of instance-create/destroy/list operations performed via cloud driver.",
133         }, []string{"operation", "error"})
134
135         // Create all counters, so they are reported with zero values
136         // (instead of being missing) until they are incremented.
137         for _, op := range []string{"Create", "List", "Destroy", "SetTags"} {
138                 for _, error := range []string{"0", "1"} {
139                         cv.WithLabelValues(op, error).Add(0)
140                 }
141         }
142
143         reg.MustRegister(cv)
144         return instrumentedInstanceSet{is, cv}
145 }
146
147 type instrumentedInstanceSet struct {
148         cloud.InstanceSet
149         cv *prometheus.CounterVec
150 }
151
152 func (is instrumentedInstanceSet) Create(it arvados.InstanceType, image cloud.ImageID, tags cloud.InstanceTags, init cloud.InitCommand, pk ssh.PublicKey) (cloud.Instance, error) {
153         inst, err := is.InstanceSet.Create(it, image, tags, init, pk)
154         is.cv.WithLabelValues("Create", boolLabelValue(err != nil)).Inc()
155         return instrumentedInstance{inst, is.cv}, err
156 }
157
158 func (is instrumentedInstanceSet) Instances(tags cloud.InstanceTags) ([]cloud.Instance, error) {
159         instances, err := is.InstanceSet.Instances(tags)
160         is.cv.WithLabelValues("List", boolLabelValue(err != nil)).Inc()
161         var instrumented []cloud.Instance
162         for _, i := range instances {
163                 instrumented = append(instrumented, instrumentedInstance{i, is.cv})
164         }
165         return instrumented, err
166 }
167
168 type instrumentedInstance struct {
169         cloud.Instance
170         cv *prometheus.CounterVec
171 }
172
173 func (inst instrumentedInstance) Destroy() error {
174         err := inst.Instance.Destroy()
175         inst.cv.WithLabelValues("Destroy", boolLabelValue(err != nil)).Inc()
176         return err
177 }
178
179 func (inst instrumentedInstance) SetTags(tags cloud.InstanceTags) error {
180         err := inst.Instance.SetTags(tags)
181         inst.cv.WithLabelValues("SetTags", boolLabelValue(err != nil)).Inc()
182         return err
183 }
184
185 func boolLabelValue(v bool) string {
186         if v {
187                 return "1"
188         }
189         return "0"
190 }