pkg/cloudprovider: remove usage of pkg/util/collections

Signed-off-by: Steve Kriss <krisss@vmware.com>
This commit is contained in:
Steve Kriss
2019-01-07 12:00:10 -07:00
parent 378011baf6
commit 296dd6617e
6 changed files with 110 additions and 63 deletions

View File

@@ -27,11 +27,12 @@ import (
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/sets"
"github.com/heptio/velero/pkg/cloudprovider"
"github.com/heptio/velero/pkg/util/collections"
)
const regionKey = "region"
@@ -254,26 +255,39 @@ func (b *blockStore) DeleteSnapshot(snapshotID string) error {
var ebsVolumeIDRegex = regexp.MustCompile("vol-.*")
func (b *blockStore) GetVolumeID(pv runtime.Unstructured) (string, error) {
if !collections.Exists(pv.UnstructuredContent(), "spec.awsElasticBlockStore") {
func (b *blockStore) GetVolumeID(unstructuredPV runtime.Unstructured) (string, error) {
pv := new(v1.PersistentVolume)
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(unstructuredPV.UnstructuredContent(), pv); err != nil {
return "", errors.WithStack(err)
}
if pv.Spec.AWSElasticBlockStore == nil {
return "", nil
}
volumeID, err := collections.GetString(pv.UnstructuredContent(), "spec.awsElasticBlockStore.volumeID")
if err != nil {
return "", err
if pv.Spec.AWSElasticBlockStore.VolumeID == "" {
return "", errors.New(".spec.awsElasticBlockStore.volumeID not found")
}
return ebsVolumeIDRegex.FindString(volumeID), nil
return ebsVolumeIDRegex.FindString(pv.Spec.AWSElasticBlockStore.VolumeID), nil
}
func (b *blockStore) SetVolumeID(pv runtime.Unstructured, volumeID string) (runtime.Unstructured, error) {
aws, err := collections.GetMap(pv.UnstructuredContent(), "spec.awsElasticBlockStore")
if err != nil {
return nil, err
func (b *blockStore) SetVolumeID(unstructuredPV runtime.Unstructured, volumeID string) (runtime.Unstructured, error) {
pv := new(v1.PersistentVolume)
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(unstructuredPV.UnstructuredContent(), pv); err != nil {
return nil, errors.WithStack(err)
}
aws["volumeID"] = volumeID
if pv.Spec.AWSElasticBlockStore == nil {
return nil, errors.New(".spec.awsElasticBlockStore not found")
}
return pv, nil
pv.Spec.AWSElasticBlockStore.VolumeID = volumeID
res, err := runtime.DefaultUnstructuredConverter.ToUnstructured(pv)
if err != nil {
return nil, errors.WithStack(err)
}
return &unstructured.Unstructured{Object: res}, nil
}

View File

@@ -24,9 +24,9 @@ import (
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"github.com/heptio/velero/pkg/util/collections"
"k8s.io/apimachinery/pkg/runtime"
)
func TestGetVolumeID(t *testing.T) {
@@ -87,9 +87,11 @@ func TestSetVolumeID(t *testing.T) {
}
updatedPV, err = b.SetVolumeID(pv, "vol-updated")
require.NoError(t, err)
actual, err := collections.GetString(updatedPV.UnstructuredContent(), "spec.awsElasticBlockStore.volumeID")
require.NoError(t, err)
assert.Equal(t, "vol-updated", actual)
res := new(v1.PersistentVolume)
require.NoError(t, runtime.DefaultUnstructuredConverter.FromUnstructured(updatedPV.UnstructuredContent(), res))
require.NotNil(t, res.Spec.AWSElasticBlockStore)
assert.Equal(t, "vol-updated", res.Spec.AWSElasticBlockStore.VolumeID)
}
func TestGetTagsForCluster(t *testing.T) {

View File

@@ -31,10 +31,11 @@ import (
"github.com/pkg/errors"
"github.com/satori/uuid"
"github.com/sirupsen/logrus"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"github.com/heptio/velero/pkg/cloudprovider"
"github.com/heptio/velero/pkg/util/collections"
)
const (
@@ -335,27 +336,40 @@ func parseFullSnapshotName(name string) (*snapshotIdentifier, error) {
return snapshotID, nil
}
func (b *blockStore) GetVolumeID(pv runtime.Unstructured) (string, error) {
if !collections.Exists(pv.UnstructuredContent(), "spec.azureDisk") {
func (b *blockStore) GetVolumeID(unstructuredPV runtime.Unstructured) (string, error) {
pv := new(v1.PersistentVolume)
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(unstructuredPV.UnstructuredContent(), pv); err != nil {
return "", errors.WithStack(err)
}
if pv.Spec.AzureDisk == nil {
return "", nil
}
volumeID, err := collections.GetString(pv.UnstructuredContent(), "spec.azureDisk.diskName")
if err != nil {
return "", err
if pv.Spec.AzureDisk.DiskName == "" {
return "", errors.New(".spec.azureDisk.diskName not found")
}
return volumeID, nil
return pv.Spec.AzureDisk.DiskName, 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
func (b *blockStore) SetVolumeID(unstructuredPV runtime.Unstructured, volumeID string) (runtime.Unstructured, error) {
pv := new(v1.PersistentVolume)
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(unstructuredPV.UnstructuredContent(), pv); err != nil {
return nil, errors.WithStack(err)
}
azure["diskName"] = volumeID
azure["diskURI"] = getComputeResourceName(b.subscription, b.disksResourceGroup, disksResource, volumeID)
if pv.Spec.AzureDisk == nil {
return nil, errors.New(".spec.azureDisk not found")
}
return pv, nil
pv.Spec.AzureDisk.DiskName = volumeID
pv.Spec.AzureDisk.DataDiskURI = getComputeResourceName(b.subscription, b.disksResourceGroup, disksResource, volumeID)
res, err := runtime.DefaultUnstructuredConverter.ToUnstructured(pv)
if err != nil {
return nil, errors.WithStack(err)
}
return &unstructured.Unstructured{Object: res}, nil
}

View File

@@ -21,9 +21,9 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"github.com/heptio/velero/pkg/util/collections"
"k8s.io/apimachinery/pkg/runtime"
)
func TestGetVolumeID(t *testing.T) {
@@ -75,23 +75,23 @@ func TestSetVolumeID(t *testing.T) {
}
updatedPV, err = b.SetVolumeID(pv, "updated")
require.NoError(t, err)
actual, err := collections.GetString(updatedPV.UnstructuredContent(), "spec.azureDisk.diskName")
require.NoError(t, err)
assert.Equal(t, "updated", actual)
actual, err = collections.GetString(updatedPV.UnstructuredContent(), "spec.azureDisk.diskURI")
require.NoError(t, err)
assert.Equal(t, "/subscriptions/sub/resourceGroups/rg/providers/Microsoft.Compute/disks/updated", actual)
res := new(v1.PersistentVolume)
require.NoError(t, runtime.DefaultUnstructuredConverter.FromUnstructured(updatedPV.UnstructuredContent(), res))
require.NotNil(t, res.Spec.AzureDisk)
assert.Equal(t, "updated", res.Spec.AzureDisk.DiskName)
assert.Equal(t, "/subscriptions/sub/resourceGroups/rg/providers/Microsoft.Compute/disks/updated", res.Spec.AzureDisk.DataDiskURI)
// with diskURI
azure["diskURI"] = "/foo/bar/updated/blarg"
updatedPV, err = b.SetVolumeID(pv, "revised")
require.NoError(t, err)
actual, err = collections.GetString(updatedPV.UnstructuredContent(), "spec.azureDisk.diskName")
require.NoError(t, err)
assert.Equal(t, "revised", actual)
actual, err = collections.GetString(updatedPV.UnstructuredContent(), "spec.azureDisk.diskURI")
require.NoError(t, err)
assert.Equal(t, "/subscriptions/sub/resourceGroups/rg/providers/Microsoft.Compute/disks/revised", actual)
res = new(v1.PersistentVolume)
require.NoError(t, runtime.DefaultUnstructuredConverter.FromUnstructured(updatedPV.UnstructuredContent(), res))
require.NotNil(t, res.Spec.AzureDisk)
assert.Equal(t, "revised", res.Spec.AzureDisk.DiskName)
assert.Equal(t, "/subscriptions/sub/resourceGroups/rg/providers/Microsoft.Compute/disks/revised", res.Spec.AzureDisk.DataDiskURI)
}
// TODO(1.0) rename to TestParseFullSnapshotName, switch to testing

View File

@@ -30,10 +30,11 @@ import (
"golang.org/x/oauth2/google"
"google.golang.org/api/compute/v1"
"google.golang.org/api/googleapi"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"github.com/heptio/velero/pkg/cloudprovider"
"github.com/heptio/velero/pkg/util/collections"
)
const projectKey = "project"
@@ -292,26 +293,39 @@ func (b *blockStore) DeleteSnapshot(snapshotID string) error {
return nil
}
func (b *blockStore) GetVolumeID(pv runtime.Unstructured) (string, error) {
if !collections.Exists(pv.UnstructuredContent(), "spec.gcePersistentDisk") {
func (b *blockStore) GetVolumeID(unstructuredPV runtime.Unstructured) (string, error) {
pv := new(v1.PersistentVolume)
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(unstructuredPV.UnstructuredContent(), pv); err != nil {
return "", errors.WithStack(err)
}
if pv.Spec.GCEPersistentDisk == nil {
return "", nil
}
volumeID, err := collections.GetString(pv.UnstructuredContent(), "spec.gcePersistentDisk.pdName")
if err != nil {
return "", err
if pv.Spec.GCEPersistentDisk.PDName == "" {
return "", errors.New(".spec.gcePersistentDisk.pdName not found")
}
return volumeID, nil
return pv.Spec.GCEPersistentDisk.PDName, nil
}
func (b *blockStore) SetVolumeID(pv runtime.Unstructured, volumeID string) (runtime.Unstructured, error) {
gce, err := collections.GetMap(pv.UnstructuredContent(), "spec.gcePersistentDisk")
if err != nil {
return nil, err
func (b *blockStore) SetVolumeID(unstructuredPV runtime.Unstructured, volumeID string) (runtime.Unstructured, error) {
pv := new(v1.PersistentVolume)
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(unstructuredPV.UnstructuredContent(), pv); err != nil {
return nil, errors.WithStack(err)
}
gce["pdName"] = volumeID
if pv.Spec.GCEPersistentDisk == nil {
return nil, errors.New(".spec.gcePersistentDisk not found")
}
return pv, nil
pv.Spec.GCEPersistentDisk.PDName = volumeID
res, err := runtime.DefaultUnstructuredConverter.ToUnstructured(pv)
if err != nil {
return nil, errors.WithStack(err)
}
return &unstructured.Unstructured{Object: res}, nil
}

View File

@@ -23,9 +23,10 @@ import (
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"github.com/heptio/velero/pkg/util/collections"
velerotest "github.com/heptio/velero/pkg/util/test"
)
@@ -75,9 +76,11 @@ func TestSetVolumeID(t *testing.T) {
}
updatedPV, err = b.SetVolumeID(pv, "123abc")
require.NoError(t, err)
actual, err := collections.GetString(updatedPV.UnstructuredContent(), "spec.gcePersistentDisk.pdName")
require.NoError(t, err)
assert.Equal(t, "123abc", actual)
res := new(v1.PersistentVolume)
require.NoError(t, runtime.DefaultUnstructuredConverter.FromUnstructured(updatedPV.UnstructuredContent(), res))
require.NotNil(t, res.Spec.GCEPersistentDisk)
assert.Equal(t, "123abc", res.Spec.GCEPersistentDisk.PDName)
}
func TestGetSnapshotTags(t *testing.T) {