mirror of https://github.com/dotnet/tye.git
Browse Source
Implements two flavor of library support for service-discovery: - GetConnectionString (arbitrary strings) augmenting existing functionality already in asp.net core - GetServiceUri (uris) can be combined from a protocol/host/port triple See the **extensive** doc `service_discovery.md` that is filled out in this PR. That documents pretty much everything about how this works now.pull/314/head
committed by
GitHub
71 changed files with 1986 additions and 345 deletions
@ -1 +1,307 @@ |
|||
TODO |
|||
# Service Discovery |
|||
|
|||
Service discovery is a general term that describes the process by which one service figures out the address of another service. Put another way... |
|||
|
|||
> If I need to talk to the backend... how do I figure out the right URI? In production? In my staging environment? In my local development? |
|||
|
|||
There are lots of possible different ways that service discovery could be done, with varying levels of complexity. |
|||
|
|||
## Tye's philosophy for service discovery |
|||
|
|||
Tye aims to provide a solution that: |
|||
|
|||
- Works the same way in local development and cloud environments |
|||
- Is based on simple primitives |
|||
- Avoids the need for external infrastructure |
|||
|
|||
Using Tye's service discovery features is *optional*. If you already have a scheme you like, or if you are using a programming model that solves service discovery in a different way then you can feel free to ignore it :+1:. |
|||
|
|||
--- |
|||
|
|||
Tye uses a combination of environment variables and files on disk for specifying connection strings and URIs of services. |
|||
|
|||
- Enviroment variables are used where possible because they are simple |
|||
- Files on disk are used for secrets in deployed applications because they are more secure |
|||
- Both of these are primitives that can be accessed from any programming langauge |
|||
|
|||
Our philosophy is that automating something you could do yourself is better than doing *magic* or requiring external services. |
|||
|
|||
It is our recommendation you avoid any hardcoding of URIs/addresses of other services in application code. Use service discovery via configuration so that deploying to different environments is easy. |
|||
|
|||
## How to do service discovery in .NET Applications |
|||
|
|||
The simple way to use Tye's service discovery is through the `Microsoft.Extensions.Configuration` system - available by default in ASP.NET Core or .NET Core Worker projects. In addition to this we provide the `Microsoft.Tye.Extensions.Configuration` package with some Tye-specific extensions layered on top of the configuration system. |
|||
|
|||
--- |
|||
|
|||
To access URIs use the `GetServiceUri()` extension method and provide the service name. |
|||
|
|||
```C# |
|||
// Get the URI of the 'backend' service and create an HttpClient. |
|||
var uri = Configuration.GetServiceUri("backend"); |
|||
var httpClient = new HttpClient() |
|||
{ |
|||
BaseAddress = uri |
|||
}; |
|||
``` |
|||
|
|||
If the service provides multiple bindings, provide the binding name as well. |
|||
|
|||
```C# |
|||
// Get the URI of the 'backend' service and create an HttpClient. |
|||
var uri = Configuration.GetServiceUri(service: "backend", binding: "myBinding"); |
|||
var httpClient = new HttpClient() |
|||
{ |
|||
BaseAddress = uri |
|||
}; |
|||
``` |
|||
|
|||
URIs are available by default for all of your services and bindings. A URI will not be available through the service discovery system for bindings that provide a `connectionString` in config. |
|||
|
|||
--- |
|||
|
|||
To access a connection string, use the `GetConnectionString()` method and provide the service name. |
|||
|
|||
```C# |
|||
// Get the connection string of the 'postgres' service and open a database connection. |
|||
var connectionString = Configuration.GetConnectionString("postgres"); |
|||
using (var connection = new NpgsqlConnection(connectionString)) |
|||
{ |
|||
... |
|||
} |
|||
``` |
|||
|
|||
To get the connection string for a named binding, pass the binding name as well. |
|||
|
|||
```C# |
|||
// Get the connection string of the 'postgres' service and open a database connection. |
|||
var connectionString = Configuration.GetConnectionString(service: "postgres", binding: "myBinding"); |
|||
using (var connection = new NpgsqlConnection(connectionString)) |
|||
{ |
|||
... |
|||
} |
|||
``` |
|||
|
|||
Connection strings will be available for bindings that use the `connectionString` property in configuration. |
|||
|
|||
--- |
|||
|
|||
Specifying a connection string in `tye.yaml` will usually involve the use of templating to fill in values that are provided by Tye. |
|||
|
|||
Example: Redis |
|||
|
|||
```yaml |
|||
services: |
|||
- name: redis |
|||
image: redis |
|||
bindings: |
|||
- port: 6379 |
|||
connectionString: ${host}:${port} |
|||
``` |
|||
|
|||
This fragment will launch `redis` when used with `tye run` on port `6379` (the typical listening port for Redis) **and** will provide a connection string to other services with the value of `localhost:6379`. |
|||
|
|||
> :bulb: It's preferrable to use `${host}` over hardcoding the string `localhost` - for instance `localhost` will not work inside a container. You'll usually see `localhost` as the names of services, but Tye has features that will replace this with hostname values that work from containers. |
|||
|
|||
--- |
|||
|
|||
Templating of connection strings can be used to avoid duplication. |
|||
|
|||
Example: Postgres |
|||
|
|||
```yaml |
|||
services: |
|||
- name: postgres |
|||
image: postgres |
|||
env: |
|||
- name: POSTGRES_PASSWORD |
|||
value: "pass@word1" |
|||
bindings: |
|||
- port: 5432 |
|||
connectionString: Server=${host};Port=${port};User Id=postgres;Password=${env:POSTGRES_PASSWORD}; |
|||
``` |
|||
|
|||
In this case a `postgres` container is being passed its password via the `POSTGRES_PASSWORD` value. The token `${env:POSTGRES_PASSWORD}` will be replaced with the value from `POSTGRES_PASSWORD` to avoid repetition. |
|||
|
|||
Currently replacement of environment variables using this mechanism is limited to environment variables defined in `tye.yaml`. |
|||
|
|||
This is a typical pattern for initializing a database for local development - initializing the password and passing it to the application in the same place. |
|||
|
|||
> :bulb: Avoid generating connection strings, or hardcoding connection string parameters in application code. Tye allows you to configure connection strings differently between local development and deployed apps. The `connectionString` property in `tye.yaml` is not used in deployed applications, it's only for development. |
|||
|
|||
|
|||
## How it works: URIs in development |
|||
|
|||
For URIs, the Tye infrastructure will generate a set of environment variables using a well-known pattern. These environment variables will through through the configuration system and by used by `GetServiceUri()`. |
|||
|
|||
These are normal environment variables and can be read directly or through the configuration system. |
|||
|
|||
The pattern for a default binding on the `backend` service: |
|||
|
|||
| | Environment Variable | Configuration Key | |
|||
|----------|------------------------------|----------------------------| |
|||
| Protocol | `SERVICE__BACKEND__PROTOCOL` | `service:backend:protocol` | |
|||
| Host | `SERVICE__BACKEND__HOST` | `service:backend:host` | |
|||
| Port | `SERVICE__BACKEND__PORT` | `service:backend:port` | |
|||
|
|||
|
|||
The pattern for a named binding called `myBinding` on the `backend` service: |
|||
|
|||
| | Environment Variable | Configuration Key | |
|||
|----------|-----------------------------------------|--------------------------------------| |
|||
| Protocol | `SERVICE__BACKEND__MYBINDING__PROTOCOL` | `service:backend:mybinding:protocol` | |
|||
| Host | `SERVICE__BACKEND__MYBINDING__HOST` | `service:backend:mybinding:host` | |
|||
| Port | `SERVICE__BACKEND__MYBINDING__PORT` | `service:backend:mybinding:port` | |
|||
|
|||
|
|||
> :bulb: That's a double-underscore (`__`) in the environment variables. The `Microsoft.Extensions.Configuration` system uses double-underscore as a separator because single underscores are already common in environment variables. |
|||
|
|||
--- |
|||
|
|||
Here's a walkthrough of how this works in practice, using the following example application: |
|||
|
|||
```yaml |
|||
name: frontend-backend |
|||
services: |
|||
- name: backend |
|||
project: backend/backend.csproj |
|||
- name: frontend |
|||
project: frontend/frontend.csproj |
|||
``` |
|||
|
|||
These services are ASP.NET Core projects. The following set of steps takes place in development when doing `tye run`. |
|||
|
|||
1. Since these are ASP.NET Core projects, Tye infers an `http` default binding, and an `https` named binding (called `https`) for each project. Since these bindings don't have any explicit configuration, ports will be automatically assigned by the host. |
|||
|
|||
2. Tye will assign each binding an available port (avoiding conflicts). |
|||
|
|||
3. Tye will generate a set of environment variables for each binding based on the assigned ports. The protocol of each binding was inferred (`http` or `https`) and the host will be `localhost` since this is local development. |
|||
|
|||
4. Each service is given access to the environment variables that contain the bindings of the *other* services in the application. So `frontend` has access to the bindings of `backend` and vice-versa. These environment variables are passed when launching the process by the Tye host. |
|||
|
|||
5. On startup, the environment variable configuration source reads all environment variables and translates them to the config key format (see table above). |
|||
|
|||
6. When the application calls `GetServiceUri("backend")`, the method will read the `service:backend:protocol`, `service:backend:host`, `service:backend:port` variables and combine them into a URI. |
|||
|
|||
## How it works: Connection Strings in development |
|||
|
|||
When a `binding` element in `tye.yaml` sets the `connectionString` property, then Tye will not set the environment variables used for URIs (previous section). Tye will instead build a connection string and make that available. |
|||
|
|||
Connection strings are typically used when a URI is not the right solution. For example, databases often have many configurable parameters beyond the hostname and port of the server. |
|||
|
|||
Use the `GetConnectionString()` method to read connection strings. |
|||
|
|||
These are normal environment variables and can be read directly or through the configuration system. |
|||
|
|||
The pattern for a default binding on the `postgres` service: |
|||
|
|||
| | Environment Variable | Configuration Key | |
|||
|-------------------|------------------------------|----------------------------| |
|||
| Connection String | `CONNECTIONSTRING__POSTGRES` | `connectionstrings:postgres` | |
|||
|
|||
|
|||
The pattern for a named binding called `myBinding` on the `postgres` service: |
|||
|
|||
| | Environment Variable | Configuration Key | |
|||
|-------------------|------------------------------|----------------------------| |
|||
| Connection String | `CONNECTIONSTRING__POSTGRES__MYBINDING` | `connectionstrings:postgres:mybinding` | |
|||
|
|||
> :bulb: That's a double-underscore (`__`) in the environment variables. The `Microsoft.Extensions.Configuration` system uses double-underscore as a separator because single underscores are already common in environment variables. |
|||
|
|||
Here's a walkthrough of how this works in practice, using the following example application: |
|||
|
|||
```yaml |
|||
services: |
|||
- name: backend |
|||
project: backend/backend.csproj |
|||
- name: frontend |
|||
project: frontend/frontend.csproj |
|||
- name: postgres |
|||
image: postgres |
|||
env: |
|||
- name: POSTGRES_PASSWORD |
|||
value: "pass@word1" |
|||
bindings: |
|||
- port: 5432 |
|||
connectionString: Server=${host};Port=${port};User Id=postgres;Password=${env:POSTGRES_PASSWORD}; |
|||
``` |
|||
|
|||
We'll follow the example of the connection string for `postgres` is generated and made available to `frontend` and `backend`. The following set of steps takes place in development when doing `tye run`. |
|||
|
|||
1. The `postgres` service has a single binding with a hardcoded port. If the port was unspecified then Tye will assign each binding an available port (avoiding conflicts). |
|||
|
|||
2. Tye will substitute the values of `${host}` and `${port}` from the binding. Tye will substitue the value of `POSTGRES_PASSWORD` for `${env:POSTGRES_PASSWORD}`. The result is generate as the `CONNECTIONSTRINGS__POSTGRES` environment variable. |
|||
|
|||
3. Each service is given access to the environment variables that contain the bindings of the *other* services in the application. So `frontend` and `backend` both have access to each-other's bindings as well as the environment variable `CONNECTIONSTRINGS__POSTGRES`. The Tye host will provide these environment variables when launching application processes. |
|||
|
|||
4. On startup, the environment variable configuration source reads all environment variables and translates them to the config key format (see table above). |
|||
|
|||
5. When the application calls `GetConnectionString("postgres")`, the method will read the `connectionstrings:postgres` key and return the value. |
|||
|
|||
## How it works: Deployed applications |
|||
|
|||
When deploying an application, Tye will deploy all of the containers built from your .NET projects. However, Tye is not able to deploy your application's dependencies - since Tye is orchestrating things, it needs to know what values (URIs and connection strings) to provide to application code. |
|||
|
|||
--- |
|||
|
|||
When deploying your .NET projects, Tye will use the environment variable format described above to to set environment variables on your pods and containers. |
|||
|
|||
To avoid hardcoding ephemeral details like pod names, Tye relies on Kubernetes Services. Each project gets its own Service, and the environment variables can refer to the hostname mapped to the service. |
|||
|
|||
This allows service discovery for URIs to work very simply in a deployed application. |
|||
|
|||
--- |
|||
|
|||
When an application contains a dependency (like Redis, or a Database), Tye will use Kubernetes Secret objects to store the connection string or URI. |
|||
|
|||
Tye will look for an existing secret based on the service and binding names. If the secret already exists then deployment will proceed. |
|||
|
|||
If the secret does not exist, then Tye will prompt (in interactive mode) for the connection string or URI value. Based on whether it's a connection string or URI, Tye will create a secret like one of the following. |
|||
|
|||
Example secret for a URI: |
|||
|
|||
```yaml |
|||
apiVersion: v1 |
|||
kind: Secret |
|||
metadata: |
|||
name: binding-production-rabbit-secret |
|||
type: Opaque |
|||
stringData: |
|||
protocol: amqp |
|||
host: rabbitmq |
|||
port: 5672 |
|||
``` |
|||
|
|||
Example secret for a connection string: |
|||
|
|||
```yaml |
|||
apiVersion: v1 |
|||
kind: Secret |
|||
metadata: |
|||
name: binding-production-redis-secret |
|||
type: Opaque |
|||
stringData: |
|||
connectionstring: <redacted> |
|||
``` |
|||
|
|||
Creating the secret is a one-time operation, and Tye will only prompt for it if it does not already exist. If desired you can use standard `kubectl` commands to update values or delete the secret and force it to be recreated. |
|||
|
|||
To get these values into the application, Tye will use Kubernetes volume mounts to create files on disk inside the container. Currrently these files will be placed in `/var/tye/bindings/<service>-<binding>/` and will use the environment-variable naming scheme described above. |
|||
|
|||
Inside the application, adding the Tye secrets provider will add these to the configuration. |
|||
|
|||
```C# |
|||
public static IHostBuilder CreateHostBuilder(string[] args) => |
|||
Host.CreateDefaultBuilder(args) |
|||
.ConfigureAppConfiguration(config => |
|||
{ |
|||
// Add Tye secrets |
|||
config.AddTyeSecrets(); |
|||
}) |
|||
.ConfigureWebHostDefaults(web => |
|||
{ |
|||
web.UseStartup<Startup>(); |
|||
}); |
|||
``` |
|||
|
|||
Tye will also inject the `TYE_SECRETS_PATH` environment variable with the value `/var/tye/bindings/`. This is done to avoid hardcoding a file-system path in the configuration provider. |
|||
@ -1,30 +0,0 @@ |
|||
// Licensed to the .NET Foundation under one or more agreements.
|
|||
// The .NET Foundation licenses this file to you under the MIT license.
|
|||
// See the LICENSE file in the project root for more information.
|
|||
|
|||
using System; |
|||
|
|||
namespace Microsoft.Extensions.Configuration |
|||
{ |
|||
public static class ConfigurationExtensions |
|||
{ |
|||
public static string? GetServiceHost(this IConfiguration configuration, string name) |
|||
{ |
|||
return configuration[$"service:{name}:host"]; |
|||
} |
|||
|
|||
public static Uri? GetServiceUri(this IConfiguration configuration, string name) |
|||
{ |
|||
var host = configuration[$"service:{name}:host"]; |
|||
var port = configuration[$"service:{name}:port"]; |
|||
var protocol = configuration[$"service:{name}:protocol"] ?? "http"; |
|||
|
|||
if (string.IsNullOrEmpty(host) || port == null) |
|||
{ |
|||
return null; |
|||
} |
|||
|
|||
return new Uri(protocol + "://" + host + ":" + port + "/"); |
|||
} |
|||
} |
|||
} |
|||
@ -1,25 +0,0 @@ |
|||
// Licensed to the .NET Foundation under one or more agreements.
|
|||
// The .NET Foundation licenses this file to you under the MIT license.
|
|||
// See the LICENSE file in the project root for more information.
|
|||
|
|||
using System; |
|||
|
|||
namespace Microsoft.Extensions.Configuration |
|||
{ |
|||
public static class ConfigurationExtensions |
|||
{ |
|||
public static Uri? GetServiceUri(this IConfiguration configuration, string name) |
|||
{ |
|||
var host = configuration[$"service:{name}:host"]; |
|||
var port = configuration[$"service:{name}:port"]; |
|||
var protocol = configuration[$"service:{name}:protocol"] ?? "http"; |
|||
|
|||
if (string.IsNullOrEmpty(host) || port == null) |
|||
{ |
|||
return null; |
|||
} |
|||
|
|||
return new Uri(protocol + "://" + host + ":" + port + "/"); |
|||
} |
|||
} |
|||
} |
|||
@ -1,13 +0,0 @@ |
|||
using System; |
|||
using Microsoft.Extensions.Configuration; |
|||
|
|||
namespace Results |
|||
{ |
|||
public static class ConfigurationExtensions |
|||
{ |
|||
public static string GetUri(this IConfiguration configuration, string name) |
|||
{ |
|||
return $"http://{configuration[$"service:{name}:host"]}:{configuration[$"service:{name}:port"]}"; |
|||
} |
|||
} |
|||
} |
|||
@ -1,16 +0,0 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.Extensions.Configuration; |
|||
|
|||
namespace Worker |
|||
{ |
|||
public static class Extensions |
|||
{ |
|||
public static string GetSqlConnectionString(this IConfiguration configuration) |
|||
{ |
|||
return configuration["connectionstring:postgres"] ?? $"Server={configuration["service:postgres:host"]};Port={configuration["service:postgres:port"]};User Id=postgres;Password=pass@word1;"; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,44 @@ |
|||
// Licensed to the .NET Foundation under one or more agreements.
|
|||
// The .NET Foundation licenses this file to you under the MIT license.
|
|||
// See the LICENSE file in the project root for more information.
|
|||
|
|||
using System; |
|||
|
|||
namespace Microsoft.Extensions.Configuration |
|||
{ |
|||
public static class TyeConfigurationExtensions |
|||
{ |
|||
public static Uri? GetServiceUri(this IConfiguration configuration, string name, string? binding = null) |
|||
{ |
|||
var key = GetKey(name, binding); |
|||
|
|||
var host = configuration[$"service:{key}:host"]; |
|||
var port = configuration[$"service:{key}:port"]; |
|||
var protocol = configuration[$"service:{key}:protocol"] ?? "http"; |
|||
|
|||
if (string.IsNullOrEmpty(host) || port == null) |
|||
{ |
|||
return null; |
|||
} |
|||
|
|||
return new Uri(protocol + "://" + host + ":" + port + "/"); |
|||
} |
|||
|
|||
public static string? GetConnectionString(this IConfiguration configuration, string name, string? binding) |
|||
{ |
|||
if (binding == null) |
|||
{ |
|||
return configuration.GetConnectionString(name); |
|||
} |
|||
|
|||
var key = GetKey(name, binding); |
|||
var connectionString = configuration[$"connectionstrings:{key}"]; |
|||
return connectionString; |
|||
} |
|||
|
|||
private static string GetKey(string name, string? binding) |
|||
{ |
|||
return binding == null ? name : $"{name}:{binding}"; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,36 @@ |
|||
// Licensed to the .NET Foundation under one or more agreements.
|
|||
// The .NET Foundation licenses this file to you under the MIT license.
|
|||
// See the LICENSE file in the project root for more information.
|
|||
|
|||
using System.Collections.Generic; |
|||
|
|||
namespace Microsoft.Tye.Hosting.Model |
|||
{ |
|||
public class EffectiveBinding |
|||
{ |
|||
public EffectiveBinding(string service, string? name, string? protocol, string host, int? port, string? connectionString, List<EnvironmentVariable> env) |
|||
{ |
|||
Service = service; |
|||
Name = name; |
|||
Protocol = protocol; |
|||
Host = host; |
|||
Port = port; |
|||
ConnectionString = connectionString; |
|||
Env = env; |
|||
} |
|||
|
|||
public string Service { get; } |
|||
|
|||
public string? Name { get; } |
|||
|
|||
public string? Protocol { get; } |
|||
|
|||
public string Host { get; } |
|||
|
|||
public int? Port { get; } |
|||
|
|||
public string? ConnectionString { get; } |
|||
|
|||
public List<EnvironmentVariable> Env { get; } |
|||
} |
|||
} |
|||
@ -0,0 +1,111 @@ |
|||
// Licensed to the .NET Foundation under one or more agreements.
|
|||
// The .NET Foundation licenses this file to you under the MIT license.
|
|||
// See the LICENSE file in the project root for more information.
|
|||
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Globalization; |
|||
using System.Linq; |
|||
using Microsoft.Tye.Hosting.Model; |
|||
|
|||
namespace Microsoft.Tye.Hosting |
|||
{ |
|||
internal static class TokenReplacement |
|||
{ |
|||
public static string ReplaceValues(string text, EffectiveBinding binding, List<EffectiveBinding> bindings) |
|||
{ |
|||
var tokens = GetTokens(text); |
|||
foreach (var token in tokens) |
|||
{ |
|||
var replacement = ResolveToken(token, binding, bindings); |
|||
if (replacement is null) |
|||
{ |
|||
throw new InvalidOperationException($"No available substitutions found for token '{token}'."); |
|||
} |
|||
|
|||
text = text.Replace(token, replacement); |
|||
} |
|||
|
|||
return text; |
|||
} |
|||
|
|||
private static HashSet<string> GetTokens(string text) |
|||
{ |
|||
var tokens = new HashSet<string>(StringComparer.Ordinal); |
|||
|
|||
var i = 0; |
|||
while ((i = text.IndexOf("${", i)) != -1) |
|||
{ |
|||
var start = i; |
|||
var end = (int?)null; |
|||
for (; i < text.Length; i++) |
|||
{ |
|||
if (text[i] == '}') |
|||
{ |
|||
end = i; |
|||
break; |
|||
} |
|||
} |
|||
|
|||
if (end is null) |
|||
{ |
|||
throw new FormatException($"Value '{text}' contains an unclosed replacement token '{text[start..text.Length]}'."); |
|||
} |
|||
|
|||
var token = text[start..(end.Value + 1)]; |
|||
tokens.Add(token); |
|||
} |
|||
|
|||
return tokens; |
|||
} |
|||
|
|||
private static string? ResolveToken(string token, EffectiveBinding binding, List<EffectiveBinding> bindings) |
|||
{ |
|||
// The language we support for tokens is meant to be pretty DRY. It supports a few different formats:
|
|||
//
|
|||
// - ${host}: only allowed inside a connection string, it can refer to the binding.
|
|||
// - ${env:SOME_VAR}: allowed anywhere. It can refer to any environment variable defined for *this* service.
|
|||
// - ${service:myservice:port}: allowed anywhere. It can refer to the protocol/host/port of bindings.
|
|||
|
|||
var keys = token[2..^1].Split(':'); |
|||
if (keys.Length == 1) |
|||
{ |
|||
// If there's a single key, it has to be the simple format.
|
|||
return GetValueFromBinding(binding, keys[0]); |
|||
} |
|||
else if (keys.Length == 2 && keys[0] == "env") |
|||
{ |
|||
return GetEnvironmentVariable(binding, keys[1]); |
|||
} |
|||
else if (keys.Length == 3 && keys[0] == "service") |
|||
{ |
|||
binding = bindings.FirstOrDefault(b => b.Service == keys[1] && b.Name == null); |
|||
return GetValueFromBinding(binding, keys[2]); |
|||
} |
|||
else if (keys.Length == 4 && keys[0] == "service") |
|||
{ |
|||
binding = bindings.FirstOrDefault(b => b.Service == keys[1] && b.Name == keys[2]); |
|||
return GetValueFromBinding(binding, keys[3]); |
|||
} |
|||
|
|||
return null; |
|||
|
|||
string? GetValueFromBinding(EffectiveBinding binding, string key) |
|||
{ |
|||
return key switch |
|||
{ |
|||
"protocol" => binding.Protocol, |
|||
"host" => binding.Host, |
|||
"port" => binding.Port?.ToString(CultureInfo.InvariantCulture), |
|||
_ => null, |
|||
}; |
|||
} |
|||
|
|||
static string? GetEnvironmentVariable(EffectiveBinding binding, string key) |
|||
{ |
|||
var envVar = binding.Env.FirstOrDefault(e => string.Equals(e.Name, key, StringComparison.OrdinalIgnoreCase)); |
|||
return envVar?.Value; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,61 @@ |
|||
kind: Deployment |
|||
apiVersion: apps/v1 |
|||
metadata: |
|||
name: frontend |
|||
labels: |
|||
app.kubernetes.io/name: 'frontend' |
|||
app.kubernetes.io/part-of: 'generate-connectionstring-dependency' |
|||
spec: |
|||
replicas: 1 |
|||
selector: |
|||
matchLabels: |
|||
app.kubernetes.io/name: frontend |
|||
template: |
|||
metadata: |
|||
labels: |
|||
app.kubernetes.io/name: 'frontend' |
|||
app.kubernetes.io/part-of: 'generate-connectionstring-dependency' |
|||
spec: |
|||
containers: |
|||
- name: frontend |
|||
image: frontend:1.0.0 |
|||
imagePullPolicy: Always |
|||
env: |
|||
- name: ASPNETCORE_URLS |
|||
value: 'http://*' |
|||
- name: PORT |
|||
value: '80' |
|||
- name: TYE_SECRETS_PATH |
|||
value: '/var/tye/bindings/' |
|||
volumeMounts: |
|||
- name: binding-dependency |
|||
mountPath: /var/tye/bindings/dependency |
|||
readOnly: true |
|||
ports: |
|||
- containerPort: 80 |
|||
volumes: |
|||
- name: binding-dependency |
|||
secret: |
|||
secretName: binding-production-dependency-secret |
|||
items: |
|||
- key: connectionstring |
|||
path: CONNECTIONSTRINGS__DEPENDENCY |
|||
... |
|||
--- |
|||
kind: Service |
|||
apiVersion: v1 |
|||
metadata: |
|||
name: frontend |
|||
labels: |
|||
app.kubernetes.io/name: 'frontend' |
|||
app.kubernetes.io/part-of: 'generate-connectionstring-dependency' |
|||
spec: |
|||
selector: |
|||
app.kubernetes.io/name: frontend |
|||
type: ClusterIP |
|||
ports: |
|||
- name: http |
|||
protocol: TCP |
|||
port: 80 |
|||
targetPort: 80 |
|||
... |
|||
@ -0,0 +1,65 @@ |
|||
kind: Deployment |
|||
apiVersion: apps/v1 |
|||
metadata: |
|||
name: frontend |
|||
labels: |
|||
app.kubernetes.io/name: 'frontend' |
|||
app.kubernetes.io/part-of: 'generate-named-binding' |
|||
spec: |
|||
replicas: 1 |
|||
selector: |
|||
matchLabels: |
|||
app.kubernetes.io/name: frontend |
|||
template: |
|||
metadata: |
|||
labels: |
|||
app.kubernetes.io/name: 'frontend' |
|||
app.kubernetes.io/part-of: 'generate-named-binding' |
|||
spec: |
|||
containers: |
|||
- name: frontend |
|||
image: frontend:1.0.0 |
|||
imagePullPolicy: Always |
|||
env: |
|||
- name: ASPNETCORE_URLS |
|||
value: 'http://*' |
|||
- name: PORT |
|||
value: '80' |
|||
- name: TYE_SECRETS_PATH |
|||
value: '/var/tye/bindings/' |
|||
volumeMounts: |
|||
- name: binding-dependency-mybinding |
|||
mountPath: /var/tye/bindings/dependency-mybinding |
|||
readOnly: true |
|||
ports: |
|||
- containerPort: 80 |
|||
volumes: |
|||
- name: binding-dependency-mybinding |
|||
secret: |
|||
secretName: binding-production-dependency-mybinding-secret |
|||
items: |
|||
- key: protocol |
|||
path: SERVICE__DEPENDENCY__MYBINDING__PROTOCOL |
|||
- key: host |
|||
path: SERVICE__DEPENDENCY__MYBINDING__HOST |
|||
- key: port |
|||
path: SERVICE__DEPENDENCY__MYBINDING__PORT |
|||
... |
|||
--- |
|||
kind: Service |
|||
apiVersion: v1 |
|||
metadata: |
|||
name: frontend |
|||
labels: |
|||
app.kubernetes.io/name: 'frontend' |
|||
app.kubernetes.io/part-of: 'generate-named-binding' |
|||
spec: |
|||
selector: |
|||
app.kubernetes.io/name: frontend |
|||
type: ClusterIP |
|||
ports: |
|||
- name: http |
|||
protocol: TCP |
|||
port: 80 |
|||
targetPort: 80 |
|||
... |
|||
@ -0,0 +1,65 @@ |
|||
kind: Deployment |
|||
apiVersion: apps/v1 |
|||
metadata: |
|||
name: frontend |
|||
labels: |
|||
app.kubernetes.io/name: 'frontend' |
|||
app.kubernetes.io/part-of: 'generate-uri-dependency' |
|||
spec: |
|||
replicas: 1 |
|||
selector: |
|||
matchLabels: |
|||
app.kubernetes.io/name: frontend |
|||
template: |
|||
metadata: |
|||
labels: |
|||
app.kubernetes.io/name: 'frontend' |
|||
app.kubernetes.io/part-of: 'generate-uri-dependency' |
|||
spec: |
|||
containers: |
|||
- name: frontend |
|||
image: frontend:1.0.0 |
|||
imagePullPolicy: Always |
|||
env: |
|||
- name: ASPNETCORE_URLS |
|||
value: 'http://*' |
|||
- name: PORT |
|||
value: '80' |
|||
- name: TYE_SECRETS_PATH |
|||
value: '/var/tye/bindings/' |
|||
volumeMounts: |
|||
- name: binding-dependency |
|||
mountPath: /var/tye/bindings/dependency |
|||
readOnly: true |
|||
ports: |
|||
- containerPort: 80 |
|||
volumes: |
|||
- name: binding-dependency |
|||
secret: |
|||
secretName: binding-production-dependency-secret |
|||
items: |
|||
- key: protocol |
|||
path: SERVICE__DEPENDENCY__PROTOCOL |
|||
- key: host |
|||
path: SERVICE__DEPENDENCY__HOST |
|||
- key: port |
|||
path: SERVICE__DEPENDENCY__PORT |
|||
... |
|||
--- |
|||
kind: Service |
|||
apiVersion: v1 |
|||
metadata: |
|||
name: frontend |
|||
labels: |
|||
app.kubernetes.io/name: 'frontend' |
|||
app.kubernetes.io/part-of: 'generate-uri-dependency' |
|||
spec: |
|||
selector: |
|||
app.kubernetes.io/name: frontend |
|||
type: ClusterIP |
|||
ports: |
|||
- name: http |
|||
protocol: TCP |
|||
port: 80 |
|||
targetPort: 80 |
|||
... |
|||
@ -0,0 +1,26 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.AspNetCore.Hosting; |
|||
using Microsoft.Extensions.Configuration; |
|||
using Microsoft.Extensions.Hosting; |
|||
using Microsoft.Extensions.Logging; |
|||
|
|||
namespace frontend |
|||
{ |
|||
public class Program |
|||
{ |
|||
public static void Main(string[] args) |
|||
{ |
|||
CreateHostBuilder(args).Build().Run(); |
|||
} |
|||
|
|||
public static IHostBuilder CreateHostBuilder(string[] args) => |
|||
Host.CreateDefaultBuilder(args) |
|||
.ConfigureWebHostDefaults(webBuilder => |
|||
{ |
|||
webBuilder.UseStartup<Startup>(); |
|||
}); |
|||
} |
|||
} |
|||
@ -0,0 +1,27 @@ |
|||
{ |
|||
"iisSettings": { |
|||
"windowsAuthentication": false, |
|||
"anonymousAuthentication": true, |
|||
"iisExpress": { |
|||
"applicationUrl": "http://localhost:28847", |
|||
"sslPort": 44342 |
|||
} |
|||
}, |
|||
"profiles": { |
|||
"IIS Express": { |
|||
"commandName": "IISExpress", |
|||
"launchBrowser": true, |
|||
"environmentVariables": { |
|||
"ASPNETCORE_ENVIRONMENT": "Development" |
|||
} |
|||
}, |
|||
"frontend": { |
|||
"commandName": "Project", |
|||
"launchBrowser": true, |
|||
"applicationUrl": "https://localhost:5001;http://localhost:5000", |
|||
"environmentVariables": { |
|||
"ASPNETCORE_ENVIRONMENT": "Development" |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,40 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.AspNetCore.Builder; |
|||
using Microsoft.AspNetCore.Hosting; |
|||
using Microsoft.AspNetCore.Http; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Hosting; |
|||
|
|||
namespace frontend |
|||
{ |
|||
public class Startup |
|||
{ |
|||
// This method gets called by the runtime. Use this method to add services to the container.
|
|||
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
|
|||
public void ConfigureServices(IServiceCollection services) |
|||
{ |
|||
} |
|||
|
|||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
|||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) |
|||
{ |
|||
if (env.IsDevelopment()) |
|||
{ |
|||
app.UseDeveloperExceptionPage(); |
|||
} |
|||
|
|||
app.UseRouting(); |
|||
|
|||
app.UseEndpoints(endpoints => |
|||
{ |
|||
endpoints.MapGet("/", async context => |
|||
{ |
|||
await context.Response.WriteAsync("Hello World!"); |
|||
}); |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,9 @@ |
|||
{ |
|||
"Logging": { |
|||
"LogLevel": { |
|||
"Default": "Information", |
|||
"Microsoft": "Warning", |
|||
"Microsoft.Hosting.Lifetime": "Information" |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,10 @@ |
|||
{ |
|||
"Logging": { |
|||
"LogLevel": { |
|||
"Default": "Information", |
|||
"Microsoft": "Warning", |
|||
"Microsoft.Hosting.Lifetime": "Information" |
|||
} |
|||
}, |
|||
"AllowedHosts": "*" |
|||
} |
|||
@ -0,0 +1,7 @@ |
|||
<Project Sdk="Microsoft.NET.Sdk.Web"> |
|||
|
|||
<PropertyGroup> |
|||
<TargetFramework>netcoreapp3.1</TargetFramework> |
|||
</PropertyGroup> |
|||
|
|||
</Project> |
|||
@ -0,0 +1,34 @@ |
|||
|
|||
Microsoft Visual Studio Solution File, Format Version 12.00 |
|||
# Visual Studio 15 |
|||
VisualStudioVersion = 15.0.26124.0 |
|||
MinimumVisualStudioVersion = 15.0.26124.0 |
|||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "frontend", "frontend\frontend.csproj", "{E2BB6C2D-4682-41B2-B044-4C78465B337A}" |
|||
EndProject |
|||
Global |
|||
GlobalSection(SolutionConfigurationPlatforms) = preSolution |
|||
Debug|Any CPU = Debug|Any CPU |
|||
Debug|x64 = Debug|x64 |
|||
Debug|x86 = Debug|x86 |
|||
Release|Any CPU = Release|Any CPU |
|||
Release|x64 = Release|x64 |
|||
Release|x86 = Release|x86 |
|||
EndGlobalSection |
|||
GlobalSection(SolutionProperties) = preSolution |
|||
HideSolutionNode = FALSE |
|||
EndGlobalSection |
|||
GlobalSection(ProjectConfigurationPlatforms) = postSolution |
|||
{E2BB6C2D-4682-41B2-B044-4C78465B337A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU |
|||
{E2BB6C2D-4682-41B2-B044-4C78465B337A}.Debug|Any CPU.Build.0 = Debug|Any CPU |
|||
{E2BB6C2D-4682-41B2-B044-4C78465B337A}.Debug|x64.ActiveCfg = Debug|Any CPU |
|||
{E2BB6C2D-4682-41B2-B044-4C78465B337A}.Debug|x64.Build.0 = Debug|Any CPU |
|||
{E2BB6C2D-4682-41B2-B044-4C78465B337A}.Debug|x86.ActiveCfg = Debug|Any CPU |
|||
{E2BB6C2D-4682-41B2-B044-4C78465B337A}.Debug|x86.Build.0 = Debug|Any CPU |
|||
{E2BB6C2D-4682-41B2-B044-4C78465B337A}.Release|Any CPU.ActiveCfg = Release|Any CPU |
|||
{E2BB6C2D-4682-41B2-B044-4C78465B337A}.Release|Any CPU.Build.0 = Release|Any CPU |
|||
{E2BB6C2D-4682-41B2-B044-4C78465B337A}.Release|x64.ActiveCfg = Release|Any CPU |
|||
{E2BB6C2D-4682-41B2-B044-4C78465B337A}.Release|x64.Build.0 = Release|Any CPU |
|||
{E2BB6C2D-4682-41B2-B044-4C78465B337A}.Release|x86.ActiveCfg = Release|Any CPU |
|||
{E2BB6C2D-4682-41B2-B044-4C78465B337A}.Release|x86.Build.0 = Release|Any CPU |
|||
EndGlobalSection |
|||
EndGlobal |
|||
@ -0,0 +1,14 @@ |
|||
# tye application configuration file |
|||
# read all about it at https://github.com/dotnet/tye |
|||
# |
|||
# when you've given us a try, we'd love to know what you think: |
|||
# https://aka.ms/AA7q20u |
|||
# |
|||
name: generate-connectionstring-dependency |
|||
services: |
|||
- name: frontend |
|||
project: frontend/frontend.csproj |
|||
- name: dependency |
|||
image: fake-image # not actually used for running |
|||
bindings: |
|||
- connectionString: test # need something here to create a binding |
|||
@ -0,0 +1,26 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.AspNetCore.Hosting; |
|||
using Microsoft.Extensions.Configuration; |
|||
using Microsoft.Extensions.Hosting; |
|||
using Microsoft.Extensions.Logging; |
|||
|
|||
namespace frontend |
|||
{ |
|||
public class Program |
|||
{ |
|||
public static void Main(string[] args) |
|||
{ |
|||
CreateHostBuilder(args).Build().Run(); |
|||
} |
|||
|
|||
public static IHostBuilder CreateHostBuilder(string[] args) => |
|||
Host.CreateDefaultBuilder(args) |
|||
.ConfigureWebHostDefaults(webBuilder => |
|||
{ |
|||
webBuilder.UseStartup<Startup>(); |
|||
}); |
|||
} |
|||
} |
|||
@ -0,0 +1,27 @@ |
|||
{ |
|||
"iisSettings": { |
|||
"windowsAuthentication": false, |
|||
"anonymousAuthentication": true, |
|||
"iisExpress": { |
|||
"applicationUrl": "http://localhost:28847", |
|||
"sslPort": 44342 |
|||
} |
|||
}, |
|||
"profiles": { |
|||
"IIS Express": { |
|||
"commandName": "IISExpress", |
|||
"launchBrowser": true, |
|||
"environmentVariables": { |
|||
"ASPNETCORE_ENVIRONMENT": "Development" |
|||
} |
|||
}, |
|||
"frontend": { |
|||
"commandName": "Project", |
|||
"launchBrowser": true, |
|||
"applicationUrl": "https://localhost:5001;http://localhost:5000", |
|||
"environmentVariables": { |
|||
"ASPNETCORE_ENVIRONMENT": "Development" |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,40 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.AspNetCore.Builder; |
|||
using Microsoft.AspNetCore.Hosting; |
|||
using Microsoft.AspNetCore.Http; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Hosting; |
|||
|
|||
namespace frontend |
|||
{ |
|||
public class Startup |
|||
{ |
|||
// This method gets called by the runtime. Use this method to add services to the container.
|
|||
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
|
|||
public void ConfigureServices(IServiceCollection services) |
|||
{ |
|||
} |
|||
|
|||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
|||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) |
|||
{ |
|||
if (env.IsDevelopment()) |
|||
{ |
|||
app.UseDeveloperExceptionPage(); |
|||
} |
|||
|
|||
app.UseRouting(); |
|||
|
|||
app.UseEndpoints(endpoints => |
|||
{ |
|||
endpoints.MapGet("/", async context => |
|||
{ |
|||
await context.Response.WriteAsync("Hello World!"); |
|||
}); |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,9 @@ |
|||
{ |
|||
"Logging": { |
|||
"LogLevel": { |
|||
"Default": "Information", |
|||
"Microsoft": "Warning", |
|||
"Microsoft.Hosting.Lifetime": "Information" |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,10 @@ |
|||
{ |
|||
"Logging": { |
|||
"LogLevel": { |
|||
"Default": "Information", |
|||
"Microsoft": "Warning", |
|||
"Microsoft.Hosting.Lifetime": "Information" |
|||
} |
|||
}, |
|||
"AllowedHosts": "*" |
|||
} |
|||
@ -0,0 +1,7 @@ |
|||
<Project Sdk="Microsoft.NET.Sdk.Web"> |
|||
|
|||
<PropertyGroup> |
|||
<TargetFramework>netcoreapp3.1</TargetFramework> |
|||
</PropertyGroup> |
|||
|
|||
</Project> |
|||
@ -0,0 +1,34 @@ |
|||
|
|||
Microsoft Visual Studio Solution File, Format Version 12.00 |
|||
# Visual Studio 15 |
|||
VisualStudioVersion = 15.0.26124.0 |
|||
MinimumVisualStudioVersion = 15.0.26124.0 |
|||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "frontend", "frontend\frontend.csproj", "{E2BB6C2D-4682-41B2-B044-4C78465B337A}" |
|||
EndProject |
|||
Global |
|||
GlobalSection(SolutionConfigurationPlatforms) = preSolution |
|||
Debug|Any CPU = Debug|Any CPU |
|||
Debug|x64 = Debug|x64 |
|||
Debug|x86 = Debug|x86 |
|||
Release|Any CPU = Release|Any CPU |
|||
Release|x64 = Release|x64 |
|||
Release|x86 = Release|x86 |
|||
EndGlobalSection |
|||
GlobalSection(SolutionProperties) = preSolution |
|||
HideSolutionNode = FALSE |
|||
EndGlobalSection |
|||
GlobalSection(ProjectConfigurationPlatforms) = postSolution |
|||
{E2BB6C2D-4682-41B2-B044-4C78465B337A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU |
|||
{E2BB6C2D-4682-41B2-B044-4C78465B337A}.Debug|Any CPU.Build.0 = Debug|Any CPU |
|||
{E2BB6C2D-4682-41B2-B044-4C78465B337A}.Debug|x64.ActiveCfg = Debug|Any CPU |
|||
{E2BB6C2D-4682-41B2-B044-4C78465B337A}.Debug|x64.Build.0 = Debug|Any CPU |
|||
{E2BB6C2D-4682-41B2-B044-4C78465B337A}.Debug|x86.ActiveCfg = Debug|Any CPU |
|||
{E2BB6C2D-4682-41B2-B044-4C78465B337A}.Debug|x86.Build.0 = Debug|Any CPU |
|||
{E2BB6C2D-4682-41B2-B044-4C78465B337A}.Release|Any CPU.ActiveCfg = Release|Any CPU |
|||
{E2BB6C2D-4682-41B2-B044-4C78465B337A}.Release|Any CPU.Build.0 = Release|Any CPU |
|||
{E2BB6C2D-4682-41B2-B044-4C78465B337A}.Release|x64.ActiveCfg = Release|Any CPU |
|||
{E2BB6C2D-4682-41B2-B044-4C78465B337A}.Release|x64.Build.0 = Release|Any CPU |
|||
{E2BB6C2D-4682-41B2-B044-4C78465B337A}.Release|x86.ActiveCfg = Release|Any CPU |
|||
{E2BB6C2D-4682-41B2-B044-4C78465B337A}.Release|x86.Build.0 = Release|Any CPU |
|||
EndGlobalSection |
|||
EndGlobal |
|||
@ -0,0 +1,14 @@ |
|||
# tye application configuration file |
|||
# read all about it at https://github.com/dotnet/tye |
|||
# |
|||
# when you've given us a try, we'd love to know what you think: |
|||
# https://aka.ms/AA7q20u |
|||
# |
|||
name: generate-named-binding |
|||
services: |
|||
- name: frontend |
|||
project: frontend/frontend.csproj |
|||
- name: dependency |
|||
image: fake-image # not actually used for running |
|||
bindings: |
|||
- name: myBinding |
|||
@ -0,0 +1,26 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.AspNetCore.Hosting; |
|||
using Microsoft.Extensions.Configuration; |
|||
using Microsoft.Extensions.Hosting; |
|||
using Microsoft.Extensions.Logging; |
|||
|
|||
namespace frontend |
|||
{ |
|||
public class Program |
|||
{ |
|||
public static void Main(string[] args) |
|||
{ |
|||
CreateHostBuilder(args).Build().Run(); |
|||
} |
|||
|
|||
public static IHostBuilder CreateHostBuilder(string[] args) => |
|||
Host.CreateDefaultBuilder(args) |
|||
.ConfigureWebHostDefaults(webBuilder => |
|||
{ |
|||
webBuilder.UseStartup<Startup>(); |
|||
}); |
|||
} |
|||
} |
|||
@ -0,0 +1,27 @@ |
|||
{ |
|||
"iisSettings": { |
|||
"windowsAuthentication": false, |
|||
"anonymousAuthentication": true, |
|||
"iisExpress": { |
|||
"applicationUrl": "http://localhost:28847", |
|||
"sslPort": 44342 |
|||
} |
|||
}, |
|||
"profiles": { |
|||
"IIS Express": { |
|||
"commandName": "IISExpress", |
|||
"launchBrowser": true, |
|||
"environmentVariables": { |
|||
"ASPNETCORE_ENVIRONMENT": "Development" |
|||
} |
|||
}, |
|||
"frontend": { |
|||
"commandName": "Project", |
|||
"launchBrowser": true, |
|||
"applicationUrl": "https://localhost:5001;http://localhost:5000", |
|||
"environmentVariables": { |
|||
"ASPNETCORE_ENVIRONMENT": "Development" |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,40 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.AspNetCore.Builder; |
|||
using Microsoft.AspNetCore.Hosting; |
|||
using Microsoft.AspNetCore.Http; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Hosting; |
|||
|
|||
namespace frontend |
|||
{ |
|||
public class Startup |
|||
{ |
|||
// This method gets called by the runtime. Use this method to add services to the container.
|
|||
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
|
|||
public void ConfigureServices(IServiceCollection services) |
|||
{ |
|||
} |
|||
|
|||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
|||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) |
|||
{ |
|||
if (env.IsDevelopment()) |
|||
{ |
|||
app.UseDeveloperExceptionPage(); |
|||
} |
|||
|
|||
app.UseRouting(); |
|||
|
|||
app.UseEndpoints(endpoints => |
|||
{ |
|||
endpoints.MapGet("/", async context => |
|||
{ |
|||
await context.Response.WriteAsync("Hello World!"); |
|||
}); |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,9 @@ |
|||
{ |
|||
"Logging": { |
|||
"LogLevel": { |
|||
"Default": "Information", |
|||
"Microsoft": "Warning", |
|||
"Microsoft.Hosting.Lifetime": "Information" |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,10 @@ |
|||
{ |
|||
"Logging": { |
|||
"LogLevel": { |
|||
"Default": "Information", |
|||
"Microsoft": "Warning", |
|||
"Microsoft.Hosting.Lifetime": "Information" |
|||
} |
|||
}, |
|||
"AllowedHosts": "*" |
|||
} |
|||
@ -0,0 +1,7 @@ |
|||
<Project Sdk="Microsoft.NET.Sdk.Web"> |
|||
|
|||
<PropertyGroup> |
|||
<TargetFramework>netcoreapp3.1</TargetFramework> |
|||
</PropertyGroup> |
|||
|
|||
</Project> |
|||
@ -0,0 +1,34 @@ |
|||
|
|||
Microsoft Visual Studio Solution File, Format Version 12.00 |
|||
# Visual Studio 15 |
|||
VisualStudioVersion = 15.0.26124.0 |
|||
MinimumVisualStudioVersion = 15.0.26124.0 |
|||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "frontend", "frontend\frontend.csproj", "{E2BB6C2D-4682-41B2-B044-4C78465B337A}" |
|||
EndProject |
|||
Global |
|||
GlobalSection(SolutionConfigurationPlatforms) = preSolution |
|||
Debug|Any CPU = Debug|Any CPU |
|||
Debug|x64 = Debug|x64 |
|||
Debug|x86 = Debug|x86 |
|||
Release|Any CPU = Release|Any CPU |
|||
Release|x64 = Release|x64 |
|||
Release|x86 = Release|x86 |
|||
EndGlobalSection |
|||
GlobalSection(SolutionProperties) = preSolution |
|||
HideSolutionNode = FALSE |
|||
EndGlobalSection |
|||
GlobalSection(ProjectConfigurationPlatforms) = postSolution |
|||
{E2BB6C2D-4682-41B2-B044-4C78465B337A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU |
|||
{E2BB6C2D-4682-41B2-B044-4C78465B337A}.Debug|Any CPU.Build.0 = Debug|Any CPU |
|||
{E2BB6C2D-4682-41B2-B044-4C78465B337A}.Debug|x64.ActiveCfg = Debug|Any CPU |
|||
{E2BB6C2D-4682-41B2-B044-4C78465B337A}.Debug|x64.Build.0 = Debug|Any CPU |
|||
{E2BB6C2D-4682-41B2-B044-4C78465B337A}.Debug|x86.ActiveCfg = Debug|Any CPU |
|||
{E2BB6C2D-4682-41B2-B044-4C78465B337A}.Debug|x86.Build.0 = Debug|Any CPU |
|||
{E2BB6C2D-4682-41B2-B044-4C78465B337A}.Release|Any CPU.ActiveCfg = Release|Any CPU |
|||
{E2BB6C2D-4682-41B2-B044-4C78465B337A}.Release|Any CPU.Build.0 = Release|Any CPU |
|||
{E2BB6C2D-4682-41B2-B044-4C78465B337A}.Release|x64.ActiveCfg = Release|Any CPU |
|||
{E2BB6C2D-4682-41B2-B044-4C78465B337A}.Release|x64.Build.0 = Release|Any CPU |
|||
{E2BB6C2D-4682-41B2-B044-4C78465B337A}.Release|x86.ActiveCfg = Release|Any CPU |
|||
{E2BB6C2D-4682-41B2-B044-4C78465B337A}.Release|x86.Build.0 = Release|Any CPU |
|||
EndGlobalSection |
|||
EndGlobal |
|||
@ -0,0 +1,14 @@ |
|||
# tye application configuration file |
|||
# read all about it at https://github.com/dotnet/tye |
|||
# |
|||
# when you've given us a try, we'd love to know what you think: |
|||
# https://aka.ms/AA7q20u |
|||
# |
|||
name: generate-uri-dependency |
|||
services: |
|||
- name: frontend |
|||
project: frontend/frontend.csproj |
|||
- name: dependency |
|||
image: fake-image # not actually used for running |
|||
bindings: |
|||
- port: 8001 # need something here to create a binding |
|||
@ -0,0 +1,16 @@ |
|||
<Project Sdk="Microsoft.NET.Sdk"> |
|||
|
|||
<PropertyGroup> |
|||
<TargetFramework>netcoreapp3.1</TargetFramework> |
|||
<IsTestProject>true</IsTestProject> |
|||
</PropertyGroup> |
|||
|
|||
<ItemGroup> |
|||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.3" /> |
|||
</ItemGroup> |
|||
|
|||
<ItemGroup> |
|||
<ProjectReference Include="..\..\src\Microsoft.Tye.Extensions.Configuration\Microsoft.Tye.Extensions.Configuration.csproj" /> |
|||
</ItemGroup> |
|||
|
|||
</Project> |
|||
@ -0,0 +1,273 @@ |
|||
// Licensed to the .NET Foundation under one or more agreements.
|
|||
// The .NET Foundation licenses this file to you under the MIT license.
|
|||
// See the LICENSE file in the project root for more information.
|
|||
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using Microsoft.Extensions.Configuration.Memory; |
|||
using Xunit; |
|||
|
|||
namespace Microsoft.Extensions.Configuration |
|||
{ |
|||
public class TyeConfigurationExtensionsTest |
|||
{ |
|||
[Fact] |
|||
public void GetConnectionString_WithoutBinding_GetsConnectionString() |
|||
{ |
|||
var builder = new ConfigurationBuilder(); |
|||
builder.AddInMemoryCollection(new Dictionary<string, string>() |
|||
{ |
|||
{ "connectionstrings:myservice", "expected" } |
|||
}); |
|||
|
|||
var configuration = builder.Build(); |
|||
|
|||
var result = configuration.GetConnectionString("myservice"); |
|||
Assert.Equal("expected", result); |
|||
} |
|||
|
|||
[Fact] |
|||
public void GetConnectionString_WithoutBinding_DoesNotCombineHostAndPort() |
|||
{ |
|||
var builder = new ConfigurationBuilder(); |
|||
builder.AddInMemoryCollection(new Dictionary<string, string>() |
|||
{ |
|||
{ "service:myservice:host", "example.com" }, |
|||
{ "service:myservice:port", "443" } |
|||
}); |
|||
|
|||
var configuration = builder.Build(); |
|||
|
|||
var result = configuration.GetConnectionString("myservice"); |
|||
Assert.Null(result); |
|||
} |
|||
|
|||
[Fact] |
|||
public void GetConnectionString_WithBinding_GetsConnectionString() |
|||
{ |
|||
var builder = new ConfigurationBuilder(); |
|||
builder.AddInMemoryCollection(new Dictionary<string, string>() |
|||
{ |
|||
{ "connectionstrings:myservice:http", "expected" } |
|||
}); |
|||
|
|||
var configuration = builder.Build(); |
|||
|
|||
var result = configuration.GetConnectionString("myservice", "http"); |
|||
Assert.Equal("expected", result); |
|||
} |
|||
|
|||
[Fact] |
|||
public void GetConnectionString_WithBinding_DoesNotMatchOtherBinding() |
|||
{ |
|||
var builder = new ConfigurationBuilder(); |
|||
builder.AddInMemoryCollection(new Dictionary<string, string>() |
|||
{ |
|||
{ "connectionstrings:myservice:https", "expected" } |
|||
}); |
|||
|
|||
var configuration = builder.Build(); |
|||
|
|||
var result = configuration.GetConnectionString("myservice", "http"); |
|||
Assert.Null(result); |
|||
} |
|||
|
|||
[Fact] |
|||
public void GetConnectionString_WithBinding_DoesNotMatchDefaultBinding() |
|||
{ |
|||
var builder = new ConfigurationBuilder(); |
|||
builder.AddInMemoryCollection(new Dictionary<string, string>() |
|||
{ |
|||
{ "connectionstrings:myservice", "expected" } |
|||
}); |
|||
|
|||
var configuration = builder.Build(); |
|||
|
|||
var result = configuration.GetConnectionString("myservice", "http"); |
|||
Assert.Null(result); |
|||
} |
|||
|
|||
[Fact] |
|||
public void GetConnectionString_WithBinding_DoesNotCombineHostAndPort() |
|||
{ |
|||
var builder = new ConfigurationBuilder(); |
|||
builder.AddInMemoryCollection(new Dictionary<string, string>() |
|||
{ |
|||
{ "service:myservice:http:host", "example.com" }, |
|||
{ "service:myservice:http:port", "443" } |
|||
}); |
|||
|
|||
var configuration = builder.Build(); |
|||
|
|||
var result = configuration.GetConnectionString("myservice", "http"); |
|||
Assert.Null(result); |
|||
} |
|||
|
|||
[Fact] |
|||
public void GetServiceUri_WithoutBinding_IgnoresConnectionStringIfSet() |
|||
{ |
|||
var builder = new ConfigurationBuilder(); |
|||
builder.AddInMemoryCollection(new Dictionary<string, string>() |
|||
{ |
|||
{ "connectionstrings:myservice", "https://example.com" }, |
|||
{ "service:myservice:protocol", "http" }, |
|||
{ "service:myservice:host", "expected.example.com" }, |
|||
{ "service:myservice:port", "80" }, |
|||
}); |
|||
|
|||
var configuration = builder.Build(); |
|||
|
|||
var result = configuration.GetServiceUri("myservice"); |
|||
Assert.Equal(new Uri("http://expected.example.com"), result); |
|||
} |
|||
|
|||
[Fact] |
|||
public void GetServiceUri_WithoutBinding_ReturnsNullIfNotFound() |
|||
{ |
|||
var builder = new ConfigurationBuilder(); |
|||
builder.AddInMemoryCollection(new Dictionary<string, string>() |
|||
{ |
|||
{ "service:anotherservice:protocol", "https" }, |
|||
{ "service:anotherservice:host", "expected.example.com" }, |
|||
{ "service:anotherservice:port", "5000" }, |
|||
}); |
|||
|
|||
var configuration = builder.Build(); |
|||
|
|||
var result = configuration.GetServiceUri("myservice"); |
|||
Assert.Null(result); |
|||
} |
|||
|
|||
[Fact] |
|||
public void GetServiceUri_WithoutBinding_CombinesHostAndPort() |
|||
{ |
|||
var builder = new ConfigurationBuilder(); |
|||
builder.AddInMemoryCollection(new Dictionary<string, string>() |
|||
{ |
|||
{ "service:myservice:protocol", "https" }, |
|||
{ "service:myservice:host", "expected.example.com" }, |
|||
{ "service:myservice:port", "5000" }, |
|||
}); |
|||
|
|||
var configuration = builder.Build(); |
|||
|
|||
var result = configuration.GetServiceUri("myservice"); |
|||
Assert.Equal(new Uri("https://expected.example.com:5000"), result); |
|||
} |
|||
|
|||
[Fact] |
|||
public void GetServiceUri_WithoutBinding_WithHttpAsDefaultProtocol() |
|||
{ |
|||
var builder = new ConfigurationBuilder(); |
|||
builder.AddInMemoryCollection(new Dictionary<string, string>() |
|||
{ |
|||
{ "service:myservice:host", "expected.example.com" }, |
|||
{ "service:myservice:port", "5000" }, |
|||
}); |
|||
|
|||
var configuration = builder.Build(); |
|||
|
|||
var result = configuration.GetServiceUri("myservice"); |
|||
Assert.Equal(new Uri("http://expected.example.com:5000"), result); |
|||
} |
|||
|
|||
[Fact] |
|||
public void GetServiceUri_WithoutBinding_WithoutHost_ReturnsNull() |
|||
{ |
|||
var builder = new ConfigurationBuilder(); |
|||
builder.AddInMemoryCollection(new Dictionary<string, string>() |
|||
{ |
|||
{ "service:myservice:protocol", "https" }, |
|||
{ "service:myservice:port", "5000" }, |
|||
}); |
|||
|
|||
var configuration = builder.Build(); |
|||
|
|||
var result = configuration.GetServiceUri("myservice"); |
|||
Assert.Null(result); |
|||
} |
|||
|
|||
[Fact] |
|||
public void GetServiceUri_WithoutBinding_WithoutPort_ReturnsNull() |
|||
{ |
|||
var builder = new ConfigurationBuilder(); |
|||
builder.AddInMemoryCollection(new Dictionary<string, string>() |
|||
{ |
|||
{ "service:myservice:protocol", "https" }, |
|||
{ "service:myservice:host", "example.com" }, |
|||
}); |
|||
|
|||
var configuration = builder.Build(); |
|||
|
|||
var result = configuration.GetServiceUri("myservice"); |
|||
Assert.Null(result); |
|||
} |
|||
|
|||
[Fact] |
|||
public void GetServiceUri_InvalidValues_Throws() |
|||
{ |
|||
var builder = new ConfigurationBuilder(); |
|||
builder.AddInMemoryCollection(new Dictionary<string, string>() |
|||
{ |
|||
{ "service:myservice:protocol", "https" }, |
|||
{ "service:myservice:host", "@#*$&$(" }, |
|||
{ "service:myservice:port", "example.com" }, |
|||
}); |
|||
|
|||
var configuration = builder.Build(); |
|||
|
|||
Assert.Throws<UriFormatException>(() => configuration.GetServiceUri("myservice")); |
|||
} |
|||
|
|||
[Fact] |
|||
public void GetServiceUri_WithBinding_IgnoresConnectionStringIfSet() |
|||
{ |
|||
var builder = new ConfigurationBuilder(); |
|||
builder.AddInMemoryCollection(new Dictionary<string, string>() |
|||
{ |
|||
{ "connectionstrings:myservice:metrics", "https://example.com" }, |
|||
{ "service:myservice:metrics:protocol", "http" }, |
|||
{ "service:myservice:metrics:host", "expected.example.com" }, |
|||
{ "service:myservice:metrics:port", "80" }, |
|||
}); |
|||
|
|||
var configuration = builder.Build(); |
|||
|
|||
var result = configuration.GetServiceUri("myservice", "metrics"); |
|||
Assert.Equal(new Uri("http://expected.example.com"), result); |
|||
} |
|||
|
|||
[Fact] |
|||
public void GetServiceUri_WithBinding_CombinesHostAndPort() |
|||
{ |
|||
var builder = new ConfigurationBuilder(); |
|||
builder.AddInMemoryCollection(new Dictionary<string, string>() |
|||
{ |
|||
{ "service:myservice:metrics:protocol", "https" }, |
|||
{ "service:myservice:metrics:host", "expected.example.com" }, |
|||
{ "service:myservice:metrics:port", "5000" }, |
|||
}); |
|||
|
|||
var configuration = builder.Build(); |
|||
|
|||
var result = configuration.GetServiceUri("myservice", "metrics"); |
|||
Assert.Equal(new Uri("https://expected.example.com:5000"), result); |
|||
} |
|||
|
|||
[Fact] |
|||
public void GetServiceUri_WithBinding_IgnoresDefaultBinding() |
|||
{ |
|||
var builder = new ConfigurationBuilder(); |
|||
builder.AddInMemoryCollection(new Dictionary<string, string>() |
|||
{ |
|||
{ "service:myservice:host", "expected.example.com" }, |
|||
{ "service:myservice:port", "5000" }, |
|||
}); |
|||
|
|||
var configuration = builder.Build(); |
|||
|
|||
var result = configuration.GetServiceUri("myservice", "metrics"); |
|||
Assert.Null(result); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue