Skip to content
This repository was archived by the owner on Nov 20, 2023. It is now read-only.

Commit fc4dc92

Browse files
authored
Fix dapr and environment variable injection (#510)
1 parent deaf0aa commit fc4dc92

16 files changed

+194
-74
lines changed

src/Microsoft.Tye.Core/CombineStep.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,18 @@ public override Task ExecuteAsync(OutputContext output, ApplicationBuilder appli
4646
}
4747
}
4848

49+
var services = new List<string>() { service.Name };
50+
services.AddRange(service.Dependencies);
51+
4952
// Process bindings and turn them into environment variables and secrets. There's
5053
// some duplication with the code in m8s (Application.cs) for populating environments.
5154
//
5255
// service.Service.Bindings is the bindings OUT - this step computes bindings IN.
53-
service.Outputs.Add(ComputeBindings(application, service.Dependencies));
56+
service.Outputs.Add(ComputeBindings(application, services));
5457

5558
foreach (var sidecar in project.Sidecars)
5659
{
57-
sidecar.Outputs.Add(ComputeBindings(application, sidecar.Dependencies));
60+
sidecar.Outputs.Add(ComputeBindings(application, services));
5861
}
5962

6063
return Task.CompletedTask;

src/Microsoft.Tye.Extensions/Dapr/DaprExtension.cs

+8
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ public override Task ProcessAsync(ExtensionContext context, ExtensionConfigurati
7171
proxy.Args += $" -log-level {logLevel}";
7272
}
7373

74+
// Add dapr proxy as a service available to everyone.
75+
proxy.Dependencies.UnionWith(context.Application.Services.Select(s => s.Name));
76+
77+
foreach (var s in context.Application.Services)
78+
{
79+
s.Dependencies.Add(proxy.Name);
80+
}
81+
7482
context.Application.Services.Add(proxy);
7583

7684
// Listen for grpc on an auto-assigned port

src/Microsoft.Tye.Hosting/Model/Application.cs

+26-20
Original file line numberDiff line numberDiff line change
@@ -132,33 +132,39 @@ private List<EffectiveBinding> ComputeBindings(Service service, string defaultHo
132132
{
133133
var bindings = new List<EffectiveBinding>();
134134

135+
var isDockerRunInfo = service.Description.RunInfo is DockerRunInfo;
136+
GetEffectiveBindings(isDockerRunInfo, defaultHost, bindings, service);
137+
135138
foreach (var serv in service.Description.Dependencies)
136139
{
137-
var s = Services[serv];
140+
GetEffectiveBindings(isDockerRunInfo, defaultHost, bindings, Services[serv]);
141+
}
138142

139-
foreach (var b in s.Description.Bindings)
140-
{
141-
var protocol = b.Protocol;
142-
var host = b.Host ?? (service.Description.RunInfo is DockerRunInfo ? s.Description.Name : defaultHost);
143+
return bindings;
144+
}
143145

144-
var port = b.Port;
145-
if (b.Port is object && service.Description.RunInfo is DockerRunInfo)
146-
{
147-
port = b.ContainerPort ?? b.Port.Value;
148-
}
146+
private static void GetEffectiveBindings(bool isDockerRunInfo, string defaultHost, List<EffectiveBinding> bindings, Service service)
147+
{
148+
foreach (var b in service.Description.Bindings)
149+
{
150+
var protocol = b.Protocol;
151+
var host = b.Host ?? (isDockerRunInfo ? service.Description.Name : defaultHost);
149152

150-
bindings.Add(new EffectiveBinding(
151-
s.Description.Name,
152-
b.Name,
153-
protocol,
154-
host,
155-
port,
156-
b.ConnectionString,
157-
s.Description.Configuration));
153+
var port = b.Port;
154+
if (b.Port is object && isDockerRunInfo)
155+
{
156+
port = b.ContainerPort ?? b.Port.Value;
158157
}
159-
}
160158

161-
return bindings;
159+
bindings.Add(new EffectiveBinding(
160+
service.Description.Name,
161+
b.Name,
162+
protocol,
163+
host,
164+
port,
165+
b.ConnectionString,
166+
service.Description.Configuration));
167+
}
162168
}
163169
}
164170
}

test/E2ETest/ApplicationTests.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@ public async Task EnvironmentVariablesOnlySetForDirectDependencies()
3131
var dictionary = new Dictionary<string, string>();
3232
app.PopulateEnvironment(app.Services["results"], (s1, s2) => dictionary[s1] = s2);
3333

34-
// Just the WORKER is defined.
35-
Assert.Equal(8, dictionary.Count);
34+
// Just the worker and results are defined.
35+
Assert.Equal(16, dictionary.Count);
3636

3737
Assert.Equal("http", dictionary["SERVICE__WORKER__PROTOCOL"]);
38+
Assert.Equal("http", dictionary["SERVICE__RESULTS__PROTOCOL"]);
3839
// No POSTGRES or REDIS
3940
Assert.False(dictionary.ContainsKey("SERVICE__POSTGRES__PROTOCOL"));
4041
Assert.False(dictionary.ContainsKey("SERVICE__REDIS__PROTOCOL"));

test/E2ETest/testassets/generate/apps-with-ingress.yaml

+13-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ spec:
2525
value: 'http://*'
2626
- name: PORT
2727
value: '80'
28+
- name: SERVICE__APPA__PROTOCOL
29+
value: 'http'
30+
- name: SERVICE__APPA__PORT
31+
value: '80'
32+
- name: SERVICE__APPA__HOST
33+
value: 'appa'
2834
- name: SERVICE__APPB__PROTOCOL
2935
value: 'http'
3036
- name: SERVICE__APPB__PORT
@@ -80,6 +86,12 @@ spec:
8086
value: 'http://*'
8187
- name: PORT
8288
value: '80'
89+
- name: SERVICE__APPB__PROTOCOL
90+
value: 'http'
91+
- name: SERVICE__APPB__PORT
92+
value: '80'
93+
- name: SERVICE__APPB__HOST
94+
value: 'appb'
8395
- name: SERVICE__APPA__PROTOCOL
8496
value: 'http'
8597
- name: SERVICE__APPA__PORT
@@ -143,4 +155,4 @@ spec:
143155
serviceName: appb
144156
servicePort: 80
145157
path: /()(.*)
146-
...
158+
...

test/E2ETest/testassets/generate/dapr.yaml

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
kind: Deployment
1+
kind: Deployment
22
apiVersion: apps/v1
33
metadata:
44
name: dapr-test-project
@@ -37,6 +37,12 @@ spec:
3737
value: 'http://*'
3838
- name: PORT
3939
value: '80'
40+
- name: SERVICE__DAPR-TEST-PROJECT__PROTOCOL
41+
value: 'http'
42+
- name: SERVICE__DAPR-TEST-PROJECT__PORT
43+
value: '80'
44+
- name: SERVICE__DAPR-TEST-PROJECT__HOST
45+
value: 'dapr-test-project'
4046
ports:
4147
- containerPort: 80
4248
...

test/E2ETest/testassets/generate/dockerfile.yaml

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
kind: Deployment
1+
kind: Deployment
22
apiVersion: apps/v1
33
metadata:
44
name: backend
@@ -25,6 +25,12 @@ spec:
2525
value: 'http://*'
2626
- name: PORT
2727
value: '80'
28+
- name: SERVICE__BACKEND__PROTOCOL
29+
value: 'http'
30+
- name: SERVICE__BACKEND__PORT
31+
value: '80'
32+
- name: SERVICE__BACKEND__HOST
33+
value: 'backend'
2834
- name: SERVICE__FRONTEND__PROTOCOL
2935
value: 'http'
3036
- name: SERVICE__FRONTEND__PORT
@@ -80,6 +86,12 @@ spec:
8086
value: 'http://*'
8187
- name: PORT
8288
value: '80'
89+
- name: SERVICE__FRONTEND__PROTOCOL
90+
value: 'http'
91+
- name: SERVICE__FRONTEND__PORT
92+
value: '80'
93+
- name: SERVICE__FRONTEND__HOST
94+
value: 'frontend'
8395
- name: SERVICE__BACKEND__PROTOCOL
8496
value: 'http'
8597
- name: SERVICE__BACKEND__PORT

test/E2ETest/testassets/generate/frontend-backend.yaml

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
kind: Deployment
1+
kind: Deployment
22
apiVersion: apps/v1
33
metadata:
44
name: backend
@@ -25,6 +25,12 @@ spec:
2525
value: 'http://*'
2626
- name: PORT
2727
value: '80'
28+
- name: SERVICE__BACKEND__PROTOCOL
29+
value: 'http'
30+
- name: SERVICE__BACKEND__PORT
31+
value: '80'
32+
- name: SERVICE__BACKEND__HOST
33+
value: 'backend'
2834
- name: SERVICE__FRONTEND__PROTOCOL
2935
value: 'http'
3036
- name: SERVICE__FRONTEND__PORT
@@ -80,6 +86,12 @@ spec:
8086
value: 'http://*'
8187
- name: PORT
8288
value: '80'
89+
- name: SERVICE__FRONTEND__PROTOCOL
90+
value: 'http'
91+
- name: SERVICE__FRONTEND__PORT
92+
value: '80'
93+
- name: SERVICE__FRONTEND__HOST
94+
value: 'frontend'
8395
- name: SERVICE__BACKEND__PROTOCOL
8496
value: 'http'
8597
- name: SERVICE__BACKEND__PORT

test/E2ETest/testassets/generate/generate-connectionstring-dependency.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ spec:
2525
value: 'http://*'
2626
- name: PORT
2727
value: '80'
28+
- name: SERVICE__FRONTEND__PROTOCOL
29+
value: 'http'
30+
- name: SERVICE__FRONTEND__PORT
31+
value: '80'
32+
- name: SERVICE__FRONTEND__HOST
33+
value: 'frontend'
2834
- name: CONNECTIONSTRINGS__DEPENDENCY
2935
valueFrom:
3036
secretKeyRef:

test/E2ETest/testassets/generate/generate-named-binding.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ spec:
2525
value: 'http://*'
2626
- name: PORT
2727
value: '80'
28+
- name: SERVICE__FRONTEND__PROTOCOL
29+
value: 'http'
30+
- name: SERVICE__FRONTEND__PORT
31+
value: '80'
32+
- name: SERVICE__FRONTEND__HOST
33+
value: 'frontend'
2834
- name: SERVICE__DEPENDENCY__MYBINDING__PROTOCOL
2935
valueFrom:
3036
secretKeyRef:

test/E2ETest/testassets/generate/generate-uri-dependency.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ spec:
2525
value: 'http://*'
2626
- name: PORT
2727
value: '80'
28+
- name: SERVICE__FRONTEND__PROTOCOL
29+
value: 'http'
30+
- name: SERVICE__FRONTEND__PORT
31+
value: '80'
32+
- name: SERVICE__FRONTEND__HOST
33+
value: 'frontend'
2834
- name: SERVICE__DEPENDENCY__PROTOCOL
2935
valueFrom:
3036
secretKeyRef:

test/E2ETest/testassets/generate/health-checks.yaml

+48-42
Original file line numberDiff line numberDiff line change
@@ -17,44 +17,50 @@ spec:
1717
app.kubernetes.io/part-of: 'health-checks'
1818
spec:
1919
containers:
20-
- name: health-all
21-
image: health-all:1.0.0
22-
imagePullPolicy: Always
23-
env:
24-
- name: ASPNETCORE_URLS
25-
value: 'http://*:8004'
26-
- name: PORT
27-
value: '8004'
28-
ports:
29-
- containerPort: 8004
30-
livenessProbe:
31-
httpGet:
32-
path: /healthy
33-
port: 8004
34-
scheme: HTTP
35-
httpHeaders:
36-
- name: name1
37-
value: value1
38-
- name: name2
39-
value: value2
40-
initialDelaySeconds: 5
41-
periodSeconds: 1
42-
successThreshold: 1
43-
failureThreshold: 1
44-
readinessProbe:
45-
httpGet:
46-
path: /ready
47-
port: 8004
48-
scheme: HTTP
49-
httpHeaders:
50-
- name: name3
51-
value: value3
52-
- name: name4
53-
value: value4
54-
initialDelaySeconds: 5
55-
periodSeconds: 1
56-
successThreshold: 1
57-
failureThreshold: 1
20+
- name: health-all
21+
image: health-all:1.0.0
22+
imagePullPolicy: Always
23+
env:
24+
- name: ASPNETCORE_URLS
25+
value: 'http://*:8004'
26+
- name: PORT
27+
value: '8004'
28+
- name: SERVICE__HEALTH-ALL__PROTOCOL
29+
value: 'http'
30+
- name: SERVICE__HEALTH-ALL__PORT
31+
value: '8004'
32+
- name: SERVICE__HEALTH-ALL__HOST
33+
value: 'health-all'
34+
ports:
35+
- containerPort: 8004
36+
livenessProbe:
37+
httpGet:
38+
path: /healthy
39+
port: 8004
40+
scheme: HTTP
41+
httpHeaders:
42+
- name: name1
43+
value: value1
44+
- name: name2
45+
value: value2
46+
initialDelaySeconds: 5
47+
periodSeconds: 1
48+
successThreshold: 1
49+
failureThreshold: 1
50+
readinessProbe:
51+
httpGet:
52+
path: /ready
53+
port: 8004
54+
scheme: HTTP
55+
httpHeaders:
56+
- name: name3
57+
value: value3
58+
- name: name4
59+
value: value4
60+
initialDelaySeconds: 5
61+
periodSeconds: 1
62+
successThreshold: 1
63+
failureThreshold: 1
5864
...
5965
---
6066
kind: Service
@@ -69,8 +75,8 @@ spec:
6975
app.kubernetes.io/name: health-all
7076
type: ClusterIP
7177
ports:
72-
- name: http
73-
protocol: TCP
74-
port: 8004
75-
targetPort: 8004
78+
- name: http
79+
protocol: TCP
80+
port: 8004
81+
targetPort: 8004
7682
...

test/E2ETest/testassets/generate/multi-project.yaml

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
kind: Deployment
1+
kind: Deployment
22
apiVersion: apps/v1
33
metadata:
44
name: backend
@@ -25,6 +25,12 @@ spec:
2525
value: 'http://*:7000'
2626
- name: PORT
2727
value: '7000'
28+
- name: SERVICE__BACKEND__PROTOCOL
29+
value: 'http'
30+
- name: SERVICE__BACKEND__PORT
31+
value: '7000'
32+
- name: SERVICE__BACKEND__HOST
33+
value: 'backend'
2834
- name: SERVICE__FRONTEND__PROTOCOL
2935
value: 'http'
3036
- name: SERVICE__FRONTEND__PORT
@@ -95,6 +101,12 @@ spec:
95101
value: 'http://*:8000'
96102
- name: PORT
97103
value: '8000'
104+
- name: SERVICE__FRONTEND__PROTOCOL
105+
value: 'http'
106+
- name: SERVICE__FRONTEND__PORT
107+
value: '8000'
108+
- name: SERVICE__FRONTEND__HOST
109+
value: 'frontend'
98110
- name: SERVICE__BACKEND__PROTOCOL
99111
value: 'http'
100112
- name: SERVICE__BACKEND__PORT

0 commit comments

Comments
 (0)