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
|
// Copyright 2020 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.node.admin.nodeagent;
import com.yahoo.config.provision.CloudName;
import com.yahoo.config.provision.SystemName;
import com.yahoo.config.provision.zone.ZoneApi;
import com.yahoo.config.provision.zone.ZoneId;
import com.yahoo.vespa.athenz.api.AthenzIdentity;
import com.yahoo.vespa.athenz.api.AthenzService;
import com.yahoo.vespa.hosted.dockerapi.ContainerName;
import com.yahoo.vespa.hosted.node.admin.configserver.noderepository.Acl;
import com.yahoo.vespa.hosted.node.admin.configserver.noderepository.NodeSpec;
import com.yahoo.vespa.hosted.node.admin.docker.DockerNetworking;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* @author freva
*/
public class NodeAgentContextImpl implements NodeAgentContext {
private static final Path ROOT = Paths.get("/");
private final String logPrefix;
private final NodeSpec node;
private final Acl acl;
private final ContainerName containerName;
private final AthenzIdentity identity;
private final DockerNetworking dockerNetworking;
private final ZoneApi zone;
private final Path pathToNodeRootOnHost;
private final Path pathToVespaHome;
private final String vespaUser;
private final String vespaUserOnHost;
private final double cpuSpeedup;
public NodeAgentContextImpl(NodeSpec node, Acl acl, AthenzIdentity identity,
DockerNetworking dockerNetworking, ZoneApi zone,
Path pathToContainerStorage, Path pathToVespaHome,
String vespaUser, String vespaUserOnHost, double cpuSpeedup) {
if (cpuSpeedup <= 0)
throw new IllegalArgumentException("cpuSpeedUp must be positive, was: " + cpuSpeedup);
this.node = Objects.requireNonNull(node);
this.acl = Objects.requireNonNull(acl);
this.containerName = ContainerName.fromHostname(node.hostname());
this.identity = Objects.requireNonNull(identity);
this.dockerNetworking = Objects.requireNonNull(dockerNetworking);
this.zone = Objects.requireNonNull(zone);
this.pathToNodeRootOnHost = Objects.requireNonNull(pathToContainerStorage).resolve(containerName.asString());
this.pathToVespaHome = Objects.requireNonNull(pathToVespaHome);
this.logPrefix = containerName.asString() + ": ";
this.vespaUser = vespaUser;
this.vespaUserOnHost = vespaUserOnHost;
this.cpuSpeedup = cpuSpeedup;
}
@Override
public NodeSpec node() {
return node;
}
@Override
public Acl acl() {
return acl;
}
@Override
public ContainerName containerName() {
return containerName;
}
@Override
public AthenzIdentity identity() {
return identity;
}
@Override
public DockerNetworking dockerNetworking() {
return dockerNetworking;
}
@Override
public ZoneApi zone() {
return zone;
}
@Override
public String vespaUser() {
return vespaUser;
}
@Override
public String vespaUserOnHost() {
return vespaUserOnHost;
}
@Override
public double unscaledVcpu() {
return node.vcpu() / cpuSpeedup;
}
@Override
public Path pathOnHostFromPathInNode(Path pathInNode) {
if (! pathInNode.isAbsolute())
throw new IllegalArgumentException("Expected an absolute path in the container, got: " + pathInNode);
return pathToNodeRootOnHost.resolve(ROOT.relativize(pathInNode).toString());
}
@Override
public Path pathOnHostFromPathInNode(String pathInNode) {
// Ensure the path is on the proper FileSystem
return pathOnHostFromPathInNode(ROOT.getFileSystem().getPath(pathInNode));
}
@Override
public Path pathInNodeFromPathOnHost(Path pathOnHost) {
if (! pathOnHost.isAbsolute())
throw new IllegalArgumentException("Expected an absolute path on the host, got: " + pathOnHost);
if (!pathOnHost.startsWith(pathToNodeRootOnHost))
throw new IllegalArgumentException("Path " + pathOnHost + " does not exist in the container");
return ROOT.resolve(pathToNodeRootOnHost.relativize(pathOnHost).toString());
}
@Override
public Path pathInNodeFromPathOnHost(String pathOnHost) {
// Ensure the path is on the proper FileSystem
return pathInNodeFromPathOnHost(pathToNodeRootOnHost.getFileSystem().getPath(pathOnHost));
}
@Override
public Path pathInNodeUnderVespaHome(Path relativePath) {
if (relativePath.isAbsolute())
throw new IllegalArgumentException("Expected a relative path to the Vespa home, got: " + relativePath);
return relativePath.getFileSystem().getPath(pathToVespaHome.resolve(relativePath.toString()).toString());
}
@Override
public Path pathInNodeUnderVespaHome(String relativePath) {
// Ensure the path is on the proper FileSystem
return pathInNodeUnderVespaHome(pathToVespaHome.getFileSystem().getPath(relativePath));
}
@Override
public void recordSystemModification(Logger logger, String message) {
log(logger, message);
}
@Override
public void log(Logger logger, Level level, String message) {
logger.log(level, logPrefix + message);
}
@Override
public void log(Logger logger, Level level, String message, Throwable throwable) {
logger.log(level, logPrefix + message, throwable);
}
@Override
public String toString() {
return "NodeAgentContextImpl{" +
"node=" + node +
", acl=" + acl +
", containerName=" + containerName +
", identity=" + identity +
", dockerNetworking=" + dockerNetworking +
", zone=" + zone +
", pathToNodeRootOnHost=" + pathToNodeRootOnHost +
", pathToVespaHome=" + pathToVespaHome +
", vespaUser='" + vespaUser + '\'' +
", vespaUserOnHost='" + vespaUserOnHost + '\'' +
'}';
}
/** For testing only! */
public static class Builder {
private NodeSpec.Builder nodeSpecBuilder;
private Acl acl;
private AthenzIdentity identity;
private DockerNetworking dockerNetworking;
private ZoneApi zone;
private Path pathToContainerStorage;
private Path pathToVespaHome;
private String vespaUser;
private String vespaUserOnHost;
private FileSystem fileSystem = FileSystems.getDefault();
private double cpuSpeedUp = 1;
public Builder(NodeSpec node) {
this.nodeSpecBuilder = new NodeSpec.Builder(node);
}
/**
* Creates a NodeAgentContext.Builder with a NodeSpec that has the given hostname and some
* reasonable values for the remaining required NodeSpec fields. Use {@link #Builder(NodeSpec)}
* if you want to control the entire NodeSpec.
*/
public Builder(String hostname) {
this.nodeSpecBuilder = NodeSpec.Builder.testSpec(hostname);
}
public Builder nodeSpecBuilder(Function<NodeSpec.Builder, NodeSpec.Builder> nodeSpecBuilderModifier) {
this.nodeSpecBuilder = nodeSpecBuilderModifier.apply(nodeSpecBuilder);
return this;
}
public Builder acl(Acl acl) {
this.acl = acl;
return this;
}
public Builder identity(AthenzIdentity identity) {
this.identity = identity;
return this;
}
public Builder dockerNetworking(DockerNetworking dockerNetworking) {
this.dockerNetworking = dockerNetworking;
return this;
}
public Builder zone(ZoneApi zone) {
this.zone = zone;
return this;
}
public Builder pathToContainerStorageFromFileSystem(FileSystem fileSystem) {
this.pathToContainerStorage = fileSystem.getPath("/home/docker");
return this;
}
public Builder pathToVespaHome(Path pathToVespaHome) {
this.pathToVespaHome = pathToVespaHome;
return this;
}
public Builder vespaUser(String vespaUser) {
this.vespaUser = vespaUser;
return this;
}
public Builder vespaUserOnHost(String vespaUserOnHost) {
this.vespaUserOnHost = vespaUserOnHost;
return this;
}
/**
* Sets the default file system to use for paths. May be overridden for each path,
* e.g. {@link #pathToVespaHome(Path)} pathToVespaHome()}.
*/
public Builder fileSystem(FileSystem fileSystem) {
this.fileSystem = fileSystem;
return this;
}
public Builder cpuSpeedUp(double cpuSpeedUp) {
this.cpuSpeedUp = cpuSpeedUp;
return this;
}
public NodeAgentContextImpl build() {
return new NodeAgentContextImpl(
nodeSpecBuilder.build(),
Optional.ofNullable(acl).orElse(Acl.EMPTY),
Optional.ofNullable(identity).orElseGet(() -> new AthenzService("domain", "service")),
Optional.ofNullable(dockerNetworking).orElse(DockerNetworking.HOST_NETWORK),
Optional.ofNullable(zone).orElseGet(() -> new ZoneApi() {
@Override
public SystemName getSystemName() {
return SystemName.defaultSystem();
}
@Override
public ZoneId getId() {
return ZoneId.defaultId();
}
@Override
public CloudName getCloudName() {
return CloudName.defaultName();
}
@Override
public String getCloudNativeRegionName() {
return getId().region().value();
}
}),
Optional.ofNullable(pathToContainerStorage).orElseGet(() -> fileSystem.getPath("/home/docker")),
Optional.ofNullable(pathToVespaHome).orElseGet(() -> fileSystem.getPath("/opt/vespa")),
Optional.ofNullable(vespaUser).orElse("vespa"),
Optional.ofNullable(vespaUserOnHost).orElse("container_vespa"),
cpuSpeedUp);
}
}
}
|