Skip to content

Commit 3cb5932

Browse files
Ryan Baxterryanjbaxter
andcommitted
Use propertyresolver revert function (#1565)
* Bumping versions * Remove function and use PropertyResolved Co-authored-by: Ryan Baxter <[email protected]>
1 parent 8c44006 commit 3cb5932

File tree

5 files changed

+137
-151
lines changed

5 files changed

+137
-151
lines changed

spring-cloud-kubernetes-client-discovery/src/main/java/org/springframework/cloud/kubernetes/client/discovery/KubernetesClientConfigServerBootstrapper.java

Lines changed: 42 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package org.springframework.cloud.kubernetes.client.discovery;
1818

1919
import java.util.Collections;
20-
import java.util.List;
2120

2221
import io.kubernetes.client.informer.SharedIndexInformer;
2322
import io.kubernetes.client.informer.SharedInformerFactory;
@@ -30,19 +29,16 @@
3029
import io.kubernetes.client.util.Namespaces;
3130
import io.kubernetes.client.util.generic.GenericKubernetesApi;
3231
import org.apache.commons.logging.Log;
32+
import org.apache.commons.logging.LogFactory;
3333

34-
import org.springframework.boot.BootstrapContext;
3534
import org.springframework.boot.BootstrapRegistry;
36-
import org.springframework.boot.context.properties.bind.BindHandler;
37-
import org.springframework.boot.context.properties.bind.Bindable;
38-
import org.springframework.boot.context.properties.bind.Binder;
39-
import org.springframework.cloud.client.ServiceInstance;
35+
import org.springframework.cloud.config.client.ConfigServerConfigDataLocationResolver;
36+
import org.springframework.cloud.config.client.ConfigServerConfigDataLocationResolver.PropertyResolver;
4037
import org.springframework.cloud.config.client.ConfigServerInstanceProvider;
4138
import org.springframework.cloud.kubernetes.client.KubernetesClientAutoConfiguration;
4239
import org.springframework.cloud.kubernetes.commons.KubernetesClientProperties;
4340
import org.springframework.cloud.kubernetes.commons.KubernetesNamespaceProvider;
4441
import org.springframework.cloud.kubernetes.commons.config.KubernetesConfigServerBootstrapper;
45-
import org.springframework.cloud.kubernetes.commons.config.KubernetesConfigServerInstanceProvider;
4642
import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties;
4743
import org.springframework.core.env.AbstractEnvironment;
4844
import org.springframework.core.env.Environment;
@@ -54,65 +50,45 @@
5450
*/
5551
class KubernetesClientConfigServerBootstrapper extends KubernetesConfigServerBootstrapper {
5652

53+
private static final Log LOG = LogFactory.getLog(KubernetesClientConfigServerBootstrapper.class);
54+
5755
@Override
5856
public void initialize(BootstrapRegistry registry) {
5957
if (hasConfigServerInstanceProvider()) {
6058
return;
6159
}
62-
// We need to pass a lambda here rather than create a new instance of
63-
// ConfigServerInstanceProvider.Function
64-
// or else we will get ClassNotFoundExceptions if Spring Cloud Config is not on
65-
// the classpath
66-
registry.registerIfAbsent(ConfigServerInstanceProvider.Function.class, KubernetesFunction::create);
67-
}
68-
69-
final static class KubernetesFunction implements ConfigServerInstanceProvider.Function {
70-
71-
private final BootstrapContext context;
72-
73-
private KubernetesFunction(BootstrapContext context) {
74-
this.context = context;
75-
}
76-
77-
static KubernetesFunction create(BootstrapContext context) {
78-
return new KubernetesFunction(context);
79-
}
80-
81-
@Override
82-
public List<ServiceInstance> apply(String serviceId, Binder binder, BindHandler bindHandler, Log log) {
83-
if (binder == null || bindHandler == null || !getDiscoveryEnabled(binder, bindHandler)) {
84-
// If we don't have the Binder or BinderHandler from the
85-
// ConfigDataLocationResolverContext
86-
// we won't be able to create the necessary configuration
87-
// properties to configure the
88-
// Kubernetes DiscoveryClient
89-
return Collections.emptyList();
60+
registry.registerIfAbsent(KubernetesDiscoveryProperties.class, context -> {
61+
if (!getDiscoveryEnabled(context)) {
62+
return null;
9063
}
91-
KubernetesDiscoveryProperties discoveryProperties = createKubernetesDiscoveryProperties(binder,
92-
bindHandler);
93-
KubernetesClientProperties clientProperties = createKubernetesClientProperties(binder, bindHandler);
94-
return getInstanceProvider(discoveryProperties, clientProperties, context, binder, bindHandler, log)
95-
.getInstances(serviceId);
96-
}
64+
return createKubernetesDiscoveryProperties(context);
65+
});
9766

98-
private KubernetesConfigServerInstanceProvider getInstanceProvider(
99-
KubernetesDiscoveryProperties discoveryProperties, KubernetesClientProperties clientProperties,
100-
BootstrapContext context, Binder binder, BindHandler bindHandler, Log log) {
67+
registry.registerIfAbsent(KubernetesClientProperties.class, context -> {
68+
if (!getDiscoveryEnabled(context)) {
69+
return null;
70+
}
71+
return createKubernetesClientProperties(context);
72+
});
73+
registry.registerIfAbsent(ConfigServerInstanceProvider.Function.class, context -> {
74+
if (!getDiscoveryEnabled(context)) {
75+
return (id) -> Collections.emptyList();
76+
}
10177
if (context.isRegistered(KubernetesInformerDiscoveryClient.class)) {
10278
KubernetesInformerDiscoveryClient client = context.get(KubernetesInformerDiscoveryClient.class);
10379
return client::getInstances;
10480
}
10581
else {
106-
82+
PropertyResolver propertyResolver = getPropertyResolver(context);
10783
ApiClient defaultApiClient = kubernetesApiClient();
108-
defaultApiClient.setUserAgent(binder.bind("spring.cloud.kubernetes.client.user-agent", String.class)
109-
.orElse(KubernetesClientProperties.DEFAULT_USER_AGENT));
84+
defaultApiClient.setUserAgent(propertyResolver.get("spring.cloud.kubernetes.client.user-agent",
85+
String.class, KubernetesClientProperties.DEFAULT_USER_AGENT));
11086
KubernetesClientAutoConfiguration clientAutoConfiguration = new KubernetesClientAutoConfiguration();
11187
ApiClient apiClient = context.getOrElseSupply(ApiClient.class, () -> defaultApiClient);
11288

11389
KubernetesNamespaceProvider kubernetesNamespaceProvider = clientAutoConfiguration
114-
.kubernetesNamespaceProvider(getNamespaceEnvironment(binder, bindHandler));
115-
90+
.kubernetesNamespaceProvider(getNamespaceEnvironment(propertyResolver));
91+
KubernetesDiscoveryProperties discoveryProperties = context.get(KubernetesDiscoveryProperties.class);
11692
String namespace = getInformerNamespace(kubernetesNamespaceProvider, discoveryProperties);
11793
SharedInformerFactory sharedInformerFactory = new SharedInformerFactory(apiClient);
11894
GenericKubernetesApi<V1Service, V1ServiceList> servicesApi = new GenericKubernetesApi<>(V1Service.class,
@@ -133,40 +109,32 @@ private KubernetesConfigServerInstanceProvider getInstanceProvider(
133109
return discoveryClient::getInstances;
134110
}
135111
catch (Exception e) {
136-
if (log != null) {
137-
log.warn("Error initiating informer discovery client", e);
138-
}
112+
LOG.warn("Error initiating informer discovery client", e);
139113
return (serviceId) -> Collections.emptyList();
140114
}
141115
finally {
142116
sharedInformerFactory.stopAllRegisteredInformers();
143117
}
144118
}
145-
}
119+
});
146120

147-
private String getInformerNamespace(KubernetesNamespaceProvider kubernetesNamespaceProvider,
148-
KubernetesDiscoveryProperties discoveryProperties) {
149-
return discoveryProperties.allNamespaces() ? Namespaces.NAMESPACE_ALL
150-
: kubernetesNamespaceProvider.getNamespace() == null ? Namespaces.NAMESPACE_DEFAULT
151-
: kubernetesNamespaceProvider.getNamespace();
152-
}
153-
154-
private Environment getNamespaceEnvironment(Binder binder, BindHandler bindHandler) {
155-
return new AbstractEnvironment() {
156-
@Override
157-
public String getProperty(String key) {
158-
return binder.bind(key, Bindable.of(String.class), bindHandler).orElse(super.getProperty(key));
159-
}
160-
};
161-
}
121+
}
162122

163-
// This method should never be called, but is there for backward
164-
// compatibility purposes
165-
@Override
166-
public List<ServiceInstance> apply(String serviceId) {
167-
return apply(serviceId, null, null, null);
168-
}
123+
private String getInformerNamespace(KubernetesNamespaceProvider kubernetesNamespaceProvider,
124+
KubernetesDiscoveryProperties discoveryProperties) {
125+
return discoveryProperties.allNamespaces() ? Namespaces.NAMESPACE_ALL
126+
: kubernetesNamespaceProvider.getNamespace() == null ? Namespaces.NAMESPACE_DEFAULT
127+
: kubernetesNamespaceProvider.getNamespace();
128+
}
169129

130+
private Environment getNamespaceEnvironment(
131+
ConfigServerConfigDataLocationResolver.PropertyResolver propertyResolver) {
132+
return new AbstractEnvironment() {
133+
@Override
134+
public String getProperty(String key) {
135+
return propertyResolver.get(key, String.class, super.getProperty(key));
136+
}
137+
};
170138
}
171139

172140
}

spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/KubernetesNamespaceProvider.java

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@
3535
*/
3636
public class KubernetesNamespaceProvider {
3737

38-
private static final DeferredLog LOG = new DeferredLog();
39-
4038
/**
4139
* Property name for namespace.
4240
*/
@@ -47,6 +45,10 @@ public class KubernetesNamespaceProvider {
4745
*/
4846
public static final String NAMESPACE_PATH_PROPERTY = "spring.cloud.kubernetes.client.serviceAccountNamespacePath";
4947

48+
private static final DeferredLog LOG = new DeferredLog();
49+
50+
private String namespacePropertyValue;
51+
5052
private BindHandler bindHandler;
5153

5254
private String serviceAccountNamespace;
@@ -65,7 +67,36 @@ public KubernetesNamespaceProvider(Binder binder, BindHandler bindHandler) {
6567
this.bindHandler = bindHandler;
6668
}
6769

70+
public KubernetesNamespaceProvider(String namespacePropertyValue) {
71+
this.namespacePropertyValue = namespacePropertyValue;
72+
}
73+
74+
public static String getNamespaceFromServiceAccountFile(String path) {
75+
String namespace = null;
76+
LOG.debug("Looking for service account namespace at: [" + path + "].");
77+
Path serviceAccountNamespacePath = Paths.get(path);
78+
boolean serviceAccountNamespaceExists = Files.isRegularFile(serviceAccountNamespacePath);
79+
if (serviceAccountNamespaceExists) {
80+
LOG.debug("Found service account namespace at: [" + serviceAccountNamespacePath + "].");
81+
82+
try {
83+
namespace = new String(Files.readAllBytes((serviceAccountNamespacePath)));
84+
LOG.debug("Service account namespace value: " + serviceAccountNamespacePath);
85+
}
86+
catch (IOException ioe) {
87+
LOG.error("Error reading service account namespace from: [" + serviceAccountNamespacePath + "].", ioe);
88+
}
89+
90+
}
91+
return namespace;
92+
}
93+
6894
public String getNamespace() {
95+
// If they provided the namespace in the constructor just return that
96+
if (!ObjectUtils.isEmpty(namespacePropertyValue)) {
97+
return namespacePropertyValue;
98+
}
99+
// No namespace provided so try to get it from another source
69100
String namespace = null;
70101
if (environment != null) {
71102
namespace = environment.getProperty(NAMESPACE_PROPERTY);
@@ -96,24 +127,4 @@ private String getServiceAccountNamespace() {
96127
return serviceAccountNamespace;
97128
}
98129

99-
public static String getNamespaceFromServiceAccountFile(String path) {
100-
String namespace = null;
101-
LOG.debug("Looking for service account namespace at: [" + path + "].");
102-
Path serviceAccountNamespacePath = Paths.get(path);
103-
boolean serviceAccountNamespaceExists = Files.isRegularFile(serviceAccountNamespacePath);
104-
if (serviceAccountNamespaceExists) {
105-
LOG.debug("Found service account namespace at: [" + serviceAccountNamespacePath + "].");
106-
107-
try {
108-
namespace = new String(Files.readAllBytes((serviceAccountNamespacePath)));
109-
LOG.debug("Service account namespace value: " + serviceAccountNamespacePath);
110-
}
111-
catch (IOException ioe) {
112-
LOG.error("Error reading service account namespace from: [" + serviceAccountNamespacePath + "].", ioe);
113-
}
114-
115-
}
116-
return namespace;
117-
}
118-
119130
}

spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/KubernetesConfigServerBootstrapper.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@
1616

1717
package org.springframework.cloud.kubernetes.commons.config;
1818

19+
import org.springframework.boot.BootstrapContext;
1920
import org.springframework.boot.BootstrapRegistryInitializer;
2021
import org.springframework.boot.context.properties.bind.BindHandler;
2122
import org.springframework.boot.context.properties.bind.Bindable;
2223
import org.springframework.boot.context.properties.bind.Binder;
2324
import org.springframework.cloud.config.client.ConfigClientProperties;
25+
import org.springframework.cloud.config.client.ConfigServerConfigDataLocationResolver;
26+
import org.springframework.cloud.config.client.ConfigServerConfigDataLocationResolver.PropertyResolver;
2427
import org.springframework.cloud.kubernetes.commons.KubernetesClientProperties;
2528
import org.springframework.cloud.kubernetes.commons.KubernetesNamespaceProvider;
2629
import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties;
@@ -41,14 +44,40 @@ public static KubernetesDiscoveryProperties createKubernetesDiscoveryProperties(
4144
bindHandler).orElseGet(() -> KubernetesDiscoveryProperties.DEFAULT);
4245
}
4346

47+
public static KubernetesDiscoveryProperties createKubernetesDiscoveryProperties(BootstrapContext bootstrapContext) {
48+
PropertyResolver propertyResolver = getPropertyResolver(bootstrapContext);
49+
return propertyResolver.resolveConfigurationProperties(KubernetesDiscoveryProperties.PREFIX,
50+
KubernetesDiscoveryProperties.class, () -> KubernetesDiscoveryProperties.DEFAULT);
51+
}
52+
4453
public static KubernetesClientProperties createKubernetesClientProperties(Binder binder, BindHandler bindHandler) {
4554
return binder.bindOrCreate(KubernetesClientProperties.PREFIX, Bindable.of(KubernetesClientProperties.class))
4655
.withNamespace(new KubernetesNamespaceProvider(binder, bindHandler).getNamespace());
4756
}
4857

58+
public static KubernetesClientProperties createKubernetesClientProperties(BootstrapContext bootstrapContext) {
59+
PropertyResolver propertyResolver = getPropertyResolver(bootstrapContext);
60+
return getPropertyResolver(bootstrapContext)
61+
.resolveOrCreateConfigurationProperties(KubernetesClientProperties.PREFIX,
62+
KubernetesClientProperties.class)
63+
.withNamespace(
64+
propertyResolver.get(KubernetesNamespaceProvider.NAMESPACE_PROPERTY, String.class, null));
65+
}
66+
4967
public static Boolean getDiscoveryEnabled(Binder binder, BindHandler bindHandler) {
5068
return binder.bind(ConfigClientProperties.CONFIG_DISCOVERY_ENABLED, Bindable.of(Boolean.class), bindHandler)
5169
.orElse(false);
5270
}
5371

72+
public static Boolean getDiscoveryEnabled(BootstrapContext bootstrapContext) {
73+
return getPropertyResolver(bootstrapContext).get(ConfigClientProperties.CONFIG_DISCOVERY_ENABLED, Boolean.class,
74+
false);
75+
}
76+
77+
protected static PropertyResolver getPropertyResolver(BootstrapContext context) {
78+
return context.getOrElseSupply(ConfigServerConfigDataLocationResolver.PropertyResolver.class,
79+
() -> new ConfigServerConfigDataLocationResolver.PropertyResolver(context.get(Binder.class),
80+
context.getOrElse(BindHandler.class, null)));
81+
}
82+
5483
}

0 commit comments

Comments
 (0)