Skip to content

Use Microsoft.Extensions.Configuration to load ACR Service Connection info at Runtime - #1880

Merged
lbussell merged 56 commits into
dotnet:mainfrom
lbussell:ms-ext-configuration
Dec 17, 2025
Merged

lbussell merged 56 commits into
dotnet:mainfrom
lbussell:ms-ext-configuration

Conversation

@lbussell

@lbussell lbussell commented Dec 9, 2025 •

Copy link
Copy Markdown
Member

Fixes #1747

This PR introduces a configuration-based approach for managing ACR references and service connections. The changeset is big, but mostly straightforward.

All of the other changes follow logically from there.

  • Removed --acr-service-connection CLI option from BuildOptions, CopyImagesOptions, CleanAcrImagesOptions, GenerateBuildMatrixOptions, AnnotateEolDigestsOptions, PublishManifestOptions.
  • No longer need to pass service connections or registries around between methods.
  • No more concept of "owned registries". We can just query the PublishConfiguration itself about what registries we have service connections for.
  • Added RegistryResolver to centralize logic around determining whether we have a service connection to a given registry.
  • Added a couple of additional abstractions (ImageName, Acr) to make reasoning about various strings easier. They handle parsing and normalizing references to those items.

Pipeline changes will come in a follow-up because otherwise this PR wouldn't pass validation. They have already been prepared and tested. They exist on this branch: https://dev.azure.com/dnceng/internal/_git/dotnet-docker-tools?version=GBdev/loganbussell/me-configuration&_a=history. Successful pipeline run: part 1, part 2

- Add RegistryUri property to Acr class for easy URI access
- Add ToAcr() extension method on AcrConfiguration for type conversion
- Remove unused using directive from AcrConfiguration.cs
Replace string parameters with ImageName type for better type safety:
- GetManifestAsync(ImageName image, ...)
- GetImageLayersAsync(ImageName image, ...)
- GetLocalImageDigestAsync(ImageName image, ...)
Remove ownedAcr and serviceConnectionOptions parameters from Create method.
Service connections are now looked up internally via IRegistryResolver and
PublishConfiguration rather than being passed through the factory chain.
- Remove TokenCredential parameter from Create method
- Inject IOptions<PublishConfiguration> and IAzureTokenCredentialProvider
- Use PublishConfiguration.FindAcrByName() to look up service connections
- Throw if no service connection is found for the requested ACR
…rnally

- Change Create method to accept Acr type instead of string acrName
- Remove IServiceConnection parameter
- Inject IOptions<PublishConfiguration> to look up service connections
- Use acr.RegistryUri directly instead of DockerHelper.GetAcrUri()
CopyImageService now looks up service connections from PublishConfiguration
internally, so the factory pattern is no longer needed.
- Accept ICopyImageService instead of ICopyImageServiceFactory
- Remove Lazy wrapper since service is now a singleton
- Remove AcrServiceConnection from CopyImagesOptions
- Remove ServiceConnectionOptionsBuilder from CopyImagesOptionsBuilder
…services

- Accept ICopyImageService instead of ICopyImageServiceFactory
- Use Acr.Parse().Name instead of CopyImageService.GetBaseAcrName()
- Update IManifestService mock setups to use ImageName instead of string
- Normalize image digest dictionary keys using ImageName.Parse() to match
  how the mock receives ImageName types
- Update GetStaleImagesCommand_BaseImageTagOverride test to use proper
  registry format (my-registry.io instead of my-registry)
- Fix BuildCommand test verify to use It.IsAny<ImageName>() instead of string

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces a significant architectural improvement by adopting Microsoft.Extensions.Configuration to centralize ACR service connection management. Instead of passing service connection details through command-line arguments and method parameters, the configuration is loaded from appsettings.json at startup and made available throughout the application via dependency injection using IOptions<PublishConfiguration>.

Key Changes:

  • Configuration-based approach replaces CLI arguments for ACR service connections across multiple commands (Build, CopyImages, CleanAcrImages, GenerateBuildMatrix, PublishManifest, AnnotateEolDigests)
  • New RegistryResolver service centralizes registry resolution logic and authentication method determination
  • Introduced strongly-typed abstractions (Acr, ImageName as record types) for better type safety and easier reasoning about registry references
  • Removed factory pattern for CopyImageService - now registered as a singleton directly

Reviewed changes

Copilot reviewed 66 out of 66 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
src/ImageBuilder/Configuration/PublishConfiguration.cs New configuration class defining ACR configurations for build, publish, and mirror registries
src/ImageBuilder/Configuration/Acr.cs New strongly-typed ACR reference abstraction with normalization logic
src/ImageBuilder/Configuration/ServiceConnection.cs New record type implementing IServiceConnection for configuration binding
src/ImageBuilder/Configuration/ConfigurationExtensions.cs Extension methods for configuration setup and ACR lookup logic
src/ImageBuilder/RegistryResolver.cs New service centralizing registry resolution and authentication method determination
src/ImageBuilder/IRegistryResolver.cs Interface defining registry resolution contract with RegistryInfo result type
src/ImageBuilder/RegistryManifestClientFactory.cs Updated to use RegistryResolver instead of direct parameter passing
src/ImageBuilder/RegistryCredentialsProvider.cs Simplified to use RegistryResolver; removed explicit registry/connection parameters
src/ImageBuilder/CopyImageService.cs No longer created via factory; added ArmClient caching and ACR lookup from configuration
src/ImageBuilder/ImageName.cs Changed from class to record; added implicit conversions and ToString(); properties now non-nullable with empty string defaults
src/ImageBuilder/AcrClientFactory.cs Updated to resolve service connections from configuration internally
src/ImageBuilder/AcrContentClientFactory.cs Updated to resolve service connections from configuration internally
src/ImageBuilder/Commands/*Options.cs Removed AcrServiceConnection CLI options from multiple command option classes
src/ImageBuilder/Commands/*Command.cs Updated commands to no longer pass service connections explicitly; rely on configuration
src/ImageBuilder/ImageBuilder.cs Added configuration loading via AddPublishConfiguration() and debug output
src/ImageBuilder.Tests/* Updated test mocks and helpers to accommodate new architecture
Comments suppressed due to low confidence (1)

src/ImageBuilder/ImageName.cs:73


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/ImageBuilder/ImageName.cs Resolved
Comment thread src/ImageBuilder/Configuration/Acr.cs Resolved
Comment thread src/ImageBuilder/ManifestService.cs Resolved
Comment thread src/ImageBuilder/CopyImageService.cs Resolved
Comment thread src/ImageBuilder/ImageBuilder.cs Resolved

@mthalman mthalman left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fantastic set of changes. I really like the simplicity this enables. Just a few comments.

public ServiceConnection? ServiceConnection { get; set; } = null;
}

public static class AcrConfigurationExtensions

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the need for this? Why not define the ToAcr method directly in AcrConfiguration?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't see a compelling reason to include it in the record itself. If the record definition only includes the properties, then it's easier to glance at and reason about what kinds of configurations are possible/acceptable via appsettings.json. I could see an argument for moving this to ConfigurationExtensions.

I would have done the same for PublishConfiguration.GetKnownAcrConfigurations(), but I decided that method was better suited in the class since it needs to stay in sync with all of the AcrConfiguration properties.

@lbussell

Copy link
Copy Markdown
Member Author

Not sure why the Windows build is still failing, because I added to the InterceptorsNamespaces property in the csproj exactly as the error message says. Same build error still. Windows only.

@lbussell
lbussell merged commit 112717d into dotnet:main Dec 17, 2025
@lbussell
lbussell deleted the ms-ext-configuration branch December 17, 2025 17:19
lbussell added a commit that referenced this pull request Jan 6, 2026
…configuration (#1900)

This PR is a follow-up to #1880. It updates the pipelines to use
appsettings.json to configure service connections instead of passing
them as command line arguments.

It includes the ImageBuilder update from #1894. I am submitting this as
a separate PR since I don't want automated builds to unexpectedly
overwrite my progress.

---------

Co-authored-by: dotnet-docker-bot <[email protected]>
Co-authored-by: Copilot <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ImageBuilder should consume a configuration file for build/publishing operations

3 participants