Skip to content

Commit 5671518

Browse files
committed
wip
Signed-off-by: wind57 <[email protected]>
1 parent 8729e1f commit 5671518

9 files changed

+103
-397
lines changed

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

Lines changed: 0 additions & 62 deletions
This file was deleted.

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

Lines changed: 0 additions & 62 deletions
This file was deleted.

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

Lines changed: 88 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
package org.springframework.cloud.kubernetes.client.discovery;
1818

19+
import java.util.ArrayList;
20+
import java.util.List;
21+
1922
import io.kubernetes.client.informer.SharedIndexInformer;
2023
import io.kubernetes.client.informer.SharedInformerFactory;
2124
import io.kubernetes.client.informer.cache.Lister;
@@ -37,7 +40,6 @@
3740
import org.springframework.cloud.client.discovery.simple.SimpleDiscoveryClientAutoConfiguration;
3841
import org.springframework.cloud.kubernetes.client.KubernetesClientAutoConfiguration;
3942
import org.springframework.cloud.kubernetes.commons.KubernetesNamespaceProvider;
40-
import org.springframework.cloud.kubernetes.commons.config.NamespaceResolutionFailedException;
4143
import org.springframework.cloud.kubernetes.commons.discovery.ConditionalOnBlockingOrReactiveDiscoveryEnabled;
4244
import org.springframework.cloud.kubernetes.commons.discovery.ConditionalOnKubernetesDiscoveryEnabled;
4345
import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties;
@@ -48,7 +50,6 @@
4850
import org.springframework.core.log.LogAccessor;
4951

5052
import static io.kubernetes.client.util.Namespaces.NAMESPACE_ALL;
51-
import static io.kubernetes.client.util.Namespaces.NAMESPACE_DEFAULT;
5253
import static org.springframework.cloud.kubernetes.client.KubernetesClientUtils.getApplicationNamespace;
5354

5455
/**
@@ -59,79 +60,119 @@
5960
@ConditionalOnKubernetesDiscoveryEnabled
6061
@ConditionalOnBlockingOrReactiveDiscoveryEnabled
6162
@ConditionalOnCloudPlatform(CloudPlatform.KUBERNETES)
62-
@Conditional(ConditionalOnSelectiveNamespacesMissing.class)
6363
@AutoConfigureBefore({ SimpleDiscoveryClientAutoConfiguration.class, CommonsClientAutoConfiguration.class })
6464
@AutoConfigureAfter({ KubernetesClientAutoConfiguration.class, KubernetesDiscoveryPropertiesAutoConfiguration.class })
6565
final class KubernetesClientInformerAutoConfiguration {
6666

6767
private static final LogAccessor LOG = new LogAccessor(
68-
LogFactory.getLog(KubernetesClientInformerAutoConfiguration.class));
68+
LogFactory.getLog(KubernetesClientInformerAutoConfiguration.class));
6969

70+
// we rely on the order of namespaces to enable listers, as such provide a bean of
71+
// namespaces as a list, instead of the incoming Set.
7072
@Bean
71-
@ConditionalOnMissingBean
72-
SharedInformerFactory sharedInformerFactory(ApiClient client) {
73-
LOG.debug(() -> "registering sharedInformerFactory for non-selective namespaces");
74-
return new SharedInformerFactory(client);
75-
}
73+
List<String> k8sDiscoveryClientNamespaces(KubernetesDiscoveryProperties properties,
74+
KubernetesNamespaceProvider provider) {
7675

77-
@Bean
78-
String kubernetesClientNamespace(KubernetesDiscoveryProperties properties, KubernetesNamespaceProvider provider) {
79-
String namespace;
8076
if (properties.allNamespaces()) {
81-
namespace = NAMESPACE_ALL;
8277
LOG.debug(() -> "serviceSharedInformer will use all-namespaces");
78+
return List.of(NAMESPACE_ALL);
8379
}
84-
else {
85-
try {
86-
namespace = getApplicationNamespace(null, "kubernetes client discovery", provider);
87-
}
88-
catch (NamespaceResolutionFailedException ex) {
89-
LOG.warn(() -> "failed to resolve namespace, defaulting to :" + NAMESPACE_DEFAULT
90-
+ ". This will fail in a future release.");
91-
namespace = NAMESPACE_DEFAULT;
92-
}
93-
LOG.debug("serviceSharedInformer will use namespace : " + namespace);
80+
81+
if (properties.namespaces() != null && !properties.namespaces().isEmpty()) {
82+
List<String> selectiveNamespaces = properties.namespaces().stream().sorted().toList();
83+
LOG.debug(() -> "serviceSharedInformers will use selective namespaces : " + selectiveNamespaces);
84+
return selectiveNamespaces;
9485
}
9586

96-
return namespace;
87+
String namespace = getApplicationNamespace(null, "kubernetes client discovery", provider);
88+
LOG.debug(() -> "using namespace : " + namespace);
89+
return List.of(namespace);
9790
}
9891

9992
@Bean
100-
@ConditionalOnMissingBean(value = V1Service.class, parameterizedContainer = SharedIndexInformer.class)
101-
SharedIndexInformer<V1Service> servicesSharedIndexInformer(SharedInformerFactory sharedInformerFactory,
102-
ApiClient apiClient, String kubernetesClientNamespace) {
93+
@ConditionalOnMissingBean(value = SharedInformerFactory.class, parameterizedContainer = List.class)
94+
List<SharedInformerFactory> sharedInformerFactories(ApiClient apiClient, List<String> selectiveNamespaces) {
10395

104-
GenericKubernetesApi<V1Service, V1ServiceList> servicesApi = new GenericKubernetesApi<>(V1Service.class,
105-
V1ServiceList.class, "", "v1", "services", apiClient);
106-
107-
return sharedInformerFactory.sharedIndexInformerFor(servicesApi, V1Service.class, 0L,
108-
kubernetesClientNamespace);
96+
int howManyNamespaces = selectiveNamespaces.size();
97+
List<SharedInformerFactory> sharedInformerFactories = new ArrayList<>(howManyNamespaces);
98+
for (int i = 0; i < howManyNamespaces; ++i) {
99+
sharedInformerFactories.add(new SharedInformerFactory(apiClient));
100+
}
101+
return sharedInformerFactories;
109102
}
110103

111104
@Bean
112-
@ConditionalOnMissingBean(value = V1Endpoints.class, parameterizedContainer = SharedIndexInformer.class)
113-
SharedIndexInformer<V1Endpoints> endpointsSharedIndexInformer(SharedInformerFactory sharedInformerFactory,
114-
ApiClient apiClient, String kubernetesClientNamespace) {
105+
@ConditionalOnMissingBean(value = V1Service.class,
106+
parameterizedContainer = { List.class, SharedIndexInformer.class })
107+
List<SharedIndexInformer<V1Service>> serviceSharedIndexInformers(
108+
List<SharedInformerFactory> sharedInformerFactories, List<String> selectiveNamespaces,
109+
ApiClient apiClient) {
110+
111+
int howManyNamespaces = selectiveNamespaces.size();
112+
List<SharedIndexInformer<V1Service>> serviceSharedIndexedInformers = new ArrayList<>(howManyNamespaces);
113+
for (int i = 0; i < howManyNamespaces; ++i) {
114+
GenericKubernetesApi<V1Service, V1ServiceList> servicesApi = new GenericKubernetesApi<>(V1Service.class,
115+
V1ServiceList.class, "", "v1", "services", apiClient);
116+
SharedIndexInformer<V1Service> sharedIndexInformer = sharedInformerFactories.get(i)
117+
.sharedIndexInformerFor(servicesApi, V1Service.class, 0L, selectiveNamespaces.get(i));
118+
serviceSharedIndexedInformers.add(sharedIndexInformer);
119+
}
120+
return serviceSharedIndexedInformers;
121+
}
115122

116-
GenericKubernetesApi<V1Endpoints, V1EndpointsList> servicesApi = new GenericKubernetesApi<>(V1Endpoints.class,
117-
V1EndpointsList.class, "", "v1", "endpoints", apiClient);
123+
@Bean
124+
@ConditionalOnMissingBean(value = V1Service.class, parameterizedContainer = { List.class, Lister.class })
125+
List<Lister<V1Service>> serviceListers(List<String> selectiveNamespaces,
126+
List<SharedIndexInformer<V1Service>> serviceSharedIndexInformers) {
127+
128+
int howManyNamespaces = selectiveNamespaces.size();
129+
List<Lister<V1Service>> serviceListers = new ArrayList<>(howManyNamespaces);
130+
131+
for (int i = 0; i < howManyNamespaces; ++i) {
132+
String namespace = selectiveNamespaces.get(i);
133+
Lister<V1Service> lister = new Lister<>(serviceSharedIndexInformers.get(i).getIndexer(), namespace);
134+
LOG.debug(() -> "registering lister (for services) in namespace : " + namespace);
135+
serviceListers.add(lister);
136+
}
118137

119-
return sharedInformerFactory.sharedIndexInformerFor(servicesApi, V1Endpoints.class, 0L,
120-
kubernetesClientNamespace);
138+
return serviceListers;
121139
}
122140

123141
@Bean
124-
@ConditionalOnMissingBean(value = V1Service.class, parameterizedContainer = Lister.class)
125-
Lister<V1Service> servicesLister(SharedIndexInformer<V1Service> servicesSharedIndexInformer,
126-
String kubernetesClientNamespace) {
127-
return new Lister<>(servicesSharedIndexInformer.getIndexer(), kubernetesClientNamespace);
142+
@ConditionalOnMissingBean(value = V1Endpoints.class,
143+
parameterizedContainer = { List.class, SharedIndexInformer.class })
144+
List<SharedIndexInformer<V1Endpoints>> endpointsSharedIndexInformers(
145+
List<SharedInformerFactory> sharedInformerFactories, List<String> selectiveNamespaces,
146+
ApiClient apiClient) {
147+
148+
int howManyNamespaces = selectiveNamespaces.size();
149+
List<SharedIndexInformer<V1Endpoints>> endpointsSharedIndexedInformers = new ArrayList<>(howManyNamespaces);
150+
for (int i = 0; i < howManyNamespaces; ++i) {
151+
GenericKubernetesApi<V1Endpoints, V1EndpointsList> endpointsApi = new GenericKubernetesApi<>(
152+
V1Endpoints.class, V1EndpointsList.class, "", "v1", "endpoints", apiClient);
153+
SharedIndexInformer<V1Endpoints> sharedIndexInformer = sharedInformerFactories.get(i)
154+
.sharedIndexInformerFor(endpointsApi, V1Endpoints.class, 0L, selectiveNamespaces.get(i));
155+
endpointsSharedIndexedInformers.add(sharedIndexInformer);
156+
}
157+
return endpointsSharedIndexedInformers;
128158
}
129159

130160
@Bean
131-
@ConditionalOnMissingBean(value = V1Endpoints.class, parameterizedContainer = Lister.class)
132-
Lister<V1Endpoints> endpointsLister(SharedIndexInformer<V1Endpoints> endpointsSharedIndexInformer,
133-
String kubernetesClientNamespace) {
134-
return new Lister<>(endpointsSharedIndexInformer.getIndexer(), kubernetesClientNamespace);
161+
@ConditionalOnMissingBean(value = V1Endpoints.class, parameterizedContainer = { List.class, Lister.class })
162+
List<Lister<V1Endpoints>> endpointsListers(List<String> selectiveNamespaces,
163+
List<SharedIndexInformer<V1Endpoints>> serviceSharedIndexInformers) {
164+
165+
int howManyNamespaces = selectiveNamespaces.size();
166+
List<Lister<V1Endpoints>> endpointsListers = new ArrayList<>(howManyNamespaces);
167+
168+
for (int i = 0; i < howManyNamespaces; ++i) {
169+
String namespace = selectiveNamespaces.get(i);
170+
Lister<V1Endpoints> lister = new Lister<>(serviceSharedIndexInformers.get(i).getIndexer());
171+
LOG.debug(() -> "registering lister (for endpoints) in namespace : " + namespace);
172+
endpointsListers.add(lister);
173+
}
174+
175+
return endpointsListers;
135176
}
136177

137178
}

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

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties;
3838
import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryPropertiesAutoConfiguration;
3939
import org.springframework.context.annotation.Bean;
40-
import org.springframework.context.annotation.Conditional;
4140
import org.springframework.context.annotation.Configuration;
4241
import org.springframework.context.annotation.Import;
4342

@@ -48,28 +47,12 @@
4847
@ConditionalOnSpringCloudKubernetesBlockingDiscovery
4948
@AutoConfigureBefore({ SimpleDiscoveryClientAutoConfiguration.class, CommonsClientAutoConfiguration.class })
5049
@AutoConfigureAfter({ KubernetesClientAutoConfiguration.class, KubernetesDiscoveryPropertiesAutoConfiguration.class,
51-
KubernetesClientInformerAutoConfiguration.class,
52-
KubernetesClientInformerSelectiveNamespacesAutoConfiguration.class,
5350
KubernetesClientDiscoveryClientSpelAutoConfiguration.class })
5451
@Import(KubernetesDiscoveryClientHealthConfiguration.class)
5552
final class KubernetesClientInformerDiscoveryClientAutoConfiguration {
5653

5754
@Bean
5855
@ConditionalOnMissingBean
59-
@Conditional(ConditionalOnSelectiveNamespacesMissing.class)
60-
KubernetesClientInformerDiscoveryClient kubernetesClientInformerDiscoveryClient(
61-
SharedInformerFactory sharedInformerFactory, Lister<V1Service> serviceLister,
62-
Lister<V1Endpoints> endpointsLister, SharedInformer<V1Service> serviceInformer,
63-
SharedInformer<V1Endpoints> endpointsInformer, KubernetesDiscoveryProperties properties,
64-
CoreV1Api coreV1Api, Predicate<V1Service> predicate) {
65-
return new KubernetesClientInformerDiscoveryClient(List.of(sharedInformerFactory), List.of(serviceLister),
66-
List.of(endpointsLister), List.of(serviceInformer), List.of(endpointsInformer), properties, coreV1Api,
67-
predicate);
68-
}
69-
70-
@Bean
71-
@ConditionalOnMissingBean
72-
@Conditional(ConditionalOnSelectiveNamespacesPresent.class)
7356
KubernetesClientInformerDiscoveryClient selectiveNamespacesKubernetesInformerDiscoveryClient(
7457
List<SharedInformerFactory> sharedInformerFactories, List<Lister<V1Service>> serviceListers,
7558
List<Lister<V1Endpoints>> endpointsListers, List<SharedInformer<V1Service>> serviceInformers,

0 commit comments

Comments
 (0)