generated from quarkiverse/quarkiverse-template
-
Notifications
You must be signed in to change notification settings - Fork 49
/
OperatorSDKResource.java
221 lines (176 loc) · 6.68 KB
/
OperatorSDKResource.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
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
package io.quarkiverse.operatorsdk.it;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import javax.enterprise.inject.Instance;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.javaoperatorsdk.operator.api.config.ControllerConfiguration;
import io.javaoperatorsdk.operator.api.config.RetryConfiguration;
import io.javaoperatorsdk.operator.api.config.Version;
import io.javaoperatorsdk.operator.api.config.dependent.DependentResourceSpec;
import io.javaoperatorsdk.operator.api.reconciler.Reconciler;
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.KubernetesDependentResourceConfig;
import io.javaoperatorsdk.operator.processing.retry.Retry;
import io.quarkiverse.operatorsdk.runtime.QuarkusConfigurationService;
import io.quarkus.runtime.annotations.RegisterForReflection;
@SuppressWarnings("unused")
@Path("/operator")
public class OperatorSDKResource {
@Inject
Instance<Reconciler<? extends HasMetadata>> controllers;
@Inject
QuarkusConfigurationService configurationService;
@GET
@Path("config")
public JSONConfiguration config() {
return new JSONConfiguration(configurationService);
}
@GET
@Path("{name}")
public boolean getController(@PathParam("name") String name) {
return configurationService.getKnownReconcilerNames().contains(name);
}
@GET
@Path("controllers")
@Produces("application/json")
public Set<String> getControllerNames() {
return config().getKnownControllerNames();
}
@GET
@Path("{name}/config")
public JSONControllerConfiguration getConfig(@PathParam("name") String name) {
return controllers.stream()
.map(c -> configurationService.getConfigurationFor(c))
.filter(c -> c.getName().equals(name))
.findFirst()
.map(JSONControllerConfiguration::new)
.orElse(null);
}
static class JSONConfiguration {
private final QuarkusConfigurationService conf;
public JSONConfiguration(QuarkusConfigurationService conf) {
this.conf = conf;
}
public Set<String> getKnownControllerNames() {
return conf.getKnownReconcilerNames();
}
public Version getVersion() {
return conf.getVersion();
}
@JsonProperty("validate")
public boolean validate() {
return conf.checkCRDAndValidateLocalModel();
}
@JsonProperty("maxThreads")
public int concurrentReconciliationThreads() {
return conf.concurrentReconciliationThreads();
}
@JsonProperty("timeout")
public int timeout() {
return conf.getTerminationTimeoutSeconds();
}
@JsonProperty("applyCRDs")
public boolean apply() {
return conf.getCRDGenerationInfo().isApplyCRDs();
}
}
static class JSONControllerConfiguration {
private final ControllerConfiguration<?> conf;
public JSONControllerConfiguration(ControllerConfiguration<?> conf) {
this.conf = conf;
}
public String getName() {
return conf.getName();
}
@JsonProperty("crdName")
public String getCRDName() {
return conf.getResourceTypeName();
}
public String getFinalizer() {
return conf.getFinalizerName();
}
public boolean isGenerationAware() {
return conf.isGenerationAware();
}
public String getCustomResourceClass() {
return conf.getResourceClass().getCanonicalName();
}
public String getAssociatedControllerClassName() {
return conf.getAssociatedReconcilerClassName();
}
public String[] getNamespaces() {
return conf.getNamespaces().toArray(new String[0]);
}
@JsonProperty("watchAllNamespaces")
public boolean watchAllNamespaces() {
return conf.watchAllNamespaces();
}
@JsonProperty("watchCurrentNamespace")
public boolean watchCurrentNamespace() {
return conf.watchCurrentNamespace();
}
public RetryConfiguration getRetryConfiguration() {
return conf.getRetryConfiguration();
}
public Retry getRetry() {
return conf.getRetry();
}
public String getLabelSelector() {
return conf.getLabelSelector();
}
public List<JSONDependentResourceSpec> getDependents() {
final var dependents = conf.getDependentResources();
final var result = new ArrayList<JSONDependentResourceSpec>(dependents.size());
return dependents.stream()
.map(JSONDependentResourceSpec::new)
.collect(Collectors.toList());
}
@JsonProperty("maxReconciliationIntervalSeconds")
public long maxReconciliationIntervalSeconds() {
return conf.maxReconciliationInterval().map(Duration::getSeconds).orElseThrow();
}
}
static class JSONDependentResourceSpec {
private final DependentResourceSpec<?, ?> spec;
JSONDependentResourceSpec(DependentResourceSpec<?, ?> spec) {
this.spec = spec;
}
public String getDependentClass() {
return spec.getDependentResourceClass().getCanonicalName();
}
public Object getDependentConfig() {
return spec.getDependentResourceConfiguration()
.map(KubernetesDependentResourceConfig.class::cast)
.map(JSONKubernetesResourceConfig::new)
.orElse(null);
}
public String getName() {
return spec.getName();
}
}
// needed for native tests, see https://quarkus.io/guides/writing-native-applications-tips#registering-for-reflection
@RegisterForReflection
static class JSONKubernetesResourceConfig {
private final KubernetesDependentResourceConfig<?> config;
JSONKubernetesResourceConfig(KubernetesDependentResourceConfig<?> config) {
this.config = config;
}
public String getOnAddFilter() {
return Optional.ofNullable(config.onAddFilter())
.map(f -> f.getClass().getCanonicalName())
.orElse(null);
}
public String getLabelSelector() {
return config.labelSelector();
}
}
}