Skip to content

Commit

Permalink
Create LoadBalancer by hand instead of via annotation
Browse files Browse the repository at this point in the history
Fixes #1505
  • Loading branch information
dsyer committed Feb 1, 2021
1 parent 70259f3 commit 46f55e4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 17 deletions.
Expand Up @@ -51,7 +51,16 @@ public MyService myService() {

public static class MyService {

@KubernetesEndpointsLoadBalanced(namespace = "default", name = "kubernetes")
private LoadBalancer defaultKubernetesLoadBalancer;

public MyService(Lister<V1Endpoints> lister) {
InformerEndpointsGetter getter = new InformerEndpointsGetter(lister);
RoundRobinLoadBalanceStrategy strategy = new RoundRobinLoadBalanceStrategy();
defaultKubernetesLoadBalancer = new EndpointsLoadBalancer(
() -> getter.get("default", "kubernetes"),
strategy
);
}

}
}
Expand Up @@ -14,11 +14,14 @@

import io.kubernetes.client.informer.cache.Lister;
import io.kubernetes.client.openapi.models.V1Endpoints;
import org.springframework.beans.factory.annotation.Autowired;

public class InformerEndpointsGetter implements EndpointsGetter {

@Autowired private Lister<V1Endpoints> endpointsLister;
private final Lister<V1Endpoints> endpointsLister;

public InformerEndpointsGetter(Lister<V1Endpoints> lister) {
this.endpointsLister = lister;
}

@Override
public V1Endpoints get(String namespace, String name) {
Expand Down
Expand Up @@ -12,10 +12,14 @@
*/
package io.kubernetes.client.spring.extended.network;

import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThrows;

import io.kubernetes.client.extended.network.EndpointsLoadBalancer;
import io.kubernetes.client.extended.network.LoadBalanceStrategy;
import io.kubernetes.client.extended.network.LoadBalancer;
import io.kubernetes.client.extended.network.RoundRobinLoadBalanceStrategy;
import io.kubernetes.client.extended.network.exception.NoAvailableAddressException;
import io.kubernetes.client.informer.cache.Cache;
import io.kubernetes.client.informer.cache.Lister;
Expand All @@ -24,8 +28,8 @@
import io.kubernetes.client.openapi.models.V1EndpointSubset;
import io.kubernetes.client.openapi.models.V1Endpoints;
import io.kubernetes.client.openapi.models.V1ObjectMeta;
import io.kubernetes.client.spring.extended.network.annotation.KubernetesEndpointsLoadBalanced;
import io.kubernetes.client.spring.extended.network.endpoints.EndpointsGetter;
import io.kubernetes.client.spring.extended.network.endpoints.InformerEndpointsGetter;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -53,28 +57,32 @@ public Lister<V1Endpoints> endpointsLister(Cache<V1Endpoints> endpointsCache) {
}

@Bean
public MyBean myBean() {
return new MyBean();
public MyBean myBean(Lister<V1Endpoints> lister) {
return new MyBean(new MyEndpointGetter(), new MyStrategy(), lister);
}
}

static class MyBean {
@KubernetesEndpointsLoadBalanced(namespace = "default", name = "no-such")

public MyBean(
MyEndpointGetter myEndpointGetter, MyStrategy myStrategy, Lister<V1Endpoints> lister) {
InformerEndpointsGetter getter = new InformerEndpointsGetter(lister);
RoundRobinLoadBalanceStrategy strategy = new RoundRobinLoadBalanceStrategy();
noSuchLoadBalancer =
new EndpointsLoadBalancer(() -> getter.get("default", "no-such"), strategy);
fooLoadBalancer = new EndpointsLoadBalancer(() -> getter.get("default", "foo"), strategy);
customStrategyLoadBalancer =
new EndpointsLoadBalancer(() -> getter.get("default", "foo"), myStrategy);
customEndpointGetterLoadBalancer =
new EndpointsLoadBalancer(() -> myEndpointGetter.get("default", "foo"), strategy);
}

private LoadBalancer noSuchLoadBalancer;

@KubernetesEndpointsLoadBalanced(namespace = "default", name = "foo")
private LoadBalancer fooLoadBalancer;

@KubernetesEndpointsLoadBalanced(
namespace = "default",
name = "foo",
strategy = MyStrategy.class)
private LoadBalancer customStrategyLoadBalancer;

@KubernetesEndpointsLoadBalanced(
namespace = "default",
name = "foo",
endpointsGetter = MyEndpointGetter.class)
private LoadBalancer customEndpointGetterLoadBalancer;
}

Expand Down Expand Up @@ -109,6 +117,7 @@ public V1Endpoints get(String namespace, String name) {
new V1EndpointSubset()
.addAddressesItem(new V1EndpointAddress().ip("127.0.0.1"))
.addPortsItem(new V1EndpointPort().port(8080)));

@Autowired private MyBean myBean;

@Autowired private Cache<V1Endpoints> endpointsCache;
Expand Down

0 comments on commit 46f55e4

Please sign in to comment.