Going back to snapshots

This commit is contained in:
buildmaster
2019-07-03 18:09:24 +00:00
parent 71afad6c06
commit 8293c42801
42 changed files with 132 additions and 202 deletions

View File

@@ -124,10 +124,6 @@ spring.cloud.kubernetes.discovery.enabled=false
Some Spring Cloud components use the `DiscoveryClient` in order to obtain information about the local service instance. For
this to work, you need to align the Kubernetes service name with the `spring.application.name` property.
Spring Cloud Kubernetes can also watch the Kubernetes service catalog for changes and update the
`DiscoveryClient` implementation accordingly. In order to enable this functionality you need to add
`@EnableScheduling` on a configuration class in your application.
== Kubernetes native service discovery
Kubernetes itself is capable of (server side) service discovery (see: https://kubernetes.io/docs/concepts/services-networking/service/#discovering-services).
@@ -142,7 +138,7 @@ Additionally, you can use Hystrix for:
== Kubernetes PropertySource implementations
The most common approach to configuring your Spring Boot application is to create an `application.properties` or `application.yaml` or
The most common approach to configuring your Spring Boot application is to create an `application.properties` or `applicaiton.yaml` or
an `application-profile.properties` or `application-profile.yaml` file that contains key-value pairs that provide customization values to your
application or Spring Boot starters. You can override these properties by specifying system properties or environment
variables.
@@ -324,89 +320,26 @@ However, if the `production` profile is active, the configuration becomes:
If both profiles are active, the property that appears last within the `ConfigMap` overwrites any preceding values.
Another option is to create a different config map per profile and spring boot will automatically fetch it based
on active profiles
To tell Spring Boot which `profile` should be enabled at bootstrap, you can pass a system property to the Java
command. To do so, you can launch your Spring Boot application with an environment variable that you can define with the OpenShift
`DeploymentConfig` or Kubernetes `ReplicationConfig` resource file, as follows:
====
[source,yaml]
----
kind: ConfigMap
apiVersion: v1
metadata:
name: demo
data:
application.yml: |-
greeting:
message: Say Hello to the World
farewell:
message: Say Goodbye
----
====
====
[source,yaml]
----
kind: ConfigMap
apiVersion: v1
metadata:
name: demo-development
data:
application.yml: |-
spring:
profiles: development
greeting:
message: Say Hello to the Developers
farewell:
message: Say Goodbye to the Developers
----
====
====
[source,yaml]
----
kind: ConfigMap
apiVersion: v1
metadata:
name: demo-production
data:
application.yml: |-
spring:
profiles: production
greeting:
message: Say Hello to the Ops
farewell:
message: Say Goodbye
----
====
To tell Spring Boot which `profile` should be enabled at bootstrap, you can pass `SPRING_PROFILES_ACTIVE` environment variable.
To do so, you can launch your Spring Boot application with an environment variable that you can define it in the PodSpec at the container specification.
Deployment resource file, as follows:
====
[source,yaml]
----
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment-name
labels:
app: deployment-name
kind: DeploymentConfig
spec:
replicas: 1
selector:
matchLabels:
app: deployment-name
template:
metadata:
labels:
app: deployment-name
spec:
containers:
- name: container-name
image: your-image
env:
- name: SPRING_PROFILES_ACTIVE
value: "development"
...
spec:
containers:
- env:
- name: JAVA_APP_DIR
value: /deployments
- name: JAVA_OPTIONS
value: -Dspring.profiles.active=developer
----
====
@@ -651,7 +584,7 @@ The reload feature supports two operating modes:
Any event produces a re-check on the configuration and, in case of changes, a reload.
The `view` role on the service account is required in order to listen for config map changes. A higher level role (such as `edit`) is required for secrets
(by default, secrets are not monitored).
* Polling: Periodically re-creates the configuration from config maps and secrets to see if it has changed.
* Polling: Oeriodically re-creates the configuration from config maps and secrets to see if it has changed.
You can configure the polling period by using the `spring.cloud.kubernetes.reload.period` property and defaults to 15 seconds.
It requires the same role as the monitored property source.
This means, for example, that using polling on file-mounted secret sources does not require particular privileges.
@@ -663,7 +596,7 @@ This means, for example, that using polling on file-mounted secret sources does
| `spring.cloud.kubernetes.reload.enabled` | `Boolean` | `false` | Enables monitoring of property sources and configuration reload
| `spring.cloud.kubernetes.reload.monitoring-config-maps` | `Boolean` | `true` | Allow monitoring changes in config maps
| `spring.cloud.kubernetes.reload.monitoring-secrets` | `Boolean` | `false` | Allow monitoring changes in secrets
| `spring.cloud.kubernetes.reload.strategy` | `Enum` | `refresh` | The strategy to use when firing a reload (`refresh`, `restart_context`, or `shutdown`)
| `spring.cloud.kubernetes.reload.strategy ` | `Enum` | `refresh` | The strategy to use when firing a reload (`refresh`, `restart_context`, or `shutdown`)
| `spring.cloud.kubernetes.reload.mode` | `Enum` | `event` | Specifies how to listen for changes in property sources (`event` or `polling`)
| `spring.cloud.kubernetes.reload.period` | `Duration`| `15s` | The period for verifying changes when using the `polling` strategy
|===
@@ -719,6 +652,7 @@ the `PortName` key. If you want to specify in which Kubernetes namespace the tar
the `KubernetesNamespace` key, remembering in both instances to prefix these keys with your service name and
`ribbon` prefix, as specified earlier.
.Spring Cloud Kubernetes Ribbon Configuration
|===
|Property Key |Type |Default Value
@@ -745,6 +679,7 @@ the `SERVICE` mode uses load balancing of the Kubernetes service to support Isti
* `spring.cloud.kubernetes.ribbon.cluster-domain` Set the custom Kubernetes cluster domain suffix. The default value is: 'cluster.local'
The following examples use this module for ribbon discovery:
* link:./spring-cloud-kubernetes-examples/kubernetes-circuitbreaker-ribbon-example[Spring Cloud Circuitbreaker and Ribbon]
@@ -1076,7 +1011,6 @@ $ touch .springformat
==== Intellij IDEA
In order to setup Intellij you should import our coding conventions, inspection profiles and set up the checkstyle plugin.
The following files can be found in the https://github.com/spring-cloud/spring-cloud-build/tree/master/spring-cloud-build-tools[Spring Cloud Build] project.
.spring-cloud-build-tools/
----
@@ -1121,4 +1055,4 @@ Go to `File` -> `Settings` -> `Other settings` -> `Checkstyle`. There click on t
- `checkstyle.suppressions.file` - default suppressions. Please point it to the Spring Cloud Build's, `spring-cloud-build-tools/src/checkstyle/checkstyle-suppressions.xml` file either in your cloned repo or via the `https://raw.githubusercontent.com/spring-cloud/spring-cloud-build/master/spring-cloud-build-tools/src/checkstyle/checkstyle-suppressions.xml` URL.
- `checkstyle.additional.suppressions.file` - this variable corresponds to suppressions in your local project. E.g. you're working on `spring-cloud-contract`. Then point to the `project-root/src/checkstyle/checkstyle-suppressions.xml` folder. Example for `spring-cloud-contract` would be: `/home/username/spring-cloud-contract/src/checkstyle/checkstyle-suppressions.xml`.
IMPORTANT: Remember to set the `Scan Scope` to `All sources` since we apply checkstyle rules for production and test sources.
IMPORTANT: Remember to set the `Scan Scope` to `All sources` since we apply checkstyle rules for production and test sources.

View File

@@ -8,7 +8,7 @@
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-kubernetes</artifactId>
<version>1.1.0.M1</version>
<version>1.1.0.BUILD-SNAPSHOT</version>
</parent>
<packaging>pom</packaging>
<name>Spring Cloud Kubernetes Docs</name>

10
pom.xml
View File

@@ -24,13 +24,13 @@
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-build</artifactId>
<version>2.2.0.M3</version>
<version>2.2.0.BUILD-SNAPSHOT</version>
<relativePath/>
</parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-kubernetes</artifactId>
<version>1.1.0.M1</version>
<version>1.1.0.BUILD-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Spring Cloud Kubernetes</name>
@@ -63,9 +63,9 @@
<properties>
<!-- Dependency Versions -->
<spring-cloud-commons.version>2.2.0.M1</spring-cloud-commons.version>
<spring-cloud-netflix.version>2.2.0.M1</spring-cloud-netflix.version>
<spring-cloud-config.version>2.2.0.M1</spring-cloud-config.version>
<spring-cloud-commons.version>2.2.0.BUILD-SNAPSHOT</spring-cloud-commons.version>
<spring-cloud-netflix.version>2.2.0.BUILD-SNAPSHOT</spring-cloud-netflix.version>
<spring-cloud-config.version>2.2.0.BUILD-SNAPSHOT</spring-cloud-config.version>
<!-- Maven Plugin Versions -->
<maven-compiler-plugin.version>3.5</maven-compiler-plugin.version>

View File

@@ -5,7 +5,7 @@
<parent>
<artifactId>spring-cloud-kubernetes</artifactId>
<groupId>org.springframework.cloud</groupId>
<version>1.1.0.M1</version>
<version>1.1.0.BUILD-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@@ -99,18 +99,16 @@ public class ConfigMapPropertySource extends MapPropertySource {
}
if (environment != null) {
for (String activeProfile : environment.getActiveProfiles()) {
for (String activeProfile:environment.getActiveProfiles()) {
String mapNameWithProfile = name + "-" + activeProfile;
ConfigMap mapWithProfile = StringUtils.isEmpty(namespace)
? client.configMaps().withName(mapNameWithProfile).get()
: client.configMaps().inNamespace(namespace)
.withName(mapNameWithProfile).get();
? client.configMaps().withName(mapNameWithProfile).get()
: client.configMaps().inNamespace(namespace).withName(mapNameWithProfile).get();
if (mapWithProfile != null) {
result.putAll(
processAllEntries(mapWithProfile.getData(), environment));
result.putAll(processAllEntries(mapWithProfile.getData(), environment));
}
}
@@ -127,6 +125,7 @@ public class ConfigMapPropertySource extends MapPropertySource {
return new HashMap<>();
}
private static Map<String, String> processAllEntries(Map<String, String> input,
Environment environment) {

View File

@@ -44,7 +44,8 @@ import static org.springframework.cloud.kubernetes.config.ConfigMapTestUtil.crea
properties = { "spring.application.name=configmap-path-example",
"spring.cloud.kubernetes.config.enableApi=false",
"spring.cloud.kubernetes.config.paths="
+ ConfigMapsFromFilePathsTests.FIRST_FILE_NAME_FULL_PATH + ","
+ ConfigMapsFromFilePathsTests.FIRST_FILE_NAME_FULL_PATH
+ ","
+ ConfigMapsFromFilePathsTests.SECOND_FILE_NAME_FULL_PATH })
public class ConfigMapsFromFilePathsTests {

View File

@@ -43,7 +43,9 @@ import static org.springframework.cloud.kubernetes.config.ConfigMapTestUtil.read
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
classes = App.class,
properties = { "spring.application.name=" + ConfigMapsMixedTests.APPLICATION_NAME,
properties = {
"spring.application.name="
+ ConfigMapsMixedTests.APPLICATION_NAME,
"spring.cloud.kubernetes.config.enableApi=true",
"spring.cloud.kubernetes.config.paths="
+ ConfigMapsMixedTests.FILE_NAME_FULL_PATH })

View File

@@ -42,10 +42,9 @@ import static org.springframework.cloud.kubernetes.config.ConfigMapTestUtil.read
* @author Ali Shahbour
*/
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = RANDOM_PORT, classes = App.class,
properties = {
"spring.application.name=configmap-with-active-profile-name-example",
"spring.cloud.kubernetes.reload.enabled=false" })
@SpringBootTest(webEnvironment = RANDOM_PORT, classes = App.class, properties = {
"spring.application.name=configmap-with-active-profile-name-example",
"spring.cloud.kubernetes.reload.enabled=false" })
@ActiveProfiles("development")
@AutoConfigureWebTestClient
public class ConfigMapsWithActiveProfilesNameTests {

View File

@@ -22,7 +22,7 @@
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-kubernetes</artifactId>
<version>1.1.0.M1</version>
<version>1.1.0.BUILD-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@@ -43,8 +43,7 @@ public class KubernetesProfileEnvironmentPostProcessor
public void postProcessEnvironment(ConfigurableEnvironment environment,
SpringApplication application) {
final String enabledStr = environment
.getProperty("spring.cloud.kubernetes.enabled", "true");
final String enabledStr = environment.getProperty("spring.cloud.kubernetes.enabled", "true");
if ("false".equals(enabledStr.toLowerCase())) {
return;
}

View File

@@ -23,11 +23,11 @@
<parent>
<artifactId>spring-cloud-dependencies-parent</artifactId>
<groupId>org.springframework.cloud</groupId>
<version>2.2.0.M3</version>
<version>2.2.0.BUILD-SNAPSHOT</version>
<relativePath/>
</parent>
<artifactId>spring-cloud-kubernetes-dependencies</artifactId>
<version>1.1.0.M1</version>
<version>1.1.0.BUILD-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Spring Cloud Kubernetes :: Dependencies</name>
<description>Spring Cloud Kubernetes Dependencies</description>

View File

@@ -22,7 +22,7 @@
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-kubernetes</artifactId>
<version>1.1.0.M1</version>
<version>1.1.0.BUILD-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@@ -23,7 +23,7 @@
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>kubernetes-circuitbreaker-ribbon-example</artifactId>
<version>1.1.0.M1</version>
<version>1.1.0.BUILD-SNAPSHOT</version>
</parent>
<artifactId>greeting-service</artifactId>

View File

@@ -23,7 +23,7 @@
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>kubernetes-circuitbreaker-ribbon-example</artifactId>
<version>1.1.0.M1</version>
<version>1.1.0.BUILD-SNAPSHOT</version>
</parent>
<artifactId>name-service</artifactId>

View File

@@ -23,7 +23,7 @@
<parent>
<artifactId>spring-cloud-kubernetes-examples</artifactId>
<groupId>org.springframework.cloud</groupId>
<version>1.1.0.M1</version>
<version>1.1.0.BUILD-SNAPSHOT</version>
</parent>
<groupId>org.springframework.cloud</groupId>

View File

@@ -5,7 +5,7 @@
<parent>
<artifactId>spring-cloud-kubernetes-examples</artifactId>
<groupId>org.springframework.cloud</groupId>
<version>1.1.0.M1</version>
<version>1.1.0.BUILD-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@@ -6,7 +6,7 @@
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-kubernetes-examples</artifactId>
<version>1.1.0.M1</version>
<version>1.1.0.BUILD-SNAPSHOT</version>
</parent>
<artifactId>kubernetes-leader-election-example</artifactId>

View File

@@ -22,7 +22,7 @@
<parent>
<artifactId>spring-cloud-kubernetes-examples</artifactId>
<groupId>org.springframework.cloud</groupId>
<version>1.1.0.M1</version>
<version>1.1.0.BUILD-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@@ -22,7 +22,7 @@
<parent>
<artifactId>spring-cloud-kubernetes-examples</artifactId>
<groupId>org.springframework.cloud</groupId>
<version>1.1.0.M1</version>
<version>1.1.0.BUILD-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@@ -23,7 +23,7 @@
<parent>
<artifactId>spring-cloud-kubernetes</artifactId>
<groupId>org.springframework.cloud</groupId>
<version>1.1.0.M1</version>
<version>1.1.0.BUILD-SNAPSHOT</version>
</parent>
<artifactId>spring-cloud-kubernetes-examples</artifactId>

View File

@@ -7,7 +7,7 @@
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>discovery-parent</artifactId>
<version>1.1.0.M1</version>
<version>1.1.0.BUILD-SNAPSHOT</version>
</parent>
<artifactId>discovery-client</artifactId>

View File

@@ -7,7 +7,7 @@
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>discovery-parent</artifactId>
<version>1.1.0.M1</version>
<version>1.1.0.BUILD-SNAPSHOT</version>
</parent>
<artifactId>discovery-service-a</artifactId>

View File

@@ -7,7 +7,7 @@
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>discovery-parent</artifactId>
<version>1.1.0.M1</version>
<version>1.1.0.BUILD-SNAPSHOT</version>
</parent>
<artifactId>discovery-service-b</artifactId>

View File

@@ -7,7 +7,7 @@
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-kubernetes-integration-tests</artifactId>
<version>1.1.0.M1</version>
<version>1.1.0.BUILD-SNAPSHOT</version>
</parent>
<name>Spring Cloud Kubernetes :: Integration Tests :: Discovery Parent</name>

View File

@@ -7,7 +7,7 @@
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>discovery-parent</artifactId>
<version>1.1.0.M1</version>
<version>1.1.0.BUILD-SNAPSHOT</version>
</parent>
<artifactId>tests</artifactId>

View File

@@ -7,7 +7,7 @@
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-kubernetes-integration-tests</artifactId>
<version>1.1.0.M1</version>
<version>1.1.0.BUILD-SNAPSHOT</version>
</parent>
<name>Spring Cloud Kubernetes :: Integration Tests :: Istio</name>

View File

@@ -6,7 +6,7 @@
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-kubernetes</artifactId>
<version>1.1.0.M1</version>
<version>1.1.0.BUILD-SNAPSHOT</version>
</parent>
<artifactId>spring-cloud-kubernetes-integration-tests</artifactId>

View File

@@ -7,7 +7,7 @@
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-kubernetes-integration-tests</artifactId>
<version>1.1.0.M1</version>
<version>1.1.0.BUILD-SNAPSHOT</version>
</parent>
<name>Spring Cloud Kubernetes :: Integration Tests :: Simple Configmap</name>

View File

@@ -7,7 +7,7 @@
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-kubernetes-integration-tests</artifactId>
<version>1.1.0.M1</version>
<version>1.1.0.BUILD-SNAPSHOT</version>
</parent>
<name>Spring Cloud Kubernetes :: Integration Tests :: Simple Core</name>

View File

@@ -5,7 +5,7 @@
<parent>
<artifactId>spring-cloud-kubernetes</artifactId>
<groupId>org.springframework.cloud</groupId>
<version>1.1.0.M1</version>
<version>1.1.0.BUILD-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@@ -22,7 +22,7 @@
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-kubernetes</artifactId>
<version>1.1.0.M1</version>
<version>1.1.0.BUILD-SNAPSHOT</version>
</parent>
<artifactId>spring-cloud-kubernetes-leader</artifactId>

View File

@@ -33,7 +33,8 @@ import static org.hamcrest.Matchers.containsString;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = {
// Make sure test passes without Kubernetes cluster
"spring.cloud.kubernetes.leader.autoStartup=false" })
"spring.cloud.kubernetes.leader.autoStartup=false"
})
public class LeaderAutoConfigurationTests {
@Value("${local.server.port}")

View File

@@ -22,7 +22,7 @@
<parent>
<artifactId>spring-cloud-kubernetes</artifactId>
<groupId>org.springframework.cloud</groupId>
<version>1.1.0.M1</version>
<version>1.1.0.BUILD-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@@ -32,7 +32,6 @@ import org.apache.commons.logging.LogFactory;
/**
* the KubernetesEndpointsServerList description.
*
* @author wuzishu
*/
public class KubernetesEndpointsServerList extends KubernetesServerList {
@@ -41,11 +40,11 @@ public class KubernetesEndpointsServerList extends KubernetesServerList {
/**
* Instantiates a new Kubernetes endpoints server list.
*
* @param client the client
* @param properties the properties
*/
KubernetesEndpointsServerList(KubernetesClient client,
KubernetesRibbonProperties properties) {
KubernetesEndpointsServerList(KubernetesClient client, KubernetesRibbonProperties properties) {
super(client, properties);
}
@@ -53,16 +52,16 @@ public class KubernetesEndpointsServerList extends KubernetesServerList {
public List<Server> getUpdatedListOfServers() {
List<Server> result = new ArrayList<>();
Endpoints endpoints = StringUtils.isNotBlank(this.getNamespace())
? this.getClient().endpoints().inNamespace(this.getNamespace())
.withName(this.getServiceId()).get()
: this.getClient().endpoints().withName(this.getServiceId()).get();
? this.getClient().endpoints().inNamespace(this.getNamespace())
.withName(this.getServiceId()).get()
: this.getClient().endpoints().withName(this.getServiceId()).get();
if (endpoints != null) {
if (LOG.isDebugEnabled()) {
LOG.debug(String.format(
"Found [%d] endpoints in l [%s] for name [%s] and portName [%s]",
endpoints.getSubsets().size(),
endpoints.getMetadata().getNamespace(), this.getServiceId(),
this.getPortName()));
"Found [%d] endpoints in l [%s] for name [%s] and portName [%s]",
endpoints.getSubsets().size(),
endpoints.getMetadata().getNamespace(), this.getServiceId(),
this.getPortName()));
}
for (EndpointSubset subset : endpoints.getSubsets()) {
@@ -75,9 +74,10 @@ public class KubernetesEndpointsServerList extends KubernetesServerList {
else {
for (EndpointPort port : subset.getPorts()) {
if (Utils.isNullOrEmpty(this.getPortName())
|| this.getPortName().endsWith(port.getName())) {
|| this.getPortName().endsWith(port.getName())) {
for (EndpointAddress address : subset.getAddresses()) {
result.add(new Server(address.getIp(), port.getPort()));
result.add(
new Server(address.getIp(), port.getPort()));
}
}
}
@@ -86,11 +86,10 @@ public class KubernetesEndpointsServerList extends KubernetesServerList {
}
if (result.isEmpty()) {
LOG.warn(String.format(
"Did not find any endpoints in ribbon in namespace [%s] for name [%s] and portName [%s]",
this.getNamespace(), this.getServiceId(), this.getPortName()));
"Did not find any endpoints in ribbon in namespace [%s] for name [%s] and portName [%s]",
this.getNamespace(), this.getServiceId(), this.getPortName()));
}
return result;
}
}

View File

@@ -40,7 +40,7 @@ public class KubernetesRibbonClientConfiguration {
@Bean
@ConditionalOnMissingBean
public ServerList<?> ribbonServerList(KubernetesClient client, IClientConfig config,
KubernetesRibbonProperties properties) {
KubernetesRibbonProperties properties) {
KubernetesServerList serverList;
if (properties.getMode() == KubernetesRibbonMode.SERVICE) {
serverList = new KubernetesServicesServerList(client, properties);

View File

@@ -32,7 +32,7 @@ import io.fabric8.kubernetes.client.KubernetesClient;
* @author wuzishu
*/
public abstract class KubernetesServerList extends AbstractServerList<Server>
implements ServerList<Server> {
implements ServerList<Server> {
private static final int FIRST = 0;
@@ -48,11 +48,12 @@ public abstract class KubernetesServerList extends AbstractServerList<Server>
/**
* Instantiates a new Kubernetes server list.
*
* @param client the client
* @param properties the properties
*/
public KubernetesServerList(KubernetesClient client,
KubernetesRibbonProperties properties) {
KubernetesRibbonProperties properties) {
this.client = client;
this.properties = properties;
}
@@ -60,17 +61,19 @@ public abstract class KubernetesServerList extends AbstractServerList<Server>
public void initWithNiwsConfig(IClientConfig clientConfig) {
this.serviceId = clientConfig.getClientName();
this.namespace = clientConfig.getPropertyAsString(KubernetesConfigKey.Namespace,
this.client.getNamespace());
this.client.getNamespace());
this.portName = clientConfig.getPropertyAsString(KubernetesConfigKey.PortName,
null);
null);
}
public List<Server> getInitialListOfServers() {
return Collections.emptyList();
}
/**
* Gets first.
*
* @return the first
*/
static int getFIRST() {
@@ -79,6 +82,7 @@ public abstract class KubernetesServerList extends AbstractServerList<Server>
/**
* Gets client.
*
* @return the client
*/
KubernetesClient getClient() {
@@ -87,6 +91,7 @@ public abstract class KubernetesServerList extends AbstractServerList<Server>
/**
* Gets service id.
*
* @return the service id
*/
String getServiceId() {
@@ -95,6 +100,7 @@ public abstract class KubernetesServerList extends AbstractServerList<Server>
/**
* Gets namespace.
*
* @return the namespace
*/
String getNamespace() {
@@ -103,6 +109,7 @@ public abstract class KubernetesServerList extends AbstractServerList<Server>
/**
* Gets port name.
*
* @return the port name
*/
String getPortName() {
@@ -111,10 +118,10 @@ public abstract class KubernetesServerList extends AbstractServerList<Server>
/**
* Gets properties.
*
* @return the properties
*/
KubernetesRibbonProperties getProperties() {
return properties;
}
}

View File

@@ -30,20 +30,18 @@ import org.apache.commons.logging.LogFactory;
/**
* the KubernetesServicesServerList description.
*
* @author wuzishu
*/
public class KubernetesServicesServerList extends KubernetesServerList {
private static final Log LOG = LogFactory.getLog(KubernetesServicesServerList.class);
/**
* Instantiates a new Kubernetes services server list.
*
* @param client the client
* @param properties the properties
*/
KubernetesServicesServerList(KubernetesClient client,
KubernetesRibbonProperties properties) {
KubernetesServicesServerList(KubernetesClient client, KubernetesRibbonProperties properties) {
super(client, properties);
}
@@ -54,32 +52,31 @@ public class KubernetesServicesServerList extends KubernetesServerList {
*/
private String concatServiceFQDN(Service service) {
return String.format("%s.%s.svc.%s", service.getMetadata().getName(),
StringUtils.isNotBlank(service.getMetadata().getNamespace())
? service.getMetadata().getNamespace() : "default",
this.getProperties().getClusterDomain());
StringUtils.isNotBlank(service.getMetadata().getNamespace()) ? service.getMetadata()
.getNamespace() : "default", this.getProperties().getClusterDomain());
}
@Override
public List<Server> getUpdatedListOfServers() {
List<Server> result = new ArrayList<>();
Service service = StringUtils.isNotBlank(this.getNamespace())
? this.getClient().services().inNamespace(this.getNamespace())
.withName(this.getServiceId()).get()
: this.getClient().services().withName(this.getServiceId()).get();
? this.getClient().services().inNamespace(this.getNamespace())
.withName(this.getServiceId()).get()
: this.getClient().services().withName(this.getServiceId()).get();
if (service != null) {
if (LOG.isDebugEnabled()) {
LOG.debug("Found Service[" + service.getMetadata().getName() + "]");
}
if (service.getSpec().getPorts().size() == 1) {
result.add(new Server(this.concatServiceFQDN(service),
service.getSpec().getPorts().get(0).getPort()));
service.getSpec().getPorts().get(0).getPort()));
}
else {
for (ServicePort servicePort : service.getSpec().getPorts()) {
if (Utils.isNotNullOrEmpty(this.getPortName())
|| this.getPortName().endsWith(servicePort.getName())) {
|| this.getPortName().endsWith(servicePort.getName())) {
result.add(new Server(concatServiceFQDN(service),
servicePort.getPort()));
servicePort.getPort()));
}
}
@@ -87,10 +84,9 @@ public class KubernetesServicesServerList extends KubernetesServerList {
}
if (result.isEmpty()) {
LOG.warn(String.format(
"Did not find any service in ribbon in namespace [%s] for name [%s] and portName [%s]",
this.getNamespace(), this.getServiceId(), this.getPortName()));
"Did not find any service in ribbon in namespace [%s] for name [%s] and portName [%s]",
this.getNamespace(), this.getServiceId(), this.getPortName()));
}
return result;
}
}

View File

@@ -43,24 +43,22 @@ import static org.assertj.core.api.Assertions.assertThat;
/**
* the RibbonWithServiceModeTest description.
*
* @author wuzishu
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestApplication.class,
properties = { "spring.application.name=testapp",
"spring.cloud.kubernetes.client.namespace=testns",
"spring.cloud.kubernetes.client.trustCerts=true",
"spring.cloud.kubernetes.config.namespace=testns",
"spring.cloud.kubernetes.enabled=true",
"spring.cloud.kubernetes.discovery.enabled=true",
"spring.cloud.kubernetes.ribbon.enabled=true",
"spring.cloud.kubernetes.ribbon.mode=SERVICE",
"spring.cloud.kubernetes.ribbon.clusterDomain=test.com" })
@SpringBootTest(classes = TestApplication.class, properties = {
"spring.application.name=testapp",
"spring.cloud.kubernetes.client.namespace=testns",
"spring.cloud.kubernetes.client.trustCerts=true",
"spring.cloud.kubernetes.config.namespace=testns",
"spring.cloud.kubernetes.enabled=true",
"spring.cloud.kubernetes.discovery.enabled=true",
"spring.cloud.kubernetes.ribbon.enabled=true",
"spring.cloud.kubernetes.ribbon.mode=SERVICE",
"spring.cloud.kubernetes.ribbon.clusterDomain=test.com"})
@EnableAutoConfiguration
@EnableDiscoveryClient
public class RibbonWithServiceModeTest {
@ClassRule
public static KubernetesServer server = new KubernetesServer();
@@ -78,22 +76,21 @@ public class RibbonWithServiceModeTest {
// Configure the kubernetes master url to point to the mock server
System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY,
mockClient.getConfiguration().getMasterUrl());
mockClient.getConfiguration().getMasterUrl());
System.setProperty(Config.KUBERNETES_TRUST_CERT_SYSTEM_PROPERTY, "true");
System.setProperty(Config.KUBERNETES_AUTH_TRYKUBECONFIG_SYSTEM_PROPERTY, "false");
System.setProperty(Config.KUBERNETES_AUTH_TRYSERVICEACCOUNT_SYSTEM_PROPERTY,
"false");
"false");
// Configured
server.expect().get().withPath("/api/v1/namespaces/testns/services/testapp")
.andReturn(200, new ServiceBuilder().withNewMetadata().withName("testapp")
.withNamespace("testns").endMetadata().withNewSpec()
.addToSelector("app", "testapp-a").addNewPort().withName("http")
.withPort(mockEndpointA.getMockServer().getPort())
.withTargetPort(
new IntOrString(mockEndpointA.getMockServer().getPort()))
.withProtocol("TCP").endPort().endSpec().build())
.always();
server.expect().get().withPath("/api/v1/namespaces/testns/services/testapp").andReturn(200,
new ServiceBuilder().withNewMetadata().withName("testapp").withNamespace("testns")
.endMetadata().withNewSpec().addToSelector("app", "testapp-a")
.addNewPort().withName("http")
.withPort(mockEndpointA.getMockServer().getPort())
.withTargetPort(new IntOrString(mockEndpointA.getMockServer().getPort()))
.withProtocol("TCP").endPort().endSpec().build())
.always();
}
@@ -102,14 +99,10 @@ public class RibbonWithServiceModeTest {
@Test
public void testGreetingWithServiceMode() {
SpringClientFactory springClientFactory = context
.getBean(SpringClientFactory.class);
SpringClientFactory springClientFactory = context.getBean(SpringClientFactory.class);
ILoadBalancer testapp = springClientFactory.getLoadBalancer("testapp");
List<Server> allServers = testapp.getAllServers();
assertThat(allServers.stream()
.map(c -> String.format("%s:%s", c.getHost(), c.getPort())))
.containsOnly("testapp.testns.svc.test.com:"
+ mockEndpointA.getMockServer().getPort());
assertThat(allServers.stream().map(c -> String.format("%s:%s", c.getHost(), c.getPort())))
.containsOnly("testapp.testns.svc.test.com:" + mockEndpointA.getMockServer().getPort());
}
}

View File

@@ -22,7 +22,7 @@
<parent>
<artifactId>spring-cloud-kubernetes</artifactId>
<groupId>org.springframework.cloud</groupId>
<version>1.1.0.M1</version>
<version>1.1.0.BUILD-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@@ -22,7 +22,7 @@
<parent>
<artifactId>spring-cloud-kubernetes</artifactId>
<groupId>org.springframework.cloud</groupId>
<version>1.1.0.M1</version>
<version>1.1.0.BUILD-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@@ -22,7 +22,7 @@
<parent>
<artifactId>spring-cloud-kubernetes</artifactId>
<groupId>org.springframework.cloud</groupId>
<version>1.1.0.M1</version>
<version>1.1.0.BUILD-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@@ -22,7 +22,7 @@
<parent>
<artifactId>spring-cloud-kubernetes</artifactId>
<groupId>org.springframework.cloud</groupId>
<version>1.1.0.M1</version>
<version>1.1.0.BUILD-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>