Merge branch '16723-kill-vs-requeue'
[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 // Adds the specified defaultTags to every Create() call.
75 type defaultTaggingInstanceSet struct {
76         cloud.InstanceSet
77         defaultTags cloud.InstanceTags
78 }
79
80 func (is defaultTaggingInstanceSet) Create(it arvados.InstanceType, image cloud.ImageID, tags cloud.InstanceTags, init cloud.InitCommand, pk ssh.PublicKey) (cloud.Instance, error) {
81         allTags := cloud.InstanceTags{}
82         for k, v := range is.defaultTags {
83                 allTags[k] = v
84         }
85         for k, v := range tags {
86                 allTags[k] = v
87         }
88         return is.InstanceSet.Create(it, image, allTags, init, pk)
89 }
90
91 // Filter the instances returned by the wrapped InstanceSet's
92 // Instances() method (in case the wrapped InstanceSet didn't do this
93 // itself).
94 type filteringInstanceSet struct {
95         cloud.InstanceSet
96         logger logrus.FieldLogger
97 }
98
99 func (is filteringInstanceSet) Instances(tags cloud.InstanceTags) ([]cloud.Instance, error) {
100         instances, err := is.InstanceSet.Instances(tags)
101
102         skipped := 0
103         var returning []cloud.Instance
104 nextInstance:
105         for _, inst := range instances {
106                 instTags := inst.Tags()
107                 for k, v := range tags {
108                         if instTags[k] != v {
109                                 skipped++
110                                 continue nextInstance
111                         }
112                 }
113                 returning = append(returning, inst)
114         }
115         is.logger.WithFields(logrus.Fields{
116                 "returning": len(returning),
117                 "skipped":   skipped,
118         }).WithError(err).Debugf("filteringInstanceSet returning instances")
119         return returning, err
120 }
121
122 func newInstrumentedInstanceSet(is cloud.InstanceSet, reg *prometheus.Registry) cloud.InstanceSet {
123         cv := prometheus.NewCounterVec(prometheus.CounterOpts{
124                 Namespace: "arvados",
125                 Subsystem: "dispatchcloud",
126                 Name:      "driver_operations",
127                 Help:      "Number of instance-create/destroy/list operations performed via cloud driver.",
128         }, []string{"operation", "error"})
129
130         // Create all counters, so they are reported with zero values
131         // (instead of being missing) until they are incremented.
132         for _, op := range []string{"Create", "List", "Destroy", "SetTags"} {
133                 for _, error := range []string{"0", "1"} {
134                         cv.WithLabelValues(op, error).Add(0)
135                 }
136         }
137
138         reg.MustRegister(cv)
139         return instrumentedInstanceSet{is, cv}
140 }
141
142 type instrumentedInstanceSet struct {
143         cloud.InstanceSet
144         cv *prometheus.CounterVec
145 }
146
147 func (is instrumentedInstanceSet) Create(it arvados.InstanceType, image cloud.ImageID, tags cloud.InstanceTags, init cloud.InitCommand, pk ssh.PublicKey) (cloud.Instance, error) {
148         inst, err := is.InstanceSet.Create(it, image, tags, init, pk)
149         is.cv.WithLabelValues("Create", boolLabelValue(err != nil)).Inc()
150         return instrumentedInstance{inst, is.cv}, err
151 }
152
153 func (is instrumentedInstanceSet) Instances(tags cloud.InstanceTags) ([]cloud.Instance, error) {
154         instances, err := is.InstanceSet.Instances(tags)
155         is.cv.WithLabelValues("List", boolLabelValue(err != nil)).Inc()
156         var instrumented []cloud.Instance
157         for _, i := range instances {
158                 instrumented = append(instrumented, instrumentedInstance{i, is.cv})
159         }
160         return instrumented, err
161 }
162
163 type instrumentedInstance struct {
164         cloud.Instance
165         cv *prometheus.CounterVec
166 }
167
168 func (inst instrumentedInstance) Destroy() error {
169         err := inst.Instance.Destroy()
170         inst.cv.WithLabelValues("Destroy", boolLabelValue(err != nil)).Inc()
171         return err
172 }
173
174 func (inst instrumentedInstance) SetTags(tags cloud.InstanceTags) error {
175         err := inst.Instance.SetTags(tags)
176         inst.cv.WithLabelValues("SetTags", boolLabelValue(err != nil)).Inc()
177         return err
178 }
179
180 func boolLabelValue(v bool) string {
181         if v {
182                 return "1"
183         }
184         return "0"
185 }