Skip to content

Commit

Permalink
Merge pull request #294 from xenit-eu/ACC-1629
Browse files Browse the repository at this point in the history
Avoid triggering service updates when the service has not changed
  • Loading branch information
vierbergenlars authored Nov 4, 2024
2 parents ba26492 + cab4875 commit aed94b0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.contentgrid.gateway;

import java.time.Duration;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;

Expand All @@ -9,5 +10,5 @@ public class ServiceDiscoveryProperties {

private boolean enabled = false;
private String namespace = "default";
private long resync = 60;
private Duration resync = Duration.ofMinutes(1);
}
Original file line number Diff line number Diff line change
@@ -1,34 +1,31 @@
package com.contentgrid.gateway.runtime.servicediscovery;


import io.fabric8.kubernetes.api.model.LabelSelector;
import io.fabric8.kubernetes.api.model.LabelSelectorBuilder;
import io.fabric8.kubernetes.api.model.Service;
import io.fabric8.kubernetes.api.model.discovery.v1.EndpointSlice;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.Watcher;
import io.fabric8.kubernetes.client.WatcherException;
import io.fabric8.kubernetes.client.informers.ResourceEventHandler;
import java.io.Closeable;
import java.io.IOException;
import io.fabric8.kubernetes.client.informers.SharedIndexInformer;
import java.time.Duration;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.kubernetes.fabric8.loadbalancer.Fabric8ServiceInstanceMapper;

@Slf4j
@RequiredArgsConstructor
public class KubernetesServiceDiscovery implements ServiceDiscovery {
public class KubernetesServiceDiscovery implements ServiceDiscovery, AutoCloseable {

private final KubernetesClient client;
private final String namespace;
private final long resyncInterval;
private final Duration resyncInterval;

private final ServiceAddedHandler serviceAddedHandler;
private final ServiceDeletedHandler serviceDeletedHandler;

private final Fabric8ServiceInstanceMapper mapper;

private SharedIndexInformer<Service> informer;

private static final LabelSelector selector = new LabelSelectorBuilder()
.addToMatchLabels("app.kubernetes.io/managed-by", "contentgrid")
Expand All @@ -38,7 +35,7 @@ public class KubernetesServiceDiscovery implements ServiceDiscovery {
// TODO this should by a bean-init method
@Override
public void discoverApis() {
client.services()
this.informer = client.services()
.inNamespace(namespace)
.withLabelSelector(selector)
.inform(new ResourceEventHandler<Service>() {
Expand All @@ -51,7 +48,10 @@ public void onAdd(Service obj) {

@Override
public void onUpdate(Service oldObj, Service newObj) {
this.onAdd(newObj);
// Only trigger an update when the service has actually changed
if(!Objects.equals(oldObj.getMetadata().getResourceVersion(), newObj.getMetadata().getResourceVersion())) {
this.onAdd(newObj);
}
}

@Override
Expand All @@ -60,6 +60,13 @@ public void onDelete(Service obj, boolean deletedFinalStateUnknown) {
serviceDeletedHandler.handleServiceDeleted(service);
log.info("{} deleted", service);
}
}, resyncInterval * 1000);
}, resyncInterval.toMillis());
}

@Override
public void close() throws Exception {
if(this.informer != null) {
this.informer.close();
}
}
}

0 comments on commit aed94b0

Please sign in to comment.