Skip to content

Commit 8ee0b9e

Browse files
committed
cleanup
1 parent 1aa44c8 commit 8ee0b9e

4 files changed

Lines changed: 47 additions & 53 deletions

File tree

spring/boot4-xds/src/main/java/com/linecorp/armeria/spring/xds/SpringConfigSourceFactory.java

Lines changed: 14 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -44,38 +44,17 @@
4444
/**
4545
* A {@link SotwConfigSourceSubscriptionFactory} that reads xDS resources from
4646
* Spring {@link Environment} properties. Each resource name is mapped to a
47-
* property key using a configurable prefix, and the property value is parsed
48-
* as the proto type corresponding to the subscribed {@link XdsType}.
49-
*
50-
* <p>Example properties:
51-
* <pre>{@code
52-
* armeria.xds.my-cluster: |
53-
* name: my-cluster
54-
* type: STATIC
55-
* load_assignment:
56-
* cluster_name: my-cluster
57-
* endpoints:
58-
* - lb_endpoints:
59-
* - endpoint:
60-
* address:
61-
* socket_address:
62-
* address: 127.0.0.1
63-
* port_value: 8080
64-
*
65-
* armeria.xds.my-listener: |
66-
* name: my-listener
67-
* address:
68-
* socket_address:
69-
* address: 0.0.0.0
70-
* port_value: 8080
71-
* }</pre>
47+
* property key of the form {@code <prefix>.<resource-name>}, and the property
48+
* value is parsed as the type corresponding to the subscribed {@link XdsType}.
7249
*
7350
* <p>The property key prefix is configured via a {@link SpringConfigSource}
7451
* packed into the {@code typed_config} field of the bootstrap's
75-
* {@code custom_config_source}.
52+
* {@code custom_config_source}. The default prefix is {@code armeria.xds.}.
7653
*
7754
* <p>Call {@link #refresh()} to re-read properties and push updated resources
7855
* to subscribers (e.g. from a Spring Cloud Config {@code EnvironmentChangeEvent}).
56+
*
57+
* @see SpringXdsAutoConfiguration
7958
*/
8059
@UnstableApi
8160
public final class SpringConfigSourceFactory implements SotwConfigSourceSubscriptionFactory {
@@ -85,11 +64,6 @@ public final class SpringConfigSourceFactory implements SotwConfigSourceSubscrip
8564
*/
8665
public static final String NAME = "armeria.config_source.spring";
8766

88-
/**
89-
* The default prefix prepended to resource names to form property keys.
90-
*/
91-
public static final String DEFAULT_PREFIX = "armeria.xds.";
92-
9367
private static final Object SIGNAL = new Object();
9468

9569
private final Environment environment;
@@ -98,7 +72,11 @@ public final class SpringConfigSourceFactory implements SotwConfigSourceSubscrip
9872
/**
9973
* Creates a new factory backed by the given Spring {@link Environment}.
10074
*/
101-
public SpringConfigSourceFactory(Environment environment) {
75+
public static SpringConfigSourceFactory of(Environment environment) {
76+
return new SpringConfigSourceFactory(environment);
77+
}
78+
79+
private SpringConfigSourceFactory(Environment environment) {
10280
this.environment = requireNonNull(environment, "environment");
10381
}
10482

@@ -111,10 +89,10 @@ public String name() {
11189
public SnapshotStream<DiscoveryResponse> create(ConfigSource configSource,
11290
FactoryContext factoryContext,
11391
SnapshotStream<InterestedResources> interestedResources) {
114-
final String rawPrefix = factoryContext.validator()
115-
.unpack(configSource.getCustomConfigSource().getTypedConfig(),
116-
SpringConfigSource.class)
117-
.getPrefix();
92+
final SpringConfigSource springConfigSource =
93+
factoryContext.validator().unpack(configSource.getCustomConfigSource().getTypedConfig(),
94+
SpringConfigSource.class);
95+
final String rawPrefix = springConfigSource.getPrefix();
11896
final String prefix = rawPrefix.endsWith(".") ? rawPrefix : rawPrefix + '.';
11997
final Map<XdsType, InterestedResources> accumulated = new EnumMap<>(XdsType.class);
12098
final SnapshotStream<Map<XdsType, InterestedResources>> allInterests =

spring/boot4-xds/src/main/java/com/linecorp/armeria/spring/xds/SpringXdsAutoConfiguration.java

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@
3939
* Spring {@link Environment} properties and automatically refreshes on
4040
* {@link EnvironmentChangeEvent}.
4141
*
42+
* <p><b>Note:</b> YAML configuration files ({@code application.yml}) are recommended
43+
* over {@code .properties} files because xDS resource values are multi-line YAML
44+
* that cannot be represented correctly in {@code .properties} format.
45+
*
46+
* <h2>xDS resources</h2>
47+
*
4248
* <p>Each xDS resource is stored as a separate property with the key
4349
* {@code armeria.xds.<resource-name>} containing the resource YAML
4450
* (without {@code @type} wrappers — the type is inferred from the
@@ -61,6 +67,28 @@
6167
* address: 127.0.0.1
6268
* port_value: 8080
6369
* }</pre>
70+
*
71+
* <h2>Bootstrap</h2>
72+
*
73+
* <p>The xDS {@link Bootstrap} is created from the {@code armeria.xds.bootstrap} property.
74+
* If not set, a default bootstrap is loaded from
75+
* {@code META-INF/armeria/xds/default-bootstrap.yml}, which configures both LDS and CDS
76+
* to use {@link SpringConfigSourceFactory} with the default prefix {@code armeria.xds.}.
77+
*
78+
* <p>To customize the bootstrap, set {@code armeria.xds.bootstrap} in your
79+
* {@code application.yml}:
80+
* <pre>{@code
81+
* armeria:
82+
* xds:
83+
* bootstrap: |
84+
* dynamic_resources:
85+
* cds_config:
86+
* custom_config_source:
87+
* name: armeria.config_source.spring
88+
* typed_config:
89+
* "@type": type.googleapis.com/armeria.xds.spring.SpringConfigSource
90+
* prefix: "my.custom.prefix."
91+
* }</pre>
6492
*/
6593
@UnstableApi
6694
@AutoConfiguration
@@ -77,13 +105,12 @@ public class SpringXdsAutoConfiguration {
77105
@Bean
78106
@ConditionalOnMissingBean
79107
SpringConfigSourceFactory springConfigSourceFactory(Environment environment) {
80-
return new SpringConfigSourceFactory(environment);
108+
return SpringConfigSourceFactory.of(environment);
81109
}
82110

83111
@Bean
84112
@ConditionalOnMissingBean
85-
XdsBootstrap xdsBootstrap(Environment environment,
86-
List<XdsExtensionFactory> extensionFactories) {
113+
XdsBootstrap xdsBootstrap(Environment environment, List<XdsExtensionFactory> extensionFactories) {
87114
final String bootstrapYaml = environment.getRequiredProperty(BOOTSTRAP_PROPERTY);
88115
final Bootstrap bootstrap = XdsResourceReader.from(bootstrapYaml, Bootstrap.class);
89116
return XdsBootstrap.builder(bootstrap)
@@ -92,8 +119,7 @@ XdsBootstrap xdsBootstrap(Environment environment,
92119
}
93120

94121
@Bean
95-
ApplicationListener<EnvironmentChangeEvent> springXdsRefreshListener(
96-
SpringConfigSourceFactory factory) {
122+
ApplicationListener<EnvironmentChangeEvent> springXdsRefreshListener(SpringConfigSourceFactory factory) {
97123
return event -> factory.refresh();
98124
}
99125
}

spring/boot4-xds/src/main/java/com/linecorp/armeria/spring/xds/SpringXdsTypeRegistryPackageProvider.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@
1818

1919
import java.util.List;
2020

21+
import com.linecorp.armeria.common.annotation.UnstableApi;
2122
import com.linecorp.armeria.xds.XdsTypeRegistryPackageProvider;
2223

2324
/**
2425
* Registers the {@link SpringConfigSource} protobuf type for xDS type registry discovery.
2526
*/
27+
@UnstableApi
2628
public final class SpringXdsTypeRegistryPackageProvider implements XdsTypeRegistryPackageProvider {
2729

2830
@Override

spring/boot4-xds/src/test/resources/application-xds-custom-bootstrap-test.yml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,6 @@ armeria:
88
typed_config:
99
"@type": type.googleapis.com/armeria.xds.spring.SpringConfigSource
1010
prefix: "custom.prefix."
11-
custom-cluster: |
12-
name: custom-cluster
13-
type: STATIC
14-
load_assignment:
15-
cluster_name: custom-cluster
16-
endpoints:
17-
- lb_endpoints:
18-
- endpoint:
19-
address:
20-
socket_address:
21-
address: 127.0.0.1
22-
port_value: 9090
2311
2412
custom:
2513
prefix:

0 commit comments

Comments
 (0)