Skip to content

Commit c0e154f

Browse files
author
Ryan Baxter
committed
Merge branch 'yue9944882-informer-based-discovery-client'
2 parents d71eaff + 40cef54 commit c0e154f

File tree

79 files changed

+1290
-127
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+1290
-127
lines changed

pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@
9393
<module>spring-cloud-kubernetes-client-config</module>
9494
<module>spring-cloud-kubernetes-fabric8-autoconfig</module>
9595
<module>spring-cloud-kubernetes-fabric8-config</module>
96-
<module>spring-cloud-kubernetes-discovery</module>
96+
<module>spring-cloud-kubernetes-fabric8-discovery</module>
97+
<module>spring-cloud-kubernetes-client-discovery</module>
9798
<module>spring-cloud-starter-kubernetes</module>
9899
<module>spring-cloud-starter-kubernetes-config</module>
99100
<module>spring-cloud-starter-kubernetes-all</module>

spring-cloud-kubernetes-client-autoconfig/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
<groupId>io.kubernetes</groupId>
2525
<artifactId>client-java-extended</artifactId>
2626
</dependency>
27+
<dependency>
28+
<groupId>io.kubernetes</groupId>
29+
<artifactId>client-java-spring-integration</artifactId>
30+
</dependency>
2731
<dependency>
2832
<groupId>org.springframework.boot</groupId>
2933
<artifactId>spring-boot-actuator-autoconfigure</artifactId>

spring-cloud-kubernetes-client-autoconfig/src/main/java/org/springframework/cloud/kubernetes/client/KubernetesClientAutoConfiguration.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,16 @@ public class KubernetesClientAutoConfiguration {
4646

4747
@Bean
4848
@ConditionalOnMissingBean
49-
public CoreV1Api coreApi() throws IOException {
49+
public ApiClient apiClient() throws IOException {
5050
ApiClient apiClient = kubernetesApiClient();
5151
io.kubernetes.client.openapi.Configuration.setDefaultApiClient(apiClient);
52-
return new CoreV1Api();
52+
return apiClient;
53+
}
54+
55+
@Bean
56+
@ConditionalOnMissingBean
57+
public CoreV1Api coreApi(ApiClient apiClient) throws IOException {
58+
return new CoreV1Api(apiClient);
5359
}
5460

5561
@Bean

spring-cloud-kubernetes-client-autoconfig/src/test/java/org/springframework/cloud/kubernetes/client/profile/KubernetesClientProfileEnvironmentPostProcessorNoProfileTests.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

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

19+
import io.kubernetes.client.openapi.ApiClient;
1920
import io.kubernetes.client.openapi.apis.CoreV1Api;
2021
import org.junit.jupiter.api.Test;
2122

@@ -40,6 +41,9 @@ class KubernetesClientProfileEnvironmentPostProcessorNoProfileTests {
4041
@MockBean
4142
CoreV1Api coreV1Api;
4243

44+
@MockBean
45+
ApiClient apiClient;
46+
4347
@Test
4448
void whenNoKubernetesEnvironmentAndNoApiAccessThenNoProfileEnabled() {
4549

spring-cloud-kubernetes-client-autoconfig/src/test/java/org/springframework/cloud/kubernetes/client/profile/KubernetesClientProfileEnvironmentPostProcessorTests.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

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

19+
import io.kubernetes.client.openapi.ApiClient;
1920
import io.kubernetes.client.openapi.apis.CoreV1Api;
2021
import org.junit.jupiter.api.Test;
2122

@@ -43,6 +44,9 @@ class KubernetesClientProfileEnvironmentPostProcessorTests {
4344
@MockBean
4445
CoreV1Api coreV1Api;
4546

47+
@MockBean
48+
ApiClient apiClient;
49+
4650
@Test
4751
void whenKubernetesEnvironmentAndNoApiAccessThenProfileEnabled() {
4852
assertThat(environment.getActiveProfiles()).contains(KUBERNETES_PROFILE);
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>spring-cloud-kubernetes</artifactId>
7+
<groupId>org.springframework.cloud</groupId>
8+
<version>2.0.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>spring-cloud-kubernetes-client-discovery</artifactId>
13+
<name>Spring Cloud Kubernetes :: Kubernetes Client Discovery</name>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>org.springframework.cloud</groupId>
18+
<artifactId>spring-cloud-kubernetes-client-autoconfig</artifactId>
19+
<version>${project.version}</version>
20+
</dependency>
21+
<dependency>
22+
<groupId>org.springframework.cloud</groupId>
23+
<artifactId>spring-cloud-commons</artifactId>
24+
<version>${spring-cloud-commons.version}</version>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-actuator</artifactId>
29+
<optional>true</optional>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.springframework.boot</groupId>
33+
<artifactId>spring-boot-autoconfigure</artifactId>
34+
<optional>true</optional>
35+
</dependency>
36+
37+
<!-- Testing Dependencies -->
38+
<dependency>
39+
<groupId>org.springframework.boot</groupId>
40+
<artifactId>spring-boot-starter-test</artifactId>
41+
<scope>test</scope>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.junit.vintage</groupId>
45+
<artifactId>junit-vintage-engine</artifactId>
46+
<scope>test</scope>
47+
</dependency>
48+
<dependency>
49+
<groupId>org.springframework.boot</groupId>
50+
<artifactId>spring-boot-starter-web</artifactId>
51+
<scope>test</scope>
52+
</dependency>
53+
<dependency>
54+
<groupId>org.springframework.cloud</groupId>
55+
<artifactId>spring-cloud-config-client</artifactId>
56+
<scope>test</scope>
57+
</dependency>
58+
59+
</dependencies>
60+
61+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2019-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.kubernetes.client.discovery;
18+
19+
import java.lang.annotation.Documented;
20+
import java.lang.annotation.ElementType;
21+
import java.lang.annotation.Inherited;
22+
import java.lang.annotation.Retention;
23+
import java.lang.annotation.RetentionPolicy;
24+
import java.lang.annotation.Target;
25+
26+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
27+
28+
@Target(ElementType.TYPE)
29+
@Retention(RetentionPolicy.RUNTIME)
30+
@Documented
31+
@Inherited
32+
@ConditionalOnProperty(value = "spring.cloud.kubernetes.discovery.enabled", matchIfMissing = true)
33+
public @interface ConditionalOnKubernetesDiscoveryEnabled {
34+
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* Copyright 2013-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.kubernetes.client.discovery;
18+
19+
import io.kubernetes.client.informer.SharedInformer;
20+
import io.kubernetes.client.informer.SharedInformerFactory;
21+
import io.kubernetes.client.informer.cache.Lister;
22+
import io.kubernetes.client.openapi.ApiClient;
23+
import io.kubernetes.client.openapi.models.V1Endpoints;
24+
import io.kubernetes.client.openapi.models.V1EndpointsList;
25+
import io.kubernetes.client.openapi.models.V1Service;
26+
import io.kubernetes.client.openapi.models.V1ServiceList;
27+
import io.kubernetes.client.spring.extended.controller.KubernetesInformerFactoryProcessor;
28+
import io.kubernetes.client.spring.extended.controller.annotation.GroupVersionResource;
29+
import io.kubernetes.client.spring.extended.controller.annotation.KubernetesInformer;
30+
import io.kubernetes.client.spring.extended.controller.annotation.KubernetesInformers;
31+
32+
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
33+
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
34+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
35+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
36+
import org.springframework.cloud.client.CommonsClientAutoConfiguration;
37+
import org.springframework.cloud.client.ConditionalOnBlockingDiscoveryEnabled;
38+
import org.springframework.cloud.client.discovery.simple.SimpleDiscoveryClientAutoConfiguration;
39+
import org.springframework.cloud.kubernetes.client.KubernetesClientAutoConfiguration;
40+
import org.springframework.cloud.kubernetes.client.discovery.gson.EndpointsTrimmingStrategy;
41+
import org.springframework.cloud.kubernetes.client.discovery.gson.ServiceTrimmingStrategy;
42+
import org.springframework.cloud.kubernetes.commons.KubernetesClientProperties;
43+
import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties;
44+
import org.springframework.context.annotation.Bean;
45+
import org.springframework.context.annotation.Configuration;
46+
47+
@Configuration(proxyBeanMethods = false)
48+
@ConditionalOnKubernetesDiscoveryEnabled
49+
@AutoConfigureBefore({ SimpleDiscoveryClientAutoConfiguration.class, CommonsClientAutoConfiguration.class })
50+
@AutoConfigureAfter({ KubernetesClientAutoConfiguration.class })
51+
@EnableConfigurationProperties(KubernetesDiscoveryProperties.class)
52+
public class KubernetesDiscoveryClientAutoConfiguration {
53+
54+
@Configuration(proxyBeanMethods = false)
55+
@ConditionalOnBlockingDiscoveryEnabled
56+
public static class KubernetesInformerDiscoveryConfiguration {
57+
58+
@Bean
59+
@ConditionalOnMissingBean
60+
public KubernetesInformerFactoryProcessor kubernetesInformerFactoryProcessor() {
61+
return new KubernetesInformerFactoryProcessor();
62+
}
63+
64+
@Bean
65+
@ConditionalOnMissingBean
66+
public CatalogSharedInformerFactory catalogSharedInformerFactory(ApiClient apiClient) {
67+
apiClient.getJSON()
68+
.setGson(apiClient.getJSON().getGson().newBuilder()
69+
.addDeserializationExclusionStrategy(new ServiceTrimmingStrategy())
70+
.addDeserializationExclusionStrategy(new EndpointsTrimmingStrategy()).create());
71+
return new CatalogSharedInformerFactory();
72+
}
73+
74+
@Bean
75+
@ConditionalOnMissingBean
76+
public KubernetesInformerDiscoveryClient kubernetesInformerDiscoveryClient(
77+
KubernetesClientProperties kubernetesClientProperties,
78+
CatalogSharedInformerFactory sharedInformerFactory, Lister<V1Service> serviceLister,
79+
Lister<V1Endpoints> endpointsLister, SharedInformer<V1Service> serviceInformer,
80+
SharedInformer<V1Endpoints> endpointsInformer, KubernetesDiscoveryProperties properties) {
81+
return new KubernetesInformerDiscoveryClient(kubernetesClientProperties.getNamespace(),
82+
sharedInformerFactory, serviceLister, endpointsLister, serviceInformer, endpointsInformer,
83+
properties);
84+
}
85+
86+
@KubernetesInformers({
87+
@KubernetesInformer(apiTypeClass = V1Service.class, apiListTypeClass = V1ServiceList.class,
88+
groupVersionResource = @GroupVersionResource(apiGroup = "", apiVersion = "v1",
89+
resourcePlural = "services")),
90+
@KubernetesInformer(apiTypeClass = V1Endpoints.class, apiListTypeClass = V1EndpointsList.class,
91+
groupVersionResource = @GroupVersionResource(apiGroup = "", apiVersion = "v1",
92+
resourcePlural = "endpoints")) })
93+
class CatalogSharedInformerFactory extends SharedInformerFactory {
94+
95+
// TODO: optimization to ease memory pressure from continuous list&watch.
96+
97+
}
98+
99+
}
100+
101+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2013-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.kubernetes.client.discovery;
18+
19+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
20+
import org.springframework.cloud.kubernetes.client.KubernetesClientAutoConfiguration;
21+
import org.springframework.context.annotation.Configuration;
22+
import org.springframework.context.annotation.Import;
23+
24+
@Configuration(proxyBeanMethods = false)
25+
@ConditionalOnProperty("spring.cloud.config.discovery.enabled")
26+
@Import({ KubernetesClientAutoConfiguration.class, KubernetesDiscoveryClientAutoConfiguration.class })
27+
public class KubernetesDiscoveryClientConfigClientBootstrapConfiguration {
28+
29+
}

0 commit comments

Comments
 (0)