Skip to content

Commit

Permalink
Add WIP Shallow Replication, need to fix unit test with new publish f…
Browse files Browse the repository at this point in the history
…unction
  • Loading branch information
JenGoldstrich committed Aug 24, 2023
1 parent fb7a250 commit 8c67ffe
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 26 deletions.
1 change: 1 addition & 0 deletions builder/azure/arm/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,7 @@ func (b *Builder) configureStateBag(stateBag multistep.StateBag) {
stateBag.Put(constants.ArmManagedImageSharedGalleryImageVersion, b.config.SharedGalleryDestination.SigDestinationImageVersion)
stateBag.Put(constants.ArmManagedImageSharedGalleryImageVersionStorageAccountType, b.config.SharedGalleryDestination.SigDestinationStorageAccountType)
stateBag.Put(constants.ArmSharedImageGalleryDestinationSpecialized, b.config.SharedGalleryDestination.SigDestinationSpecialized)
stateBag.Put(constants.ArmSharedImageGalleryDestinationShallowReplication, b.config.SharedGalleryDestination.SigDestinationShallowReplicationMode)
stateBag.Put(constants.ArmManagedImageSubscription, b.config.ClientConfig.SubscriptionID)
stateBag.Put(constants.ArmManagedImageSharedGalleryImageVersionEndOfLifeDate, b.config.SharedGalleryImageVersionEndOfLifeDate)
stateBag.Put(constants.ArmManagedImageSharedGalleryImageVersionReplicaCount, b.config.SharedGalleryImageVersionReplicaCount)
Expand Down
2 changes: 2 additions & 0 deletions builder/azure/arm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ type SharedImageGalleryDestination struct {
SigDestinationStorageAccountType string `mapstructure:"storage_account_type"`
// Set to true if publishing to a Specialized Gallery, this skips a call to set the build VM's OS state as Generalized
SigDestinationSpecialized bool `mapstructure:"specialized"`
// Set to true if you want to publish using Shallow Replication, this is faster but skips copying the full image to the gallery image region, read more about Shallow Replication [here](https://learn.microsoft.com/en-us/azure/virtual-machines/shared-image-galleries?tabs=azure-cli#shallow-replication)
SigDestinationShallowReplicationMode bool `mapstructure:"shallow_replication"`
}

type Spot struct {
Expand Down
18 changes: 10 additions & 8 deletions builder/azure/arm/config.hcl2spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

69 changes: 51 additions & 18 deletions builder/azure/arm/step_publish_to_shared_image_gallery.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,26 @@ import (

type StepPublishToSharedImageGallery struct {
client *AzureClient
publish func(ctx context.Context, subscriptionID string, sourceID string, sharedImageGallery SharedImageGalleryDestination, miSGImageVersionEndOfLifeDate string, miSGImageVersionExcludeFromLatest bool, miSigReplicaCount int64, location string, diskEncryptionSetId string, tags map[string]string) (string, error)
publish func(publishArgs PublishArgs) (string, error)
say func(message string)
error func(e error)
toSIG func() bool
}

type PublishArgs struct {
ctx context.Context
subscriptionID string
sourceID string
sharedImageGallery SharedImageGalleryDestination
miSGImageVersionEndOfLifeDate string
miSGImageVersionExcludeFromLatest bool
miSigReplicaCount int64
location string
diskEncryptionSetId string
shallowReplicationMode bool
tags map[string]string
}

func NewStepPublishToSharedImageGallery(client *AzureClient, ui packersdk.Ui, config *Config) *StepPublishToSharedImageGallery {
var step = &StepPublishToSharedImageGallery{
client: client,
Expand Down Expand Up @@ -72,58 +86,63 @@ func getSigDestination(state multistep.StateBag) SharedImageGalleryDestination {
}
}

func (s *StepPublishToSharedImageGallery) publishToSig(ctx context.Context, subscriptionID string, sourceID string, sharedImageGallery SharedImageGalleryDestination, miSGImageVersionEndOfLifeDate string, miSGImageVersionExcludeFromLatest bool, miSigReplicaCount int64, location string, diskEncryptionSetId string, tags map[string]string) (string, error) {
replicationRegions := make([]galleryimageversions.TargetRegion, len(sharedImageGallery.SigDestinationReplicationRegions))
for i, v := range sharedImageGallery.SigDestinationReplicationRegions {
func (s *StepPublishToSharedImageGallery) publishToSig(publishArgs PublishArgs) (string, error) {
replicationRegions := make([]galleryimageversions.TargetRegion, len(publishArgs.sharedImageGallery.SigDestinationReplicationRegions))
for i, v := range publishArgs.sharedImageGallery.SigDestinationReplicationRegions {
regionName := v
replicationRegions[i] = galleryimageversions.TargetRegion{Name: regionName}
}

storageAccountType, err := getSigDestinationStorageAccountType(sharedImageGallery.SigDestinationStorageAccountType)
storageAccountType, err := getSigDestinationStorageAccountType(publishArgs.sharedImageGallery.SigDestinationStorageAccountType)
if err != nil {
s.error(err)
return "", err
}

if diskEncryptionSetId != "" {
if publishArgs.diskEncryptionSetId != "" {
for index, targetRegion := range replicationRegions {
targetRegion.Encryption = &galleryimageversions.EncryptionImages{
OsDiskImage: &galleryimageversions.OSDiskImageEncryption{
DiskEncryptionSetId: &diskEncryptionSetId,
DiskEncryptionSetId: &publishArgs.diskEncryptionSetId,
},
}
replicationRegions[index] = targetRegion
}
}
replicationMode := galleryimageversions.ReplicationModeFull
if true {
replicationMode = galleryimageversions.ReplicationModeShallow
}
galleryImageVersion := galleryimageversions.GalleryImageVersion{
Location: location,
Tags: &tags,
Location: publishArgs.location,
Tags: &publishArgs.tags,
Properties: &galleryimageversions.GalleryImageVersionProperties{
StorageProfile: galleryimageversions.GalleryImageVersionStorageProfile{
Source: &galleryimageversions.GalleryArtifactVersionFullSource{
Id: &sourceID,
Id: &publishArgs.sourceID,
},
},
PublishingProfile: &galleryimageversions.GalleryArtifactPublishingProfileBase{
TargetRegions: &replicationRegions,
EndOfLifeDate: &miSGImageVersionEndOfLifeDate,
ExcludeFromLatest: &miSGImageVersionExcludeFromLatest,
ReplicaCount: &miSigReplicaCount,
EndOfLifeDate: &publishArgs.miSGImageVersionEndOfLifeDate,
ExcludeFromLatest: &publishArgs.miSGImageVersionExcludeFromLatest,
ReplicaCount: &publishArgs.miSigReplicaCount,
ReplicationMode: &replicationMode,
StorageAccountType: &storageAccountType,
},
},
}

pollingContext, cancel := context.WithTimeout(ctx, s.client.SharedGalleryTimeout)
pollingContext, cancel := context.WithTimeout(publishArgs.ctx, s.client.SharedGalleryTimeout)
defer cancel()
galleryImageVersionId := galleryimageversions.NewImageVersionID(subscriptionID, sharedImageGallery.SigDestinationResourceGroup, sharedImageGallery.SigDestinationGalleryName, sharedImageGallery.SigDestinationImageName, sharedImageGallery.SigDestinationImageVersion)
galleryImageVersionId := galleryimageversions.NewImageVersionID(publishArgs.subscriptionID, publishArgs.sharedImageGallery.SigDestinationResourceGroup, publishArgs.sharedImageGallery.SigDestinationGalleryName, publishArgs.sharedImageGallery.SigDestinationImageName, publishArgs.sharedImageGallery.SigDestinationImageVersion)
err = s.client.GalleryImageVersionsClient.CreateOrUpdateThenPoll(pollingContext, galleryImageVersionId, galleryImageVersion)
if err != nil {
s.say(s.client.LastError.Error())
return "", err
}

createdSGImageVersion, err := s.client.GalleryImageVersionsClient.Get(ctx, galleryImageVersionId, galleryimageversions.DefaultGetOperationOptions())
createdSGImageVersion, err := s.client.GalleryImageVersionsClient.Get(publishArgs.ctx, galleryImageVersionId, galleryimageversions.DefaultGetOperationOptions())

if err != nil {
s.say(s.client.LastError.Error())
Expand Down Expand Up @@ -187,9 +206,23 @@ func (s *StepPublishToSharedImageGallery) Run(ctx context.Context, stateBag mult
s.say(fmt.Sprintf(" -> SIG image version endoflife date : '%s'", miSGImageVersionEndOfLifeDate))
s.say(fmt.Sprintf(" -> SIG image version exclude from latest : '%t'", miSGImageVersionExcludeFromLatest))
s.say(fmt.Sprintf(" -> SIG replica count [1, 100] : '%d'", miSigReplicaCount))

shallowReplicationMode := stateBag.Get(constants.ArmSharedImageGalleryDestinationShallowReplication).(bool)
subscriptionID := stateBag.Get(constants.ArmSharedImageGalleryDestinationSubscription).(string)
createdGalleryImageVersionID, err := s.publish(ctx, subscriptionID, sourceID, sharedImageGallery, miSGImageVersionEndOfLifeDate, miSGImageVersionExcludeFromLatest, miSigReplicaCount, location, diskEncryptionSetId, tags)
createdGalleryImageVersionID, err := s.publish(
PublishArgs{
ctx: ctx,
subscriptionID: subscriptionID,
sourceID: sourceID,
sharedImageGallery: sharedImageGallery,
miSGImageVersionEndOfLifeDate: miSGImageVersionEndOfLifeDate,
miSGImageVersionExcludeFromLatest: miSGImageVersionExcludeFromLatest,
miSigReplicaCount: miSigReplicaCount,
location: location,
diskEncryptionSetId: diskEncryptionSetId,
shallowReplicationMode: shallowReplicationMode,
tags: tags,
},
)

if err != nil {
stateBag.Put(constants.Error, err)
Expand Down
1 change: 1 addition & 0 deletions builder/azure/common/constants/stateBag.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ const (
ArmManagedImageSharedGalleryImageVersionStorageAccountType string = "arm.ArmManagedImageSharedGalleryImageVersionStorageAccountType"
ArmSharedImageGalleryDestinationSubscription string = "arm.ArmSharedImageGalleryDestinationSubscription"
ArmSharedImageGalleryDestinationSpecialized string = "arm.ArmSharedImageGalleryDestinationSpecialized"
ArmSharedImageGalleryDestinationShallowReplication string = "arm.ArmSharedImageGalleryDestinationShallowReplication"
ArmManagedImageSubscription string = "arm.ArmManagedImageSubscription"
ArmAsyncResourceGroupDelete string = "arm.AsyncResourceGroupDelete"
ArmManagedImageOSDiskSnapshotName string = "arm.ManagedImageOSDiskSnapshotName"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@

- `specialized` (bool) - Set to true if publishing to a Specialized Gallery, this skips a call to set the build VM's OS state as Generalized

- `shallow_replication` (bool) - Set to true if you want to publish using Shallow Replication, this is faster but skips copying the full image to the gallery image region, read more about Shallow Replication [here](https://learn.microsoft.com/en-us/azure/virtual-machines/shared-image-galleries?tabs=azure-cli#shallow-replication)

<!-- End of code generated from the comments of the SharedImageGalleryDestination struct in builder/azure/arm/config.go; -->

0 comments on commit 8c67ffe

Please sign in to comment.