Tag dynamically created GCE PD disks.

GCE disks don't have tags, we must encode the tags into Description field.
It's encoded as JSON, which is both human and machine readable:
description: '{"kubernetes.io/created-for/pv/name":"pv-gce-oxwts","kubernetes.io/created-for/pvc/name":"myclaim","kubernetes.io/created-for/pvc/namespace":"default"}'
This commit is contained in:
Jan Safranek
2016-01-27 15:16:05 +01:00
parent 63ec304e42
commit 23cd0913f7
3 changed files with 33 additions and 7 deletions

View File

@@ -17,6 +17,7 @@ limitations under the License.
package gce
import (
"encoding/json"
"fmt"
"io"
"net"
@@ -1617,12 +1618,36 @@ func (gce *GCECloud) GetZone() (cloudprovider.Zone, error) {
}, nil
}
// Create a new Persistent Disk, with the specified name & size, in the specified zone.
func (gce *GCECloud) CreateDisk(name string, zone string, sizeGb int64) error {
diskToCreate := &compute.Disk{
Name: name,
SizeGb: sizeGb,
// encodeDiskTags encodes requested volume tags into JSON string, as GCE does
// not support tags on GCE PDs and we use Description field as fallback.
func (gce *GCECloud) encodeDiskTags(tags map[string]string) (string, error) {
if len(tags) == 0 {
// No tags -> empty JSON
return "", nil
}
enc, err := json.Marshal(tags)
if err != nil {
return "", err
}
return string(enc), nil
}
// CreateDisk creates a new Persistent Disk, with the specified name & size, in
// the specified zone. It stores specified tags endoced in JSON in Description
// field.
func (gce *GCECloud) CreateDisk(name string, zone string, sizeGb int64, tags map[string]string) error {
tagsStr, err := gce.encodeDiskTags(tags)
if err != nil {
return err
}
diskToCreate := &compute.Disk{
Name: name,
SizeGb: sizeGb,
Description: tagsStr,
}
createOp, err := gce.service.Disks.Insert(gce.projectID, zone, diskToCreate).Do()
if err != nil {
return err