Skip to content

Commit

Permalink
Merge 651e20b into f846321
Browse files Browse the repository at this point in the history
  • Loading branch information
krzyzy committed Jul 21, 2018
2 parents f846321 + 651e20b commit 2a12032
Show file tree
Hide file tree
Showing 76 changed files with 1,142 additions and 2,099 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
@SpringBootApplication
public class App {

public static void main( String[] args )
{
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
Expand All @@ -12,12 +11,11 @@

public interface JudgeDPublisher {

@PutMapping(path="environments/{environment}", produces = "application/json")
void publish(@RequestParam("environment") String environment, @RequestBody Set<ServiceForm> serviceForms);
@PutMapping(path = "environments/{environment}", produces = "application/json")
void publish(@RequestParam("environment") String environment, @RequestBody Set<ServiceForm> serviceForms);

@Getter
@AllArgsConstructor
@ToString
@EqualsAndHashCode
class ServiceForm {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ public class UpdateServicesTask {
private JudgeDPublisher publisher;

@Autowired
public UpdateServicesTask(@Value("${hltech.contracts.judge-d.environment}")String environment, ServiceLocator serviceLocator, JudgeDPublisher publisher) {
public UpdateServicesTask(@Value("${hltech.contracts.judge-d.environment}") String environment, ServiceLocator serviceLocator, JudgeDPublisher publisher) {
this.environment = environment;
this.serviceLocator = serviceLocator;
this.publisher = publisher;
}

@Scheduled(fixedDelay = 5_000)
public void updateServices(){
@Scheduled(fixedDelay = 5_000, initialDelay = 10_000)
public void updateServices() {
LOGGER.debug("Searching for available services...");
Set<ServiceLocator.Service> serviceForms = serviceLocator.locateServices();
LOGGER.debug("Done - found following services: "+serviceForms);
LOGGER.debug("Done - found following services: " + serviceForms);
publisher.publish(
environment,
serviceForms.stream().map(UpdateServicesTask::toForm).collect(Collectors.toSet())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package com.hltech.contracts.judged.agent.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Sets;
import com.hltech.contracts.judged.agent.JudgeDPublisher;
import feign.Client;
import feign.Feign;
import feign.Retryer;
import feign.Target;
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClient;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -31,37 +28,22 @@
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import java.util.Set;

@Configuration
public class BeanFactory {

private static final Logger LOGGER = LoggerFactory.getLogger(BeanFactory.class);

@Bean
public JudgeDPublisher judgeDEnvironmentPublisher(Feign feign, @Value("${hltech.contracts.judge-d.baseUrl}") String baseUrl){
return new JudgeDPublisher() {

private Set<ServiceForm> previouslySentEnvironment;

@Override
public void publish(String environment, Set<ServiceForm> serviceForms) {
if (previouslySentEnvironment == null || !previouslySentEnvironment.equals(serviceForms)) {
LOGGER.info("publishing services to Judge-D: "+serviceForms);
feign.newInstance(new Target.HardCodedTarget<>(JudgeDPublisher.class, baseUrl));
previouslySentEnvironment = Sets.newHashSet(serviceForms);
} else {
LOGGER.debug("Services not changed since last update. Skipping update.");
}
}
};
public JudgeDPublisher judgeDEnvironmentPublisher(Feign feign, @Value("${hltech.contracts.judge-d.baseUrl}") String baseUrl) {
return new CachingDelegatingDPublisher(feign.newInstance(new Target.HardCodedTarget<>(JudgeDPublisher.class, baseUrl)));
}

@Bean
public Feign feign(ObjectFactory<HttpMessageConverters> messageConverters, ObjectMapper objectMapper) throws KeyManagementException, NoSuchAlgorithmException {
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(null, new TrustManager[]{new TrustAllX509TrustManager()}, null);
SSLSocketFactory trustingSSLSocketFactory = ctx.getSocketFactory();
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(null, new TrustManager[]{new TrustAllX509TrustManager()}, null);
SSLSocketFactory trustingSSLSocketFactory = ctx.getSocketFactory();

Client client = new Client.Default(
trustingSSLSocketFactory,
Expand All @@ -82,17 +64,19 @@ public Feign feign(ObjectFactory<HttpMessageConverters> messageConverters, Objec
}



private static class TrustAllX509TrustManager implements X509TrustManager {
@Override
public void checkClientTrusted(X509Certificate[] x509Certificates, String s) {}
public void checkClientTrusted(X509Certificate[] x509Certificates, String s) {
}

@Override
public void checkServerTrusted(X509Certificate[] x509Certificates, String s) {}
public void checkServerTrusted(X509Certificate[] x509Certificates, String s) {
}

@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.hltech.contracts.judged.agent.config;

import com.google.common.collect.Sets;
import com.hltech.contracts.judged.agent.JudgeDPublisher;
import lombok.extern.slf4j.Slf4j;

import java.util.Set;

@Slf4j
public class CachingDelegatingDPublisher implements JudgeDPublisher {

private final JudgeDPublisher judgeDPublisher;

private Set<ServiceForm> previouslySentEnvironment;

public CachingDelegatingDPublisher(JudgeDPublisher judgeDPublisher) {
this.judgeDPublisher = judgeDPublisher;
}

@Override
public void publish(String environment, Set<ServiceForm> serviceForms) {
if (previouslySentEnvironment == null || !previouslySentEnvironment.equals(serviceForms)) {
log.info("publishing services to Judge-D: " + serviceForms);

judgeDPublisher.publish(environment, serviceForms);

previouslySentEnvironment = Sets.newHashSet(serviceForms);
} else {
log.debug("Services not changed since last update. Skipping update.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public Set<Service> locateServices() {
String imageVersion = container.getImage().split(":")[1];

return Optional.of(new Service(
imageName.contains("/") ? imageName.substring(imageName.lastIndexOf("/")+1) : imageName,
imageName.contains("/") ? imageName.substring(imageName.lastIndexOf("/") + 1) : imageName,
imageVersion
));
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.hltech.contracts.judged.agent

import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.ActiveProfiles
import spock.lang.Specification

import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT

@SpringBootTest(webEnvironment = RANDOM_PORT, properties = ["management.port=0"])
@ActiveProfiles(["test-integration", "kubernetes"])
class AppIT extends Specification {

def test() {
when:
def a = 2
then:
a == 2
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,27 @@ package com.hltech.contracts.judged.agent

import spock.lang.Specification

import static com.hltech.contracts.judged.agent.JudgeDPublisher.*
import static com.hltech.contracts.judged.agent.ServiceLocator.*

class UpdateServicesTaskUT extends Specification {

UpdateServicesTask task;
ServiceLocator locator;
JudgeDPublisher publisher;

ServiceLocator locator = Mock()
JudgeDPublisher publisher = Mock()

def setup(){
task = new UpdateServicesTask("TEST", locator, publisher);
task = new UpdateServicesTask("TEST", locator, publisher)
}

def "UpdateServices"() {
setup:
locator.locateServices() >> [new Service("service1", "1.0"),new Service("service2", "1.0")]
when:
def a =1
task.updateServices()
then:
a ==1
1* publisher.publish("TEST", {it.size() == 2})

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.hltech.contracts.judged.agent.config

import com.hltech.contracts.judged.agent.JudgeDPublisher
import spock.lang.Specification

class CachingDelegatingDPublisherUT extends Specification {

private CachingDelegatingDPublisher publisher
private JudgeDPublisher targetPublisher

def setup() {
targetPublisher = Mock(JudgeDPublisher)
publisher = new CachingDelegatingDPublisher(targetPublisher)
}

def "should delegate to target publisher when publish for the first time"() {
given:
def forms = [new JudgeDPublisher.ServiceForm("name", "version")] as Set
when:
publisher.publish("SIT", forms)
then:
1 * targetPublisher.publish("SIT", forms)
}

def "should delegate to target publisher once when subsequently publish the same set of service multiple times "() {
given:
def forms = [new JudgeDPublisher.ServiceForm("name", "version")] as Set
when:
publisher.publish("SIT", forms)
publisher.publish("SIT", forms)
publisher.publish("SIT", forms)
then:
1 * targetPublisher.publish("SIT", forms)
}

def 'should delegate to target publisher when subsequently publish different set of services'() {
given:
def forms1 = [new JudgeDPublisher.ServiceForm("name1", "version")] as Set
def forms2 = [new JudgeDPublisher.ServiceForm("name2", "version")] as Set
when:
publisher.publish("SIT", forms1)
publisher.publish("SIT", forms2)
then:
1 * targetPublisher.publish("SIT", forms1)
then:
1 * targetPublisher.publish("SIT", forms2)
}
}
Loading

0 comments on commit 2a12032

Please sign in to comment.