Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename WithServiceBinding to WithEndpoint #1484

Merged
merged 1 commit into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Aspire.Hosting.Azure/AzureResourceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,9 @@ private static void WriteQueueStorageToManifest(ManifestPublishingContext contex
/// <returns>A reference to the <see cref="IResourceBuilder{AzureQueueStorageResource}"/>.</returns>
public static IResourceBuilder<AzureStorageResource> UseEmulator(this IResourceBuilder<AzureStorageResource> builder, int? blobPort = null, int? queuePort = null, int? tablePort = null)
{
return builder.WithAnnotation(new ServiceBindingAnnotation(ProtocolType.Tcp, name: "blob", port: blobPort, containerPort: 10000))
.WithAnnotation(new ServiceBindingAnnotation(ProtocolType.Tcp, name: "queue", port: queuePort, containerPort: 10001))
.WithAnnotation(new ServiceBindingAnnotation(ProtocolType.Tcp, name: "table", port: tablePort, containerPort: 10002))
return builder.WithAnnotation(new EndpointAnnotation(ProtocolType.Tcp, name: "blob", port: blobPort, containerPort: 10000))
.WithAnnotation(new EndpointAnnotation(ProtocolType.Tcp, name: "queue", port: queuePort, containerPort: 10001))
.WithAnnotation(new EndpointAnnotation(ProtocolType.Tcp, name: "table", port: tablePort, containerPort: 10002))
.WithAnnotation(new ContainerImageAnnotation { Image = "mcr.microsoft.com/azure-storage/azurite", Tag = "latest" });
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,12 @@ public async Task BeforeStartAsync(DistributedApplicationModel appModel, Cancell
context.EnvironmentVariables.TryAdd("DAPR_HTTP_PORT", $"{{{{- portFor \"{daprSideCarResourceName}_http\" -}}}}");
}));

daprSideCar.Annotations.Add(new ServiceBindingAnnotation(ProtocolType.Tcp, name: "grpc", port: sidecarOptions?.DaprGrpcPort));
daprSideCar.Annotations.Add(new ServiceBindingAnnotation(ProtocolType.Tcp, name: "http", port: sidecarOptions?.DaprHttpPort));
daprSideCar.Annotations.Add(new ServiceBindingAnnotation(ProtocolType.Tcp, name: "metrics", port: sidecarOptions?.MetricsPort));
daprSideCar.Annotations.Add(new EndpointAnnotation(ProtocolType.Tcp, name: "grpc", port: sidecarOptions?.DaprGrpcPort));
daprSideCar.Annotations.Add(new EndpointAnnotation(ProtocolType.Tcp, name: "http", port: sidecarOptions?.DaprHttpPort));
daprSideCar.Annotations.Add(new EndpointAnnotation(ProtocolType.Tcp, name: "metrics", port: sidecarOptions?.MetricsPort));
if (sidecarOptions?.EnableProfiling == true)
{
daprSideCar.Annotations.Add(new ServiceBindingAnnotation(ProtocolType.Tcp, name: "profile", port: sidecarOptions?.ProfilePort));
daprSideCar.Annotations.Add(new EndpointAnnotation(ProtocolType.Tcp, name: "profile", port: sidecarOptions?.ProfilePort));
}

// NOTE: Telemetry is enabled by default.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
namespace Aspire.Hosting.ApplicationModel;

/// <summary>
/// Represents a service binding annotation that describes how a service should be bound to a network.
/// Represents a endpoint annotation that describes how a service should be bound to a network.
/// </summary>
/// <remarks>
/// This class is used to specify the network protocol, port, URI scheme, transport, and other details for a service.
/// </remarks>
[DebuggerDisplay("Type = {GetType().Name,nq}, Name = {Name}")]
public sealed class ServiceBindingAnnotation : IResourceAnnotation
public sealed class EndpointAnnotation : IResourceAnnotation
{
public ServiceBindingAnnotation(ProtocolType protocol, string? uriScheme = null, string? transport = null, string? name = null, int? port = null, int? containerPort = null, bool? isExternal = null, string? env = null)
public EndpointAnnotation(ProtocolType protocol, string? uriScheme = null, string? transport = null, string? name = null, int? port = null, int? containerPort = null, bool? isExternal = null, string? env = null)
{
// If the URI scheme is null, we'll adopt either udp:// or tcp:// based on the
// protocol. If the name is null, we'll use the URI scheme as the default. This
Expand Down Expand Up @@ -66,12 +66,12 @@ public ServiceBindingAnnotation(ProtocolType protocol, string? uriScheme = null,
public string Transport { get; internal set; }

/// <summary>
/// Indicates that this service binding should be exposed externally at publish time.
/// Indicates that this endpoint should be exposed externally at publish time.
/// </summary>
public bool IsExternal { get; internal set; }

/// <summary>
/// The name of the environment variable that will be set to the port number of this service binding.
/// The name of the environment variable that will be set to the port number of this endpoint.
/// </summary>
public string? EnvironmentVariable { get; internal set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Aspire.Hosting.ApplicationModel;

/// <summary>
/// Provides extension methods for <see cref="EndpointAnnotation"/>.
/// </summary>
public static class EndpointAnnotationExtensions
{
/// <summary>
/// Sets the transport to HTTP/2 and the URI scheme to HTTPS for the specified <see cref="EndpointAnnotation"/> object.
/// </summary>
/// <param name="binding">The <see cref="EndpointAnnotation"/> object to modify.</param>
/// <returns>The modified <see cref="EndpointAnnotation"/> object.</returns>
public static EndpointAnnotation AsHttp2(this EndpointAnnotation binding)
{
binding.Transport = "http2";
binding.UriScheme = "https";
return binding;
}

/// <summary>
/// Sets the <see cref="EndpointAnnotation.IsExternal"/> property to true for the specified <see cref="EndpointAnnotation"/> object.
/// </summary>
/// <param name="binding">The <see cref="EndpointAnnotation"/> object to modify.</param>
/// <returns>The modified <see cref="EndpointAnnotation"/> object.</returns>
public static EndpointAnnotation AsExternal(this EndpointAnnotation binding)
{
binding.IsExternal = true;
return binding;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

namespace Aspire.Hosting.ApplicationModel;

internal sealed class ServiceReferenceAnnotation(IResource resource) : IResourceAnnotation
internal sealed class EndpointReferenceAnnotation(IResource resource) : IResourceAnnotation
{
public IResource Resource { get; } = resource;
public bool UseAllBindings { get; set; }
public Collection<string> BindingNames { get; } = new();
public bool UseAllEndpoints { get; set; }
public Collection<string> EndpointNames { get; } = new();
}

This file was deleted.

This file was deleted.

This file was deleted.

38 changes: 19 additions & 19 deletions src/Aspire.Hosting/Dcp/ApplicationExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public AppResource(IResource modelResource, CustomResource dcpResource)
internal sealed class ServiceAppResource : AppResource
{
public Service Service => (Service)DcpResource;
public ServiceBindingAnnotation ServiceBindingAnnotation { get; private set; }
public EndpointAnnotation EndpointAnnotation { get; private set; }
public ServiceProducerAnnotation DcpServiceProducerAnnotation { get; private set; }

public override List<ServiceAppResource> ServicesProduced
Expand All @@ -38,9 +38,9 @@ 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)
public ServiceAppResource(IResource modelResource, Service service, EndpointAnnotation sba) : base(modelResource, service)
{
ServiceBindingAnnotation = sba;
EndpointAnnotation = sba;
DcpServiceProducerAnnotation = new(service.Metadata.Name);
}
}
Expand Down Expand Up @@ -174,11 +174,11 @@ private static void AddAllocatedEndpointInfo(IEnumerable<AppResource> resources)
}

var a = new AllocatedEndpointAnnotation(
sp.ServiceBindingAnnotation.Name,
sp.EndpointAnnotation.Name,
PortProtocol.ToProtocolType(svc.Spec.Protocol),
svc.AllocatedAddress!,
(int)svc.AllocatedPort!,
sp.ServiceBindingAnnotation.UriScheme
sp.EndpointAnnotation.UriScheme
);

appResource.ModelResource.Annotations.Add(a);
Expand All @@ -189,14 +189,14 @@ private static void AddAllocatedEndpointInfo(IEnumerable<AppResource> resources)
private void PrepareServices()
{
var serviceProducers = _model.Resources
.Select(r => (ModelResource: r, SBAnnotations: r.Annotations.OfType<ServiceBindingAnnotation>()))
.Select(r => (ModelResource: r, SBAnnotations: r.Annotations.OfType<EndpointAnnotation>()))
.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)
void addServiceAppResource(Service svc, IResource producingResource, EndpointAnnotation sba)
{
svc.Spec.Protocol = PortProtocol.FromProtocolType(sba.Protocol);
svc.Annotate(CustomResource.UriSchemeAnnotation, sba.UriScheme);
Expand Down Expand Up @@ -388,9 +388,9 @@ private async Task CreateExecutablesAsync(IEnumerable<AppResource> executableRes
{
if (er.ModelResource is ProjectResource)
{
var urls = er.ServicesProduced.Where(s => s.ServiceBindingAnnotation.UriScheme is "http" or "https").Select(sar =>
var urls = er.ServicesProduced.Where(s => s.EndpointAnnotation.UriScheme is "http" or "https").Select(sar =>
{
var url = sar.ServiceBindingAnnotation.UriScheme + "://localhost:{{- portForServing \"" + sar.Service.Metadata.Name + "\" -}}";
var url = sar.EndpointAnnotation.UriScheme + "://localhost:{{- portForServing \"" + sar.Service.Metadata.Name + "\" -}}";
return url;
});

Expand Down Expand Up @@ -439,7 +439,7 @@ private static void ApplyLaunchProfile(AppResource executableResource, Dictionar
{
var urls = executableResource.ServicesProduced.Select(sar =>
{
var url = sar.ServiceBindingAnnotation.UriScheme + "://localhost:{{- portForServing \"" + sar.Service.Metadata.Name + "\" -}}";
var url = sar.EndpointAnnotation.UriScheme + "://localhost:{{- portForServing \"" + sar.Service.Metadata.Name + "\" -}}";
return url;
});

Expand Down Expand Up @@ -467,14 +467,14 @@ private static void InjectPortEnvVars(AppResource executableResource, Dictionary
foreach (var serviceProduced in executableResource.ServicesProduced)
{
var name = serviceProduced.Service.Metadata.Name;
var envVar = serviceProduced.ServiceBindingAnnotation.EnvironmentVariable;
var envVar = serviceProduced.EndpointAnnotation.EnvironmentVariable;

if (envVar is not null)
{
config.Add(envVar, $"{{{{- portForServing \"{name}\" }}}}");
}

if (httpsServiceAppResource is null && serviceProduced.ServiceBindingAnnotation.UriScheme == "https")
if (httpsServiceAppResource is null && serviceProduced.EndpointAnnotation.UriScheme == "https")
{
httpsServiceAppResource = serviceProduced;
}
Expand Down Expand Up @@ -564,7 +564,7 @@ private async Task CreateContainersAsync(IEnumerable<AppResource> containerResou
portSpec.HostIP = sp.DcpServiceProducerAnnotation.Address;
}

switch (sp.ServiceBindingAnnotation.Protocol)
switch (sp.EndpointAnnotation.Protocol)
{
case ProtocolType.Tcp:
portSpec.Protocol = PortProtocol.TCP; break;
Expand All @@ -575,7 +575,7 @@ private async Task CreateContainersAsync(IEnumerable<AppResource> containerResou
dcpContainerResource.Spec.Ports.Add(portSpec);

var name = sp.Service.Metadata.Name;
var envVar = sp.ServiceBindingAnnotation.EnvironmentVariable;
var envVar = sp.EndpointAnnotation.EnvironmentVariable;

if (envVar is not null)
{
Expand Down Expand Up @@ -629,18 +629,18 @@ private void AddServicesProducedInfo(IResource modelResource, IAnnotationHolder
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
// Projects/Executables have their ports auto-allocated; the the port specified by the EndpointAnnotation
// is applied to the Service objects and used by clients.
// Containers use the port from the ServiceBindingAnnotation directly.
// Containers use the port from the EndpointAnnotation directly.

if (modelResource.IsContainer())
{
if (sp.ServiceBindingAnnotation.ContainerPort is null)
if (sp.EndpointAnnotation.ContainerPort is null)
{
throw new InvalidOperationException($"The ServiceBindingAnnotation for container resource {modelResourceName} must specify the ContainerPort");
throw new InvalidOperationException($"The endpoint for container resource {modelResourceName} must specify the ContainerPort");
}

sp.DcpServiceProducerAnnotation.Port = sp.ServiceBindingAnnotation.ContainerPort;
sp.DcpServiceProducerAnnotation.Port = sp.EndpointAnnotation.ContainerPort;
}

dcpResource.AnnotateAsObjectList(CustomResource.ServiceProducerAnnotation, sp.DcpServiceProducerAnnotation);
Expand Down
12 changes: 6 additions & 6 deletions src/Aspire.Hosting/Dcp/DcpDistributedApplicationLifecycleHook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public Task BeforeStartAsync(DistributedApplicationModel appModel, CancellationT

private static void PrepareServices(DistributedApplicationModel model)
{
// Automatically add ServiceBindingAnnotations to project resources based on ApplicationUrl set in the launch profile.
// Automatically add EndpointAnnotation to project resources based on ApplicationUrl set in the launch profile.
foreach (var projectResource in model.Resources.OfType<ProjectResource>())
{
var launchProfile = projectResource.GetEffectiveLaunchProfile();
Expand All @@ -41,20 +41,20 @@ private static void PrepareServices(DistributedApplicationModel model)
{
var uri = new Uri(url);

if (projectResource.Annotations.OfType<ServiceBindingAnnotation>().Any(sb => sb.Name == uri.Scheme))
if (projectResource.Annotations.OfType<EndpointAnnotation>().Any(sb => sb.Name == uri.Scheme))
{
// If someone uses WithServiceBinding in the dev host to register a service binding with the name
// If someone uses WithEndpoint in the dev host to register a endpoint with the name
// http or https this exception will be thrown.
throw new DistributedApplicationException($"Service binding annotation with name '{uri.Scheme}' already exists.");
throw new DistributedApplicationException($"Endpoint with name '{uri.Scheme}' already exists.");
}

var generatedServiceBindingAnnotation = new ServiceBindingAnnotation(
var generatedEndpointAnnotation = new EndpointAnnotation(
ProtocolType.Tcp,
uriScheme: uri.Scheme,
port: uri.Port
);

projectResource.Annotations.Add(generatedServiceBindingAnnotation);
projectResource.Annotations.Add(generatedEndpointAnnotation);
}
}
}
Expand Down
Loading