1
1
package com .contentgrid .gateway .runtime .servicediscovery ;
2
2
3
-
4
3
import io .fabric8 .kubernetes .api .model .LabelSelector ;
5
4
import io .fabric8 .kubernetes .api .model .LabelSelectorBuilder ;
6
5
import io .fabric8 .kubernetes .api .model .Service ;
7
- import io .fabric8 .kubernetes .api .model .discovery .v1 .EndpointSlice ;
8
6
import io .fabric8 .kubernetes .client .KubernetesClient ;
9
- import io .fabric8 .kubernetes .client .Watcher ;
10
- import io .fabric8 .kubernetes .client .WatcherException ;
11
7
import io .fabric8 .kubernetes .client .informers .ResourceEventHandler ;
12
- import java .io .Closeable ;
13
- import java .io .IOException ;
8
+ import io .fabric8 .kubernetes .client .informers .SharedIndexInformer ;
9
+ import java .time .Duration ;
10
+ import java .util .Objects ;
14
11
import lombok .RequiredArgsConstructor ;
15
12
import lombok .extern .slf4j .Slf4j ;
16
- import org .springframework .cloud .client .ServiceInstance ;
17
13
import org .springframework .cloud .kubernetes .fabric8 .loadbalancer .Fabric8ServiceInstanceMapper ;
18
14
19
15
@ Slf4j
20
16
@ RequiredArgsConstructor
21
- public class KubernetesServiceDiscovery implements ServiceDiscovery {
17
+ public class KubernetesServiceDiscovery implements ServiceDiscovery , AutoCloseable {
22
18
23
19
private final KubernetesClient client ;
24
20
private final String namespace ;
25
- private final long resyncInterval ;
21
+ private final Duration resyncInterval ;
26
22
27
23
private final ServiceAddedHandler serviceAddedHandler ;
28
24
private final ServiceDeletedHandler serviceDeletedHandler ;
29
25
30
26
private final Fabric8ServiceInstanceMapper mapper ;
31
27
28
+ private SharedIndexInformer <Service > informer ;
32
29
33
30
private static final LabelSelector selector = new LabelSelectorBuilder ()
34
31
.addToMatchLabels ("app.kubernetes.io/managed-by" , "contentgrid" )
@@ -38,7 +35,7 @@ public class KubernetesServiceDiscovery implements ServiceDiscovery {
38
35
// TODO this should by a bean-init method
39
36
@ Override
40
37
public void discoverApis () {
41
- client .services ()
38
+ this . informer = client .services ()
42
39
.inNamespace (namespace )
43
40
.withLabelSelector (selector )
44
41
.inform (new ResourceEventHandler <Service >() {
@@ -51,7 +48,10 @@ public void onAdd(Service obj) {
51
48
52
49
@ Override
53
50
public void onUpdate (Service oldObj , Service newObj ) {
54
- this .onAdd (newObj );
51
+ // Only trigger an update when the service has actually changed
52
+ if (!Objects .equals (oldObj .getMetadata ().getResourceVersion (), newObj .getMetadata ().getResourceVersion ())) {
53
+ this .onAdd (newObj );
54
+ }
55
55
}
56
56
57
57
@ Override
@@ -60,6 +60,13 @@ public void onDelete(Service obj, boolean deletedFinalStateUnknown) {
60
60
serviceDeletedHandler .handleServiceDeleted (service );
61
61
log .info ("{} deleted" , service );
62
62
}
63
- }, resyncInterval * 1000 );
63
+ }, resyncInterval .toMillis ());
64
+ }
65
+
66
+ @ Override
67
+ public void close () throws Exception {
68
+ if (this .informer != null ) {
69
+ this .informer .close ();
70
+ }
64
71
}
65
72
}
0 commit comments