2017-08-02 17:27:17 +00:00
|
|
|
/*
|
2018-01-02 18:51:49 +00:00
|
|
|
Copyright 2017 the Heptio Ark contributors.
|
2017-08-02 17:27:17 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package azure
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2017-08-15 20:20:23 +00:00
|
|
|
"os"
|
2018-03-06 00:09:23 +00:00
|
|
|
"regexp"
|
2018-03-06 23:03:05 +00:00
|
|
|
"strings"
|
2017-08-02 17:27:17 +00:00
|
|
|
"time"
|
|
|
|
|
2017-08-15 20:20:23 +00:00
|
|
|
"github.com/Azure/azure-sdk-for-go/arm/disk"
|
|
|
|
"github.com/Azure/azure-sdk-for-go/arm/examples/helpers"
|
2017-08-18 17:53:05 +00:00
|
|
|
"github.com/Azure/go-autorest/autorest"
|
2017-08-15 20:20:23 +00:00
|
|
|
"github.com/Azure/go-autorest/autorest/azure"
|
2017-09-14 21:27:31 +00:00
|
|
|
"github.com/pkg/errors"
|
2017-08-02 17:27:17 +00:00
|
|
|
"github.com/satori/uuid"
|
2018-03-06 00:09:23 +00:00
|
|
|
|
2017-11-29 17:23:21 +00:00
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
2017-08-02 17:27:17 +00:00
|
|
|
|
|
|
|
"github.com/heptio/ark/pkg/cloudprovider"
|
2017-11-29 17:23:21 +00:00
|
|
|
"github.com/heptio/ark/pkg/util/collections"
|
2017-08-02 17:27:17 +00:00
|
|
|
)
|
|
|
|
|
2017-08-15 20:20:23 +00:00
|
|
|
const (
|
2018-03-06 00:09:23 +00:00
|
|
|
azureClientIDKey = "AZURE_CLIENT_ID"
|
|
|
|
azureClientSecretKey = "AZURE_CLIENT_SECRET"
|
|
|
|
azureSubscriptionIDKey = "AZURE_SUBSCRIPTION_ID"
|
|
|
|
azureTenantIDKey = "AZURE_TENANT_ID"
|
|
|
|
azureStorageAccountIDKey = "AZURE_STORAGE_ACCOUNT_ID"
|
|
|
|
azureStorageKeyKey = "AZURE_STORAGE_KEY"
|
|
|
|
azureResourceGroupKey = "AZURE_RESOURCE_GROUP"
|
|
|
|
apiTimeoutKey = "apiTimeout"
|
|
|
|
snapshotsResource = "snapshots"
|
|
|
|
disksResource = "disks"
|
2017-08-15 20:20:23 +00:00
|
|
|
)
|
|
|
|
|
2017-11-13 23:31:36 +00:00
|
|
|
type blockStore struct {
|
|
|
|
disks *disk.DisksClient
|
|
|
|
snaps *disk.SnapshotsClient
|
|
|
|
subscription string
|
|
|
|
resourceGroup string
|
|
|
|
apiTimeout time.Duration
|
|
|
|
}
|
|
|
|
|
2018-03-06 00:09:23 +00:00
|
|
|
type snapshotIdentifier struct {
|
|
|
|
subscription string
|
|
|
|
resourceGroup string
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
2018-04-23 19:43:24 +00:00
|
|
|
func (si *snapshotIdentifier) String() string {
|
|
|
|
return getComputeResourceName(si.subscription, si.resourceGroup, snapshotsResource, si.name)
|
|
|
|
}
|
|
|
|
|
2017-08-15 20:20:23 +00:00
|
|
|
func getConfig() map[string]string {
|
|
|
|
cfg := map[string]string{
|
|
|
|
azureClientIDKey: "",
|
|
|
|
azureClientSecretKey: "",
|
|
|
|
azureSubscriptionIDKey: "",
|
|
|
|
azureTenantIDKey: "",
|
|
|
|
azureStorageAccountIDKey: "",
|
|
|
|
azureStorageKeyKey: "",
|
|
|
|
azureResourceGroupKey: "",
|
|
|
|
}
|
|
|
|
|
|
|
|
for key := range cfg {
|
|
|
|
cfg[key] = os.Getenv(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
return cfg
|
|
|
|
}
|
|
|
|
|
2017-11-13 23:31:36 +00:00
|
|
|
func NewBlockStore() cloudprovider.BlockStore {
|
|
|
|
return &blockStore{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *blockStore) Init(config map[string]string) error {
|
|
|
|
var (
|
|
|
|
apiTimeoutVal = config[apiTimeoutKey]
|
|
|
|
apiTimeout time.Duration
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
if apiTimeout, err = time.ParseDuration(apiTimeoutVal); err != nil {
|
|
|
|
return errors.Wrapf(err, "could not parse %s (expected time.Duration)", apiTimeoutKey)
|
2017-08-15 20:20:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if apiTimeout == 0 {
|
2017-09-13 23:20:12 +00:00
|
|
|
apiTimeout = 2 * time.Minute
|
2017-08-15 20:20:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cfg := getConfig()
|
|
|
|
|
|
|
|
spt, err := helpers.NewServicePrincipalTokenFromCredentials(cfg, azure.PublicCloud.ResourceManagerEndpoint)
|
|
|
|
if err != nil {
|
2017-11-13 23:31:36 +00:00
|
|
|
return errors.Wrap(err, "error creating new service principal token")
|
2017-08-15 20:20:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
disksClient := disk.NewDisksClient(cfg[azureSubscriptionIDKey])
|
|
|
|
snapsClient := disk.NewSnapshotsClient(cfg[azureSubscriptionIDKey])
|
|
|
|
|
2018-03-06 22:00:10 +00:00
|
|
|
disksClient.PollingDelay = 5 * time.Second
|
|
|
|
snapsClient.PollingDelay = 5 * time.Second
|
|
|
|
|
2017-08-18 17:53:05 +00:00
|
|
|
authorizer := autorest.NewBearerAuthorizer(spt)
|
|
|
|
disksClient.Authorizer = authorizer
|
|
|
|
snapsClient.Authorizer = authorizer
|
2017-08-15 20:20:23 +00:00
|
|
|
|
2017-11-13 23:31:36 +00:00
|
|
|
b.disks = &disksClient
|
|
|
|
b.snaps = &snapsClient
|
|
|
|
b.subscription = cfg[azureSubscriptionIDKey]
|
|
|
|
b.resourceGroup = cfg[azureResourceGroupKey]
|
|
|
|
b.apiTimeout = apiTimeout
|
|
|
|
|
|
|
|
return nil
|
2017-08-15 20:20:23 +00:00
|
|
|
}
|
|
|
|
|
2017-11-13 23:31:36 +00:00
|
|
|
func (b *blockStore) CreateVolumeFromSnapshot(snapshotID, volumeType, volumeAZ string, iops *int64) (string, error) {
|
2018-04-23 17:11:16 +00:00
|
|
|
snapshotIdentifier, err := b.parseSnapshotName(snapshotID)
|
2018-03-06 00:09:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2018-03-06 23:03:05 +00:00
|
|
|
// Lookup snapshot info for its Location & Tags so we can apply them to the volume
|
2018-03-06 00:09:23 +00:00
|
|
|
snapshotInfo, err := b.snaps.Get(snapshotIdentifier.resourceGroup, snapshotIdentifier.name)
|
2018-03-01 17:00:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
2017-08-02 17:27:17 +00:00
|
|
|
diskName := "restore-" + uuid.NewV4().String()
|
|
|
|
|
2017-08-15 20:20:23 +00:00
|
|
|
disk := disk.Model{
|
2017-08-02 17:27:17 +00:00
|
|
|
Name: &diskName,
|
2018-03-01 17:00:30 +00:00
|
|
|
Location: snapshotInfo.Location,
|
2017-08-15 20:20:23 +00:00
|
|
|
Properties: &disk.Properties{
|
|
|
|
CreationData: &disk.CreationData{
|
|
|
|
CreateOption: disk.Copy,
|
2018-04-23 19:43:24 +00:00
|
|
|
SourceResourceID: stringPtr(snapshotIdentifier.String()),
|
2017-08-02 17:27:17 +00:00
|
|
|
},
|
2017-08-15 20:20:23 +00:00
|
|
|
AccountType: disk.StorageAccountTypes(volumeType),
|
2017-08-02 17:27:17 +00:00
|
|
|
},
|
2018-03-06 23:03:05 +00:00
|
|
|
Tags: snapshotInfo.Tags,
|
2017-08-02 17:27:17 +00:00
|
|
|
}
|
|
|
|
|
2017-11-13 23:31:36 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), b.apiTimeout)
|
2017-08-02 17:27:17 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2017-11-13 23:31:36 +00:00
|
|
|
_, errChan := b.disks.CreateOrUpdate(b.resourceGroup, *disk.Name, disk, ctx.Done())
|
2017-08-02 17:27:17 +00:00
|
|
|
|
2018-03-01 17:00:30 +00:00
|
|
|
err = <-errChan
|
2017-08-02 17:27:17 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2017-09-14 21:27:31 +00:00
|
|
|
return "", errors.WithStack(err)
|
2017-08-02 17:27:17 +00:00
|
|
|
}
|
|
|
|
return diskName, nil
|
|
|
|
}
|
|
|
|
|
2017-11-13 23:31:36 +00:00
|
|
|
func (b *blockStore) GetVolumeInfo(volumeID, volumeAZ string) (string, *int64, error) {
|
|
|
|
res, err := b.disks.Get(b.resourceGroup, volumeID)
|
2017-08-02 17:27:17 +00:00
|
|
|
if err != nil {
|
2017-09-14 21:27:31 +00:00
|
|
|
return "", nil, errors.WithStack(err)
|
2017-08-02 17:27:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return string(res.AccountType), nil, nil
|
|
|
|
}
|
|
|
|
|
2017-11-13 23:31:36 +00:00
|
|
|
func (b *blockStore) IsVolumeReady(volumeID, volumeAZ string) (ready bool, err error) {
|
|
|
|
res, err := b.disks.Get(b.resourceGroup, volumeID)
|
2017-08-02 17:27:17 +00:00
|
|
|
if err != nil {
|
2017-09-14 21:27:31 +00:00
|
|
|
return false, errors.WithStack(err)
|
2017-08-02 17:27:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if res.ProvisioningState == nil {
|
|
|
|
return false, errors.New("nil ProvisioningState returned from Get call")
|
|
|
|
}
|
|
|
|
|
|
|
|
return *res.ProvisioningState == "Succeeded", nil
|
|
|
|
}
|
|
|
|
|
2017-11-13 23:31:36 +00:00
|
|
|
func (b *blockStore) CreateSnapshot(volumeID, volumeAZ string, tags map[string]string) (string, error) {
|
2018-03-01 17:00:30 +00:00
|
|
|
// Lookup disk info for its Location
|
|
|
|
diskInfo, err := b.disks.Get(b.resourceGroup, volumeID)
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
2018-03-06 00:09:23 +00:00
|
|
|
fullDiskName := getComputeResourceName(b.subscription, b.resourceGroup, disksResource, volumeID)
|
2017-08-02 17:27:17 +00:00
|
|
|
// snapshot names must be <= 80 characters long
|
|
|
|
var snapshotName string
|
|
|
|
suffix := "-" + uuid.NewV4().String()
|
|
|
|
|
|
|
|
if len(volumeID) <= (80 - len(suffix)) {
|
|
|
|
snapshotName = volumeID + suffix
|
|
|
|
} else {
|
|
|
|
snapshotName = volumeID[0:80-len(suffix)] + suffix
|
|
|
|
}
|
|
|
|
|
2017-08-15 20:20:23 +00:00
|
|
|
snap := disk.Snapshot{
|
2017-08-02 17:27:17 +00:00
|
|
|
Name: &snapshotName,
|
2017-08-15 20:20:23 +00:00
|
|
|
Properties: &disk.Properties{
|
|
|
|
CreationData: &disk.CreationData{
|
|
|
|
CreateOption: disk.Copy,
|
2017-08-02 17:27:17 +00:00
|
|
|
SourceResourceID: &fullDiskName,
|
|
|
|
},
|
|
|
|
},
|
2018-03-06 23:03:05 +00:00
|
|
|
Tags: getSnapshotTags(tags, diskInfo.Tags),
|
2018-03-01 17:00:30 +00:00
|
|
|
Location: diskInfo.Location,
|
2017-08-02 17:27:17 +00:00
|
|
|
}
|
|
|
|
|
2017-11-13 23:31:36 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), b.apiTimeout)
|
2017-08-02 17:27:17 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2017-11-13 23:31:36 +00:00
|
|
|
_, errChan := b.snaps.CreateOrUpdate(b.resourceGroup, *snap.Name, snap, ctx.Done())
|
2018-03-01 17:00:30 +00:00
|
|
|
err = <-errChan
|
2017-08-02 17:27:17 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2017-09-14 21:27:31 +00:00
|
|
|
return "", errors.WithStack(err)
|
2017-08-02 17:27:17 +00:00
|
|
|
}
|
|
|
|
|
2018-03-06 00:09:23 +00:00
|
|
|
return getComputeResourceName(b.subscription, b.resourceGroup, snapshotsResource, snapshotName), nil
|
2017-08-02 17:27:17 +00:00
|
|
|
}
|
|
|
|
|
2018-03-06 23:03:05 +00:00
|
|
|
func getSnapshotTags(arkTags map[string]string, diskTags *map[string]*string) *map[string]*string {
|
|
|
|
if diskTags == nil && len(arkTags) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
snapshotTags := make(map[string]*string)
|
|
|
|
|
|
|
|
// copy tags from disk to snapshot
|
|
|
|
if diskTags != nil {
|
|
|
|
for k, v := range *diskTags {
|
|
|
|
snapshotTags[k] = stringPtr(*v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// merge Ark-assigned tags with the disk's tags (note that we want current
|
|
|
|
// Ark-assigned tags to overwrite any older versions of them that may exist
|
|
|
|
// due to prior snapshots/restores)
|
|
|
|
for k, v := range arkTags {
|
|
|
|
// Azure does not allow slashes in tag keys, so replace
|
|
|
|
// with dash (inline with what Kubernetes does)
|
|
|
|
key := strings.Replace(k, "/", "-", -1)
|
|
|
|
snapshotTags[key] = stringPtr(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &snapshotTags
|
|
|
|
}
|
|
|
|
|
|
|
|
func stringPtr(s string) *string {
|
|
|
|
return &s
|
|
|
|
}
|
|
|
|
|
2017-11-13 23:31:36 +00:00
|
|
|
func (b *blockStore) DeleteSnapshot(snapshotID string) error {
|
2018-04-23 17:11:16 +00:00
|
|
|
snapshotInfo, err := b.parseSnapshotName(snapshotID)
|
2018-03-14 00:03:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-11-13 23:31:36 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), b.apiTimeout)
|
2017-08-02 17:27:17 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2018-03-14 00:03:14 +00:00
|
|
|
_, errChan := b.snaps.Delete(snapshotInfo.resourceGroup, snapshotInfo.name, ctx.Done())
|
2017-08-02 17:27:17 +00:00
|
|
|
|
2018-03-14 00:03:14 +00:00
|
|
|
err = <-errChan
|
2017-08-02 17:27:17 +00:00
|
|
|
|
2017-09-14 21:27:31 +00:00
|
|
|
return errors.WithStack(err)
|
2017-08-02 17:27:17 +00:00
|
|
|
}
|
|
|
|
|
2018-03-06 00:09:23 +00:00
|
|
|
func getComputeResourceName(subscription, resourceGroup, resource, name string) string {
|
|
|
|
return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Compute/%s/%s", subscription, resourceGroup, resource, name)
|
2017-08-02 17:27:17 +00:00
|
|
|
}
|
|
|
|
|
2018-03-06 00:09:23 +00:00
|
|
|
var snapshotURIRegexp = regexp.MustCompile(
|
|
|
|
`^\/subscriptions\/(?P<subscription>.*)\/resourceGroups\/(?P<resourceGroup>.*)\/providers\/Microsoft.Compute\/snapshots\/(?P<snapshotName>.*)$`)
|
|
|
|
|
2018-04-23 17:11:16 +00:00
|
|
|
// parseSnapshotName takes a snapshot name, either fully-qualified or not, and returns
|
|
|
|
// a snapshot identifier or an error if the name is not in a valid format. If the name
|
|
|
|
// is not fully-qualified, the subscription and resource group are assumed to be the
|
|
|
|
// ones that the block store is configured with.
|
|
|
|
//
|
|
|
|
// TODO(1.0) remove this function and replace usage with `parseFullSnapshotName` since
|
|
|
|
// we won't support the legacy snapshot name format for 1.0.
|
|
|
|
func (b *blockStore) parseSnapshotName(name string) (*snapshotIdentifier, error) {
|
|
|
|
switch {
|
|
|
|
// legacy format - name only (not fully-qualified)
|
|
|
|
case !strings.Contains(name, "/"):
|
|
|
|
return &snapshotIdentifier{
|
|
|
|
subscription: b.subscription,
|
|
|
|
resourceGroup: b.resourceGroup,
|
|
|
|
name: name,
|
|
|
|
}, nil
|
|
|
|
// current format - fully qualified
|
|
|
|
case snapshotURIRegexp.MatchString(name):
|
|
|
|
return parseFullSnapshotName(name)
|
|
|
|
// unrecognized format
|
|
|
|
default:
|
|
|
|
return nil, errors.New("snapshot name is not in a valid format")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseFullSnapshotName takes a fully-qualified snapshot name and returns
|
|
|
|
// a snapshot identifier or an error if the snapshot name does not match the
|
|
|
|
// regexp.
|
2018-03-06 00:09:23 +00:00
|
|
|
func parseFullSnapshotName(name string) (*snapshotIdentifier, error) {
|
|
|
|
submatches := snapshotURIRegexp.FindStringSubmatch(name)
|
|
|
|
if len(submatches) != len(snapshotURIRegexp.SubexpNames()) {
|
|
|
|
return nil, errors.New("snapshot URI could not be parsed")
|
|
|
|
}
|
|
|
|
|
|
|
|
snapshotID := &snapshotIdentifier{}
|
|
|
|
|
|
|
|
// capture names start at index 1 to line up with the corresponding indexes
|
|
|
|
// of submatches (see godoc on SubexpNames())
|
|
|
|
for i, names := 1, snapshotURIRegexp.SubexpNames(); i < len(names); i++ {
|
|
|
|
switch names[i] {
|
|
|
|
case "subscription":
|
|
|
|
snapshotID.subscription = submatches[i]
|
|
|
|
case "resourceGroup":
|
|
|
|
snapshotID.resourceGroup = submatches[i]
|
|
|
|
case "snapshotName":
|
|
|
|
snapshotID.name = submatches[i]
|
|
|
|
default:
|
|
|
|
return nil, errors.New("unexpected named capture from snapshot URI regex")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return snapshotID, nil
|
2017-08-02 17:27:17 +00:00
|
|
|
}
|
2017-11-29 17:23:21 +00:00
|
|
|
|
|
|
|
func (b *blockStore) GetVolumeID(pv runtime.Unstructured) (string, error) {
|
|
|
|
if !collections.Exists(pv.UnstructuredContent(), "spec.azureDisk") {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
volumeID, err := collections.GetString(pv.UnstructuredContent(), "spec.azureDisk.diskName")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return volumeID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *blockStore) SetVolumeID(pv runtime.Unstructured, volumeID string) (runtime.Unstructured, error) {
|
|
|
|
azure, err := collections.GetMap(pv.UnstructuredContent(), "spec.azureDisk")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
azure["diskName"] = volumeID
|
2018-03-06 00:09:23 +00:00
|
|
|
azure["diskURI"] = getComputeResourceName(b.subscription, b.resourceGroup, disksResource, volumeID)
|
2017-11-29 17:23:21 +00:00
|
|
|
|
|
|
|
return pv, nil
|
|
|
|
}
|