-
Notifications
You must be signed in to change notification settings - Fork 26
/
OperatorManagedResource.java
96 lines (78 loc) · 2.84 KB
/
OperatorManagedResource.java
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
package io.quarkus.test.services.operator;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import io.quarkus.test.bootstrap.ManagedResource;
import io.quarkus.test.bootstrap.OpenShiftExtensionBootstrap;
import io.quarkus.test.bootstrap.OperatorService;
import io.quarkus.test.bootstrap.Protocol;
import io.quarkus.test.bootstrap.ServiceContext;
import io.quarkus.test.bootstrap.inject.OpenShiftClient;
import io.quarkus.test.services.URILike;
import io.quarkus.test.services.operator.model.CustomResourceDefinition;
import io.quarkus.test.utils.FileUtils;
public class OperatorManagedResource implements ManagedResource {
private final OperatorManagedResourceBuilder model;
private final OpenShiftClient client;
private final List<CustomResourceDefinition> crdsToWatch = new ArrayList<>();
private boolean running;
public OperatorManagedResource(OperatorManagedResourceBuilder model) {
this.model = model;
this.client = model.getContext().get(OpenShiftExtensionBootstrap.CLIENT);
}
@Override
public String getDisplayName() {
return model.getName();
}
@Override
public void start() {
if (!running) {
installOperator();
applyCRDs();
running = true;
}
}
@Override
public void stop() {
// Stop method in operators is not supported yet
}
@Override
public boolean isRunning() {
return running && customResourcesAreReady();
}
@Override
public URILike getURI(Protocol protocol) {
throw new UnsupportedOperationException("Operator does not expose services.");
}
@Override
public List<String> logs() {
// Logs in operators is not supported yet
return Collections.emptyList();
}
private void applyCRDs() {
if (model.getContext().getOwner() instanceof OperatorService<?> service) {
for (var crd : service.getCrds()) {
applyCRD(crd);
}
}
}
private void applyCRD(CustomResourceDefinition crd) {
ServiceContext serviceContext = model.getContext();
Path crdFileDefinition = serviceContext.getServiceFolder().resolve(crd.getName());
String content = FileUtils.loadFile(crd.getFile());
FileUtils.copyContentTo(content, crdFileDefinition);
client.apply(crdFileDefinition);
crdsToWatch.add(crd);
}
private boolean customResourcesAreReady() {
return crdsToWatch.stream().allMatch(crd -> client.isCustomResourceReady(crd.getName(), crd.getType()));
}
private void installOperator() {
client.installOperator(model.getContext().getOwner(),
model.getName(),
model.getChannel(),
model.getSource(),
model.getSourceNamespace());
}
}