aboutsummaryrefslogtreecommitdiffstats
path: root/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/lb/LoadBalancerServiceMock.java
blob: 0807d13d7a809aa1dd5939ee0a0ba4e354855667 (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
// 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.ApplicationId;
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 static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.toMap;

/**
 * @author mpolden
 */
public class LoadBalancerServiceMock implements LoadBalancerService {

    private record Key(ApplicationId application, ClusterSpec.Id cluster, String idSeed) {
        @Override public int hashCode() { return idSeed.hashCode(); }
        @Override public boolean equals(Object o) {
            if (o == this) return true;
            if ( ! (o instanceof Key key)) return false;
            return Objects.equals(idSeed, key.idSeed);
        }
    }
    private final Map<Key, LoadBalancerInstance> instances = new HashMap<>();
    private boolean throwOnCreate = false;
    private boolean supportsProvisioning = true;

    public Map<LoadBalancerId, LoadBalancerInstance> instances() {
        return instances.entrySet().stream().collect(toMap(e -> new LoadBalancerId(e.getKey().application, e.getKey().cluster),
                                                           Map.Entry::getValue));
    }

    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");
        var instance = new LoadBalancerInstance(
                Optional.of(DomainName.of("lb-" + spec.application().toShortString().replaceAll("_", "--") + "-" + 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(new Key(spec.application(), spec.cluster(), spec.idSeed()), instance);
        return instance;
    }

    @Override
    public LoadBalancerInstance configure(LoadBalancerInstance instance, LoadBalancerSpec spec, boolean force) {
        var id = new Key(spec.application(), spec.cluster(), spec.idSeed());
        var oldInstance = 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 reallocate(LoadBalancerSpec spec) {
        instances.put(new Key(spec.application(), spec.cluster(), spec.idSeed()),
                      requireNonNull(instances.remove(new Key(null, null, spec.idSeed())))); // ᕙ༼◕_◕༽ᕤ
    }

    @Override
    public void remove(LoadBalancer loadBalancer) {
        requireNonNull(instances.remove(new Key(loadBalancer.id().application(),
                                                loadBalancer.id().cluster(),
                                                loadBalancer.idSeed())),
                       "expected load balancer to exist: " + loadBalancer.id());
    }

    @Override
    public Availability healthy(Endpoint endpoint, String idSeed) {
        return Availability.ready;
    }

}