aboutsummaryrefslogtreecommitdiffstats
path: root/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/application/EndpointTest.java
blob: fbc5567101f7221c75615dda49aa42f4bb5a1cfd (plain) (blame)
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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.application;

import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.CloudName;
import com.yahoo.config.provision.ClusterSpec;
import com.yahoo.config.provision.SystemName;
import com.yahoo.config.provision.zone.AuthMethod;
import com.yahoo.config.provision.zone.RoutingMethod;
import com.yahoo.config.provision.zone.ZoneId;
import com.yahoo.vespa.hosted.controller.api.identifiers.DeploymentId;
import com.yahoo.vespa.hosted.controller.application.Endpoint.Port;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.Map;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
 * @author mpolden
 */
public class EndpointTest {

    private static final ApplicationId instance1 = ApplicationId.from("t1", "a1", "default");
    private static final ApplicationId instance2 = ApplicationId.from("t2", "a2", "i2");
    private static final TenantAndApplicationId app1 = TenantAndApplicationId.from(instance1);
    private static final TenantAndApplicationId app2 = TenantAndApplicationId.from(instance2);

    @Test
    void global_endpoints() {
        DeploymentId deployment1 = new DeploymentId(instance1, ZoneId.from("prod", "us-north-1"));
        DeploymentId deployment2 = new DeploymentId(instance2, ZoneId.from("prod", "us-north-1"));
        ClusterSpec.Id cluster = ClusterSpec.Id.from("default");
        EndpointId endpointId = EndpointId.defaultId();

        Map<String, Endpoint> tests = Map.of(
                // Main endpoint with direct routing and default TLS port
                "https://a1.t1.global.vespa.oath.cloud/",
                Endpoint.of(instance1).target(endpointId, cluster, List.of(deployment1)).on(Port.tls()).routingMethod(RoutingMethod.exclusive).in(SystemName.main),

                // Main endpoint with custom rotation name
                "https://r1.a1.t1.global.vespa.oath.cloud/",
                Endpoint.of(instance1).target(EndpointId.of("r1"), cluster, List.of(deployment1)).on(Port.tls()).routingMethod(RoutingMethod.exclusive).in(SystemName.main),

                // Main endpoint for custom instance in default rotation
                "https://i2.a2.t2.global.vespa.oath.cloud/",
                Endpoint.of(instance2).target(endpointId, cluster, List.of(deployment2)).on(Port.tls()).routingMethod(RoutingMethod.exclusive).in(SystemName.main),

                // Main endpoint for custom instance with custom rotation name
                "https://r2.i2.a2.t2.global.vespa.oath.cloud/",
                Endpoint.of(instance2).target(EndpointId.of("r2"), cluster, List.of(deployment2)).on(Port.tls()).routingMethod(RoutingMethod.exclusive).in(SystemName.main),

                // Main endpoint in public system
                "https://a1.t1.g.vespa-app.cloud/",
                Endpoint.of(instance1).target(endpointId, cluster, List.of(deployment1)).on(Port.tls()).routingMethod(RoutingMethod.exclusive).in(SystemName.Public)
        );
        tests.forEach((expected, endpoint) -> assertEquals(expected, endpoint.url().toString()));

        Map<String, Endpoint> tests2 = Map.of(
                // Default endpoint in public system
                "https://a1.t1.g.vespa-app.cloud/",
                Endpoint.of(instance1).target(endpointId, cluster, List.of(deployment1)).on(Port.tls()).routingMethod(RoutingMethod.exclusive).in(SystemName.Public),

                // Default endpoint in public CD system
                "https://a1.t1.g.cd.vespa-app.cloud/",
                Endpoint.of(instance1).target(endpointId, cluster, List.of(deployment1)).on(Port.tls()).routingMethod(RoutingMethod.exclusive).in(SystemName.PublicCd),

                // Custom instance in public system
                "https://i2.a2.t2.g.vespa-app.cloud/",
                Endpoint.of(instance2).target(endpointId, cluster, List.of(deployment2)).on(Port.tls()).routingMethod(RoutingMethod.exclusive).in(SystemName.Public)
        );
        tests2.forEach((expected, endpoint) -> assertEquals(expected, endpoint.url().toString()));
    }

    @Test
    void global_endpoints_with_endpoint_id() {
        DeploymentId deployment1 = new DeploymentId(instance1, ZoneId.from("prod", "us-north-1"));
        DeploymentId deployment2 = new DeploymentId(instance2, ZoneId.from("prod", "us-north-1"));
        ClusterSpec.Id cluster = ClusterSpec.Id.from("default");
        EndpointId endpointId = EndpointId.defaultId();

        Map<String, Endpoint> tests = Map.of(
                // Main endpoint with direct routing and default TLS port
                "https://a1.t1.global.vespa.oath.cloud/",
                Endpoint.of(instance1).target(endpointId, cluster, List.of(deployment1)).on(Port.tls()).routingMethod(RoutingMethod.exclusive).in(SystemName.main),

                // Main endpoint with custom rotation name
                "https://r1.a1.t1.global.vespa.oath.cloud/",
                Endpoint.of(instance1).target(EndpointId.of("r1"), cluster, List.of(deployment1)).on(Port.tls()).routingMethod(RoutingMethod.exclusive).in(SystemName.main),

                // Main endpoint for custom instance in default rotation
                "https://i2.a2.t2.global.vespa.oath.cloud/",
                Endpoint.of(instance2).target(endpointId, cluster, List.of(deployment2)).on(Port.tls()).routingMethod(RoutingMethod.exclusive).in(SystemName.main),

                // Main endpoint for custom instance with custom rotation name
                "https://r2.i2.a2.t2.global.vespa.oath.cloud/",
                Endpoint.of(instance2).target(EndpointId.of("r2"), cluster, List.of(deployment2)).on(Port.tls()).routingMethod(RoutingMethod.exclusive).in(SystemName.main),

                // Main endpoint in public system
                "https://a1.t1.g.vespa-app.cloud/",
                Endpoint.of(instance1).target(endpointId, cluster, List.of(deployment1)).on(Port.tls()).routingMethod(RoutingMethod.exclusive).in(SystemName.Public)
        );
        tests.forEach((expected, endpoint) -> assertEquals(expected, endpoint.url().toString()));

        Map<String, Endpoint> tests2 = Map.of(
                // Custom endpoint and instance in public CD system)
                "https://foo.i2.a2.t2.g.cd.vespa-app.cloud/",
                Endpoint.of(instance2).target(EndpointId.of("foo"), cluster, List.of(deployment2)).on(Port.tls()).routingMethod(RoutingMethod.exclusive).in(SystemName.PublicCd),

                // Custom endpoint and instance in public system
                "https://foo.i2.a2.t2.g.vespa-app.cloud/",
                Endpoint.of(instance2).target(EndpointId.of("foo"), cluster, List.of(deployment2)).on(Port.tls()).routingMethod(RoutingMethod.exclusive).in(SystemName.Public)
        );
        tests2.forEach((expected, endpoint) -> assertEquals(expected, endpoint.url().toString()));
    }

    @Test
    void zone_endpoints() {
        var cluster = ClusterSpec.Id.from("default"); // Always default for non-direct routing
        var prodZone = new DeploymentId(instance1, ZoneId.from("prod", "us-north-1"));
        var prodZone2 = new DeploymentId(instance2, ZoneId.from("prod", "us-north-1"));
        var testZone = new DeploymentId(instance1, ZoneId.from("test", "us-north-2"));

        Map<String, Endpoint> tests = Map.of(
                // Prod endpoint in main
                "https://a1.t1.us-north-1.vespa.oath.cloud/",
                Endpoint.of(instance1).target(cluster, prodZone).on(Port.tls()).in(SystemName.main),

                // Prod endpoint in CD
                "https://cd.a1.t1.us-north-1.cd.vespa.oath.cloud/",
                Endpoint.of(instance1).target(cluster, prodZone).on(Port.tls()).in(SystemName.cd),

                // Test endpoint in main
                "https://a1.t1.us-north-2.test.vespa.oath.cloud/",
                Endpoint.of(instance1).target(cluster, testZone).on(Port.tls()).in(SystemName.main),

                // Non-default cluster in main
                "https://c1.a1.t1.us-north-1.vespa.oath.cloud/",
                Endpoint.of(instance1).target(ClusterSpec.Id.from("c1"), prodZone).on(Port.tls()).in(SystemName.main),

                // Non-default instance in main
                "https://i2.a2.t2.us-north-1.vespa.oath.cloud/",
                Endpoint.of(instance2).target(cluster, prodZone2).on(Port.tls()).in(SystemName.main),

                // Non-default cluster in public
                "https://c1.a1.t1.us-north-1.z.vespa-app.cloud/",
                Endpoint.of(instance1).target(ClusterSpec.Id.from("c1"), prodZone).on(Port.tls()).routingMethod(RoutingMethod.exclusive).in(SystemName.Public),

                // Non-default cluster and instance in public
                "https://c2.i2.a2.t2.us-north-1.z.vespa-app.cloud/",
                Endpoint.of(instance2).target(ClusterSpec.Id.from("c2"), prodZone2).on(Port.tls()).routingMethod(RoutingMethod.exclusive).in(SystemName.Public)
        );
        tests.forEach((expected, endpoint) -> assertEquals(expected, endpoint.url().toString()));

        Map<String, Endpoint> tests2 = Map.of(
                // Non-default cluster and instance in public CD (legacy)
                "https://c2.i2.a2.t2.us-north-1.z.cd.vespa-app.cloud/",
                Endpoint.of(instance2).target(ClusterSpec.Id.from("c2"), prodZone2).on(Port.tls()).routingMethod(RoutingMethod.exclusive).in(SystemName.PublicCd),

                // Custom cluster name in public
                "https://c1.a1.t1.us-north-1.z.vespa-app.cloud/",
                Endpoint.of(instance1).target(ClusterSpec.Id.from("c1"), prodZone).on(Port.tls()).routingMethod(RoutingMethod.exclusive).in(SystemName.Public),

                // Default cluster name in non-production zone in public
                "https://a1.t1.us-north-2.test.z.vespa-app.cloud/",
                Endpoint.of(instance1).target(ClusterSpec.Id.from("default"), testZone).on(Port.tls()).routingMethod(RoutingMethod.exclusive).in(SystemName.Public),

                // Default cluster name in public CD
                "https://a1.t1.us-north-1.z.cd.vespa-app.cloud/",
                Endpoint.of(instance1).target(ClusterSpec.Id.from("default"), prodZone).on(Port.tls()).routingMethod(RoutingMethod.exclusive).in(SystemName.PublicCd)
        );
        tests2.forEach((expected, endpoint) -> assertEquals(expected, endpoint.url().toString()));
    }

    @Test
    void certificate_endpoints() {
        var defaultCluster = ClusterSpec.Id.from("default");
        var prodZone = new DeploymentId(instance1, ZoneId.from("prod", "us-north-1"));
        var testZone = new DeploymentId(instance1, ZoneId.from("test", "us-north-2"));

        var tests = Map.of(
                // Default rotation
                "https://a1.t1.g.vespa-app.cloud/",
                Endpoint.of(instance1)
                        .target(EndpointId.defaultId())
                        .certificateName()
                        .routingMethod(RoutingMethod.exclusive)
                        .on(Port.tls())
                        .in(SystemName.Public),

                // Wildcard to match other rotations
                "https://*.a1.t1.g.vespa-app.cloud/",
                Endpoint.of(instance1)
                        .wildcard()
                        .certificateName()
                        .routingMethod(RoutingMethod.exclusive)
                        .on(Port.tls())
                        .in(SystemName.Public),

                // Default cluster in zone
                "https://a1.t1.us-north-1.z.vespa-app.cloud/",
                Endpoint.of(instance1)
                        .target(defaultCluster, prodZone)
                        .certificateName()
                        .routingMethod(RoutingMethod.exclusive)
                        .on(Port.tls())
                        .in(SystemName.Public),

                // Default cluster in test zone
                "https://a1.t1.us-north-2.test.z.vespa-app.cloud/",
                Endpoint.of(instance1)
                        .target(defaultCluster, testZone)
                        .certificateName()
                        .routingMethod(RoutingMethod.exclusive)
                        .on(Port.tls())
                        .in(SystemName.Public),

                // Wildcard to match other clusters in test zone
                "https://*.a1.t1.us-north-2.test.z.vespa-app.cloud/",
                Endpoint.of(instance1)
                        .wildcard(testZone)
                        .certificateName()
                        .routingMethod(RoutingMethod.exclusive)
                        .on(Port.tls())
                        .in(SystemName.Public),

                // Wildcard to match other clusters in zone
                "https://*.a1.t1.us-north-1.z.vespa-app.cloud/",
                Endpoint.of(instance1)
                        .wildcard(prodZone)
                        .certificateName()
                        .routingMethod(RoutingMethod.exclusive)
                        .on(Port.tls())
                        .in(SystemName.Public)
        );

        tests.forEach((expected, endpoint) -> assertEquals(expected, endpoint.url().toString()));
    }

    @Test
    void region_endpoints() {
        var cluster = ClusterSpec.Id.from("default");
        var prodZone = ZoneId.from("prod", "us-north-2");
        Map<String, Endpoint> tests = Map.of(
                "https://a1.t1.aws-us-north-1.w.vespa-app.cloud/",
                Endpoint.of(instance1)
                        .targetRegion(cluster, "us-north-1", CloudName.AWS)
                        .routingMethod(RoutingMethod.exclusive)
                        .on(Port.tls())
                        .in(SystemName.Public),
                "https://a1.t1.gcp-us-south1.w.vespa-app.cloud/",
                Endpoint.of(instance1)
                        .targetRegion(cluster, "us-south1", CloudName.GCP)
                        .routingMethod(RoutingMethod.exclusive)
                        .on(Port.tls())
                        .in(SystemName.Public),
                "https://c1.a1.t1.aws-us-north-2.w.vespa-app.cloud/",
                Endpoint.of(instance1)
                        .targetRegion(ClusterSpec.Id.from("c1"), "us-north-2", CloudName.AWS)
                        .routingMethod(RoutingMethod.exclusive)
                        .on(Port.tls())
                        .in(SystemName.Public),
                "https://deadbeef.cafed00d.aws-us-north-2.w.vespa-app.cloud/",
                Endpoint.of(instance1)
                        .targetRegion(ClusterSpec.Id.from("c1"), "us-north-2", CloudName.AWS)
                        .routingMethod(RoutingMethod.exclusive)
                        .generatedFrom(new GeneratedEndpoint("deadbeef", "cafed00d", AuthMethod.mtls, Optional.empty()))
                        .on(Port.tls())
                        .in(SystemName.Public),
                "https://c1.a1.t1.aws-us-north-2-w.vespa.oath.cloud/",
                Endpoint.of(instance1)
                        .targetRegion(ClusterSpec.Id.from("c1"), "us-north-2", CloudName.AWS)
                        .routingMethod(RoutingMethod.exclusive)
                        .on(Port.tls())
                        .in(SystemName.main)
        );
        tests.forEach((expected, endpoint) -> assertEquals(expected, endpoint.url().toString()));
    }

    @Test
    void application_endpoints() {
        Map<String, Endpoint> tests = Map.of(
                "https://weighted.a1.t1.a.vespa-app.cloud/",
                Endpoint.of(app1)
                        .targetApplication(EndpointId.of("weighted"), ClusterSpec.Id.from("qrs"),
                                Map.of(new DeploymentId(app1.instance("i1"), ZoneId.from("prod", "us-west-1")), 1))
                        .routingMethod(RoutingMethod.exclusive)
                        .on(Port.tls())
                        .in(SystemName.Public),
                "https://weighted.a1.t1.a.cd.vespa-app.cloud/",
                Endpoint.of(app1)
                        .targetApplication(EndpointId.of("weighted"), ClusterSpec.Id.from("qrs"),
                                Map.of(new DeploymentId(app1.instance("i1"), ZoneId.from("prod", "us-west-1")), 1))
                        .routingMethod(RoutingMethod.exclusive)
                        .on(Port.tls())
                        .in(SystemName.PublicCd),
                "https://a2.t2.a.vespa.oath.cloud/",
                Endpoint.of(app2)
                        .targetApplication(EndpointId.defaultId(), ClusterSpec.Id.from("qrs"),
                                Map.of(new DeploymentId(app2.instance("i1"), ZoneId.from("prod", "us-east-3")), 1))
                        .routingMethod(RoutingMethod.exclusive)
                        .on(Port.tls())
                        .in(SystemName.main),
                "https://cd.a2.t2.a.cd.vespa.oath.cloud/",
                Endpoint.of(app2)
                        .targetApplication(EndpointId.defaultId(), ClusterSpec.Id.from("qrs"),
                                Map.of(new DeploymentId(app2.instance("i1"), ZoneId.from("prod", "us-east-3")), 1))
                        .routingMethod(RoutingMethod.exclusive)
                        .on(Port.tls())
                        .in(SystemName.cd)
        );
        tests.forEach((expected, endpoint) -> assertEquals(expected, endpoint.url().toString()));
    }

    @Test
    void upstream_name() {
        var zone = new DeploymentId(instance1, ZoneId.from("prod", "us-north-1"));
        var zone2 = new DeploymentId(instance2, ZoneId.from("prod", "us-north-1"));
        var tests1 = Map.of(
                // With default cluster
                "a1.t1.us-north-1.prod",
                Endpoint.of(instance1).target(EndpointId.defaultId(), ClusterSpec.Id.from("default"), List.of(zone)).on(Port.tls()).in(SystemName.main),

                // With non-default cluster
                "c1.a1.t1.us-north-1.prod",
                Endpoint.of(instance1).target(EndpointId.of("ignored1"), ClusterSpec.Id.from("c1"), List.of(zone)).on(Port.tls()).in(SystemName.main),

                // With application endpoint
                "c2.a1.t1.us-north-1.prod",
                Endpoint.of(app1).targetApplication(EndpointId.defaultId(), ClusterSpec.Id.from("c2"), Map.of(new DeploymentId(app1.instance("i1"), zone.zoneId()), 1))
                        .routingMethod(RoutingMethod.sharedLayer4)
                        .on(Port.tls())
                        .in(SystemName.main)
        );
        var tests2 = Map.of(
                // With non-default instance and default cluster
                "i2.a2.t2.us-north-1.prod",
                Endpoint.of(instance2).target(EndpointId.defaultId(), ClusterSpec.Id.from("default"), List.of(zone2)).on(Port.tls()).in(SystemName.main),

                // With non-default instance and cluster
                "c2.i2.a2.t2.us-north-1.prod",
                Endpoint.of(instance2).target(EndpointId.of("ignored2"), ClusterSpec.Id.from("c2"), List.of(zone2)).on(Port.tls()).in(SystemName.main)
        );
        tests1.forEach((expected, endpoint) -> assertEquals(expected, endpoint.upstreamName(zone)));
        tests2.forEach((expected, endpoint) -> assertEquals(expected, endpoint.upstreamName(zone2)));
    }

    @Test
    public void generated_id() {
        GeneratedEndpoint ge1 = new GeneratedEndpoint("cafed00d", "deadbeef", AuthMethod.mtls, Optional.empty());
        GeneratedEndpoint ge2 = new GeneratedEndpoint("dead2bad", "deadbeef", AuthMethod.mtls, Optional.of(EndpointId.of("foo")));
        var deployment = new DeploymentId(instance1, ZoneId.from("prod", "us-north-1"));
        var tests = Map.of(
                // Zone endpoint in main, unlike named endpoints, this includes the scope symbol 'z'
                "cafed00d.deadbeef.z.vespa.oath.cloud",
                Endpoint.of(instance1).target(ClusterSpec.Id.from("c1"), deployment).generatedFrom(ge1)
                        .routingMethod(RoutingMethod.sharedLayer4).on(Port.tls()).in(SystemName.main),
                // Zone endpoint in public
                "cafed00d.deadbeef.z.vespa-app.cloud",
                Endpoint.of(instance1).target(ClusterSpec.Id.from("c1"), deployment).generatedFrom(ge1)
                        .routingMethod(RoutingMethod.exclusive).on(Port.tls()).in(SystemName.Public),
                // Global endpoint in public
                "dead2bad.deadbeef.g.vespa-app.cloud",
                Endpoint.of(instance1).target(EndpointId.of("foo"), ClusterSpec.Id.from("c1"), List.of(deployment))
                        .generatedFrom(ge2)
                        .routingMethod(RoutingMethod.exclusive).on(Port.tls()).in(SystemName.Public),
                // Application endpoint in public
                "dead2bad.deadbeef.a.vespa-app.cloud",
                Endpoint.of(TenantAndApplicationId.from(instance1)).targetApplication(EndpointId.of("foo"), deployment)
                        .generatedFrom(ge2)
                        .routingMethod(RoutingMethod.exclusive).on(Port.tls()).in(SystemName.Public)
        );
        tests.forEach((expected, endpoint) -> assertEquals(expected, endpoint.dnsName()));
    }

}