Skip to content

Commit 7a49788

Browse files
committed
sample with smallrye config
Signed-off-by: Attila Mészáros <a_meszaros@apple.com>
1 parent b6e03a1 commit 7a49788

File tree

5 files changed

+146
-2
lines changed

5 files changed

+146
-2
lines changed

operator-framework/src/main/java/io/javaoperatorsdk/operator/config/loader/ConfigLoader.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
import java.util.Optional;
2121
import java.util.function.Consumer;
2222

23+
import org.slf4j.Logger;
24+
import org.slf4j.LoggerFactory;
25+
2326
import io.fabric8.kubernetes.api.model.HasMetadata;
2427
import io.javaoperatorsdk.operator.api.config.ConfigurationServiceOverrider;
2528
import io.javaoperatorsdk.operator.api.config.ControllerConfigurationOverrider;
@@ -30,6 +33,8 @@
3033

3134
public class ConfigLoader {
3235

36+
private static final Logger log = LoggerFactory.getLogger(ConfigLoader.class);
37+
3338
private static final ConfigLoader DEFAULT = new ConfigLoader();
3439

3540
public static ConfigLoader getDefault() {
@@ -251,7 +256,13 @@ private <O> Consumer<O> buildConsumer(List<ConfigBinding<O, ?>> bindings, String
251256
private <O, T> Consumer<O> resolveStep(ConfigBinding<O, T> binding, String key) {
252257
return configProvider
253258
.getValue(key, binding.type())
254-
.map(value -> (Consumer<O>) overrider -> binding.setter().accept(overrider, value))
259+
.map(
260+
value ->
261+
(Consumer<O>)
262+
overrider -> {
263+
log.debug("Found config property: {} = {}", key, value);
264+
binding.setter().accept(overrider, value);
265+
})
255266
.orElse(null);
256267
}
257268
}

sample-operators/tomcat-operator/pom.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@
3939
<type>pom</type>
4040
<scope>import</scope>
4141
</dependency>
42+
<dependency>
43+
<groupId>io.smallrye.config</groupId>
44+
<artifactId>smallrye-config-bom</artifactId>
45+
<version>3.16.0</version>
46+
<type>pom</type>
47+
<scope>import</scope>
48+
</dependency>
4249
</dependencies>
4350
</dependencyManagement>
4451

@@ -92,6 +99,22 @@
9299
<artifactId>operator-framework-junit</artifactId>
93100
<scope>test</scope>
94101
</dependency>
102+
103+
<dependency>
104+
<groupId>io.smallrye.config</groupId>
105+
<artifactId>smallrye-config</artifactId>
106+
<version>3.11.4</version>
107+
</dependency>
108+
<dependency>
109+
<groupId>io.smallrye.config</groupId>
110+
<artifactId>smallrye-config-source-yaml</artifactId>
111+
<version>3.11.4</version>
112+
</dependency>
113+
<dependency>
114+
<groupId>org.eclipse.microprofile.config</groupId>
115+
<artifactId>microprofile-config-api</artifactId>
116+
<version>3.1</version>
117+
</dependency>
95118
</dependencies>
96119

97120
<build>

sample-operators/tomcat-operator/src/main/java/io/javaoperatorsdk/operator/sample/TomcatOperator.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package io.javaoperatorsdk.operator.sample;
1717

1818
import java.io.IOException;
19+
import java.net.URL;
1920

2021
import org.takes.facets.fork.FkRegex;
2122
import org.takes.facets.fork.TkFork;
@@ -24,11 +25,23 @@
2425

2526
import io.javaoperatorsdk.operator.Operator;
2627
import io.javaoperatorsdk.operator.config.loader.ConfigLoader;
28+
import io.javaoperatorsdk.operator.sample.smallryeconfig.SmallryeConfigProvider;
29+
import io.smallrye.config.SmallRyeConfigBuilder;
30+
import io.smallrye.config.source.yaml.YamlConfigSource;
2731

2832
public class TomcatOperator {
2933

3034
public static void main(String[] args) throws IOException {
31-
var configLoader = ConfigLoader.getDefault();
35+
36+
URL configUrl = TomcatOperator.class.getResource("/application.yaml");
37+
if (configUrl == null) {
38+
throw new IllegalStateException("application.yaml not found on classpath");
39+
}
40+
var configLoader =
41+
new ConfigLoader(
42+
new SmallryeConfigProvider(
43+
new SmallRyeConfigBuilder().withSources(new YamlConfigSource(configUrl)).build()));
44+
3245
Operator operator = new Operator(configLoader.applyConfigs());
3346
operator.register(
3447
new TomcatReconciler(), configLoader.applyControllerConfigs(TomcatReconciler.NAME));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright Java Operator SDK Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.javaoperatorsdk.operator.sample.smallryeconfig;
17+
18+
import java.util.Optional;
19+
20+
import io.javaoperatorsdk.operator.config.loader.ConfigProvider;
21+
import io.smallrye.config.SmallRyeConfig;
22+
23+
public class SmallryeConfigProvider implements ConfigProvider {
24+
25+
private final SmallRyeConfig smallRyeConfig;
26+
27+
public SmallryeConfigProvider(SmallRyeConfig smallRyeConfig) {
28+
this.smallRyeConfig = smallRyeConfig;
29+
}
30+
31+
@Override
32+
public <T> Optional<T> getValue(String key, Class<T> type) {
33+
return smallRyeConfig.getOptionalValue(key, type);
34+
}
35+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#
2+
# Copyright Java Operator SDK Authors
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
17+
# JOSDK operator-level configuration (josdk.<property>)
18+
josdk:
19+
check-crd: true
20+
close-client-on-stop: true
21+
use-ssa-to-patch-primary-resource: false
22+
clone-secondary-resources-when-getting-from-cache: false
23+
reconciliation:
24+
termination-timeout: PT30S
25+
concurrent-threads: 10
26+
workflow:
27+
executor-threads: 10
28+
informer:
29+
stop-on-error-during-startup: true
30+
cache-sync-timeout: PT2M
31+
dependent-resources:
32+
ssa-based-create-update-match: true
33+
34+
# Controller-level configuration (josdk.controller.<name>.<property>)
35+
controller:
36+
tomcat:
37+
finalizer: tomcat.sample.javaoperatorsdk.io/finalizer
38+
generation-aware: true
39+
label-selector: ""
40+
max-reconciliation-interval: PT10M
41+
field-manager: tomcat-controller
42+
trigger-reconciler-on-all-events: false
43+
informer-list-limit: 500
44+
retry:
45+
max-attempts: 10
46+
initial-interval: 2000
47+
interval-multiplier: 1.5
48+
max-interval: 60000
49+
50+
webapp:
51+
finalizer: webapp.sample.javaoperatorsdk.io/finalizer
52+
generation-aware: true
53+
label-selector: ""
54+
max-reconciliation-interval: PT10M
55+
field-manager: webapp-controller
56+
trigger-reconciler-on-all-events: false
57+
informer-list-limit: 500
58+
retry:
59+
max-attempts: 10
60+
initial-interval: 2000
61+
interval-multiplier: 1.5
62+
max-interval: 60000

0 commit comments

Comments
 (0)