mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-16 22:53:22 +00:00
Adds support for attaching GCEPersitentDisks
Adds GCEPersistentDisk volume struct Adds gce-utils to attach disk to kubelet's VM. Updates config to give compute-rw to every minion. Adds GCEPersistentDisk to API Adds ability to mount attached disks Generalizes PD and adds tests. PD now uses an pluggable API interface. Unit Tests more cleanly separates TearDown and SetUp Modify boilerplate hook to omit build tags Adds Mounter interface; mount is now built by OS TearDown() for PD now detaches disk on final refcount Un-generalized PD; GCE calls moved to cloudprovider Address comments.
This commit is contained in:
committed by
Brendan Burns
parent
48a9ed3147
commit
4ec25f3b81
@@ -23,6 +23,7 @@ import (
|
||||
"net"
|
||||
"net/http"
|
||||
"os/exec"
|
||||
"path"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -41,43 +42,71 @@ type GCECloud struct {
|
||||
service *compute.Service
|
||||
projectID string
|
||||
zone string
|
||||
instanceRE string
|
||||
instanceID string
|
||||
}
|
||||
|
||||
func init() {
|
||||
cloudprovider.RegisterCloudProvider("gce", func(config io.Reader) (cloudprovider.Interface, error) { return newGCECloud() })
|
||||
}
|
||||
|
||||
func getProjectAndZone() (string, string, error) {
|
||||
func getMetadata(url string) (string, error) {
|
||||
client := http.Client{}
|
||||
url := "http://metadata/computeMetadata/v1/instance/zone"
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
return "", err
|
||||
}
|
||||
req.Header.Add("X-Google-Metadata-Request", "True")
|
||||
res, err := client.Do(req)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
return "", err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
data, err := ioutil.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(data), nil
|
||||
}
|
||||
|
||||
func getProjectAndZone() (string, string, error) {
|
||||
url := "http://metadata/computeMetadata/v1/instance/zone"
|
||||
result, err := getMetadata(url)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
parts := strings.Split(string(data), "/")
|
||||
parts := strings.Split(result, "/")
|
||||
if len(parts) != 4 {
|
||||
return "", "", fmt.Errorf("Unexpected response: %s", string(data))
|
||||
return "", "", fmt.Errorf("Unexpected response: %s", result)
|
||||
}
|
||||
return parts[1], parts[3], nil
|
||||
}
|
||||
|
||||
func getInstanceID() (string, error) {
|
||||
url := "http://metadata/computeMetadata/v1/instance/hostname"
|
||||
result, err := getMetadata(url)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
parts := strings.Split(result, ".")
|
||||
if len(parts) == 0 {
|
||||
return "", fmt.Errorf("Unexpected response: %s", result)
|
||||
}
|
||||
return parts[0], nil
|
||||
}
|
||||
|
||||
// newGCECloud creates a new instance of GCECloud.
|
||||
func newGCECloud() (*GCECloud, error) {
|
||||
projectID, zone, err := getProjectAndZone()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// TODO: if we want to use this on a machine that doesn't have the http://metadata server
|
||||
// e.g. on a user's machine (not VM) somewhere, we need to have an alternative for
|
||||
// instance id lookup.
|
||||
instanceID, err := getInstanceID()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
client, err := serviceaccount.NewClient(&serviceaccount.Options{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -87,9 +116,10 @@ func newGCECloud() (*GCECloud, error) {
|
||||
return nil, err
|
||||
}
|
||||
return &GCECloud{
|
||||
service: svc,
|
||||
projectID: projectID,
|
||||
zone: zone,
|
||||
service: svc,
|
||||
projectID: projectID,
|
||||
zone: zone,
|
||||
instanceID: instanceID,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -310,6 +340,29 @@ func (gce *GCECloud) GetZone() (cloudprovider.Zone, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (gce *GCECloud) AttachDisk(diskName string, readOnly bool) error {
|
||||
disk, err := gce.getDisk(diskName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
readWrite := "READ_WRITE"
|
||||
if readOnly {
|
||||
readWrite = "READ_ONLY"
|
||||
}
|
||||
attachedDisk := gce.convertDiskToAttachedDisk(disk, readWrite)
|
||||
_, err = gce.service.Instances.AttachDisk(gce.projectID, gce.zone, gce.instanceID, attachedDisk).Do()
|
||||
return err
|
||||
}
|
||||
|
||||
func (gce *GCECloud) DetachDisk(devicePath string) error {
|
||||
_, err := gce.service.Instances.DetachDisk(gce.projectID, gce.zone, gce.instanceID, devicePath).Do()
|
||||
return err
|
||||
}
|
||||
|
||||
func (gce *GCECloud) getDisk(diskName string) (*compute.Disk, error) {
|
||||
return gce.service.Disks.Get(gce.projectID, gce.zone, diskName).Do()
|
||||
}
|
||||
|
||||
// getGceRegion returns region of the gce zone. Zone names
|
||||
// are of the form: ${region-name}-${ix}.
|
||||
// For example "us-central1-b" has a region of "us-central1".
|
||||
@@ -321,3 +374,14 @@ func getGceRegion(zone string) (string, error) {
|
||||
}
|
||||
return zone[:ix], nil
|
||||
}
|
||||
|
||||
// Converts a Disk resource to an AttachedDisk resource.
|
||||
func (gce *GCECloud) convertDiskToAttachedDisk(disk *compute.Disk, readWrite string) *compute.AttachedDisk {
|
||||
return &compute.AttachedDisk{
|
||||
DeviceName: disk.Name,
|
||||
Kind: disk.Kind,
|
||||
Mode: readWrite,
|
||||
Source: "https://" + path.Join("www.googleapis.com/compute/v1/projects/", gce.projectID, "zones", gce.zone, "disks", disk.Name),
|
||||
Type: "PERSISTENT",
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user