14931: Support extra tags on resources created by dispatchcloud.
authorTom Clegg <tclegg@veritasgenetics.com>
Tue, 28 May 2019 19:39:11 +0000 (15:39 -0400)
committerTom Clegg <tclegg@veritasgenetics.com>
Fri, 31 May 2019 17:57:32 +0000 (13:57 -0400)
Arvados-DCO-1.1-Signed-off-by: Tom Clegg <tclegg@veritasgenetics.com>

lib/cloud/azure/azure.go
lib/cloud/ec2/ec2.go
lib/cloud/ec2/ec2_test.go
lib/config/config.default.yml
lib/config/generated_config.go
lib/dispatchcloud/dispatcher_test.go
lib/dispatchcloud/driver.go
sdk/go/arvados/config.go

index ac7ff14cc2539ff7c1305fc7df393c7e36d0a795..03d2550bb693665e5fc246a13b169cb73c63cba7 100644 (file)
@@ -50,8 +50,6 @@ type azureInstanceSetConfig struct {
        AdminUsername                string
 }
 
-const tagKeyInstanceSecret = "InstanceSecret"
-
 type containerWrapper interface {
        GetBlobReference(name string) *storage.Blob
        ListBlobs(params storage.ListBlobsParameters) (storage.BlobListResponse, error)
@@ -352,14 +350,11 @@ func (az *azureInstanceSet) Create(
 
        name = az.namePrefix + name
 
-       timestamp := time.Now().Format(time.RFC3339Nano)
-
-       tags := make(map[string]*string)
-       tags["created-at"] = &timestamp
+       tags := map[string]*string{}
        for k, v := range newTags {
-               newstr := v
-               tags["dispatch-"+k] = &newstr
+               tags[k] = to.StringPtr(v)
        }
+       tags["created-at"] = to.StringPtr(time.Now().Format(time.RFC3339Nano))
 
        nicParameters := network.Interface{
                Location: &az.azconfig.Location,
@@ -499,9 +494,9 @@ func (az *azureInstanceSet) Instances(cloud.InstanceTags) ([]cloud.Instance, err
 }
 
 // ManageNics returns a list of Azure network interface resources.
-// Also performs garbage collection of NICs which have "namePrefix", are
-// not associated with a virtual machine and have a "create-at" time
-// more than DeleteDanglingResourcesAfter (to prevent racing and
+// Also performs garbage collection of NICs which have "namePrefix",
+// are not associated with a virtual machine and have a "created-at"
+// time more than DeleteDanglingResourcesAfter (to prevent racing and
 // deleting newly created NICs) in the past are deleted.
 func (az *azureInstanceSet) manageNics() (map[string]network.Interface, error) {
        az.stopWg.Add(1)
@@ -603,16 +598,12 @@ func (ai *azureInstance) SetTags(newTags cloud.InstanceTags) error {
        ai.provider.stopWg.Add(1)
        defer ai.provider.stopWg.Done()
 
-       tags := make(map[string]*string)
-
+       tags := map[string]*string{}
        for k, v := range ai.vm.Tags {
-               if !strings.HasPrefix(k, "dispatch-") {
-                       tags[k] = v
-               }
+               tags[k] = v
        }
        for k, v := range newTags {
-               newstr := v
-               tags["dispatch-"+k] = &newstr
+               tags[k] = to.StringPtr(v)
        }
 
        vmParameters := compute.VirtualMachine{
@@ -629,14 +620,10 @@ func (ai *azureInstance) SetTags(newTags cloud.InstanceTags) error {
 }
 
 func (ai *azureInstance) Tags() cloud.InstanceTags {
-       tags := make(map[string]string)
-
+       tags := cloud.InstanceTags{}
        for k, v := range ai.vm.Tags {
-               if strings.HasPrefix(k, "dispatch-") {
-                       tags[k[9:]] = *v
-               }
+               tags[k] = *v
        }
-
        return tags
 }
 
index c5565d424559f0bba2841dd46df62d3af883cc19..e2ad6b42b287e9470161f08d1401992c2e54fced 100644 (file)
@@ -13,7 +13,6 @@ import (
        "encoding/json"
        "fmt"
        "math/big"
-       "strings"
        "sync"
 
        "git.curoverse.com/arvados.git/lib/cloud"
@@ -26,8 +25,7 @@ import (
        "golang.org/x/crypto/ssh"
 )
 
-const arvadosDispatchID = "arvados-dispatch-id"
-const tagPrefix = "arvados-dispatch-tag-"
+const tagKeyInstanceSetID = "arvados-dispatch-id"
 
 // Driver is the ec2 implementation of the cloud.Driver interface.
 var Driver = cloud.DriverFunc(newEC2InstanceSet)
@@ -52,18 +50,18 @@ type ec2Interface interface {
 }
 
 type ec2InstanceSet struct {
-       ec2config    ec2InstanceSetConfig
-       dispatcherID cloud.InstanceSetID
-       logger       logrus.FieldLogger
-       client       ec2Interface
-       keysMtx      sync.Mutex
-       keys         map[string]string
+       ec2config     ec2InstanceSetConfig
+       instanceSetID cloud.InstanceSetID
+       logger        logrus.FieldLogger
+       client        ec2Interface
+       keysMtx       sync.Mutex
+       keys          map[string]string
 }
 
-func newEC2InstanceSet(config json.RawMessage, dispatcherID cloud.InstanceSetID, logger logrus.FieldLogger) (prv cloud.InstanceSet, err error) {
+func newEC2InstanceSet(config json.RawMessage, instanceSetID cloud.InstanceSetID, logger logrus.FieldLogger) (prv cloud.InstanceSet, err error) {
        instanceSet := &ec2InstanceSet{
-               dispatcherID: dispatcherID,
-               logger:       logger,
+               instanceSetID: instanceSetID,
+               logger:        logger,
        }
        err = json.Unmarshal(config, &instanceSet.ec2config)
        if err != nil {
@@ -159,17 +157,13 @@ func (instanceSet *ec2InstanceSet) Create(
 
        ec2tags := []*ec2.Tag{
                &ec2.Tag{
-                       Key:   aws.String(arvadosDispatchID),
-                       Value: aws.String(string(instanceSet.dispatcherID)),
-               },
-               &ec2.Tag{
-                       Key:   aws.String("arvados-class"),
-                       Value: aws.String("dynamic-compute"),
+                       Key:   aws.String(tagKeyInstanceSetID),
+                       Value: aws.String(string(instanceSet.instanceSetID)),
                },
        }
        for k, v := range newTags {
                ec2tags = append(ec2tags, &ec2.Tag{
-                       Key:   aws.String(tagPrefix + k),
+                       Key:   aws.String(k),
                        Value: aws.String(v),
                })
        }
@@ -191,12 +185,12 @@ func (instanceSet *ec2InstanceSet) Create(
                        }},
                DisableApiTermination:             aws.Bool(false),
                InstanceInitiatedShutdownBehavior: aws.String("terminate"),
-               UserData: aws.String(base64.StdEncoding.EncodeToString([]byte("#!/bin/sh\n" + initCommand + "\n"))),
                TagSpecifications: []*ec2.TagSpecification{
                        &ec2.TagSpecification{
                                ResourceType: aws.String("instance"),
                                Tags:         ec2tags,
                        }},
+               UserData: aws.String(base64.StdEncoding.EncodeToString([]byte("#!/bin/sh\n" + initCommand + "\n"))),
        }
 
        if instanceType.AddedScratch > 0 {
@@ -233,8 +227,8 @@ func (instanceSet *ec2InstanceSet) Create(
 func (instanceSet *ec2InstanceSet) Instances(cloud.InstanceTags) (instances []cloud.Instance, err error) {
        dii := &ec2.DescribeInstancesInput{
                Filters: []*ec2.Filter{&ec2.Filter{
-                       Name:   aws.String("tag:" + arvadosDispatchID),
-                       Values: []*string{aws.String(string(instanceSet.dispatcherID))},
+                       Name:   aws.String("tag:" + tagKeyInstanceSetID),
+                       Values: []*string{aws.String(string(instanceSet.instanceSetID))},
                }}}
 
        for {
@@ -280,13 +274,13 @@ func (inst *ec2Instance) ProviderType() string {
 func (inst *ec2Instance) SetTags(newTags cloud.InstanceTags) error {
        ec2tags := []*ec2.Tag{
                &ec2.Tag{
-                       Key:   aws.String(arvadosDispatchID),
-                       Value: aws.String(string(inst.provider.dispatcherID)),
+                       Key:   aws.String(tagKeyInstanceSetID),
+                       Value: aws.String(string(inst.provider.instanceSetID)),
                },
        }
        for k, v := range newTags {
                ec2tags = append(ec2tags, &ec2.Tag{
-                       Key:   aws.String(tagPrefix + k),
+                       Key:   aws.String(k),
                        Value: aws.String(v),
                })
        }
@@ -303,9 +297,7 @@ func (inst *ec2Instance) Tags() cloud.InstanceTags {
        tags := make(map[string]string)
 
        for _, t := range inst.instance.Tags {
-               if strings.HasPrefix(*t.Key, tagPrefix) {
-                       tags[(*t.Key)[len(tagPrefix):]] = *t.Value
-               }
+               tags[*t.Key] = *t.Value
        }
 
        return tags
index 50ba01174e6045d3967f5e7cb7f8f668847f59bd..62a0b8564f6e1c0b21e66a126a0ce33255740d0c 100644 (file)
@@ -125,11 +125,11 @@ func GetInstanceSet() (cloud.InstanceSet, cloud.ImageID, arvados.Cluster, error)
                return ap, cloud.ImageID(exampleCfg.ImageIDForTestSuite), cluster, err
        }
        ap := ec2InstanceSet{
-               ec2config:    ec2InstanceSetConfig{},
-               dispatcherID: "test123",
-               logger:       logrus.StandardLogger(),
-               client:       &ec2stub{},
-               keys:         make(map[string]string),
+               ec2config:     ec2InstanceSetConfig{},
+               instanceSetID: "test123",
+               logger:        logrus.StandardLogger(),
+               client:        &ec2stub{},
+               keys:          make(map[string]string),
        }
        return &ap, cloud.ImageID("blob"), cluster, nil
 }
index f19238f42adc722010ae2a14f4fa7a70fe507d06..b1ed91d4bbc7250a0736081abd61100569c70118 100644 (file)
@@ -530,6 +530,13 @@ Clusters:
         # Worker VM image ID.
         ImageID: ami-01234567890abcdef
 
+        # Tags to add on all resources (VMs, NICs, disks) created by
+        # the container dispatcher. (Arvados's own tags --
+        # InstanceType, IdleBehavior, and InstanceSecret -- will also
+        # be added.)
+        ResourceTags:
+          SAMPLE: "tag value"
+
         # Cloud driver: "azure" (Microsoft Azure) or "ec2" (Amazon AWS).
         Driver: ec2
 
index 59feeec4939cc7bd523034f97cbebf94b02376cb..513eee3622463d8209dd9cd26ba713964ef9233f 100644 (file)
@@ -536,6 +536,13 @@ Clusters:
         # Worker VM image ID.
         ImageID: ami-01234567890abcdef
 
+        # Tags to add on all resources (VMs, NICs, disks) created by
+        # the container dispatcher. (Arvados's own tags --
+        # InstanceType, IdleBehavior, and InstanceSecret -- will also
+        # be added.)
+        ResourceTags:
+          SAMPLE: "tag value"
+
         # Cloud driver: "azure" (Microsoft Azure) or "ec2" (Amazon AWS).
         Driver: ec2
 
index 6b8620ade3d3dbf4f36a1230cb17ae12426c31c5..11786bd3cc5713119821601ecd18d1aeb76a045d 100644 (file)
@@ -65,6 +65,7 @@ func (s *DispatcherSuite) SetUpTest(c *check.C) {
                                MaxProbesPerSecond:   1000,
                                TimeoutSignal:        arvados.Duration(3 * time.Millisecond),
                                TimeoutTERM:          arvados.Duration(20 * time.Millisecond),
+                               ResourceTags:         map[string]string{"testtag": "test value"},
                        },
                },
                InstanceTypes: arvados.InstanceTypeMap{
index 5ec0f73e75bc035a3f2dad725c028e8c5b8502a8..3f1601188062b9e99703412d4a737012ccef762f 100644 (file)
@@ -33,6 +33,10 @@ func newInstanceSet(cluster *arvados.Cluster, setID cloud.InstanceSetID, logger
                        ticker:      time.NewTicker(time.Second / time.Duration(maxops)),
                }
        }
+       is = defaultTaggingInstanceSet{
+               InstanceSet: is,
+               defaultTags: cloud.InstanceTags(cluster.Containers.CloudVMs.ResourceTags),
+       }
        return is, err
 }
 
@@ -56,3 +60,20 @@ func (inst *rateLimitedInstance) Destroy() error {
        <-inst.ticker.C
        return inst.Instance.Destroy()
 }
+
+// Adds the specified defaultTags to every Create() call.
+type defaultTaggingInstanceSet struct {
+       cloud.InstanceSet
+       defaultTags cloud.InstanceTags
+}
+
+func (is defaultTaggingInstanceSet) Create(it arvados.InstanceType, image cloud.ImageID, tags cloud.InstanceTags, init cloud.InitCommand, pk ssh.PublicKey) (cloud.Instance, error) {
+       allTags := cloud.InstanceTags{}
+       for k, v := range is.defaultTags {
+               allTags[k] = v
+       }
+       for k, v := range tags {
+               allTags[k] = v
+       }
+       return is.InstanceSet.Create(it, image, allTags, init, pk)
+}
index 275eb9e1905262fb970f2db5ad6e33ea5ba96757..4356f0003d3421ff1d4f8b4a27e16bcf0c810701 100644 (file)
@@ -169,6 +169,7 @@ type CloudVMsConfig struct {
        TimeoutShutdown      Duration
        TimeoutSignal        Duration
        TimeoutTERM          Duration
+       ResourceTags         map[string]string
 
        Driver           string
        DriverParameters json.RawMessage