-
Notifications
You must be signed in to change notification settings - Fork 496
/
ApplicationExecutor.cs
754 lines (632 loc) · 29.6 KB
/
ApplicationExecutor.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Net.Sockets;
using Aspire.Hosting.ApplicationModel;
using Aspire.Hosting.Dcp.Model;
using Aspire.Hosting.Lifecycle;
using k8s;
namespace Aspire.Hosting.Dcp;
internal class AppResource
{
public IResource ModelResource { get; private set; }
public CustomResource DcpResource { get; private set; }
public virtual List<ServiceAppResource> ServicesProduced { get; private set; } = new();
public virtual List<ServiceAppResource> ServicesConsumed { get; private set; } = new();
public AppResource(IResource modelResource, CustomResource dcpResource)
{
this.ModelResource = modelResource;
this.DcpResource = dcpResource;
}
}
internal sealed class ServiceAppResource : AppResource
{
public Service Service => (Service)DcpResource;
public ServiceBindingAnnotation ServiceBindingAnnotation { get; private set; }
public ServiceProducerAnnotation DcpServiceProducerAnnotation { get; private set; }
public override List<ServiceAppResource> ServicesProduced
{
get { throw new InvalidOperationException("Service resources do not produce any services"); }
}
public override List<ServiceAppResource> ServicesConsumed
{
get { throw new InvalidOperationException("Service resources do not consume any services"); }
}
public ServiceAppResource(IResource modelResource, Service service, ServiceBindingAnnotation sba) : base(modelResource, service)
{
ServiceBindingAnnotation = sba;
DcpServiceProducerAnnotation = new(service.Metadata.Name);
}
}
internal sealed class ApplicationExecutor(DistributedApplicationModel model,
KubernetesService kubernetesService,
IEnumerable<IDistributedApplicationLifecycleHook> lifecycleHooks)
{
private const string DebugSessionPortVar = "DEBUG_SESSION_PORT";
private readonly IDistributedApplicationLifecycleHook[] _lifecycleHooks = lifecycleHooks.ToArray();
// These environment variables should never be inherited from app host;
// they only make sense if they come from a launch profile of a service project.
private static readonly string[] s_doNotInheritEnvironmentVars =
{
"ASPNETCORE_URLS",
"DOTNET_LAUNCH_PROFILE",
"ASPNETCORE_ENVIRONMENT",
"DOTNET_ENVIRONMENT"
};
private readonly DistributedApplicationModel _model = model;
private readonly List<AppResource> _appResources = new();
public async Task RunApplicationAsync(CancellationToken cancellationToken = default)
{
AspireEventSource.Instance.DcpModelCreationStart();
try
{
PrepareServices();
PrepareContainers();
PrepareExecutables();
await CreateServicesAsync(cancellationToken).ConfigureAwait(false);
await CreateContainersAndExecutablesAsync(cancellationToken).ConfigureAwait(false);
}
finally
{
AspireEventSource.Instance.DcpModelCreationStop();
}
}
public async Task StopApplicationAsync(CancellationToken cancellationToken = default)
{
try
{
AspireEventSource.Instance.DcpModelCleanupStart();
await DeleteResourcesAsync<ExecutableReplicaSet>("project", cancellationToken).ConfigureAwait(false);
await DeleteResourcesAsync<Executable>("project", cancellationToken).ConfigureAwait(false);
await DeleteResourcesAsync<Container>("container", cancellationToken).ConfigureAwait(false);
await DeleteResourcesAsync<Service>("service", cancellationToken).ConfigureAwait(false);
}
finally
{
AspireEventSource.Instance.DcpModelCleanupStop();
_appResources.Clear();
}
}
private async Task CreateServicesAsync(CancellationToken cancellationToken = default)
{
try
{
AspireEventSource.Instance.DcpServicesCreationStart();
var needAddressAllocated = _appResources.OfType<ServiceAppResource>().Where(sr => !sr.Service.HasCompleteAddress).ToList();
await CreateResourcesAsync<Service>(cancellationToken).ConfigureAwait(false);
if (needAddressAllocated.Count == 0)
{
// No need to wait for any updates to Service objects from the orchestrator.
return;
}
// We do not specify the initial list version, so the watcher will give us all updates to Service objects.
IAsyncEnumerable<(WatchEventType, Service)> serviceChangeEnumerator = kubernetesService.WatchAsync<Service>(cancellationToken: cancellationToken);
await foreach (var (evt, updated) in serviceChangeEnumerator)
{
if (evt == WatchEventType.Bookmark) { continue; } // Bookmarks do not contain any data.
var srvResource = needAddressAllocated.Where(sr => sr.Service.Metadata.Name == updated.Metadata.Name).FirstOrDefault();
if (srvResource == null) { continue; } // This service most likely already has full address information, so it is not on needAddressAllocated list.
if (updated.HasCompleteAddress)
{
srvResource.Service.ApplyAddressInfoFrom(updated);
needAddressAllocated.Remove(srvResource);
}
if (needAddressAllocated.Count == 0)
{
return; // We are done
}
}
}
finally
{
AspireEventSource.Instance.DcpServicesCreationStop();
}
}
private async Task CreateContainersAndExecutablesAsync(CancellationToken cancellationToken)
{
var toCreate = _appResources.Where(r => r.DcpResource is Container || r.DcpResource is Executable || r.DcpResource is ExecutableReplicaSet);
AddAllocatedEndpointInfo(toCreate);
foreach (var lifecycleHook in _lifecycleHooks)
{
await lifecycleHook.AfterEndpointsAllocatedAsync(_model, cancellationToken).ConfigureAwait(false);
}
await CreateContainersAsync(toCreate.Where(ar => ar.DcpResource is Container), cancellationToken).ConfigureAwait(false);
await CreateExecutablesAsync(toCreate.Where(ar => ar.DcpResource is Executable || ar.DcpResource is ExecutableReplicaSet), cancellationToken).ConfigureAwait(false);
}
private static void AddAllocatedEndpointInfo(IEnumerable<AppResource> resources)
{
foreach (var appResource in resources)
{
foreach (var sp in appResource.ServicesProduced)
{
var svc = (Service)sp.DcpResource;
if (!svc.HasCompleteAddress)
{
// This should never happen; if it does, we have a bug without a workaround for th the user.
throw new InvalidDataException($"Service {svc.Metadata.Name} should have valid address at this point");
}
var a = new AllocatedEndpointAnnotation(
sp.ServiceBindingAnnotation.Name,
PortProtocol.ToProtocolType(svc.Spec.Protocol),
svc.AllocatedAddress!,
(int)svc.AllocatedPort!,
sp.ServiceBindingAnnotation.UriScheme
);
appResource.ModelResource.Annotations.Add(a);
}
}
}
private void PrepareServices()
{
var serviceProducers = _model.Resources
.Select(r => (ModelResource: r, SBAnnotations: r.Annotations.OfType<ServiceBindingAnnotation>()))
.Where(sp => sp.SBAnnotations.Any());
// We need to ensure that Services have unique names (otherwise we cannot really distinguish between
// services produced by different resources).
List<string> serviceNames = new();
void addServiceAppResource(Service svc, IResource producingResource, ServiceBindingAnnotation sba)
{
svc.Spec.Protocol = PortProtocol.FromProtocolType(sba.Protocol);
svc.Spec.AddressAllocationMode = AddressAllocationModes.Localhost;
svc.Annotate(CustomResource.UriSchemeAnnotation, sba.UriScheme);
_appResources.Add(new ServiceAppResource(producingResource, svc, sba));
}
foreach (var sp in serviceProducers)
{
var sbAnnotations = sp.SBAnnotations.ToArray();
foreach (var sba in sbAnnotations)
{
var candidateServiceName = sbAnnotations.Length == 1 ?
GetObjectNameForResource(sp.ModelResource) : GetObjectNameForResource(sp.ModelResource, sba.Name);
var uniqueServiceName = GenerateUniqueServiceName(serviceNames, candidateServiceName);
var svc = Service.Create(uniqueServiceName);
svc.Spec.Port = sba.Port;
addServiceAppResource(svc, sp.ModelResource, sba);
}
}
}
private void PrepareExecutables()
{
PrepareProjectExecutables();
PreparePlainExecutables();
}
private void PreparePlainExecutables()
{
var modelExecutableResources = _model.GetExecutableResources();
foreach (var executable in modelExecutableResources)
{
var exeName = GetObjectNameForResource(executable);
var exePath = executable.Command;
var exe = Executable.Create(exeName, exePath);
// The working directory is always relative to the app host project directory (if it exists).
exe.Spec.WorkingDirectory = executable.WorkingDirectory;
exe.Spec.Args = executable.Args?.ToList();
exe.Spec.ExecutionType = ExecutionType.Process;
exe.Annotate(Executable.OtelServiceNameAnnotation, exe.Metadata.Name);
var exeAppResource = new AppResource(executable, exe);
AddServicesProducedInfo(executable, exe, exeAppResource);
_appResources.Add(exeAppResource);
}
}
private void PrepareProjectExecutables()
{
var modelProjectResources = _model.GetProjectResources();
foreach (var project in modelProjectResources)
{
if (!project.TryGetLastAnnotation<IServiceMetadata>(out var projectMetadata))
{
throw new InvalidOperationException("A project resource is missing required metadata"); // Should never happen.
}
int replicas = project.GetReplicaCount();
var ers = ExecutableReplicaSet.Create(GetObjectNameForResource(project), replicas, "dotnet");
var exeSpec = ers.Spec.Template.Spec;
IAnnotationHolder annotationHolder = ers.Spec.Template;
exeSpec.WorkingDirectory = Path.GetDirectoryName(projectMetadata.ProjectPath);
annotationHolder.Annotate(Executable.CSharpProjectPathAnnotation, projectMetadata.ProjectPath);
annotationHolder.Annotate(Executable.OtelServiceNameAnnotation, ers.Metadata.Name);
if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable(DebugSessionPortVar)))
{
exeSpec.ExecutionType = ExecutionType.IDE;
if (project.TryGetLastAnnotation<LaunchProfileAnnotation>(out var lpa))
{
annotationHolder.Annotate(Executable.CSharpLaunchProfileAnnotation, lpa.LaunchProfileName);
}
}
else
{
exeSpec.ExecutionType = ExecutionType.Process;
if (Environment.GetEnvironmentVariable("DOTNET_WATCH") != "1")
{
exeSpec.Args = [
"run",
"--no-build",
"--project",
projectMetadata.ProjectPath,
];
}
else
{
exeSpec.Args = [
"watch",
"--non-interactive",
"--no-hot-reload",
"--project",
projectMetadata.ProjectPath
];
}
// We pretty much always want to suppress the normal launch profile handling
// because the settings from the profile will override the ambient environment settings, which is not what we want
// (the ambient environment settings for service processes come from the application model
// and should be HIGHER priority than the launch profile settings).
// This means we need to apply the launch profile settings manually--the invocation parameters here,
// and the environment variables/application URLs inside CreateExecutableAsync().
exeSpec.Args.Add("--no-launch-profile");
string? launchProfileName = project.SelectLaunchProfileName();
if (!string.IsNullOrEmpty(launchProfileName))
{
var launchProfile = project.GetEffectiveLaunchProfile();
if (launchProfile is not null && !string.IsNullOrWhiteSpace(launchProfile.CommandLineArgs))
{
var cmdArgs = launchProfile.CommandLineArgs.Split((string?)null, StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries);
if (cmdArgs is not null && cmdArgs.Length > 0)
{
exeSpec.Args.Add("--");
exeSpec.Args.AddRange(cmdArgs);
}
}
}
}
var exeAppResource = new AppResource(project, ers);
AddServicesProducedInfo(project, annotationHolder, exeAppResource);
_appResources.Add(exeAppResource);
}
}
private async Task CreateExecutablesAsync(IEnumerable<AppResource> executableResources, CancellationToken cancellationToken)
{
try
{
AspireEventSource.Instance.DcpExecutablesCreateStart();
foreach (var er in executableResources)
{
ExecutableSpec spec;
Func<Task<CustomResource>> createResource;
switch (er.DcpResource)
{
case Executable exe:
spec = exe.Spec;
createResource = async () => await kubernetesService.CreateAsync(exe, cancellationToken).ConfigureAwait(false);
break;
case ExecutableReplicaSet ers:
spec = ers.Spec.Template.Spec;
createResource = async () => await kubernetesService.CreateAsync(ers, cancellationToken).ConfigureAwait(false);
break;
default:
throw new InvalidOperationException($"Expected an Executable-like resource, but got {er.DcpResource.Kind} instead");
}
spec.Args ??= new();
if (er.ModelResource.TryGetAnnotationsOfType<ExecutableArgsCallbackAnnotation>(out var exeArgsCallbacks))
{
foreach (var exeArgsCallback in exeArgsCallbacks)
{
exeArgsCallback.Callback(spec.Args);
}
}
var config = new Dictionary<string, string>();
var context = new EnvironmentCallbackContext("dcp", config);
// Need to apply configuration settings manually; see PrepareExecutables() for details.
if (er.ModelResource is ProjectResource project && project.SelectLaunchProfileName() is { } launchProfileName && project.GetLaunchSettings() is { } launchSettings)
{
ApplyLaunchProfile(er, config, launchProfileName, launchSettings);
}
else
{
// If there is no launch profile, we want to make sure that certain environment variables are NOT inherited
foreach (var envVar in s_doNotInheritEnvironmentVars)
{
config.Add(envVar, "");
}
if (er.ServicesProduced.Count > 0)
{
if (er.ModelResource is ProjectResource)
{
var urls = er.ServicesProduced.Where(s => s.ServiceBindingAnnotation.UriScheme is "http" or "https").Select(sar =>
{
var url = sar.ServiceBindingAnnotation.UriScheme + "://localhost:{{- portForServing \"" + sar.Service.Metadata.Name + "\" -}}";
return url;
});
// REVIEW: Should we assume ASP.NET Core?
// We're going to use http and https urls as ASPNETCORE_URLS
config["ASPNETCORE_URLS"] = string.Join(";", urls);
}
InjectPortEnvVars(er, config);
}
}
if (er.ModelResource.TryGetEnvironmentVariables(out var envVarAnnotations))
{
foreach (var ann in envVarAnnotations)
{
ann.Callback(context);
}
}
spec.Env = new();
foreach (var c in config)
{
spec.Env.Add(new EnvVar { Name = c.Key, Value = c.Value });
}
await createResource().ConfigureAwait(false);
}
}
finally
{
AspireEventSource.Instance.DcpExecutablesCreateStop();
}
}
private static void ApplyLaunchProfile(AppResource executableResource, Dictionary<string, string> config, string launchProfileName, LaunchSettings launchSettings)
{
// Populate DOTNET_LAUNCH_PROFILE environment variable for consistency with "dotnet run" and "dotnet watch".
config.Add("DOTNET_LAUNCH_PROFILE", launchProfileName);
var launchProfile = launchSettings.Profiles[launchProfileName];
if (!string.IsNullOrWhiteSpace(launchProfile.ApplicationUrl))
{
if (executableResource.DcpResource is ExecutableReplicaSet)
{
var urls = executableResource.ServicesProduced.Select(sar =>
{
var url = sar.ServiceBindingAnnotation.UriScheme + "://localhost:{{- portForServing \"" + sar.Service.Metadata.Name + "\" -}}";
return url;
});
config.Add("ASPNETCORE_URLS", string.Join(";", urls));
}
else
{
config.Add("ASPNETCORE_URLS", launchProfile.ApplicationUrl);
}
InjectPortEnvVars(executableResource, config);
}
foreach (var envVar in launchProfile.EnvironmentVariables)
{
string value = Environment.ExpandEnvironmentVariables(envVar.Value);
config[envVar.Key] = value;
}
}
private static void InjectPortEnvVars(AppResource executableResource, Dictionary<string, string> config)
{
// Inject environment variables for services produced by this executable.
foreach (var serviceProduced in executableResource.ServicesProduced)
{
var name = serviceProduced.Service.Metadata.Name;
var envVar = serviceProduced.ServiceBindingAnnotation.EnvironmentVariable;
if (envVar is not null)
{
config.Add(envVar, $"{{{{- portForServing \"{name}\" }}}}");
}
}
}
private void PrepareContainers()
{
var modelContainerResources = _model.GetContainerResources();
foreach (var container in modelContainerResources)
{
if (!container.TryGetContainerImageName(out var containerImageName))
{
// This should never happen! In order to get into this loop we need
// to have the annotation, if we don't have the annotation by the time
// we get here someone is doing something wrong.
throw new InvalidOperationException();
}
var ctr = Container.Create(container.Name, containerImageName);
if (container.TryGetVolumeMounts(out var volumeMounts))
{
ctr.Spec.VolumeMounts = new();
foreach (var mount in volumeMounts)
{
bool isBound = mount.Type == ApplicationModel.VolumeMountType.Bind;
var volumeSpec = new VolumeMount()
{
Source = isBound ? Path.GetFullPath(mount.Source) : mount.Source,
Target = mount.Target,
Type = isBound ? Model.VolumeMountType.Bind : Model.VolumeMountType.Named,
IsReadOnly = mount.IsReadOnly
};
ctr.Spec.VolumeMounts.Add(volumeSpec);
}
}
// Ensure no duplicate target volumes are mounted.
ValidateNamedVolumes(ctr);
var containerAppResource = new AppResource(container, ctr);
AddServicesProducedInfo(container, ctr, containerAppResource);
_appResources.Add(containerAppResource);
}
}
private static void ValidateNamedVolumes(Container? container)
{
if(container is null)
{
return;
}
var volumeList = container.Spec.VolumeMounts;
if(volumeList is null)
{
return;
}
if(volumeList.Count == 0)
{
return;
}
var volumeNameToMountCount = new Dictionary<string, int>();
var namedVolumes = volumeList.Where(v => v.Type == Model.VolumeMountType.Named).ToArray();
foreach(VolumeMount namedVolume in namedVolumes)
{
if(namedVolume.Source is null)
{
continue;
}
if(volumeNameToMountCount.TryGetValue(namedVolume.Source, out var value))
{
volumeNameToMountCount[namedVolume.Source] = ++value;
}
else
{
volumeNameToMountCount.Add(namedVolume.Source, 1);
}
}
foreach(var volumeName in volumeNameToMountCount.Keys)
{
if (volumeNameToMountCount[volumeName] > 1)
{
throw new InvalidOperationException($"Volume {volumeName} is mounted more than once in container {container.Metadata.Name}");
}
}
}
private async Task CreateContainersAsync(IEnumerable<AppResource> containerResources, CancellationToken cancellationToken)
{
try
{
AspireEventSource.Instance.DcpContainersCreateStart();
foreach (var cr in containerResources)
{
var dcpContainerResource = (Container)cr.DcpResource;
var modelContainerResource = cr.ModelResource;
var config = new Dictionary<string, string>();
dcpContainerResource.Spec.Env = new();
if (cr.ServicesProduced.Count > 0)
{
dcpContainerResource.Spec.Ports = new();
foreach (var sp in cr.ServicesProduced)
{
var portSpec = new ContainerPortSpec()
{
ContainerPort = sp.DcpServiceProducerAnnotation.Port,
};
if (!string.IsNullOrEmpty(sp.DcpServiceProducerAnnotation.Address))
{
portSpec.HostIP = sp.DcpServiceProducerAnnotation.Address;
}
if (sp.ServiceBindingAnnotation.Port is not null)
{
portSpec.HostPort = sp.ServiceBindingAnnotation.Port;
}
switch (sp.ServiceBindingAnnotation.Protocol)
{
case ProtocolType.Tcp:
portSpec.Protocol = PortProtocol.TCP; break;
case ProtocolType.Udp:
portSpec.Protocol = PortProtocol.UDP; break;
}
dcpContainerResource.Spec.Ports.Add(portSpec);
var name = sp.Service.Metadata.Name;
var envVar = sp.ServiceBindingAnnotation.EnvironmentVariable;
if (envVar is not null)
{
config.Add(envVar, $"{{{{- portForServing \"{name}\" }}}}");
}
}
}
if (modelContainerResource.TryGetEnvironmentVariables(out var containerEnvironmentVariables))
{
var context = new EnvironmentCallbackContext("dcp", config);
foreach (var v in containerEnvironmentVariables)
{
v.Callback(context);
}
}
foreach (var kvp in config)
{
dcpContainerResource.Spec.Env.Add(new EnvVar { Name = kvp.Key, Value = kvp.Value });
}
if (modelContainerResource.TryGetAnnotationsOfType<ExecutableArgsCallbackAnnotation>(out var argsCallback))
{
dcpContainerResource.Spec.Args ??= [];
foreach (var callback in argsCallback)
{
callback.Callback(dcpContainerResource.Spec.Args);
}
}
await kubernetesService.CreateAsync(dcpContainerResource, cancellationToken).ConfigureAwait(false);
}
}
finally
{
AspireEventSource.Instance.DcpContainersCreateStop();
}
}
private void AddServicesProducedInfo(IResource modelResource, IAnnotationHolder dcpResource, AppResource appResource)
{
string modelResourceName = "(unknown)";
try
{
modelResourceName = GetObjectNameForResource(modelResource);
}
catch { } // For error messages only, OK to fall back to (unknown)
var servicesProduced = _appResources.OfType<ServiceAppResource>().Where(r => r.ModelResource == modelResource);
foreach (var sp in servicesProduced)
{
// Projects/Executables have their ports auto-allocated; the the port specified by the ServiceBindingAnnotation
// is applied to the Service objects and used by clients.
// Containers use the port from the ServiceBindingAnnotation directly.
if (modelResource.IsContainer())
{
if (sp.ServiceBindingAnnotation.ContainerPort is null)
{
throw new InvalidOperationException($"The ServiceBindingAnnotation for container resource {modelResourceName} must specify the ContainerPort");
}
sp.DcpServiceProducerAnnotation.Port = sp.ServiceBindingAnnotation.ContainerPort;
}
dcpResource.AnnotateAsObjectList(CustomResource.ServiceProducerAnnotation, sp.DcpServiceProducerAnnotation);
appResource.ServicesProduced.Add(sp);
}
}
private async Task CreateResourcesAsync<RT>(CancellationToken cancellationToken) where RT : CustomResource
{
var resourcesToCreate = _appResources.Select(r => r.DcpResource).OfType<RT>();
if (!resourcesToCreate.Any())
{
return;
}
// CONSIDER batched creation
foreach (var res in resourcesToCreate)
{
await kubernetesService.CreateAsync(res, cancellationToken).ConfigureAwait(false);
}
}
private async Task DeleteResourcesAsync<RT>(string resourceName, CancellationToken cancellationToken) where RT : CustomResource
{
var resourcesToDelete = _appResources.Select(r => r.DcpResource).OfType<RT>();
if (!resourcesToDelete.Any())
{
return;
}
foreach (var res in resourcesToDelete)
{
try
{
await kubernetesService.DeleteAsync<RT>(res.Metadata.Name, res.Metadata.NamespaceProperty, cancellationToken).ConfigureAwait(false);
}
catch (Exception ex)
{
Console.WriteLine($"Could not stop {resourceName} '{res.Metadata.Name}': {ex}");
}
}
}
private static string GetObjectNameForResource(IResource resource, string suffix = "")
{
string maybeWithSuffix(string s) => string.IsNullOrWhiteSpace(suffix) ? s : $"{s}_{suffix}";
return maybeWithSuffix(resource.Name);
}
private static string GenerateUniqueServiceName(List<string> serviceNames, string candidateName)
{
int suffix = 1;
string uniqueName = candidateName;
while (serviceNames.Contains(uniqueName))
{
uniqueName = $"{candidateName}_{suffix}";
suffix++;
if (suffix == 100)
{
// Should never happen, but we do not want to ever get into a infinite loop situation either.
throw new ArgumentException($"Could not generate a unique name for service '{candidateName}'");
}
}
serviceNames.Add(uniqueName);
return uniqueName;
}
}