1717package org .springframework .cloud .kubernetes .client .discovery ;
1818
1919import java .util .Collections ;
20- import java .util .List ;
2120
2221import io .kubernetes .client .informer .SharedIndexInformer ;
2322import io .kubernetes .client .informer .SharedInformerFactory ;
3029import io .kubernetes .client .util .Namespaces ;
3130import io .kubernetes .client .util .generic .GenericKubernetesApi ;
3231import org .apache .commons .logging .Log ;
32+ import org .apache .commons .logging .LogFactory ;
3333
34- import org .springframework .boot .BootstrapContext ;
3534import 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 ;
4037import org .springframework .cloud .config .client .ConfigServerInstanceProvider ;
4138import org .springframework .cloud .kubernetes .client .KubernetesClientAutoConfiguration ;
4239import org .springframework .cloud .kubernetes .commons .KubernetesClientProperties ;
4340import org .springframework .cloud .kubernetes .commons .KubernetesNamespaceProvider ;
4441import org .springframework .cloud .kubernetes .commons .config .KubernetesConfigServerBootstrapper ;
45- import org .springframework .cloud .kubernetes .commons .config .KubernetesConfigServerInstanceProvider ;
4642import org .springframework .cloud .kubernetes .commons .discovery .KubernetesDiscoveryProperties ;
4743import org .springframework .core .env .AbstractEnvironment ;
4844import org .springframework .core .env .Environment ;
5450 */
5551class 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}
0 commit comments