summaryrefslogtreecommitdiffstats
path: root/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/lb/SharedLoadBalancerService.java
blob: 44fc8cf53b5217063979fad2c58b1693ad92eb38 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.provision.lb;

import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.ClusterSpec;
import com.yahoo.config.provision.HostName;
import com.yahoo.config.provision.NodeType;
import com.yahoo.vespa.hosted.provision.Node;
import com.yahoo.vespa.hosted.provision.NodeList;
import com.yahoo.vespa.hosted.provision.NodeRepository;
import com.yahoo.vespa.hosted.provision.node.IP;

import java.util.Comparator;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * This code mimics provisioning load balancers that already exist in the routing layer.
 * It will just map the load balancer request to the proxy nodes available in the node repository.
 *
 * @author ogronnesby
 */
public class SharedLoadBalancerService implements LoadBalancerService {

    private static final Comparator<Node> hostnameComparator = Comparator.comparing(Node::hostname);

    private final NodeRepository nodeRepository;

    public SharedLoadBalancerService(NodeRepository nodeRepository) {
        this.nodeRepository = Objects.requireNonNull(nodeRepository);
    }

    @Override
    public LoadBalancerInstance create(LoadBalancerSpec spec, boolean force) {
        NodeList proxyNodes = nodeRepository.nodes().list().nodeType(NodeType.proxy).sortedBy(hostnameComparator);

        if (proxyNodes.size() == 0) {
            throw new IllegalStateException("Missing proxy nodes in node repository");
        }

        Node firstProxyNode = proxyNodes.first().get();
        Set<String> networks = proxyNodes.stream()
                                         .flatMap(node -> node.ipConfig().primary().stream())
                                         .map(SharedLoadBalancerService::withPrefixLength)
                                         .collect(Collectors.toSet());

        return new LoadBalancerInstance(
                HostName.from(firstProxyNode.hostname()),
                Optional.empty(),
                Set.of(4080, 4443),
                networks,
                spec.reals()
        );
    }

    @Override
    public void remove(ApplicationId application, ClusterSpec.Id cluster) {
        // Do nothing, we have no external state to modify
    }

    @Override
    public Protocol protocol() {
        return Protocol.dualstack;
    }

    @Override
    public boolean supports(NodeType nodeType, ClusterSpec.Type clusterType) {
        // Shared routing layer only supports routing to tenant nodes
        return nodeType == NodeType.tenant && clusterType.isContainer();
    }

    private static String withPrefixLength(String address) {
        if (IP.isV6(address)) {
            return address + "/128";
        }
        return address + "/32";
    }

}