This repository has been archived by the owner on Nov 30, 2023. It is now read-only.
/
delete.go
59 lines (47 loc) · 1.79 KB
/
delete.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package cloudconfigblob
import (
"context"
"github.com/Azure/azure-storage-blob-go/azblob"
"github.com/giantswarm/microerror"
"github.com/giantswarm/azure-operator/v8/service/controller/blobclient"
"github.com/giantswarm/azure-operator/v8/service/controller/key"
)
// EnsureDeleted will delete the blob in the Storage Account containing the cloud config for the node pool.
func (r *Resource) EnsureDeleted(ctx context.Context, obj interface{}) error {
azureMachinePool, err := key.ToAzureMachinePool(obj)
if err != nil {
return microerror.Mask(err)
}
machinePool, err := r.getOwnerMachinePool(ctx, azureMachinePool.ObjectMeta)
if err != nil {
return microerror.Mask(err)
}
if machinePool == nil {
// If MachinePool doesn't exist anymore, there's nothing we can do
// about it. Returning an error here would just keep finalizer and
// prevent CR from deletion forever.
return nil
}
blobName := key.BootstrapBlobName(azureMachinePool)
r.logger.Debugf(ctx, "deleting container object %#q", blobName)
var containerURL azblob.ContainerURL
{
containerURL, err = r.getContainerURL(ctx, &azureMachinePool, key.ClusterID(&azureMachinePool), key.StorageAccountName(&azureMachinePool))
if IsNotFound(err) {
// Resource Group deleted or deletion in progress. Storage Account already gone.
return nil
} else if err != nil {
return microerror.Mask(err)
}
}
blob := containerURL.NewBlockBlobURL(blobName)
_, err = blob.Delete(ctx, azblob.DeleteSnapshotsOptionInclude, azblob.BlobAccessConditions{})
if blobclient.IsNotFound(err) || blobclient.IsBlobNotFound(err) {
r.logger.Debugf(ctx, "Bootstrap blob not found when trying to delete it")
return nil
} else if err != nil {
return microerror.Mask(err)
}
r.logger.Debugf(ctx, "deleted container object %#q", blobName)
return nil
}