Merged PR 797274: Allow loading hosted pool configuration file from env variables

This commit is contained in:
Julian Bayardo 2024-07-29 17:32:25 +00:00
Родитель b633c1e25b
Коммит 4bc86f2548
3 изменённых файлов: 22 добавлений и 6 удалений

Просмотреть файл

@ -41,9 +41,7 @@ namespace BuildXL.Cache.BuildCacheResource.Helper
throw new ArgumentNullException(nameof(content), "The containing JSON is null"); throw new ArgumentNullException(nameof(content), "The containing JSON is null");
} }
var hostedPoolBuildCacheConfiguration = new HostedPoolBuildCacheConfiguration { AssociatedBuildCaches = buildCaches! }; return new HostedPoolBuildCacheConfiguration { AssociatedBuildCaches = buildCaches! };
return hostedPoolBuildCacheConfiguration;
} }
/// <summary> /// <summary>
@ -53,7 +51,7 @@ namespace BuildXL.Cache.BuildCacheResource.Helper
public static bool TrySelectBuildCache(this HostedPoolBuildCacheConfiguration hostedPoolBuildCacheConfiguration, string? hostedPoolActiveBuildCacheName, out BuildCacheConfiguration? buildCacheConfiguration) public static bool TrySelectBuildCache(this HostedPoolBuildCacheConfiguration hostedPoolBuildCacheConfiguration, string? hostedPoolActiveBuildCacheName, out BuildCacheConfiguration? buildCacheConfiguration)
{ {
// If an active cache is not provided, the first one in the collection of associated caches is the default // If an active cache is not provided, the first one in the collection of associated caches is the default
if (hostedPoolBuildCacheConfiguration is null) if (string.IsNullOrEmpty(hostedPoolActiveBuildCacheName))
{ {
buildCacheConfiguration = hostedPoolBuildCacheConfiguration!.AssociatedBuildCaches.First(); buildCacheConfiguration = hostedPoolBuildCacheConfiguration!.AssociatedBuildCaches.First();
return true; return true;

Просмотреть файл

@ -177,6 +177,18 @@ namespace BuildXL.Cache.MemoizationStoreAdapter
[DefaultValue(false)] [DefaultValue(false)]
public bool IsReadOnly { get; set; } public bool IsReadOnly { get; set; }
/// <summary>
/// Authenticate by using a file that contains a <see cref="HostedPoolBuildCacheConfigurationFile"/>
/// </summary>
/// <remarks>
/// The preferred authentication method is to use a managed identity (<see cref="StorageAccountEndpoint"/>
/// and <see cref="ManagedIdentityId"/>). However, this is unsupported for sharded scenarios and isn't
/// available outside of Azure. Use <see cref="ConnectionStringFileEnvironmentVariableName"/> if that's
/// your use-case.
/// </remarks>
[DefaultValue("BlobCacheFactoryHostedPoolConfigurationFile")]
public string HostedPoolBuildCacheConfigurationFileEnvironmentVariableName { get; set; }
/// <summary> /// <summary>
/// When not null, we are running on the context of 1ESHP and a set of cache resources are associated to the pool. The value of this string points /// When not null, we are running on the context of 1ESHP and a set of cache resources are associated to the pool. The value of this string points
/// to the JSON file describing this topology. /// to the JSON file describing this topology.

Просмотреть файл

@ -43,9 +43,15 @@ namespace BuildXL.Cache.MemoizationStoreAdapter
var context = new OperationContext(tracingContext); var context = new OperationContext(tracingContext);
// Case where an ADO build cache resource is configured // Case where an ADO build cache resource is configured
if (configuration.HostedPoolBuildCacheConfigurationFile != null) var hostedPoolConfigurationFilePath = configuration.HostedPoolBuildCacheConfigurationFile;
if (string.IsNullOrEmpty(hostedPoolConfigurationFilePath) && !string.IsNullOrEmpty(configuration.HostedPoolBuildCacheConfigurationFileEnvironmentVariableName))
{ {
var hostedPoolBuildCacheConfiguration = BuildCacheResourceHelper.LoadFromJSONAsync(configuration.HostedPoolBuildCacheConfigurationFile).GetAwaiter().GetResult(); hostedPoolConfigurationFilePath = Environment.GetEnvironmentVariable(configuration.HostedPoolBuildCacheConfigurationFileEnvironmentVariableName);
}
if (!string.IsNullOrEmpty(hostedPoolConfigurationFilePath))
{
var hostedPoolBuildCacheConfiguration = BuildCacheResourceHelper.LoadFromJSONAsync(hostedPoolConfigurationFilePath).GetAwaiter().GetResult();
if (!hostedPoolBuildCacheConfiguration.TrySelectBuildCache(configuration.HostedPoolActiveBuildCacheName, out var selectedBuildCacheConfiguration)) if (!hostedPoolBuildCacheConfiguration.TrySelectBuildCache(configuration.HostedPoolActiveBuildCacheName, out var selectedBuildCacheConfiguration))
{ {