Skip to content

Commit

Permalink
cloudapi: Override the request distro with the blueprint distro
Browse files Browse the repository at this point in the history
When the blueprint sets a specific distribution it should be used
instead of the distribution from the compose request.

Includes a test to make sure it uses repositories from the blueprint,
not the request.
  • Loading branch information
bcl authored and achilleas-k committed Mar 22, 2024
1 parent f311adf commit d3fc53b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
23 changes: 16 additions & 7 deletions internal/cloudapi/v2/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -967,11 +967,6 @@ func (request *ComposeRequest) GetPartitioningMode() (disk.PartitioningMode, err
// GetImageRequests converts a composeRequest structure from the API to an intermediate imageRequest structure
// that's used for generating manifests and orchestrating worker jobs.
func (request *ComposeRequest) GetImageRequests(distroFactory *distrofactory.Factory, repoRegistry *reporegistry.RepoRegistry) ([]imageRequest, error) {
distribution := distroFactory.GetDistro(request.Distribution)
if distribution == nil {
return nil, HTTPError(ErrorUnsupportedDistribution)
}

// OpenAPI enforces blueprint or customization, not both
// but check anyway
if request.Customizations != nil && request.Blueprint != nil {
Expand All @@ -984,6 +979,19 @@ func (request *ComposeRequest) GetImageRequests(distroFactory *distrofactory.Fac
return nil, err
}

// Used when no repositories are included. Must be the original name because it may
// be an alias and you cannot map back from the distrofactory to the original string.
originalDistroName := request.Distribution

// If there is a distribution in the blueprint it overrides the request's distro
if len(bp.Distro) > 0 {
originalDistroName = bp.Distro
}
distribution := distroFactory.GetDistro(originalDistroName)
if distribution == nil {
return nil, HTTPError(ErrorUnsupportedDistribution)
}

// add the user-defined repositories only to the depsolve job for the
// payload (the packages for the final image)
payloadRepositories := request.GetPayloadRepositories()
Expand Down Expand Up @@ -1024,9 +1032,10 @@ func (request *ComposeRequest) GetImageRequests(distroFactory *distrofactory.Fac
return nil, err
}

// If no repositories are included with the imageRequest use the defaults for the distro
// If no repositories are included with the imageRequest use the defaults for
// the distro selected by the blueprint, or the compose request.
if len(ir.Repositories) == 0 {
dr, err := repoRegistry.ReposByImageTypeName(request.Distribution, arch.Name(), imageType.Name())
dr, err := repoRegistry.ReposByImageTypeName(originalDistroName, arch.Name(), imageType.Name())
if err != nil {
return nil, err
}
Expand Down
30 changes: 29 additions & 1 deletion internal/cloudapi/v2/compose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -708,5 +708,33 @@ func TestGetImageRequests_NoRepositories(t *testing.T) {
got, err := request.GetImageRequests(distrofactory.NewDefault(), rr)
assert.NoError(t, err)
require.Len(t, got, 1)
assert.Greater(t, len(got[0].repositories), 0)
require.Greater(t, len(got[0].repositories), 0)
assert.Contains(t, got[0].repositories[0].Metalink, "40")
}

// TestGetImageRequests_BlueprintDistro test to make sure blueprint distro overrides request distro
func TestGetImageRequests_BlueprintDistro(t *testing.T) {
uo := UploadOptions(struct{}{})
request := &ComposeRequest{
Distribution: "fedora-40",
ImageRequest: &ImageRequest{
Architecture: "x86_64",
ImageType: ImageTypesAws,
UploadOptions: &uo,
Repositories: []Repository{},
},
Blueprint: &Blueprint{
Name: "distro-test",
Distro: common.ToPtr("fedora-38"),
},
}
// NOTE: current directory is the location of this file, back up so it can use ./repositories/
rr, err := reporegistry.New([]string{"../../../"})
require.NoError(t, err)
got, err := request.GetImageRequests(distrofactory.NewDefault(), rr)
assert.NoError(t, err)
require.Len(t, got, 1)
require.Greater(t, len(got[0].repositories), 0)
assert.Contains(t, got[0].repositories[0].Metalink, "38")
assert.Equal(t, got[0].blueprint.Distro, "fedora-38")
}

0 comments on commit d3fc53b

Please sign in to comment.