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
|
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.provision.lb;
import ai.vespa.http.DomainName;
import com.google.common.collect.ImmutableSet;
import com.yahoo.config.provision.ClusterSpec;
import com.yahoo.config.provision.EndpointsChecker.Availability;
import com.yahoo.config.provision.EndpointsChecker.Endpoint;
import com.yahoo.config.provision.NodeType;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* @author mpolden
*/
public class LoadBalancerServiceMock implements LoadBalancerService {
private final Map<LoadBalancerId, LoadBalancerInstance> instances = new HashMap<>();
private boolean throwOnCreate = false;
private boolean supportsProvisioning = true;
private final AtomicBoolean uuid = new AtomicBoolean(true);
public Map<LoadBalancerId, LoadBalancerInstance> instances() {
return Collections.unmodifiableMap(instances);
}
public LoadBalancerServiceMock throwOnCreate(boolean throwOnCreate) {
this.throwOnCreate = throwOnCreate;
return this;
}
public LoadBalancerServiceMock supportsProvisioning(boolean supportsProvisioning) {
this.supportsProvisioning = supportsProvisioning;
return this;
}
@Override
public boolean supports(NodeType nodeType, ClusterSpec.Type clusterType) {
if (!supportsProvisioning) return false;
return (nodeType == NodeType.tenant && clusterType.isContainer()) ||
nodeType.isConfigServerLike();
}
@Override
public Protocol protocol(boolean enclave) {
return Protocol.ipv4;
}
@Override
public LoadBalancerInstance provision(LoadBalancerSpec spec) {
if (throwOnCreate) throw new IllegalStateException("Did not expect a new load balancer to be created");
Optional<UUID> idSeed = uuid.getAndSet(false) ? Optional.of(UUID.fromString("c11272ab-d20e-4c86-b808-ffedaa00c480")) : Optional.empty();
var id = new LoadBalancerId(spec.application(), spec.cluster());
var instance = new LoadBalancerInstance(
idSeed,
Optional.of(DomainName.of("lb-" + spec.application().toShortString() + "-" + spec.cluster().value())),
Optional.empty(),
Optional.empty(),
Optional.of(new DnsZone("zone-id-1")),
Collections.singleton(4443),
ImmutableSet.of("10.2.3.0/24", "10.4.5.0/24"),
spec.reals(),
spec.settings(),
spec.settings().isPrivateEndpoint() ? List.of(PrivateServiceId.of("service")) : List.of(),
spec.cloudAccount());
instances.put(id, instance);
return instance;
}
@Override
public LoadBalancerInstance configure(LoadBalancerInstance instance, LoadBalancerSpec spec, boolean force) {
var id = new LoadBalancerId(spec.application(), spec.cluster());
var oldInstance = Objects.requireNonNull(instances.get(id), "expected existing load balancer " + id);
if (!force && !oldInstance.reals().isEmpty() && spec.reals().isEmpty()) {
throw new IllegalArgumentException("Refusing to remove all reals from load balancer " + id);
}
var updated = instance.with(spec.reals(),
spec.settings(),
spec.settings().isPrivateEndpoint() ? Optional.of(PrivateServiceId.of("service")) : Optional.empty());
instances.put(id, updated);
return updated;
}
@Override
public void remove(LoadBalancer loadBalancer) {
instances.remove(loadBalancer.id());
}
@Override
public Availability healthy(Endpoint endpoint, Optional<UUID> idSeed) {
return Availability.ready;
}
}
|