Created
September 22, 2021 16:36
-
-
Save keymon/bdc6068c6e6399cadd76758867fb9c2a to your computer and use it in GitHub Desktop.
Clone PV in AKS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func CloneAzureDisk( | |
ctx context.Context, | |
azureClients AzureClients, | |
originSubscriptionID, | |
originResourceGroupName, | |
originDiskName, | |
targetSubscriptionID, | |
targetResourceGroupName, | |
targetDiskName string, | |
) (snapshot compute.Snapshot, disk compute.Disk, err error) { | |
originSnapshotClient := azureClients.SnapshotsClient(originSubscriptionID) | |
originDiskClient := azureClients.DisksClient(originSubscriptionID) | |
targetDiskClient := azureClients.DisksClient(targetSubscriptionID) | |
originDisk, err := originDiskClient.Get(ctx, originResourceGroupName, originDiskName) | |
if err != nil { | |
return snapshot, disk, err | |
} | |
if originDisk.DiskProperties.DiskState == compute.Attached { | |
return snapshot, disk, fmt.Errorf("Refusing to clone a disk that is attached: %s", to.String(originDisk.ID)) | |
} | |
// Check if target disk exists | |
// TODO: improve this heuristic | |
targetDisk, err := targetDiskClient.Get(ctx, targetResourceGroupName, targetDiskName) | |
if err == nil { | |
return snapshot, targetDisk, NewTargetDiskAlreadyExistsError(to.String(targetDisk.ID)) | |
} else if err != nil && !IsResourceNotFoundError(err) { | |
return snapshot, disk, err | |
} | |
// Add a kops-migration-ctl tag | |
snapshotTags := map[string]*string{} | |
for k, v := range originDisk.Tags { | |
snapshotTags[k] = v | |
} | |
snapshotTags["kops-migration-ctl"] = to.StringPtr("true") | |
snapshotRequest := compute.Snapshot{ | |
// TODO: is this type ok | |
Sku: &compute.SnapshotSku{Name: compute.SnapshotStorageAccountTypesStandardLRS}, | |
// TODO: Is this encription fine? I guess | |
SnapshotProperties: &compute.SnapshotProperties{ | |
Encryption: &compute.Encryption{ | |
Type: "EncryptionAtRestWithPlatformKey", | |
}, | |
CreationData: &compute.CreationData{ | |
CreateOption: compute.Copy, | |
//ImageReference: &compute.ImageDiskReference{ | |
//ID: originDisk.ID, | |
//}, | |
SourceURI: originDisk.ID, | |
}, | |
}, | |
Location: originDisk.Location, | |
// TODO: add a tag to identify the snapshot? | |
Tags: originDisk.Tags, | |
} | |
//snapshotName := fmt.Sprintf( | |
//"%s-%s-%s", | |
//originDiskName, snapshotSuffix, | |
//time.Now().Format("20060102T150405Z"), | |
//) | |
snapshotName := fmt.Sprintf( | |
"%s-%s", | |
originDiskName, snapshotSuffix, | |
) | |
snapshotFutureResult, err := originSnapshotClient.CreateOrUpdate(ctx, | |
originResourceGroupName, | |
snapshotName, | |
snapshotRequest, | |
) | |
if err != nil { | |
return snapshot, disk, err | |
} | |
// TODO: Fix mocking the client (maybe autorest sender?) | |
err = snapshotFutureResult.WaitForCompletionRef(ctx, originSnapshotClient.(compute.SnapshotsClient).Client) | |
if err != nil { | |
return snapshot, disk, err | |
} | |
snapshot, err = snapshotFutureResult.Result(originSnapshotClient.(compute.SnapshotsClient)) | |
if err != nil { | |
return snapshot, disk, err | |
} | |
diskRequest := compute.Disk{ | |
DiskProperties: &compute.DiskProperties{ | |
Encryption: &compute.Encryption{ | |
Type: compute.EncryptionAtRestWithPlatformKey, | |
}, | |
CreationData: &compute.CreationData{ | |
CreateOption: compute.Copy, | |
SourceURI: snapshot.ID, | |
}, | |
}, | |
Sku: &compute.DiskSku{ | |
Name: originDisk.Sku.Name, | |
}, | |
Zones: originDisk.Zones, | |
Type: originDisk.Type, | |
Location: originDisk.Location, | |
Tags: originDisk.Tags, | |
} | |
diskFutureResult, err := targetDiskClient.CreateOrUpdate(ctx, | |
targetResourceGroupName, | |
targetDiskName, | |
diskRequest, | |
) | |
if err != nil { | |
return snapshot, disk, err | |
} | |
// TODO: Fix mocking the client (maybe autorest sender?) | |
err = diskFutureResult.WaitForCompletionRef(ctx, targetDiskClient.(compute.DisksClient).Client) | |
if err != nil { | |
return snapshot, disk, err | |
} | |
disk, err = diskFutureResult.Result(targetDiskClient.(compute.DisksClient)) | |
if err != nil { | |
return snapshot, disk, err | |
} | |
// TODO ensure this is safe | |
// Note: Better do it in a dedicated step | |
//_, err := originDisksClient.Delete( | |
//context.Background(), | |
//originResourceGroupName, | |
//originDiskName, | |
//) | |
//if err != nil { | |
//return snapshot, disk, err | |
//} | |
_, err = originSnapshotClient.Delete( | |
context.Background(), | |
originResourceGroupName, | |
snapshotName, | |
) | |
if err != nil { | |
return snapshot, disk, err | |
} | |
return snapshot, disk, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment