forked from spring-cloud/spring-cloud-kubernetes
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
19 changed files
with
1,423 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
~ Copyright 2013-2018 the original author or authors. | ||
~ | ||
~ Licensed under the Apache License, Version 2.0 (the "License"); | ||
~ you may not use this file except in compliance with the License. | ||
~ You may obtain a copy of the License at | ||
~ | ||
~ https://www.apache.org/licenses/LICENSE-2.0 | ||
~ | ||
~ Unless required by applicable law or agreed to in writing, software | ||
~ distributed under the License is distributed on an "AS IS" BASIS, | ||
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
~ See the License for the specific language governing permissions and | ||
~ limitations under the License. | ||
~ | ||
--> | ||
|
||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns="http://maven.apache.org/POM/4.0.0" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>spring-cloud-kubernetes</artifactId> | ||
<groupId>org.springframework.cloud</groupId> | ||
<version>2.0.0.BUILD-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>org.springframework.cloud</groupId> | ||
<artifactId>spring-cloud-kubernetes-ribbon</artifactId> | ||
<name>Spring Cloud Kubernetes :: Ribbon</name> | ||
|
||
<properties> | ||
<service.occurrence>1</service.occurrence> | ||
</properties> | ||
|
||
<dependencies> | ||
|
||
<dependency> | ||
<groupId>org.springframework.cloud</groupId> | ||
<artifactId>spring-cloud-kubernetes-discovery</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
<optional>true</optional> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.cloud</groupId> | ||
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId> | ||
<optional>true</optional> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>io.fabric8</groupId> | ||
<artifactId>kubernetes-client</artifactId> | ||
<type>test-jar</type> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>io.fabric8</groupId> | ||
<artifactId>kubernetes-server-mock</artifactId> | ||
<scope>test</scope> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>com.fasterxml.jackson.core</groupId> | ||
<artifactId>jackson-databind</artifactId> | ||
</exclusion> | ||
<exclusion> | ||
<groupId>com.fasterxml.jackson.core</groupId> | ||
<artifactId>jackson-core</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.spockframework</groupId> | ||
<artifactId>spock-spring</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.codehaus.groovy</groupId> | ||
<artifactId>groovy-all</artifactId> | ||
<version>${groovy.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-configuration-processor</artifactId> | ||
<optional>true</optional> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-surefire-plugin</artifactId> | ||
<configuration> | ||
<argLine>-Dservice</argLine> | ||
<systemPropertyVariables> | ||
<service.occurrence>${service.occurrence}</service.occurrence> | ||
</systemPropertyVariables> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
138 changes: 138 additions & 0 deletions
138
...ribbon/src/main/java/org/springframework/cloud/kubernetes/ribbon/KubernetesConfigKey.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
/* | ||
* Copyright 2013-2019 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.cloud.kubernetes.ribbon; | ||
|
||
import java.lang.reflect.Field; | ||
import java.lang.reflect.Modifier; | ||
import java.lang.reflect.ParameterizedType; | ||
import java.lang.reflect.Type; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
import com.netflix.client.config.IClientConfigKey; | ||
|
||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* Kubernetes implementation of a Ribbon {@link IClientConfigKey}. | ||
* | ||
* @param <T> type of key | ||
* @author Ioannis Canellos | ||
*/ | ||
public abstract class KubernetesConfigKey<T> implements IClientConfigKey<T> { | ||
|
||
/** | ||
* Namespace configuration key. | ||
*/ | ||
public static final IClientConfigKey<String> Namespace = new KubernetesConfigKey<String>( | ||
"KubernetesNamespace") { | ||
}; | ||
|
||
/** | ||
* Port name configuration key. | ||
*/ | ||
public static final IClientConfigKey<String> PortName = new KubernetesConfigKey<String>( | ||
"PortName") { | ||
}; | ||
|
||
private static final Set<IClientConfigKey> keys = new HashSet<IClientConfigKey>(); | ||
|
||
static { | ||
for (Field f : KubernetesConfigKey.class.getDeclaredFields()) { | ||
if (Modifier.isStatic(f.getModifiers()) // && | ||
// Modifier.isPublic(f.getModifiers()) | ||
&& IClientConfigKey.class.isAssignableFrom(f.getType())) { | ||
try { | ||
keys.add((IClientConfigKey) f.get(null)); | ||
} | ||
catch (IllegalAccessException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private final String configKey; | ||
|
||
private final Class<T> type; | ||
|
||
@SuppressWarnings("unchecked") | ||
protected KubernetesConfigKey(String configKey) { | ||
this.configKey = configKey; | ||
Type superclass = getClass().getGenericSuperclass(); | ||
Assert.isTrue(superclass instanceof ParameterizedType, | ||
superclass + " isn't parameterized"); | ||
Type runtimeType = ((ParameterizedType) superclass).getActualTypeArguments()[0]; | ||
this.type = (Class<T>) Types.rawType(runtimeType); | ||
} | ||
|
||
/** | ||
* @deprecated see {@link #keys()} | ||
* @return array of {@link IClientConfigKey} | ||
*/ | ||
@Deprecated | ||
public static IClientConfigKey[] values() { | ||
return keys().toArray(new IClientConfigKey[0]); | ||
} | ||
|
||
/** | ||
* @return all the public static keys defined in this class | ||
*/ | ||
public static Set<IClientConfigKey> keys() { | ||
return keys; | ||
} | ||
|
||
public static IClientConfigKey valueOf(final String name) { | ||
for (IClientConfigKey key : keys()) { | ||
if (key.key().equals(name)) { | ||
return key; | ||
} | ||
} | ||
return new IClientConfigKey() { | ||
@Override | ||
public String key() { | ||
return name; | ||
} | ||
|
||
@Override | ||
public Class type() { | ||
return String.class; | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
public Class<T> type() { | ||
return this.type; | ||
} | ||
|
||
/* | ||
* (non-Javadoc) | ||
* | ||
* @see com.netflix.niws.client.ClientConfig#key() | ||
*/ | ||
@Override | ||
public String key() { | ||
return this.configKey; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return this.configKey; | ||
} | ||
|
||
} |
96 changes: 96 additions & 0 deletions
96
.../main/java/org/springframework/cloud/kubernetes/ribbon/KubernetesEndpointsServerList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Copyright 2013-2019 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.cloud.kubernetes.ribbon; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import com.netflix.loadbalancer.Server; | ||
import io.fabric8.kubernetes.api.model.EndpointAddress; | ||
import io.fabric8.kubernetes.api.model.EndpointPort; | ||
import io.fabric8.kubernetes.api.model.EndpointSubset; | ||
import io.fabric8.kubernetes.api.model.Endpoints; | ||
import io.fabric8.kubernetes.client.KubernetesClient; | ||
import io.fabric8.kubernetes.client.utils.Utils; | ||
import org.apache.commons.lang.StringUtils; | ||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
|
||
/** | ||
* the KubernetesEndpointsServerList description. | ||
* | ||
* @author wuzishu | ||
*/ | ||
public class KubernetesEndpointsServerList extends KubernetesServerList { | ||
|
||
private static final Log LOG = LogFactory.getLog(KubernetesEndpointsServerList.class); | ||
|
||
/** | ||
* Instantiates a new Kubernetes endpoints server list. | ||
* @param client the client | ||
* @param properties the properties | ||
*/ | ||
KubernetesEndpointsServerList(KubernetesClient client, | ||
KubernetesRibbonProperties properties) { | ||
super(client, properties); | ||
} | ||
|
||
@Override | ||
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(); | ||
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())); | ||
} | ||
for (EndpointSubset subset : endpoints.getSubsets()) { | ||
|
||
if (subset.getPorts().size() == 1) { | ||
EndpointPort port = subset.getPorts().get(getFIRST()); | ||
for (EndpointAddress address : subset.getAddresses()) { | ||
result.add(new Server(address.getIp(), port.getPort())); | ||
} | ||
} | ||
else { | ||
for (EndpointPort port : subset.getPorts()) { | ||
if (Utils.isNullOrEmpty(this.getPortName()) | ||
|| this.getPortName().endsWith(port.getName())) { | ||
for (EndpointAddress address : subset.getAddresses()) { | ||
result.add(new Server(address.getIp(), port.getPort())); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
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())); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
} |
Oops, something went wrong.