Documentation Improvements

This changes makes a bunch documentation improvements, case normalizations,
and general improvements left over from the previous addition of the
Kubernetes Service Binding support.

Signed-off-by: Ben Hale <bhale@vmware.com>
This commit is contained in:
Ben Hale
2020-07-15 17:21:05 -07:00
parent 819ce87bfe
commit 60c9bcbe7e
30 changed files with 254 additions and 221 deletions

View File

@@ -15,6 +15,8 @@
*/
package org.springframework.cloud.bindings;
import org.springframework.lang.Nullable;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
@@ -72,8 +74,8 @@ public final class Binding {
this.path = path;
this.secret = new HashMap<>();
String provider = null;
String type = null;
String provider = null;
for (Map.Entry<String, String> entry : secret.entrySet()) {
switch (entry.getKey()) {
case TYPE:
@@ -88,8 +90,12 @@ public final class Binding {
}
}
this.provider = provider;
if (type == null) {
throw new IllegalArgumentException(String.format("%s has no type and is not a valid binding", path));
}
this.type = type;
this.provider = provider;
}
private static Map<String, String> createSecretMap(Path path) {
@@ -163,6 +169,7 @@ public final class Binding {
/**
* Returns the provider of the binding.
*/
@Nullable
public String getProvider() {
return provider;
}

View File

@@ -49,9 +49,10 @@ public final class Bindings {
private final List<Binding> bindings;
/**
* Creates a new {@code Bindings} instance, using the {@code $CNB_BINDINGS} environment variable to determine the
* file system root. If the {@code $CNB_BINDINGS} environment variable is not set, an empty {@code Bindings} is
* returned. If the directory does not exist, an empty {@code Bindings} is returned.
* Creates a new {@code Bindings} instance, using the {@code $SERVICE_BINDING_ROOT} environment variable or the
* {@code $CNB_BINDINGS} environment variable if it does not exist to determine the file system root. If neither
* the {@code $SERVICE_BINDING_ROOT} nor {@code $CNB_BINDINGS} environment variables are set, an empty
* {@code Bindings} is returned. If the directory does not exist, an empty {@code Bindings} is returned.
*/
public Bindings() {
this(getBindingRoot());
@@ -150,9 +151,8 @@ public final class Bindings {
*/
public List<Binding> filterBindings(@Nullable String type, @Nullable String provider) {
return bindings.stream()
.filter(binding ->
(type == null || binding.getType().equalsIgnoreCase(type)) &&
(provider == null || binding.getProvider().equalsIgnoreCase(provider)))
.filter(b -> type == null || b.getType().equalsIgnoreCase(type))
.filter(b -> provider == null || b.getProvider() != null && b.getProvider().equalsIgnoreCase(provider))
.collect(Collectors.toList());
}

View File

@@ -33,12 +33,12 @@ import static org.springframework.cloud.bindings.boot.PropertySourceContributor.
/**
* An implementation of {@link EnvironmentPostProcessor} that generates properties from {@link Bindings} with a
* flattened format: {@code cnb.bindings.{name}.{metadata,secret}.*}.
* flattened format: {@code k8s.bindings.{name}.*}.
*/
public final class BindingFlattenedEnvironmentPostProcessor implements ApplicationListener<ApplicationPreparedEvent>,
EnvironmentPostProcessor, Ordered {
public static final String BINDING_FLATTENED_PROPERTY_SOURCE_NAME = "cnbBindingFlattened";
public static final String BINDING_FLATTENED_PROPERTY_SOURCE_NAME = "kubernetesServiceBindingFlattened";
private final DeferredLog log = new DeferredLog();
@@ -72,16 +72,16 @@ public final class BindingFlattenedEnvironmentPostProcessor implements Applicati
Map<String, Object> properties = new HashMap<>();
bindings.getBindings().forEach(binding -> {
binding.getSecret().forEach((key, value) -> {
properties.put(String.format("cnb.bindings.%s.%s", binding.getName(), key), value);
properties.put(String.format("k8s.bindings.%s.%s", binding.getName(), key), value);
});
});
if (properties.isEmpty()) {
log.debug("No properties set from CNB Bindings. Skipping PropertySource creation.");
log.debug("No properties set from Kubernetes Service Bindings. Skipping PropertySource creation.");
return;
}
log.info("Creating flattened PropertySource from CNB Bindings");
log.info("Creating flattened PropertySource from Kubernetes Service Bindings");
contributePropertySource(BINDING_FLATTENED_PROPERTY_SOURCE_NAME, properties, environment);
}

View File

@@ -51,7 +51,7 @@ public final class BindingSpecificEnvironmentPostProcessor implements Applicatio
/**
* The name of the {@link PropertySource} created by the {@code BindingsEnvironmentPostProcessor}: {@value}.
*/
public static final String BINDING_SPECIFIC_PROPERTY_SOURCE_NAME = "cnbBindingSpecific";
public static final String BINDING_SPECIFIC_PROPERTY_SOURCE_NAME = "kubernetesServiceBindingSpecific";
private static final DeferredLog LOG = new DeferredLog();
@@ -92,18 +92,18 @@ public final class BindingSpecificEnvironmentPostProcessor implements Applicatio
}
if (bindings.getBindings().isEmpty()) {
LOG.debug("No CNB Bindings found. Skipping Environment post-processing.");
LOG.debug("No Kubernetes Service Bindings found. Skipping Environment post-processing.");
return;
}
Map<String, Object> properties = new HashMap<>();
processors.forEach(processor -> processor.process(environment, bindings, properties));
if (properties.isEmpty()) {
LOG.debug("No properties set from CNB Bindings. Skipping PropertySource creation.");
LOG.debug("No properties set from Kubernetes Service Bindings. Skipping PropertySource creation.");
return;
}
LOG.info("Creating binding-specific PropertySource from CNB Bindings");
LOG.info("Creating binding-specific PropertySource from Kubernetes Service Bindings");
contributePropertySource(BINDING_SPECIFIC_PROPERTY_SOURCE_NAME, properties, environment);
}

View File

@@ -32,7 +32,7 @@ public final class CassandraBindingsPropertiesProcessor implements BindingsPrope
/**
* The {@link Binding} type that this processor is interested in: {@value}.
**/
public static final String TYPE = "Cassandra";
public static final String TYPE = "cassandra";
@Override
public void process(Environment environment, Bindings bindings, Map<String, Object> properties) {

View File

@@ -31,7 +31,7 @@ final class ConfigServerBindingsPropertiesProcessor implements BindingsPropertie
/**
* The {@link Binding} type that this processor is interested in: {@value}.
**/
public static final String TYPE = "Config";
public static final String TYPE = "config";
@Override
public void process(Environment environment, Bindings bindings, Map<String, Object> properties) {

View File

@@ -32,7 +32,7 @@ final class CouchbaseBindingsPropertiesProcessor implements BindingsPropertiesPr
/**
* The {@link Binding} type that this processor is interested in: {@value}.
**/
public static final String TYPE = "Couchbase";
public static final String TYPE = "couchbase";
@Override
public void process(Environment environment, Bindings bindings, Map<String, Object> properties) {

View File

@@ -34,7 +34,7 @@ public final class Db2BindingsPropertiesProcessor implements BindingsPropertiesP
/**
* The {@link Binding} type that this processor is interested in: {@value}.
**/
public static final String TYPE = "DB2";
public static final String TYPE = "db2";
@Override
public void process(Environment environment, Bindings bindings, Map<String, Object> properties) {

View File

@@ -32,7 +32,7 @@ final class ElasticsearchBindingsPropertiesProcessor implements BindingsProperti
/**
* The {@link Binding} type that this processor is interested in: {@value}.
**/
public static final String TYPE = "Elasticsearch";
public static final String TYPE = "elasticsearch";
@Override

View File

@@ -31,7 +31,7 @@ final class EurekaBindingsPropertiesProcessor implements BindingsPropertiesProce
/**
* The {@link Binding} type that this processor is interested in: {@value}.
**/
public static final String TYPE = "Eureka";
public static final String TYPE = "eureka";
@Override
public void process(Environment environment, Bindings bindings, Map<String, Object> properties) {

View File

@@ -26,7 +26,7 @@ final class Guards {
static boolean isTypeEnabled(Environment environment, String type) {
return environment.getProperty(
String.format("org.springframework.cloud.bindings.boot.%s.enable", type.toLowerCase()),
String.format("org.springframework.cloud.bindings.boot.%s.enable", type),
Boolean.class, true);
}

View File

@@ -31,7 +31,7 @@ final class KafkaBindingsPropertiesProcessor implements BindingsPropertiesProces
/**
* The {@link Binding} type that this processor is interested in: {@value}.
**/
public static final String TYPE = "Kafka";
public static final String TYPE = "kafka";
@Override
public void process(Environment environment, Bindings bindings, Map<String, Object> properties) {

View File

@@ -31,7 +31,7 @@ public final class LDAPBindingsPropertiesProcessor implements BindingsProperties
/**
* The {@link Binding} type that this processor is interested in: {@value}.
**/
public static final String TYPE = "LDAP";
public static final String TYPE = "ldap";
@Override
public void process(Environment environment, Bindings bindings, Map<String, Object> properties) {

View File

@@ -32,7 +32,7 @@ public final class MongoDbBindingsPropertiesProcessor implements BindingsPropert
/**
* The {@link Binding} type that this processor is interested in: {@value}.
**/
public static final String TYPE = "MongoDB";
public static final String TYPE = "mongodb";
@Override
public void process(Environment environment, Bindings bindings, Map<String, Object> properties) {

View File

@@ -34,7 +34,7 @@ public final class MySqlBindingsPropertiesProcessor implements BindingsPropertie
/**
* The {@link Binding} type that this processor is interested in: {@value}.
**/
public static final String TYPE = "MySQL";
public static final String TYPE = "mysql";
@Override
public void process(Environment environment, Bindings bindings, Map<String, Object> properties) {

View File

@@ -31,7 +31,7 @@ final class Neo4JBindingsPropertiesProcessor implements BindingsPropertiesProces
/**
* The {@link Binding} type that this processor is interested in: {@value}.
**/
public static final String TYPE = "Neo4J";
public static final String TYPE = "neo4j";
@Override
public void process(Environment environment, Bindings bindings, Map<String, Object> properties) {

View File

@@ -34,7 +34,7 @@ public final class OracleBindingsPropertiesProcessor implements BindingsProperti
/**
* The {@link Binding} type that this processor is interested in: {@value}.
**/
public static final String TYPE = "Oracle";
public static final String TYPE = "oracle";
@Override
public void process(Environment environment, Bindings bindings, Map<String, Object> properties) {

View File

@@ -34,7 +34,7 @@ public final class PostgreSqlBindingsPropertiesProcessor implements BindingsProp
/**
* The {@link Binding} type that this processor is interested in: {@value}.
**/
public static final String TYPE = "PostgreSQL";
public static final String TYPE = "postgresql";
@Override
public void process(Environment environment, Bindings bindings, Map<String, Object> properties) {

View File

@@ -32,7 +32,7 @@ final class RabbitMqBindingsPropertiesProcessor implements BindingsPropertiesPro
/**
* The {@link Binding} type that this processor is interested in: {@value}.
**/
public static final String TYPE = "RabbitMQ";
public static final String TYPE = "rabbitmq";
@Override
public void process(Environment environment, Bindings bindings, Map<String, Object> properties) {

View File

@@ -32,7 +32,7 @@ public final class RedisBindingsPropertiesProcessor implements BindingsPropertie
/**
* The {@link Binding} type that this processor is interested in: {@value}.
**/
public static final String TYPE = "Redis";
public static final String TYPE = "redis";
@Override
public void process(Environment environment, Bindings bindings, Map<String, Object> properties) {

View File

@@ -35,7 +35,7 @@ public final class SpringSecurityOAuth2BindingsPropertiesProcessor implements Bi
/**
* The {@link Binding} type that this processor is interested in: {@value}.
**/
public static final String TYPE = "OAuth2";
public static final String TYPE = "oauth2";
private static final DeferredLog LOG = new DeferredLog();

View File

@@ -34,7 +34,7 @@ public final class SqlServerBindingsPropertiesProcessor implements BindingsPrope
/**
* The {@link Binding} type that this processor is interested in: {@value}.
**/
public static final String TYPE = "SQLServer";
public static final String TYPE = "sqlserver";
@Override
public void process(Environment environment, Bindings bindings, Map<String, Object> properties) {

View File

@@ -35,7 +35,7 @@ public final class VaultBindingsPropertiesProcessor implements BindingsPropertie
/**
* The {@link Binding} type that this processor is interested in: {@value}.
**/
public static final String TYPE = "Vault";
public static final String TYPE = "vault";
private static final DeferredLog LOG = new DeferredLog();

View File

@@ -32,7 +32,7 @@ public final class WavefrontBindingsPropertiesProcessor implements BindingsPrope
/**
* The {@link Binding} type that this processor is interested in: {@value}.
**/
public static final String TYPE = "Wavefront";
public static final String TYPE = "wavefront";
@Override
public void process(Environment environment, Bindings bindings, Map<String, Object> properties) {

View File

@@ -20,14 +20,25 @@ import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
@DisplayName("Binding")
final class BindingTest {
@Test
@DisplayName("fails to create invalid binding")
void testInvalid() throws IOException {
Path path = Files.createTempDirectory("invalid-binding");
assertThatIllegalArgumentException().isThrownBy(() -> new Binding(path));
}
@Nested
@DisplayName("CNB Bindings")
final class CNBBindings {
@@ -60,7 +71,6 @@ final class BindingTest {
assertThat(binding.getSecretFilePath("test-secret-key"))
.isEqualTo(root.resolve("test-k8s/secret/test-secret-key"));
}
}
@Nested

View File

@@ -88,13 +88,17 @@ final class BindingsTest {
new FluentMap()
.withEntry("kind", "test-kind-2")
.withEntry("provider", "test-provider-2")
),
new Binding("test-name-3", root.resolve("test-name-3"),
new FluentMap()
.withEntry("kind", "test-kind-2")
)
);
@Test
@DisplayName("returns content")
void getBindings() {
assertThat(bindings.getBindings()).hasSize(2);
assertThat(bindings.getBindings()).hasSize(3);
}
@Test
@@ -171,13 +175,17 @@ final class BindingsTest {
new FluentMap()
.withEntry("type", "test-type-2")
.withEntry("provider", "test-provider-2")
),
new Binding("test-name-3", root.resolve("test-name-3"),
new FluentMap()
.withEntry("type", "test-type-3")
)
);
@Test
@DisplayName("returns content")
void getBindings() {
assertThat(bindings.getBindings()).hasSize(2);
assertThat(bindings.getBindings()).hasSize(3);
}
@Test

View File

@@ -22,10 +22,10 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.config.ConfigFileApplicationListener;
import org.springframework.cloud.bindings.Binding;
import org.springframework.cloud.bindings.Bindings;
import org.springframework.cloud.bindings.FluentMap;
import org.springframework.mock.env.MockEnvironment;
import java.nio.file.Paths;
import java.util.Collections;
import static org.assertj.core.api.Assertions.assertThat;
@@ -43,7 +43,8 @@ final class BindingFlattenedEnvironmentPostProcessorTest {
new BindingFlattenedEnvironmentPostProcessor(
new Bindings(
new Binding("test-name", Paths.get("test-path"),
Collections.emptyMap()
new FluentMap()
.withEntry(Binding.TYPE, "test-type")
)
)
).postProcessEnvironment(new MockEnvironment(), application);
@@ -66,13 +67,15 @@ final class BindingFlattenedEnvironmentPostProcessorTest {
new BindingFlattenedEnvironmentPostProcessor(
new Bindings(
new Binding("test-name", Paths.get("test-path"),
Collections.singletonMap("test-secret-key", "test-secret-value")
new FluentMap()
.withEntry(Binding.TYPE, "test-type")
.withEntry("test-secret-key", "test-secret-value")
)
)
).postProcessEnvironment(environment, application);
assertThat(environment.getPropertySources()).hasSize(2);
assertThat(environment.getProperty("cnb.bindings.test-name.test-secret-key")).isEqualTo("test-secret-value");
assertThat(environment.getProperty("k8s.bindings.test-name.test-secret-key")).isEqualTo("test-secret-value");
}
@Test

View File

@@ -22,10 +22,10 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.config.ConfigFileApplicationListener;
import org.springframework.cloud.bindings.Binding;
import org.springframework.cloud.bindings.Bindings;
import org.springframework.cloud.bindings.FluentMap;
import org.springframework.mock.env.MockEnvironment;
import java.nio.file.Paths;
import java.util.Collections;
import static org.assertj.core.api.Assertions.assertThat;
@@ -43,7 +43,8 @@ final class BindingSpecificEnvironmentPostProcessorTest {
new BindingSpecificEnvironmentPostProcessor(
new Bindings(
new Binding("test-name", Paths.get("test-path"),
Collections.emptyMap()
new FluentMap()
.withEntry(Binding.TYPE, "test-type")
)
),
(environment, bindings, properties) -> properties.put("test-key", "test-value")
@@ -67,7 +68,8 @@ final class BindingSpecificEnvironmentPostProcessorTest {
new BindingSpecificEnvironmentPostProcessor(
new Bindings(
new Binding("test-name", Paths.get("test-path"),
Collections.emptyMap()
new FluentMap()
.withEntry(Binding.TYPE, "test-type")
)
)
).postProcessEnvironment(environment, application);
@@ -81,7 +83,8 @@ final class BindingSpecificEnvironmentPostProcessorTest {
new BindingSpecificEnvironmentPostProcessor(
new Bindings(
new Binding("test-name", Paths.get("test-path"),
Collections.emptyMap()
new FluentMap()
.withEntry(Binding.TYPE, "test-type")
)
),
(environment, bindings, properties) -> properties.put("test-key", "test-value")

View File

@@ -79,7 +79,7 @@ final class GuardsTest {
@DisplayName("returns the set value of false")
void setFalse() {
environment.setProperty("org.springframework.cloud.bindings.boot.test.enable", "false");
assertThat(isTypeEnabled(environment, "Test")).isFalse();
assertThat(isTypeEnabled(environment, "test")).isFalse();
}
}