1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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"
19 var drivers = map[string]cloud.Driver{
20 "azure": azure.Driver,
24 func newInstanceSet(cluster *arvados.Cluster, setID cloud.InstanceSetID, logger logrus.FieldLogger) (cloud.InstanceSet, error) {
25 driver, ok := drivers[cluster.Containers.CloudVMs.Driver]
27 return nil, fmt.Errorf("unsupported cloud driver %q", cluster.Containers.CloudVMs.Driver)
29 is, err := driver.InstanceSet(cluster.Containers.CloudVMs.DriverParameters, setID, logger)
30 if maxops := cluster.Containers.CloudVMs.MaxCloudOpsPerSecond; maxops > 0 {
31 is = rateLimitedInstanceSet{
33 ticker: time.NewTicker(time.Second / time.Duration(maxops)),
36 is = defaultTaggingInstanceSet{
38 defaultTags: cloud.InstanceTags(cluster.Containers.CloudVMs.ResourceTags),
40 is = filteringInstanceSet{
47 type rateLimitedInstanceSet struct {
52 func (is rateLimitedInstanceSet) Create(it arvados.InstanceType, image cloud.ImageID, tags cloud.InstanceTags, init cloud.InitCommand, pk ssh.PublicKey) (cloud.Instance, error) {
54 inst, err := is.InstanceSet.Create(it, image, tags, init, pk)
55 return &rateLimitedInstance{inst, is.ticker}, err
58 type rateLimitedInstance struct {
63 func (inst *rateLimitedInstance) Destroy() error {
65 return inst.Instance.Destroy()
68 // Adds the specified defaultTags to every Create() call.
69 type defaultTaggingInstanceSet struct {
71 defaultTags cloud.InstanceTags
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 {
79 for k, v := range tags {
82 return is.InstanceSet.Create(it, image, allTags, init, pk)
85 // Filters the instances returned by the wrapped InstanceSet's
86 // Instances() method (in case the wrapped InstanceSet didn't do this
88 type filteringInstanceSet struct {
90 logger logrus.FieldLogger
93 func (is filteringInstanceSet) Instances(tags cloud.InstanceTags) ([]cloud.Instance, error) {
94 instances, err := is.InstanceSet.Instances(tags)
97 var returning []cloud.Instance
99 for _, inst := range instances {
100 instTags := inst.Tags()
101 for k, v := range tags {
102 if instTags[k] != v {
104 continue nextInstance
107 returning = append(returning, inst)
109 is.logger.WithFields(logrus.Fields{
110 "returning": len(returning),
112 }).WithError(err).Debugf("filteringInstanceSet returning instances")
113 return returning, err