From 85a61b8e8de8c484f09beb39bc961a356070ae66 Mon Sep 17 00:00:00 2001 From: Steve Kriss Date: Thu, 19 Jul 2018 13:31:30 -0700 Subject: [PATCH] return nil error if 404 encountered when deleting snapshots Signed-off-by: Steve Kriss --- pkg/cloudprovider/aws/block_store.go | 13 ++++++++++++- pkg/cloudprovider/azure/block_store.go | 12 +++++++++++- pkg/cloudprovider/gcp/block_store.go | 13 ++++++++++++- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/pkg/cloudprovider/aws/block_store.go b/pkg/cloudprovider/aws/block_store.go index 69b08b387..8960e7c9b 100644 --- a/pkg/cloudprovider/aws/block_store.go +++ b/pkg/cloudprovider/aws/block_store.go @@ -20,6 +20,7 @@ import ( "regexp" "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/ec2" "github.com/pkg/errors" @@ -219,7 +220,17 @@ func (b *blockStore) DeleteSnapshot(snapshotID string) error { _, err := b.ec2.DeleteSnapshot(req) - return errors.WithStack(err) + // if it's a NotFound error, we don't need to return an error + // since the snapshot is not there. + // see https://docs.aws.amazon.com/AWSEC2/latest/APIReference/errors-overview.html + if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "InvalidSnapshot.NotFound" { + return nil + } + if err != nil { + return errors.WithStack(err) + } + + return nil } var ebsVolumeIDRegex = regexp.MustCompile("vol-.*") diff --git a/pkg/cloudprovider/azure/block_store.go b/pkg/cloudprovider/azure/block_store.go index 0745066be..ca42f80a4 100644 --- a/pkg/cloudprovider/azure/block_store.go +++ b/pkg/cloudprovider/azure/block_store.go @@ -19,6 +19,7 @@ package azure import ( "context" "fmt" + "net/http" "os" "regexp" "strings" @@ -280,7 +281,16 @@ func (b *blockStore) DeleteSnapshot(snapshotID string) error { err = <-errChan - return errors.WithStack(err) + // if it's a 404 (not found) error, we don't need to return an error + // since the snapshot is not there. + if azureErr, ok := err.(autorest.DetailedError); ok && azureErr.StatusCode == http.StatusNotFound { + return nil + } + if err != nil { + return errors.WithStack(err) + } + + return nil } func getComputeResourceName(subscription, resourceGroup, resource, name string) string { diff --git a/pkg/cloudprovider/gcp/block_store.go b/pkg/cloudprovider/gcp/block_store.go index 47e3900f6..0fe257131 100644 --- a/pkg/cloudprovider/gcp/block_store.go +++ b/pkg/cloudprovider/gcp/block_store.go @@ -19,6 +19,7 @@ package gcp import ( "encoding/json" "io/ioutil" + "net/http" "os" "github.com/pkg/errors" @@ -27,6 +28,7 @@ import ( "golang.org/x/oauth2" "golang.org/x/oauth2/google" "google.golang.org/api/compute/v1" + "google.golang.org/api/googleapi" "k8s.io/apimachinery/pkg/runtime" @@ -212,7 +214,16 @@ func getSnapshotTags(arkTags map[string]string, diskDescription string, log logr func (b *blockStore) DeleteSnapshot(snapshotID string) error { _, err := b.gce.Snapshots.Delete(b.project, snapshotID).Do() - return errors.WithStack(err) + // if it's a 404 (not found) error, we don't need to return an error + // since the snapshot is not there. + if gcpErr, ok := err.(*googleapi.Error); ok && gcpErr.Code == http.StatusNotFound { + return nil + } + if err != nil { + return errors.WithStack(err) + } + + return nil } func (b *blockStore) GetVolumeID(pv runtime.Unstructured) (string, error) {