Skip to content

Commit

Permalink
Added ec2 tags to volumes and snapshots
Browse files Browse the repository at this point in the history
Signed-off-by: Eugene Chupriyanov <tchu@tchu.ru>
  • Loading branch information
echupriyanov committed Mar 9, 2016
1 parent 6f75452 commit 3b4fdb9
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 5 deletions.
16 changes: 15 additions & 1 deletion .docs/user-guide/storage-providers.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ aws:
accessKey: MyAccessKey
secretKey: MySecretKey
```
### Volume tagging (optional)

By default, Rexray has access to all volumes and snapshots defined in Amazon account. Though sometimes it can be useful. it can be also overwhelming. Therefore, Rexray has an optional `rexrayTag` key in `aws` section to limit its view. When it is present, all volumes and snapshots created will have an additional EC2 tag `rexraySet` with a value defined in config. That way it not only limits Rexray view, but also makes possible to have different environments, like `prod`, `testing` or `development` each with its own set of volumes/snapshots.

It can be defined like that:
```yaml
rexray:
storageDrivers:
- ec2
aws:
accessKey: MyAccessKey
secretKey: MySecretKey
rexrayTag: prod
```

## Google Compute Engine
The Google Compute Engine (GCE) registers a storage driver named `gce` with the
Expand Down Expand Up @@ -327,7 +341,7 @@ parameter.
- By default the password is the same as your administrative MDM password.
- Start the gateway `service scaleio-gateway start`.
- With 1.32 we have noticed a restart of the gateway may be necessary as well
after an initial install with `service scaleio-gateway restart`.
after an initial install with `service scaleio-gateway restart`.

### Activating the Driver
To activate the ScaleIO driver please follow the instructions for
Expand Down
48 changes: 44 additions & 4 deletions drivers/storage/ec2/ec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ import (
)

const providerName = "ec2"
const rexrayTag = "rexraySet"

// The EC2 storage driver.
type driver struct {
instanceDocument *instanceIdentityDocument
ec2Instance *ec2.EC2
ec2Tag string
r *core.RexRay
}

Expand Down Expand Up @@ -84,6 +86,9 @@ func (d *driver) Init(r *core.RexRay) error {
if region == "" {
region = d.instanceDocument.Region
}

d.ec2Tag = d.rexrayTag()

d.ec2Instance = ec2.New(
auth,
aws.Regions[region],
Expand Down Expand Up @@ -231,9 +236,19 @@ func (d *driver) CreateSnapshot(
return nil, err
}

snapshotEc2Tags := []ec2.Tag{}

if snapshotName != "" {
snapshotEc2Tags = append(snapshotEc2Tags, ec2.Tag{"Name", snapshotName})
}

if d.ec2Tag != "" {
snapshotEc2Tags = append(snapshotEc2Tags, ec2.Tag{rexrayTag, d.ec2Tag})
}

if len(snapshotEc2Tags) > 0 {
_, err := d.ec2Instance.CreateTags(
[]string{resp.Id}, []ec2.Tag{{"Name", snapshotName}})
[]string{resp.Id}, snapshotEc2Tags)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -268,6 +283,10 @@ func (d *driver) getSnapshot(
filter.Add("volume-id", volumeID)
}

if d.ec2Tag != "" {
filter.Add(fmt.Sprintf("tag:%s", rexrayTag), d.ec2Tag)
}

snapshotList := []string{}
if snapshotID != "" {
//using snapshotList is returning stale data
Expand Down Expand Up @@ -513,8 +532,12 @@ func (d *driver) createVolumeCreateTags(
if volumeName == "" {
return
}
volumeEc2Tags := []ec2.Tag{{"Name", volumeName}}
if d.ec2Tag != "" {
volumeEc2Tags = append(volumeEc2Tags, ec2.Tag{rexrayTag, d.ec2Tag})
}
_, err = d.ec2Instance.CreateTags(
[]string{resp.VolumeId}, []ec2.Tag{{"Name", volumeName}})
[]string{resp.VolumeId}, volumeEc2Tags)

return
}
Expand Down Expand Up @@ -553,6 +576,10 @@ func (d *driver) getVolume(
filter.Add("tag:Name", fmt.Sprintf("%s", volumeName))
}

if d.ec2Tag != "" {
filter.Add(fmt.Sprintf("tag:%s", rexrayTag), d.ec2Tag)
}

volumeList := []string{}
if volumeID != "" {
volumeList = append(volumeList, volumeID)
Expand Down Expand Up @@ -871,10 +898,18 @@ func (d *driver) CopySnapshot(runAsync bool,
return nil, err
}

snapshotEc2Tags := []ec2.Tag{}
if destinationSnapshotName != "" {
snapshotEc2Tags = append(snapshotEc2Tags, ec2.Tag{"Name", destinationSnapshotName})
}

if d.ec2Tag != "" {
snapshotEc2Tags = append(snapshotEc2Tags, ec2.Tag{rexrayTag, d.ec2Tag})
}

if len(snapshotEc2Tags) > 0 {
_, err := d.ec2Instance.CreateTags(
[]string{resp.SnapshotId},
[]ec2.Tag{{"Name", destinationSnapshotName}})
[]string{resp.SnapshotId}, snapshotEc2Tags)

if err != nil {
return nil, err
Expand Down Expand Up @@ -902,6 +937,10 @@ func (d *driver) CopySnapshot(runAsync bool,
return snapshot[0], nil
}

func (d *driver) rexrayTag() string {
return d.r.Config.GetString("aws.rexrayTag")
}

func (d *driver) accessKey() string {
return d.r.Config.GetString("aws.accessKey")
}
Expand All @@ -919,5 +958,6 @@ func configRegistration() *gofig.Registration {
r.Key(gofig.String, "", "", "", "aws.accessKey")
r.Key(gofig.String, "", "", "", "aws.secretKey")
r.Key(gofig.String, "", "", "", "aws.region")
r.Key(gofig.String, "", "", "", "aws.rexrayTag")
return r
}

0 comments on commit 3b4fdb9

Please sign in to comment.