-
Notifications
You must be signed in to change notification settings - Fork 513
/
AzureContainerAppsInfrastructure.cs
1056 lines (847 loc) · 46.3 KB
/
AzureContainerAppsInfrastructure.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;
using Aspire.Hosting.ApplicationModel;
using Aspire.Hosting.Lifecycle;
using Azure.Provisioning;
using Azure.Provisioning.AppContainers;
using Azure.Provisioning.Expressions;
using Azure.Provisioning.KeyVault;
using Azure.Provisioning.Resources;
using Microsoft.Extensions.Logging;
namespace Aspire.Hosting.Azure;
/// <summary>
/// Represents the infrastructure for Azure Container Apps within the Aspire Hosting environment.
/// Implements the <see cref="IDistributedApplicationLifecycleHook"/> interface to provide lifecycle hooks for distributed applications.
/// </summary>
internal sealed class AzureContainerAppsInfrastructure(ILogger<AzureContainerAppsInfrastructure> logger, DistributedApplicationExecutionContext executionContext) : IDistributedApplicationLifecycleHook
{
public async Task BeforeStartAsync(DistributedApplicationModel appModel, CancellationToken cancellationToken = default)
{
if (executionContext.IsRunMode)
{
return;
}
var containerAppEnvironmentContext = new ContainerAppEnvironmentContext(
logger,
AzureContainerAppsEnvironment.AZURE_CONTAINER_APPS_ENVIRONMENT_ID,
AzureContainerAppsEnvironment.AZURE_CONTAINER_APPS_ENVIRONMENT_DEFAULT_DOMAIN,
AzureContainerAppsEnvironment.AZURE_CONTAINER_REGISTRY_MANAGED_IDENTITY_ID,
AzureContainerAppsEnvironment.AZURE_CONTAINER_REGISTRY_ENDPOINT,
AzureContainerAppsEnvironment.AZURE_CONTAINER_REGISTRY_MANAGED_IDENTITY_ID,
AzureContainerAppsEnvironment.MANAGED_IDENTITY_CLIENT_ID);
foreach (var r in appModel.Resources)
{
if (r.TryGetLastAnnotation<ManifestPublishingCallbackAnnotation>(out var lastAnnotation) && lastAnnotation == ManifestPublishingCallbackAnnotation.Ignore)
{
continue;
}
if (!r.IsContainer() && r is not ProjectResource)
{
continue;
}
var containerApp = await containerAppEnvironmentContext.CreateContainerAppAsync(r, executionContext, cancellationToken).ConfigureAwait(false);
r.Annotations.Add(new DeploymentTargetAnnotation(containerApp));
}
}
private sealed class ContainerAppEnvironmentContext(
ILogger logger,
IManifestExpressionProvider containerAppEnvironmentId,
IManifestExpressionProvider containerAppDomain,
IManifestExpressionProvider managedIdentityId,
IManifestExpressionProvider containerRegistryUrl,
IManifestExpressionProvider containerRegistryManagedIdentityId,
IManifestExpressionProvider clientId
)
{
private ILogger Logger => logger;
private IManifestExpressionProvider ContainerAppEnvironmentId => containerAppEnvironmentId;
private IManifestExpressionProvider ContainerAppDomain => containerAppDomain;
private IManifestExpressionProvider ManagedIdentityId => managedIdentityId;
private IManifestExpressionProvider ContainerRegistryUrl => containerRegistryUrl;
private IManifestExpressionProvider ContainerRegistryManagedIdentityId => containerRegistryManagedIdentityId;
private IManifestExpressionProvider ClientId => clientId;
private readonly Dictionary<IResource, ContainerAppContext> _containerApps = [];
public async Task<AzureBicepResource> CreateContainerAppAsync(IResource resource, DistributedApplicationExecutionContext executionContext, CancellationToken cancellationToken)
{
var context = await ProcessResourceAsync(resource, executionContext, cancellationToken).ConfigureAwait(false);
var provisioningResource = new AzureProvisioningResource(resource.Name, context.BuildContainerApp);
provisioningResource.Annotations.Add(new ManifestPublishingCallbackAnnotation(provisioningResource.WriteToManifest));
return provisioningResource;
}
private async Task<ContainerAppContext> ProcessResourceAsync(IResource resource, DistributedApplicationExecutionContext executionContext, CancellationToken cancellationToken)
{
if (!_containerApps.TryGetValue(resource, out var context))
{
_containerApps[resource] = context = new ContainerAppContext(resource, this);
await context.ProcessResourceAsync(executionContext, cancellationToken).ConfigureAwait(false);
}
return context;
}
private sealed class ContainerAppContext(IResource resource, ContainerAppEnvironmentContext containerAppEnvironmentContext)
{
private readonly Dictionary<object, string> _allocatedParameters = [];
private readonly Dictionary<string, ProvisioningParameter> _provisioningParameters = [];
private readonly ContainerAppEnvironmentContext _containerAppEnvironmentContext = containerAppEnvironmentContext;
record struct EndpointMapping(string Scheme, string Host, int Port, int? TargetPort, bool IsHttpIngress, bool External);
private readonly Dictionary<string, EndpointMapping> _endpointMapping = [];
private (int? Port, bool Http2, bool External)? _httpIngress;
private readonly List<int> _additionalPorts = [];
private ProvisioningParameter? _managedIdentityIdParameter;
private ProvisioningParameter? _containerRegistryUrlParameter;
private ProvisioningParameter? _containerRegistryManagedIdentityIdParameter;
public IResource Resource => resource;
// Set the parameters to add to the bicep file
public Dictionary<string, IManifestExpressionProvider> Parameters { get; } = [];
public List<ContainerAppEnvironmentVariable> EnvironmentVariables { get; } = [];
public List<ContainerAppWritableSecret> Secrets { get; } = [];
public List<BicepValue<string>> Args { get; } = [];
public List<(ContainerAppVolume, ContainerAppVolumeMount)> Volumes { get; } = [];
public Dictionary<string, KeyVaultService> KeyVaultRefs { get; } = [];
public Dictionary<string, KeyVaultSecret> KeyVaultSecretRefs { get; } = [];
public void BuildContainerApp(AzureResourceInfrastructure c)
{
var containerAppIdParam = AllocateParameter(_containerAppEnvironmentContext.ContainerAppEnvironmentId);
ProvisioningParameter? containerImageParam = null;
if (!TryGetContainerImageName(resource, out var containerImageName))
{
AllocateContainerRegistryParameters();
containerImageParam = AllocateContainerImageParameter();
}
var containerAppResource = new ContainerApp(Infrastructure.NormalizeIdentifierName(resource.Name))
{
Name = resource.Name.ToLowerInvariant()
};
// TODO: Add managed identities only when required
AddManagedIdentites(containerAppResource);
containerAppResource.EnvironmentId = containerAppIdParam;
var configuration = new ContainerAppConfiguration()
{
ActiveRevisionsMode = ContainerAppActiveRevisionsMode.Single,
};
containerAppResource.Configuration = configuration;
AddIngress(configuration);
AddContainerRegistryParameters(configuration);
AddSecrets(configuration);
var template = new ContainerAppTemplate();
containerAppResource.Template = template;
foreach (var (volume, _) in Volumes)
{
template.Volumes.Add(volume);
}
template.Scale = new ContainerAppScale()
{
MinReplicas = resource.GetReplicaCount()
};
var containerAppContainer = new ContainerAppContainer();
template.Containers = [containerAppContainer];
containerAppContainer.Image = containerImageParam is null ? containerImageName : containerImageParam;
containerAppContainer.Name = resource.Name;
AddEnvironmentVariablesAndCommandLineArgs(containerAppContainer);
foreach (var (_, mountedVolume) in Volumes)
{
containerAppContainer.VolumeMounts.Add(mountedVolume);
}
// Add parameters to the provisioningResource
foreach (var (_, parameter) in _provisioningParameters)
{
c.Add(parameter);
}
// Keyvault
foreach (var (_, v) in KeyVaultRefs)
{
c.Add(v);
}
foreach (var (_, v) in KeyVaultSecretRefs)
{
c.Add(v);
}
c.Add(containerAppResource);
// Write the parameters we generated to the construct so they are included in the manifest
foreach (var (key, value) in Parameters)
{
c.AspireResource.Parameters[key] = value;
}
if (resource.TryGetAnnotationsOfType<AzureContainerAppCustomizationAnnotation>(out var annotations))
{
foreach (var a in annotations)
{
a.Configure(c, containerAppResource);
}
}
}
private static bool TryGetContainerImageName(IResource resource, out string? containerImageName)
{
// If the resource has a Dockerfile build annotation, we don't have the image name
// it will come as a parameter
if (resource.TryGetLastAnnotation<DockerfileBuildAnnotation>(out _))
{
containerImageName = null;
return false;
}
return resource.TryGetContainerImageName(out containerImageName);
}
public async Task ProcessResourceAsync(DistributedApplicationExecutionContext executionContext, CancellationToken cancellationToken)
{
ProcessEndpoints();
ProcessVolumes();
await ProcessEnvironmentAsync(executionContext, cancellationToken).ConfigureAwait(false);
await ProcessArgumentsAsync(executionContext, cancellationToken).ConfigureAwait(false);
}
private void ProcessEndpoints()
{
if (!resource.TryGetEndpoints(out var endpoints) || !endpoints.Any())
{
return;
}
// Only http, https, and tcp are supported
var unsupportedEndpoints = endpoints.Where(e => e.UriScheme is not ("tcp" or "http" or "https")).ToArray();
if (unsupportedEndpoints.Length > 0)
{
throw new NotSupportedException($"The endpoint(s) {string.Join(", ", unsupportedEndpoints.Select(e => $"'{e.Name}'"))} specify an unsupported scheme. The supported schemes are 'http', 'https', and 'tcp'.");
}
// We can allocate ports per endpoint
var portAllocator = new PortAllocator();
var endpointIndexMap = new Dictionary<string, int>();
// This is used to determine if an endpoint should be treated as the Default endpoint.
// Endpoints can come from 3 different sources (in this order):
// 1. Kestrel configuration
// 2. Default endpoints added by the framework
// 3. Explicitly added endpoints
// But wherever they come from, we treat the first one as Default, for each scheme.
var httpSchemesEncountered = new HashSet<string>();
static bool IsHttpScheme(string scheme) => scheme is "http" or "https";
// Allocate ports for the endpoints
foreach (var endpoint in endpoints)
{
endpointIndexMap[endpoint.Name] = endpointIndexMap.Count;
int? targetPort = (resource, endpoint.UriScheme, endpoint.TargetPort, endpoint.Port) switch
{
// The port was specified so use it
(_, _, int target, _) => target,
// Container resources get their default listening port from the exposed port.
(ContainerResource, _, null, int port) => port,
// Check whether the project view this endpoint as Default (for its scheme).
// If so, we don't specify the target port, as it will get one from the deployment tool.
(ProjectResource project, string uriScheme, null, _) when IsHttpScheme(uriScheme) && !httpSchemesEncountered.Contains(uriScheme) => null,
// Allocate a dynamic port
_ => portAllocator.AllocatePort()
};
// We only keep track of schemes for project resources, since we don't want
// a non-project scheme to affect what project endpoints are considered default.
if (resource is ProjectResource && IsHttpScheme(endpoint.UriScheme))
{
httpSchemesEncountered.Add(endpoint.UriScheme);
}
int? exposedPort = (endpoint.UriScheme, endpoint.Port, targetPort) switch
{
// Exposed port and target port are the same, we don't need to mention the exposed port
(_, int p0, int p1) when p0 == p1 => null,
// Port was specified, so use it
(_, int port, _) => port,
// We have a target port, not need to specify an exposedPort
// it will default to the targetPort
(_, null, int port) => null,
// Let the tool infer the default http and https ports
("http", null, null) => null,
("https", null, null) => null,
// Other schemes just allocate a port
_ => portAllocator.AllocatePort()
};
if (exposedPort is int ep)
{
portAllocator.AddUsedPort(ep);
endpoint.Port = ep;
}
if (targetPort is int tp)
{
portAllocator.AddUsedPort(tp);
endpoint.TargetPort = tp;
}
}
// First we group the endpoints by container port (aka destinations), this gives us the logical bindings or destinations
var endpointsByTargetPort = endpoints.GroupBy(e => e.TargetPort)
.Select(g => new
{
Port = g.Key,
Endpoints = g.ToArray(),
External = g.Any(e => e.IsExternal),
IsHttpOnly = g.All(e => e.UriScheme is "http" or "https"),
AnyH2 = g.Any(e => e.Transport is "http2"),
UniqueSchemes = g.Select(e => e.UriScheme).Distinct().ToArray(),
Index = g.Min(e => endpointIndexMap[e.Name])
})
.ToList();
// Failure cases
// Multiple external endpoints are not supported
if (endpointsByTargetPort.Count(g => g.External) > 1)
{
throw new NotSupportedException("Multiple external endpoints are not supported");
}
// Any external non-http endpoints are not supported
if (endpointsByTargetPort.Any(g => g.External && !g.IsHttpOnly))
{
throw new NotSupportedException("External non-HTTP(s) endpoints are not supported");
}
// Don't allow mixing http and tcp endpoints
// This means we want to fail if we see a group with http/https and tcp endpoints
static bool Compatible(string[] schemes) =>
schemes.All(s => s is "http" or "https") || schemes.All(s => s is "tcp");
if (endpointsByTargetPort.Any(g => !Compatible(g.UniqueSchemes)))
{
throw new NotSupportedException("HTTP(s) and TCP endpoints cannot be mixed");
}
// Get all http only groups
var httpOnlyEndpoints = endpointsByTargetPort.Where(g => g.IsHttpOnly).OrderBy(g => g.Index).ToArray();
// Do we only have one?
var httpIngress = httpOnlyEndpoints.Length == 1 ? httpOnlyEndpoints[0] : null;
if (httpIngress is null)
{
// We have more than one, pick prefer external one
var externalHttp = httpOnlyEndpoints.Where(g => g.External).ToArray();
if (externalHttp.Length == 1)
{
httpIngress = externalHttp[0];
}
else if (httpOnlyEndpoints.Length > 0)
{
httpIngress = httpOnlyEndpoints[0];
}
}
if (httpIngress is not null)
{
// We're processed the http ingress, remove it from the list
endpointsByTargetPort.Remove(httpIngress);
var targetPort = httpIngress.Port ?? (resource is ProjectResource ? null : 80);
_httpIngress = (targetPort, httpIngress.AnyH2, httpIngress.External);
foreach (var e in httpIngress.Endpoints)
{
if (e.UriScheme is "http" && e.Port is not null and not 80)
{
throw new NotSupportedException($"The endpoint '{e.Name}' is an http endpoint and must use port 80");
}
if (e.UriScheme is "https" && e.Port is not null and not 443)
{
throw new NotSupportedException($"The endpoint '{e.Name}' is an https endpoint and must use port 443");
}
// For the http ingress port is always 80 or 443
var port = e.UriScheme is "http" ? 80 : 443;
_endpointMapping[e.Name] = new(e.UriScheme, resource.Name, port, targetPort, true, httpIngress.External);
}
}
if (endpointsByTargetPort.Count > 5)
{
_containerAppEnvironmentContext.Logger.LogWarning("More than 5 additional ports are not supported. See https://learn.microsoft.com/en-us/azure/container-apps/ingress-overview#tcp for more details.");
}
foreach (var g in endpointsByTargetPort)
{
if (g.Port is null)
{
throw new NotSupportedException("Container port is required for all endpoints");
}
_additionalPorts.Add(g.Port.Value);
foreach (var e in g.Endpoints)
{
_endpointMapping[e.Name] = new(e.UriScheme, resource.Name, e.Port ?? g.Port.Value, g.Port.Value, false, g.External);
}
}
}
private async Task ProcessArgumentsAsync(DistributedApplicationExecutionContext executionContext, CancellationToken cancellationToken)
{
if (resource.TryGetAnnotationsOfType<CommandLineArgsCallbackAnnotation>(out var commandLineArgsCallbackAnnotations))
{
var context = new CommandLineArgsCallbackContext([], cancellationToken: cancellationToken);
foreach (var c in commandLineArgsCallbackAnnotations)
{
await c.Callback(context).ConfigureAwait(false);
}
foreach (var arg in context.Args)
{
var (val, _) = await ProcessValueAsync(arg, executionContext, cancellationToken).ConfigureAwait(false);
var argValue = ResolveValue(val);
Args.Add(argValue);
}
}
}
private async Task ProcessEnvironmentAsync(DistributedApplicationExecutionContext executionContext, CancellationToken cancellationToken)
{
if (resource.TryGetAnnotationsOfType<EnvironmentCallbackAnnotation>(out var environmentCallbacks))
{
var context = new EnvironmentCallbackContext(executionContext, cancellationToken: cancellationToken);
foreach (var c in environmentCallbacks)
{
await c.Callback(context).ConfigureAwait(false);
}
foreach (var kv in context.EnvironmentVariables)
{
var (val, secretType) = await ProcessValueAsync(kv.Value, executionContext, cancellationToken).ConfigureAwait(false);
var argValue = ResolveValue(val);
if (secretType != SecretType.None)
{
var secretName = kv.Key.Replace("_", "-").ToLowerInvariant();
var secret = new ContainerAppWritableSecret()
{
Name = secretName
};
if (secretType == SecretType.KeyVault)
{
var managedIdentityParameter = AllocateManagedIdentityIdParameter();
secret.Identity = managedIdentityParameter;
secret.KeyVaultUri = new BicepValue<Uri>(argValue.Expression!);
}
else
{
secret.Value = argValue;
}
Secrets.Add(secret);
// The value is the secret name
val = secretName;
}
EnvironmentVariables.Add(secretType switch
{
SecretType.None => new ContainerAppEnvironmentVariable { Name = kv.Key, Value = argValue },
SecretType.Normal or SecretType.KeyVault => new ContainerAppEnvironmentVariable { Name = kv.Key, SecretRef = (string)val },
_ => throw new NotSupportedException()
});
}
}
// TODO: Add managed identity only if needed
AllocateManagedIdentityIdParameter();
var clientIdParameter = AllocateParameter(_containerAppEnvironmentContext.ClientId);
EnvironmentVariables.Add(new ContainerAppEnvironmentVariable { Name = "AZURE_CLIENT_ID", Value = clientIdParameter });
}
private static BicepValue<string> ResolveValue(object val)
{
return val switch
{
BicepValue<string> s => s,
string s => s,
BicepValueFormattableString fs => Interpolate(fs),
ProvisioningParameter p => p,
_ => throw new NotSupportedException("Unsupported value type " + val.GetType())
};
}
private void ProcessVolumes()
{
if (resource.TryGetContainerMounts(out var mounts))
{
var bindMountIndex = 0;
var volumeIndex = 0;
foreach (var volume in mounts)
{
var (index, volumeName) = volume.Type switch
{
ContainerMountType.BindMount => ($"{bindMountIndex}", $"bm{bindMountIndex}"),
ContainerMountType.Volume => ($"{volumeIndex}", $"v{volumeIndex}"),
_ => throw new NotSupportedException()
};
if (volume.Type == ContainerMountType.BindMount)
{
bindMountIndex++;
}
else
{
volumeIndex++;
}
var volumeStorageParameter = AllocateVolumeStorageAccount(volume.Type, index);
var containerAppVolume = new ContainerAppVolume
{
Name = volumeName,
StorageType = ContainerAppStorageType.AzureFile,
StorageName = volumeStorageParameter,
};
var containerAppVolumeMount = new ContainerAppVolumeMount
{
VolumeName = volumeName,
MountPath = volume.Target,
};
Volumes.Add((containerAppVolume, containerAppVolumeMount));
}
}
}
private BicepValue<string> GetValue(EndpointMapping mapping, EndpointProperty property)
{
var (scheme, host, port, targetPort, isHttpIngress, external) = mapping;
BicepValue<string> GetHostValue(string? prefix = null, string? suffix = null)
{
if (isHttpIngress)
{
var domain = AllocateParameter(_containerAppEnvironmentContext.ContainerAppDomain);
return external ? BicepFunction.Interpolate($$"""{{prefix}}{{host}}.{{domain}}{{suffix}}""") : BicepFunction.Interpolate($$"""{{prefix}}{{host}}.internal.{{domain}}{{suffix}}""");
}
return $"{prefix}{host}{suffix}";
}
return property switch
{
EndpointProperty.Url => GetHostValue($"{scheme}://", suffix: isHttpIngress ? null : $":{port}"),
EndpointProperty.Host or EndpointProperty.IPV4Host => GetHostValue(),
EndpointProperty.Port => port.ToString(CultureInfo.InvariantCulture),
EndpointProperty.TargetPort => targetPort is null ? AllocateContainerPortParameter() : targetPort,
EndpointProperty.Scheme => scheme,
_ => throw new NotSupportedException(),
};
}
private async Task<(object, SecretType)> ProcessValueAsync(object value, DistributedApplicationExecutionContext executionContext, CancellationToken cancellationToken, SecretType secretType = SecretType.None, object? parent = null)
{
if (value is string s)
{
return (s, secretType);
}
if (value is EndpointReference ep)
{
var context = ep.Resource == resource
? this
: await _containerAppEnvironmentContext.ProcessResourceAsync(ep.Resource, executionContext, cancellationToken).ConfigureAwait(false);
var mapping = context._endpointMapping[ep.EndpointName];
var url = GetValue(mapping, EndpointProperty.Url);
return (url, secretType);
}
if (value is ConnectionStringReference cs)
{
return await ProcessValueAsync(cs.Resource.ConnectionStringExpression, executionContext, cancellationToken, secretType: secretType, parent: parent).ConfigureAwait(false);
}
if (value is IResourceWithConnectionString csrs)
{
return await ProcessValueAsync(csrs.ConnectionStringExpression, executionContext, cancellationToken, secretType: secretType, parent: parent).ConfigureAwait(false);
}
if (value is ParameterResource param)
{
var st = param.Secret ? SecretType.Normal : secretType;
return (AllocateParameter(param, secretType: st), st);
}
if (value is BicepOutputReference output)
{
return (AllocateParameter(output, secretType: secretType), secretType);
}
if (value is BicepSecretOutputReference secretOutputReference)
{
if (parent is null)
{
return (AllocateKeyVaultSecretUriReference(secretOutputReference), SecretType.KeyVault);
}
return (AllocateParameter(secretOutputReference, secretType: SecretType.KeyVault), SecretType.KeyVault);
}
if (value is EndpointReferenceExpression epExpr)
{
var context = epExpr.Endpoint.Resource == resource
? this
: await _containerAppEnvironmentContext.ProcessResourceAsync(epExpr.Endpoint.Resource, executionContext, cancellationToken).ConfigureAwait(false);
var mapping = context._endpointMapping[epExpr.Endpoint.EndpointName];
var val = GetValue(mapping, epExpr.Property);
return (val, secretType);
}
if (value is ReferenceExpression expr)
{
// Special case simple expressions
if (expr.Format == "{0}" && expr.ValueProviders.Count == 1)
{
return await ProcessValueAsync(expr.ValueProviders[0], executionContext, cancellationToken, secretType, parent: parent).ConfigureAwait(false);
}
var args = new object[expr.ValueProviders.Count];
var index = 0;
var finalSecretType = SecretType.None;
foreach (var vp in expr.ValueProviders)
{
var (val, secret) = await ProcessValueAsync(vp, executionContext, cancellationToken, secretType, parent: expr).ConfigureAwait(false);
if (secret != SecretType.None)
{
finalSecretType = SecretType.Normal;
}
args[index++] = val;
}
return (new BicepValueFormattableString(expr.Format, args), finalSecretType);
}
throw new NotSupportedException("Unsupported value type " + value.GetType());
}
private ProvisioningParameter AllocateVolumeStorageAccount(ContainerMountType type, string volumeIndex) =>
AllocateParameter(VolumeStorageExpression.GetVolumeStorage(resource, type, volumeIndex));
private BicepValue<string> AllocateKeyVaultSecretUriReference(BicepSecretOutputReference secretOutputReference)
{
if (!KeyVaultRefs.TryGetValue(secretOutputReference.Resource.Name, out var kv))
{
// We resolve the keyvault that represents the storage for secret outputs
var parameter = AllocateParameter(SecretOutputExpression.GetSecretOutputKeyVault(secretOutputReference.Resource));
kv = KeyVaultService.FromExisting($"{parameter.IdentifierName}_kv");
kv.Name = parameter;
KeyVaultRefs[secretOutputReference.Resource.Name] = kv;
}
if (!KeyVaultSecretRefs.TryGetValue(secretOutputReference.ValueExpression, out var secret))
{
// Now we resolve the secret
var secretIdentifierName = Infrastructure.NormalizeIdentifierName($"{kv.IdentifierName}_{secretOutputReference.Name}");
secret = KeyVaultSecret.FromExisting(secretIdentifierName);
secret.Name = secretOutputReference.Name;
secret.Parent = kv;
KeyVaultSecretRefs[secretOutputReference.ValueExpression] = secret;
}
// TODO: There should be a better way to do this?
return new MemberExpression(
new MemberExpression(
new IdentifierExpression(secret.IdentifierName), "properties"),
"secretUri");
}
private ProvisioningParameter AllocateContainerImageParameter()
=> AllocateParameter(ResourceExpression.GetContainerImageExpression(resource));
private BicepValue<int> AllocateContainerPortParameter()
=> AllocateParameter(ResourceExpression.GetContainerPortExpression(resource));
private ProvisioningParameter AllocateManagedIdentityIdParameter()
=> _managedIdentityIdParameter ??= AllocateParameter(_containerAppEnvironmentContext.ManagedIdentityId);
private void AllocateContainerRegistryParameters()
{
_containerRegistryUrlParameter ??= AllocateParameter(_containerAppEnvironmentContext.ContainerRegistryUrl);
_containerRegistryManagedIdentityIdParameter ??= AllocateParameter(_containerAppEnvironmentContext.ContainerRegistryManagedIdentityId);
}
private ProvisioningParameter AllocateParameter(IManifestExpressionProvider parameter, Type? type = null, SecretType secretType = SecretType.None)
{
if (!_allocatedParameters.TryGetValue(parameter, out var parameterName))
{
parameterName = parameter.ValueExpression.Replace("{", "").Replace("}", "").Replace(".", "_").Replace("-", "_").ToLowerInvariant();
if (parameterName[0] == '_')
{
parameterName = parameterName[1..];
}
_allocatedParameters[parameter] = parameterName;
}
if (!_provisioningParameters.TryGetValue(parameterName, out var provisioningParameter))
{
_provisioningParameters[parameterName] = provisioningParameter = new ProvisioningParameter(parameterName, type ?? typeof(string)) { IsSecure = secretType != SecretType.None };
}
Parameters[parameterName] = parameter;
return provisioningParameter;
}
private void AddIngress(ContainerAppConfiguration config)
{
if (_httpIngress is null && _additionalPorts.Count == 0)
{
return;
}
// Now we map the remaining endpoints. These should be internal only tcp/http based endpoints
var skipAdditionalPort = 0;
var caIngress = new ContainerAppIngressConfiguration();
if (_httpIngress is { } ingress)
{
caIngress.External = ingress.External;
caIngress.TargetPort = ingress.Port ?? AllocateContainerPortParameter();
caIngress.Transport = ingress.Http2 ? ContainerAppIngressTransportMethod.Http2 : ContainerAppIngressTransportMethod.Http;
}
else if (_additionalPorts.Count > 0)
{
// First port is the default
var port = _additionalPorts[0];
skipAdditionalPort++;
caIngress.External = false;
caIngress.TargetPort = port;
caIngress.Transport = ContainerAppIngressTransportMethod.Tcp;
}
// Add additional ports
// https://learn.microsoft.com/en-us/azure/container-apps/ingress-how-to?pivots=azure-cli#use-additional-tcp-ports
var additionalPorts = _additionalPorts.Skip(skipAdditionalPort);
if (additionalPorts.Any())
{
foreach (var port in additionalPorts)
{
caIngress.AdditionalPortMappings.Add(new IngressPortMapping
{
External = false,
TargetPort = port
});
}
}
config.Ingress = caIngress;
}
private void AddEnvironmentVariablesAndCommandLineArgs(ContainerAppContainer container)
{
if (EnvironmentVariables.Count > 0)
{
container.Env = [];
foreach (var ev in EnvironmentVariables)
{
container.Env.Add(ev);
}
}
if (Args.Count > 0)
{
container.Args = new(Args);
}
}
private void AddSecrets(ContainerAppConfiguration config)
{
if (Secrets.Count == 0)
{
return;
}
config.Secrets = [];
foreach (var s in Secrets)
{
config.Secrets.Add(s);
}
}
private void AddManagedIdentites(ContainerApp app)
{
if (_managedIdentityIdParameter is null)
{
return;
}
// REVIEW: This is is a little hacky, we should probably have a better way to do this
var id = BicepFunction.Interpolate($"{_managedIdentityIdParameter}").Compile().ToString();
app.Identity = new ManagedServiceIdentity()
{
ManagedServiceIdentityType = ManagedServiceIdentityType.UserAssigned,
UserAssignedIdentities = new()
{
[id] = new UserAssignedIdentityDetails()
}
};
}
private void AddContainerRegistryParameters(ContainerAppConfiguration app)
{
if (_containerRegistryUrlParameter is null || _containerRegistryManagedIdentityIdParameter is null)
{
return;
}
app.Registries = [
new ContainerAppRegistryCredentials
{
Server = _containerRegistryUrlParameter,
Identity = _containerRegistryManagedIdentityIdParameter
}
];
}
}
}
// REVIEW: BicepFunction.Interpolate is buggy and doesn't handle nested formattable strings correctly
// This is a workaround to handle nested formattable strings until the bug is fixed.
private static BicepValue<string> Interpolate(BicepValueFormattableString text)
{
var formatStringBuilder = new StringBuilder();
var arguments = new List<BicepValue<string>>();
void ProcessFormattableString(BicepValueFormattableString formattableString, int argumentIndex)
{
var span = formattableString.Format.AsSpan();
var skip = 0;
foreach (var match in Regex.EnumerateMatches(span, @"{\d+}"))
{
formatStringBuilder.Append(span[..(match.Index - skip)]);
var argument = formattableString.GetArgument(argumentIndex);
if (argument is BicepValueFormattableString nested)
{
// Inline the nested formattable string
ProcessFormattableString(nested, 0);
}
else
{
formatStringBuilder.Append(CultureInfo.InvariantCulture, $"{{{arguments.Count}}}");
if (argument is BicepValue<string> bicepValue)
{
arguments.Add(bicepValue);
}
else if (argument is string s)
{
arguments.Add(s);
}
else if (argument is ProvisioningParameter provisioningParameter)
{
arguments.Add(provisioningParameter);
}
else
{
throw new NotSupportedException($"{argument} is not supported");
}
}
argumentIndex++;
span = span[(match.Index + match.Length - skip)..];
skip = match.Index + match.Length;
}
formatStringBuilder.Append(span);
}
ProcessFormattableString(text, 0);
var formatString = formatStringBuilder.ToString();
if (formatString == "{0}")
{
return arguments[0];
}
return BicepFunction.Interpolate(new BicepValueFormattableString(formatString, [.. arguments]));
}
/// <summary>
/// A custom FormattableString implementation that allows us to inline nested formattable strings.
/// </summary>
private sealed class BicepValueFormattableString(string formatString, object[] values) : FormattableString
{
public override int ArgumentCount => values.Length;
public override string Format => formatString;
public override object? GetArgument(int index) => values[index];
public override object?[] GetArguments() => values;
public override string ToString(IFormatProvider? formatProvider) => Format;
public override string ToString() => formatString;
}
/// <summary>
/// These are referencing outputs from azd's main.bicep file. We represent the global namespace in the manifest
/// by using {.outputs.property} expressions.
/// </summary>
private sealed class AzureContainerAppsEnvironment(string outputName) : IManifestExpressionProvider
{
public string ValueExpression => $"{{.outputs.{outputName}}}";
public static IManifestExpressionProvider MANAGED_IDENTITY_CLIENT_ID => GetExpression("MANAGED_IDENTITY_CLIENT_ID");
public static IManifestExpressionProvider MANAGED_IDENTITY_NAME => GetExpression("MANAGED_IDENTITY_NAME");
public static IManifestExpressionProvider MANAGED_IDENTITY_PRINCIPAL_ID => GetExpression("MANAGED_IDENTITY_PRINCIPAL_ID");
public static IManifestExpressionProvider AZURE_CONTAINER_REGISTRY_MANAGED_IDENTITY_ID => GetExpression("AZURE_CONTAINER_REGISTRY_MANAGED_IDENTITY_ID");
public static IManifestExpressionProvider AZURE_CONTAINER_REGISTRY_ENDPOINT => GetExpression("AZURE_CONTAINER_REGISTRY_ENDPOINT");
public static IManifestExpressionProvider AZURE_CONTAINER_APPS_ENVIRONMENT_ID => GetExpression("AZURE_CONTAINER_APPS_ENVIRONMENT_ID");
public static IManifestExpressionProvider AZURE_CONTAINER_APPS_ENVIRONMENT_DEFAULT_DOMAIN => GetExpression("AZURE_CONTAINER_APPS_ENVIRONMENT_DEFAULT_DOMAIN");
private static IManifestExpressionProvider GetExpression(string propertyExpression) =>
new AzureContainerAppsEnvironment(propertyExpression);
}
private sealed class SecretOutputExpression(AzureBicepResource resource) : IManifestExpressionProvider
{
public string ValueExpression => $"{{{resource.Name}.secretOutputs}}";
public static IManifestExpressionProvider GetSecretOutputKeyVault(AzureBicepResource resource) =>
new SecretOutputExpression(resource);
}