-
Notifications
You must be signed in to change notification settings - Fork 159
/
App.fs
509 lines (486 loc) · 30.2 KB
/
App.fs
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
[<AutoOpen>]
module Farmer.Arm.App
open System
open Farmer.ContainerApp
open Farmer
let containerApps = ResourceType("Microsoft.App/containerApps", "2022-03-01")
let managedEnvironments =
ResourceType("Microsoft.App/managedEnvironments", "2022-03-01")
let storages =
ResourceType("Microsoft.App/managedEnvironments/storages", "2022-03-01")
open Farmer.ContainerAppValidation
open Farmer.Identity
type Container =
{
Name: string
DockerImage: Containers.DockerImage
VolumeMounts: Map<string, string>
Resources: {| CPU: float<VCores>
Memory: float<Gb>
EphemeralStorage: float<Gb> option |}
}
type ManagedEnvironmentStorage =
{
Name: ResourceName
Environment: ResourceId
AzureFile: {| ShareName: ResourceName
AccountName: Storage.StorageAccountName
AccountKey: string
AccessMode: StorageAccessMode |}
Dependencies: Set<ResourceId>
}
interface IArmResource with
member this.ResourceId = storages.resourceId this.Name
member this.JsonModel =
{| storages.Create(
ResourceName $"{this.Environment.Name.Value}/{this.Name.Value}",
dependsOn = this.Dependencies
) with
properties =
{|
azureFile =
{|
shareName = this.AzureFile.ShareName.Value
accountName = this.AzureFile.AccountName.ResourceName.Value
accountKey = this.AzureFile.AccountKey
accessMode = this.AzureFile.AccessMode.ArmValue
|}
|}
|}
static member from(env: ResourceId) =
function
| KeyValue (name, Volume.AzureFileShare (share, accountName, accessMode)) ->
Some
{
Name = ResourceName name
Environment = env
Dependencies = Set.ofList [ env; Storage.storageAccounts.resourceId accountName.ResourceName ]
AzureFile =
{|
ShareName = share
AccountName = accountName
AccountKey =
$"[listKeys('Microsoft.Storage/storageAccounts/{accountName.ResourceName.Value}', '2018-07-01').keys[0].value]"
AccessMode = accessMode
|}
}
| _ -> None
type ContainerApp =
{
Name: ResourceName
Environment: ResourceId
ActiveRevisionsMode: ActiveRevisionsMode
IngressMode: IngressMode option
ScaleRules: Map<string, ScaleRule>
Identity: ManagedIdentity
Replicas: {| Min: int; Max: int |} option
DaprConfig: {| AppId: string |} option
Secrets: Map<ContainerAppSettingKey, SecretValue>
EnvironmentVariables: Map<string, EnvVar>
ImageRegistryCredentials: ImageRegistryAuthentication list
Containers: Container list
Location: Location
Dependencies: Set<ResourceId>
Volumes: Map<string, Volume>
}
member private this.dependencies =
[
this.Environment
yield! this.Dependencies
yield!
this.Volumes
|> Seq.choose (function
| KeyValue (name, Volume.AzureFileShare (_)) ->
storages.resourceId (this.Environment.Name, ResourceName name) |> Some
| _ -> None)
yield! this.Identity.Dependencies
]
member private this.ResourceId = containerApps.resourceId this.Name
member this.SystemIdentity = SystemIdentity this.ResourceId
interface IParameters with
member this.SecureParameters =
[
for secret in this.Secrets do
match secret.Value with
| ParameterSecret sp -> sp
| ExpressionSecret _ -> ()
for credential in this.ImageRegistryCredentials do
match credential with
| ImageRegistryAuthentication.Credential credential -> credential.Password
| ImageRegistryAuthentication.ListCredentials _ -> ()
| ImageRegistryAuthentication.ManagedIdentityCredential _ -> ()
]
interface IArmResource with
member this.ResourceId = containerApps.resourceId this.Name
member this.JsonModel =
{| containerApps.Create(this.Name, this.Location, this.dependencies) with
kind = "containerapp"
identity =
if this.Identity = ManagedIdentity.Empty then
Unchecked.defaultof<_>
else
this.Identity.ToArmJson
properties =
{|
managedEnvironmentId = this.Environment.Eval()
configuration =
let buildPasswordRef (resourceId: ResourceId) =
$"password-for-%s{resourceId.Name.Value}-registry"
{|
secrets =
[|
for cred in this.ImageRegistryCredentials do
match cred with
| ImageRegistryAuthentication.Credential cred ->
{|
name = cred.Username
value = cred.Password.ArmExpression.Eval()
|}
| ImageRegistryAuthentication.ListCredentials resourceId ->
{|
name = buildPasswordRef resourceId
value =
ArmExpression
.create(
$"listCredentials({resourceId.ArmExpression.Value}, '2019-05-01').passwords[0].value"
)
.Eval()
|}
| ImageRegistryAuthentication.ManagedIdentityCredential cred ->
{|
name = cred.Server
value =
match cred.Identity.Dependencies with
| [] -> String.Empty
| primaryDependency :: _ ->
primaryDependency.ArmExpression.Eval()
|}
for setting in this.Secrets do
{|
name = setting.Key.Value
value = setting.Value.Value
|}
|]
activeRevisionsMode =
match this.ActiveRevisionsMode with
| Single -> "Single"
| Multiple -> "Multiple"
registries =
[|
for cred in this.ImageRegistryCredentials do
match cred with
| ImageRegistryAuthentication.Credential cred ->
{|
server = cred.Server
username = cred.Username
passwordSecretRef = cred.Username
identity = null
|}
| ImageRegistryAuthentication.ListCredentials resourceId ->
{|
server =
ArmExpression
.create(
$"reference({resourceId.ArmExpression.Value}, '2019-05-01').loginServer"
)
.Eval()
username =
ArmExpression
.create(
$"listCredentials({resourceId.ArmExpression.Value}, '2019-05-01').username"
)
.Eval()
passwordSecretRef = buildPasswordRef resourceId
identity = null
|}
| ImageRegistryAuthentication.ManagedIdentityCredential cred ->
{|
server = cred.Server
username = String.Empty
passwordSecretRef = null
identity =
if cred.Identity.Dependencies.Length > 0 then
cred.Identity.Dependencies.Head.ArmExpression.Eval()
else
String.Empty
|}
|]
ingress =
match this.IngressMode with
| Some InternalOnly -> box {| external = false |}
| Some (External (targetPort, transport)) ->
box
{|
external = true
targetPort = targetPort
transport =
match transport with
| Some HTTP1 -> "http"
| Some HTTP2 -> "http2"
| Some Auto -> "auto"
| None -> null
|}
| None -> null
|}
template =
{|
containers =
[|
for container in this.Containers do
{|
image = container.DockerImage.ImageTag
name = container.Name
env =
[|
for env in this.EnvironmentVariables do
match env.Value with
| EnvValue value ->
{|
name = env.Key
value = value
secretref = null
|}
| SecureEnvExpression armExpr ->
{|
name = env.Key
value = armExpr.Eval()
secretref = null
|}
| SecureEnvValue _ ->
{|
name = env.Key
value = null
secretref = env.Key
|}
|]
resources =
{|
cpu = container.Resources.CPU
ephemeralStorage =
container.Resources.EphemeralStorage
|> Option.map (sprintf "%.2fGi")
|> Option.toObj
memory = container.Resources.Memory |> sprintf "%.2fGi"
|}
:> obj
volumeMounts =
container.VolumeMounts
|> Seq.map (fun kvp ->
{|
volumeName = kvp.Key
mountPath = kvp.Value
|})
|> List.ofSeq
|> function
| [] -> Unchecked.defaultof<_>
| vms -> vms
|}
|]
scale =
{|
minReplicas = this.Replicas |> Option.map (fun c -> c.Min) |> Option.toNullable
maxReplicas = this.Replicas |> Option.map (fun c -> c.Max) |> Option.toNullable
rules =
[|
for rule in this.ScaleRules do
match rule.Value with
| ScaleRule.Custom customRule ->
{|
name = rule.Key
custom = customRule
|}
:> obj
| ScaleRule.EventHub settings ->
{|
name = rule.Key
custom =
{|
``type`` = "azure-eventhub"
metadata =
{|
consumerGroup = settings.ConsumerGroup
unprocessedEventThreshold =
string
settings.UnprocessedEventThreshold
blobContainer =
settings.CheckpointBlobContainerName
checkpointStrategy = "blobMetadata"
|}
auth =
[|
{|
secretRef =
settings.EventHubConnectionSecretRef
triggerParameter = "connection"
|}
{|
secretRef =
settings.StorageConnectionSecretRef
triggerParameter = "storageConnection"
|}
|]
|}
|}
:> obj
| ScaleRule.ServiceBus settings ->
{|
name = rule.Key
custom =
{|
``type`` = "azure-servicebus"
metadata =
{|
queueName = settings.QueueName
messageCount = string settings.MessageCount
|}
auth =
[|
{|
secretRef = settings.SecretRef
triggerParameter = "connection"
|}
|]
|}
|}
:> obj
| ScaleRule.Http settings ->
{|
name = rule.Key
http =
{|
metadata =
{|
concurrentRequests =
string settings.ConcurrentRequests
|}
|}
|}
:> obj
| ScaleRule.CPU settings ->
{|
name = rule.Key
custom =
{|
``type`` = "cpu"
metadata =
{|
``type`` =
match settings with
| Utilization _ -> "Utilization"
| AverageValue _ -> "AverageValue"
value =
match settings with
| Utilization v ->
v.Utilization |> string
| AverageValue v ->
v.AverageValue |> string
|}
|}
|}
:> obj
| ScaleRule.Memory settings ->
{|
name = rule.Key
custom =
{|
``type`` = "memory"
metadata =
{|
``type`` =
match settings with
| Utilization _ -> "Utilization"
| AverageValue _ -> "AverageValue"
value =
match settings with
| Utilization v ->
v.Utilization |> string
| AverageValue v ->
v.AverageValue |> string
|}
|}
|}
:> obj
| ScaleRule.StorageQueue settings ->
{|
name = rule.Key
custom =
{|
``type`` = "azure-queue"
metadata =
{|
queueName = settings.QueueName
queueLength = string settings.QueueLength
connectionFromEnv =
settings.StorageConnectionSecretRef
accountName = settings.AccountName
|}
|}
|}
|]
|}
dapr =
match this.DaprConfig with
| Some settings ->
{|
enabled = true
appId = settings.AppId
|}
:> obj
| None -> {| enabled = false |}
volumes =
[
for key, value in Map.toSeq this.Volumes do
match key, value with
| volumeName, Volume.AzureFileShare (shareName, accountName, _) ->
{|
name = volumeName
storageType = "AzureFile"
storageName = volumeName
|}
| volumeName, Volume.EmptyDirectory ->
{|
name = volumeName
storageType = "EmptyDir"
storageName = null
|}
]
|> function
| [] -> Unchecked.defaultof<_>
| vs -> vs
|}
|}
|}
type ManagedEnvironment =
{
Name: ResourceName
Location: Location
InternalLoadBalancerState: FeatureFlag
LogAnalytics: ResourceId
AppInsightsInstrumentationKey: ArmExpression option
Dependencies: Set<ResourceId>
Tags: Map<string, string>
}
interface IArmResource with
member this.ResourceId = managedEnvironments.resourceId this.Name
member this.JsonModel =
{| managedEnvironments.Create(this.Name, this.Location, this.Dependencies, this.Tags) with
kind = "containerenvironment"
properties =
{|
``type`` = "managed"
internalLoadBalancerEnabled = this.InternalLoadBalancerState.AsBoolean
daprAIInstrumentationKey =
this.AppInsightsInstrumentationKey
|> Option.map (fun key -> key.Eval())
|> Option.toObj
appLogsConfiguration =
{|
destination = "log-analytics"
logAnalyticsConfiguration =
{|
customerId = LogAnalytics.getCustomerId(this.LogAnalytics).Eval()
sharedKey = LogAnalytics.getPrimarySharedKey(this.LogAnalytics).Eval()
|}
|}
|}
|}