aboutsummaryrefslogtreecommitdiffstats
path: root/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/component/Environment.java
blob: aaadd3cb24e9137534a63514d6dd611376a4e2f4 (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
// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.node.admin.component;

import com.yahoo.config.provision.NodeType;
import com.yahoo.vespa.athenz.api.AthenzService;
import com.yahoo.vespa.hosted.dockerapi.ContainerName;
import com.yahoo.vespa.hosted.node.admin.config.ConfigServerConfig;
import com.yahoo.vespa.hosted.node.admin.docker.DockerNetworking;
import com.yahoo.vespa.hosted.node.admin.task.util.network.IPAddresses;
import com.yahoo.vespa.hosted.node.admin.task.util.network.IPAddressesImpl;

import java.net.URI;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * Various utilities for getting values from node-admin's environment. Immutable.
 *
 * @author Øyvind Bakksjø
 * @author hmusum
 */
public class Environment {
    private final ConfigServerInfo configServerInfo;
    private final String environment;
    private final String region;
    private final String system;
    private final String cloud;
    private final String parentHostHostname;
    private final IPAddresses ipAddresses;
    private final PathResolver pathResolver;
    private final List<String> logstashNodes;
    private final NodeType nodeType;
    private final ContainerEnvironmentResolver containerEnvironmentResolver;
    private final String certificateDnsSuffix;
    private final URI ztsUri;
    private final AthenzService nodeAthenzIdentity;
    private final boolean nodeAgentCertEnabled;
    private final Path trustStorePath;
    private final DockerNetworking dockerNetworking;

    private Environment(ConfigServerInfo configServerInfo,
                        Path trustStorePath,
                        String environment,
                        String region,
                        String system,
                        String cloud,
                        String parentHostHostname,
                        IPAddresses ipAddresses,
                        PathResolver pathResolver,
                        List<String> logstashNodes,
                        NodeType nodeType,
                        ContainerEnvironmentResolver containerEnvironmentResolver,
                        String certificateDnsSuffix,
                        URI ztsUri,
                        AthenzService nodeAthenzIdentity,
                        boolean nodeAgentCertEnabled,
                        DockerNetworking dockerNetworking) {
        this.configServerInfo = Objects.requireNonNull(configServerInfo, "configServerConfig cannot be null");
        this.environment = Objects.requireNonNull(environment, "environment cannot be null");;
        this.region = Objects.requireNonNull(region, "region cannot be null");;
        this.system = Objects.requireNonNull(system, "system cannot be null");;
        this.cloud = Objects.requireNonNull(cloud, "cloud cannot be null");
        this.parentHostHostname = parentHostHostname;
        this.ipAddresses = ipAddresses;
        this.pathResolver = pathResolver;
        this.logstashNodes = logstashNodes;
        this.nodeType = nodeType;
        this.containerEnvironmentResolver = containerEnvironmentResolver;
        this.certificateDnsSuffix = certificateDnsSuffix;
        this.ztsUri = ztsUri;
        this.nodeAthenzIdentity = nodeAthenzIdentity;
        this.nodeAgentCertEnabled = nodeAgentCertEnabled;
        this.trustStorePath = trustStorePath;
        this.dockerNetworking = Objects.requireNonNull(dockerNetworking, "dockerNetworking cannot be null");
    }

    public List<String> getConfigServerHostNames() { return configServerInfo.getConfigServerHostNames(); }

    public String getEnvironment() { return environment; }

    public String getRegion() {
        return region;
    }

    public String getSystem() {
        return system;
    }

    public String getCloud() { return cloud; }

    public String getParentHostHostname() {
        return parentHostHostname;
    }

    public String getZone() {
        return getEnvironment() + "." + getRegion();
    }

    public IPAddresses getIpAddresses() {
        return ipAddresses;
    }

    public PathResolver getPathResolver() {
        return pathResolver;
    }

    /**
     * Translates an absolute path in node agent container to an absolute path in node admin container.
     * @param containerName name of the node agent container
     * @param pathInNode absolute path in that container
     * @return the absolute path in node admin container pointing at the same inode
     */
    public Path pathInNodeAdminFromPathInNode(ContainerName containerName, Path pathInNode) {
        if (! pathInNode.isAbsolute()) {
            throw new IllegalArgumentException("The specified path in node was not absolute: " + pathInNode);
        }

        return pathResolver.getApplicationStoragePathForNodeAdmin()
                .resolve(containerName.asString())
                .resolve(PathResolver.ROOT.relativize(pathInNode));
    }

    /**
     * Translates an absolute path in node agent container to an absolute path in host.
     * @param containerName name of the node agent container
     * @param pathInNode absolute path in that container
     * @return the absolute path in host pointing at the same inode
     */
    public Path pathInHostFromPathInNode(ContainerName containerName, Path pathInNode) {
        if (! pathInNode.isAbsolute()) {
            throw new IllegalArgumentException("The specified path in node was not absolute: " + pathInNode);
        }

        return pathResolver.getApplicationStoragePathForHost()
                .resolve(containerName.asString())
                .resolve(PathResolver.ROOT.relativize(pathInNode));
    }

    public Path pathInNodeUnderVespaHome(String relativePath) {
        return pathResolver.getVespaHomePathForContainer()
                .resolve(relativePath);
    }

    public List<String> getLogstashNodes() {
        return logstashNodes;
    }

    public NodeType getNodeType() { return nodeType; }

    public ContainerEnvironmentResolver getContainerEnvironmentResolver() {
        return containerEnvironmentResolver;
    }

    public Path getTrustStorePath() {
        return trustStorePath;
    }

    public AthenzService getConfigserverAthenzIdentity() {
        return configServerInfo.getConfigServerIdentity();
    }

    public AthenzService getNodeAthenzIdentity() {
        return nodeAthenzIdentity;
    }

    public String getCertificateDnsSuffix() {
        return certificateDnsSuffix;
    }

    public URI getZtsUri() {
        return ztsUri;
    }

    public URI getConfigserverLoadBalancerEndpoint() {
        return configServerInfo.getLoadBalancerEndpoint();
    }

    public boolean isNodeAgentCertEnabled() {
        return nodeAgentCertEnabled;
    }

    public DockerNetworking getDockerNetworking() {
        return dockerNetworking;
    }

    public static class Builder {
        private ConfigServerInfo configServerInfo;
        private String environment;
        private String region;
        private String system;
        private String cloud;
        private String parentHostHostname;
        private IPAddresses ipAddresses;
        private PathResolver pathResolver;
        private List<String> logstashNodes = Collections.emptyList();
        private NodeType nodeType = NodeType.tenant;
        private ContainerEnvironmentResolver containerEnvironmentResolver;
        private String certificateDnsSuffix;
        private URI ztsUri;
        private AthenzService nodeAthenzIdentity;
        private boolean nodeAgentCertEnabled;
        private Path trustStorePath;
        private DockerNetworking dockerNetworking;

        public Builder configServerConfig(ConfigServerConfig configServerConfig) {
            this.configServerInfo = new ConfigServerInfo(configServerConfig);
            return this;
        }

        public Builder configServerInfo(ConfigServerInfo configServerInfo) {
            this.configServerInfo = configServerInfo;
            return this;
        }

        public Builder environment(String environment) {
            this.environment = environment;
            return this;
        }

        public Builder region(String region) {
            this.region = region;
            return this;
        }

        public Builder system(String system) {
            this.system = system;
            return this;
        }

        public Builder cloud(String cloud) {
            this.cloud = cloud;
            return this;
        }

        public Builder parentHostHostname(String parentHostHostname) {
            this.parentHostHostname = parentHostHostname;
            return this;
        }

        public Builder ipAddresses(IPAddresses ipAddresses) {
            this.ipAddresses = ipAddresses;
            return this;
        }

        public Builder pathResolver(PathResolver pathResolver) {
            this.pathResolver = pathResolver;
            return this;
        }

        public Builder containerEnvironmentResolver(ContainerEnvironmentResolver containerEnvironmentResolver) {
            this.containerEnvironmentResolver = containerEnvironmentResolver;
            return this;
        }

        public Builder logstashNodes(List<String> hosts) {
            this.logstashNodes = hosts;
            return this;
        }

        public Builder nodeType(NodeType nodeType) {
            this.nodeType = nodeType;
            return this;
        }

        public Builder certificateDnsSuffix(String certificateDnsSuffix) {
            this.certificateDnsSuffix = certificateDnsSuffix;
            return this;
        }

        public Builder ztsUri(URI ztsUri) {
            this.ztsUri = ztsUri;
            return this;
        }

        public Builder nodeAthenzIdentity(AthenzService nodeAthenzIdentity) {
            this.nodeAthenzIdentity = nodeAthenzIdentity;
            return this;
        }

        public Builder enableNodeAgentCert(boolean nodeAgentCertEnabled) {
            this.nodeAgentCertEnabled = nodeAgentCertEnabled;
            return this;
        }

        public Builder trustStorePath(Path trustStorePath) {
            this.trustStorePath = trustStorePath;
            return this;
        }

        public Builder dockerNetworking(DockerNetworking dockerNetworking) {
            this.dockerNetworking = dockerNetworking;
            return this;
        }

        public Environment build() {
            return new Environment(configServerInfo,
                                   trustStorePath,
                                   environment,
                                   region,
                                   system,
                                   cloud,
                                   parentHostHostname,
                                   Optional.ofNullable(ipAddresses).orElseGet(IPAddressesImpl::new),
                                   Optional.ofNullable(pathResolver).orElseGet(PathResolver::new),
                                   logstashNodes,
                                   nodeType,
                                   Optional.ofNullable(containerEnvironmentResolver).orElseGet(() -> node -> ""),
                                   certificateDnsSuffix,
                                   ztsUri,
                                   nodeAthenzIdentity,
                                   nodeAgentCertEnabled,
                                   dockerNetworking);
        }
    }
}