Skip to content

Commit

Permalink
terraform: only fetch image for non-marketplace images
Browse files Browse the repository at this point in the history
  • Loading branch information
msanft committed Nov 28, 2023
1 parent a24f23c commit b64ee05
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
20 changes: 12 additions & 8 deletions cli/internal/cloudcmd/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,18 @@ func (a *Applier) WorkingDirIsEmpty() (bool, error) {
}

func (a *Applier) terraformApplyVars(ctx context.Context, conf *config.Config) (terraform.Variables, error) {
imageRef, err := a.imageFetcher.FetchReference(
ctx,
conf.GetProvider(),
conf.GetAttestationConfig().GetVariant(),
conf.Image, conf.GetRegion(),
)
if err != nil {
return nil, fmt.Errorf("fetching image reference: %w", err)
var imageRef string
var err error
if !conf.UseMarketplaceImage() {
imageRef, err = a.imageFetcher.FetchReference(
ctx,
conf.GetProvider(),
conf.GetAttestationConfig().GetVariant(),
conf.Image, conf.GetRegion(),
)
if err != nil {
return nil, fmt.Errorf("fetching image reference: %w", err)
}
}

switch conf.GetProvider() {
Expand Down
7 changes: 5 additions & 2 deletions cli/internal/cloudcmd/tfvars.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ func azureTerraformVars(conf *config.Config, imageRef string) (*terraform.AzureC
Name: conf.Name,
NodeGroups: nodeGroups,
Location: conf.Provider.Azure.Location,
ImageID: imageRef,
CreateMAA: toPtr(conf.GetAttestationConfig().GetVariant().Equal(variant.AzureSEVSNP{})),
Debug: toPtr(conf.IsDebugCluster()),
ConfidentialVM: toPtr(conf.GetAttestationConfig().GetVariant().Equal(variant.AzureSEVSNP{})),
Expand All @@ -159,7 +158,8 @@ func azureTerraformVars(conf *config.Config, imageRef string) (*terraform.AzureC
InternalLoadBalancer: conf.InternalLoadBalancer,
}

if conf.Provider.Azure.UseMarketplaceImage != nil {
if conf.UseMarketplaceImage() {
// If a marketplace image is used, only the marketplace reference is required.
imageVersion, err := semver.New(conf.Image)
if err != nil {
return nil, fmt.Errorf("parsing image version: %w. %s does not look like a valid release image", err, conf.Image)
Expand All @@ -171,6 +171,9 @@ func azureTerraformVars(conf *config.Config, imageRef string) (*terraform.AzureC
Name: constants.AzureMarketplaceImagePlan,
Version: imageVersion.StringWithoutPrefix(),
}
} else {
// If not, we need to specify the exact CommunityGalleries/.. image reference.
vars.ImageID = imageRef
}

vars = normalizeAzureURIs(vars)
Expand Down
5 changes: 5 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,11 @@ func (c *Config) DeployYawolLoadBalancer() bool {
return c.Provider.OpenStack != nil && c.Provider.OpenStack.DeployYawolLoadBalancer != nil && *c.Provider.OpenStack.DeployYawolLoadBalancer
}

// UseMarketplaceImage returns whether a marketplace image should be used.
func (c *Config) UseMarketplaceImage() bool {
return c.Provider.Azure != nil && c.Provider.Azure.UseMarketplaceImage != nil && *c.Provider.Azure.UseMarketplaceImage
}

// Validate checks the config values and returns validation errors.
func (c *Config) Validate(force bool) error {
trans := ut.New(en.New()).GetFallback()
Expand Down

0 comments on commit b64ee05

Please sign in to comment.