Skip to content

Commit

Permalink
Use GCE Beta API for labels
Browse files Browse the repository at this point in the history
  • Loading branch information
codenrhoden committed Feb 13, 2017
1 parent 65b13ab commit d7ee6bd
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 80 deletions.
131 changes: 60 additions & 71 deletions drivers/storage/gcepd/storage/gce_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
"sync"
"time"
Expand All @@ -26,15 +27,23 @@ import (
"github.com/codedellemc/libstorage/drivers/storage/gcepd/utils"

"golang.org/x/oauth2/google"
compute "google.golang.org/api/compute/v1"
compute "google.golang.org/api/compute/v0.beta"
"google.golang.org/api/googleapi"
)

const (
cacheKeyC = "cacheKey"
tagKey = "libstoragetag"
minDiskSizeGB = 10
)

var (
// GCE labels have to start with a lowercase letter, and have to end
// with a lowercase letter or numeral. In between can be lowercase
// letters, numbers or dashes
tagRegex = regexp.MustCompile(`^[a-z](?:[a-z0-9\-]*[a-z0-9])?$`)
)

type driver struct {
config gofig.Config
keyFile string
Expand Down Expand Up @@ -95,6 +104,9 @@ func (d *driver) Init(context types.Context, config gofig.Config) error {
}

d.tag = config.GetString("gcepd.tag")
if d.tag != "" && !tagRegex.MatchString(d.tag) {
return goof.New("Invalid GCE tag format")
}

context.Info("storage driver initialized")
return nil
Expand Down Expand Up @@ -606,20 +618,33 @@ func (d *driver) getDisks(
ctx types.Context,
zone *string) ([]*compute.Disk, error) {

diskList, err := mustSession(ctx).Disks.List(*d.projectID, *zone).Do()
diskListQ := mustSession(ctx).Disks.List(*d.projectID, *zone)
if d.tag != "" {
filter := fmt.Sprintf("labels.%s eq %s", tagKey, d.tag)
ctx.Debugf("query filter: %s", filter)
diskListQ.Filter(filter)
}

diskList, err := diskListQ.Do()
if err != nil {
ctx.Errorf("Error listing disks: %s", err)
return nil, err
}

disks := filterDisksByTag(d.tag, diskList.Items)
return disks, nil
return diskList.Items, nil
}

func (d *driver) getAggregatedDisks(
ctx types.Context) ([]*compute.Disk, error) {

aggList, err := mustSession(ctx).Disks.AggregatedList(*d.projectID).Do()
aggListQ := mustSession(ctx).Disks.AggregatedList(*d.projectID)
if d.tag != "" {
filter := fmt.Sprintf("labels.%s eq %s", tagKey, d.tag)
ctx.Debugf("query filter: %s", filter)
aggListQ.Filter(filter)
}

aggList, err := aggListQ.Do()
if err != nil {
ctx.Errorf("Error listing aggregated disks: %s", err)
return nil, err
Expand All @@ -633,7 +658,6 @@ func (d *driver) getAggregatedDisks(
}
}

disks = filterDisksByTag(d.tag, disks)
return disks, nil
}

Expand All @@ -653,13 +677,7 @@ func (d *driver) getDisk(
return nil, err
}

disks := []*compute.Disk{disk}
disks = filterDisksByTag(d.tag, disks)
if len(disks) == 0 {
return nil, nil
}

return disks[0], nil
return disk, nil
}

func (d *driver) getInstance(
Expand Down Expand Up @@ -795,16 +813,10 @@ func (d *driver) createVolume(
diskTypeURI := fmt.Sprintf("zones/%s/diskTypes/%s",
*opts.AvailabilityZone, diskType)

description, err := marshalDescription(&d.tag)
if err != nil {
return goof.WithError("Failed to marshal disk description", err)
}

createDisk := &compute.Disk{
Description: description,
Name: *volumeName,
SizeGb: *opts.Size,
Type: diskTypeURI,
Name: *volumeName,
SizeGb: *opts.Size,
Type: diskTypeURI,
}

asyncOp, err := mustSession(ctx).Disks.Insert(
Expand All @@ -819,6 +831,28 @@ func (d *driver) createVolume(
return err
}

if d.tag != "" {
/* In order to set the labels on a disk, we have to query the
disk first in order to get the generated label fingerprint
*/
disk, err := d.getDisk(ctx, opts.AvailabilityZone, volumeName)
if err != nil {
ctx.WithError(err).Warn(
"Unable to query disk for labeling")
return nil
}
labels := getLabels(&d.tag)
_, err = mustSession(ctx).Disks.SetLabels(
*d.projectID, *opts.AvailabilityZone, *volumeName,
&compute.ZoneSetLabelsRequest{
Labels: labels,
LabelFingerprint: disk.LabelFingerprint,
}).Do()
if err != nil {
ctx.WithError(err).Warn("Unable to label disk")
}
}

return nil
}

Expand Down Expand Up @@ -944,55 +978,10 @@ func (d *driver) detachVolume(
return asyncErr
}

type diskDescriptionFields struct {
Tag string `json:"libstoragetag"`
Comment string `json:"comment"`
}

const createdByLibS = "created by libStorage"

func marshalDescription(tag *string) (string, error) {
description := diskDescriptionFields{
Tag: *tag,
Comment: createdByLibS,
}

enc, err := json.Marshal(description)
if err != nil {
return "", err
func getLabels(tag *string) map[string]string {
labels := map[string]string{
tagKey: *tag,
}

return string(enc), nil
}

func unmarshalDescription(description *string) (*diskDescriptionFields, error) {
if description == nil || *description == "" {
return nil, nil
}

fields := &diskDescriptionFields{}

err := json.Unmarshal([]byte(*description), fields)
if err != nil {
return nil, err
}
return fields, nil
}

func filterDisksByTag(tag string, disks []*compute.Disk) []*compute.Disk {
if tag == "" {
return disks
}

var filteredDisks []*compute.Disk
for _, disk := range disks {
desc, err := unmarshalDescription(&disk.Description)
if err != nil || desc == nil || (desc != nil && desc.Tag != tag) {
log.Debugf("Skipping %s due to unmatched tag",
disk.Name)
continue
}
filteredDisks = append(filteredDisks, disk)
}
return filteredDisks
return labels
}
8 changes: 4 additions & 4 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,8 @@ import:
ref: d61af0b67e5c8213e8c260ac29a762487bad020d
repo: https://github.com/google/google-api-go-client
subpackages:
- compute/v1
- package: cloud.google.com/go
ref: b4ca3d4ba32e251f6fee7bda65c5727ccbf3faa9
subpackages:
- compute/metadata
- compute/v0.beta
- googleapi

################################################################################
## Build System Tools ##
Expand Down

0 comments on commit d7ee6bd

Please sign in to comment.