Skip to content

Commit

Permalink
Removed unnecessary publics
Browse files Browse the repository at this point in the history
  • Loading branch information
wlodarcp committed Oct 7, 2021
1 parent b6c4574 commit ed75f20
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 157 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
@Component
public class UpdateServicesTask {

private String environment;
private String space;
private ServiceLocator serviceLocator;
private JudgeDPublisher publisher;
private final String environment;
private final String space;
private final ServiceLocator serviceLocator;
private final JudgeDPublisher publisher;

@Autowired
public UpdateServicesTask(
UpdateServicesTask(
@Value("${hltech.contracts.judge-d.environment}") String environment,
@Value("${hltech.contracts.judge-d.space:default}") String space,
ServiceLocator serviceLocator,
Expand Down Expand Up @@ -49,6 +49,4 @@ public void updateServices() {
private static JudgeDPublisher.ServiceForm toForm(ServiceLocator.Service service) {
return new JudgeDPublisher.ServiceForm(service.getName(), service.getVersion());
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,20 @@

@Configuration
@EnableScheduling
public class BeanFactory {
class BeanFactory {

@Bean
public ObjectMapper objectMapper() {
ObjectMapper objectMapper() {
return new ObjectMapper();
}

@Bean
public JudgeDPublisher judgeDEnvironmentPublisher(Feign feign, @Value("${hltech.contracts.judge-d.baseUrl}") String baseUrl) {
JudgeDPublisher judgeDEnvironmentPublisher(Feign feign, @Value("${hltech.contracts.judge-d.baseUrl}") String baseUrl) {
return feign.newInstance(new Target.HardCodedTarget<>(JudgeDPublisher.class, baseUrl));
}

@Bean
public Feign feign(ObjectFactory<HttpMessageConverters> messageConverters, ObjectMapper objectMapper) throws KeyManagementException, NoSuchAlgorithmException {
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();
Expand Down Expand Up @@ -81,5 +81,4 @@ public X509Certificate[] getAcceptedIssuers() {
return null;
}
}

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

import com.hltech.judged.agent.JudgeDPublisher;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.HashSet;
import java.util.Set;

@Slf4j
public class CachingDelegatingDPublisher {
@RequiredArgsConstructor
class CachingDelegatingDPublisher {

private final JudgeDPublisher judgeDPublisher;

private Set<JudgeDPublisher.ServiceForm> previouslySentEnvironment;

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
@Configuration
@Profile("consul")
@RequiredArgsConstructor
public class ConsulBeanFactory {
class ConsulBeanFactory {

@Value("${hltech.contracts.judge-d.consul-host}")
private String consulUrl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@
import java.util.Set;
import java.util.stream.Collectors;


@RequiredArgsConstructor
public class ConsulTagBasedServiceLocator implements ServiceLocator {
class ConsulTagBasedServiceLocator implements ServiceLocator {

private static final String VERSION_TAG_PREFIX = "version=";
private static final String TAG_SEPARATOR = "=";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@

@Configuration
@Profile("kubernetes")
public class K8sBeanFactory {
class K8sBeanFactory {

@Bean
public KubernetesClient kubernetesClient() {
KubernetesClient kubernetesClient() {
return new DefaultKubernetesClient();
}

@Bean
public ServiceLocator serviceLocator(
ServiceLocator serviceLocator(
KubernetesClient kubernetesClient,
@Value("${hltech.contracts.judge-d.requiredLabel}") String requiredLabel,
@Value("${hltech.contracts.judge-d.excluded-namespaces:#{''}}") Set<String> excludedNamespaces,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import static java.util.stream.Collectors.toList;

public class K8sLabelBasedServiceLocator implements ServiceLocator {
class K8sLabelBasedServiceLocator implements ServiceLocator {

private static final String EXCLUDE_FROM_JURISDICTION_LABEL = "exclude-from-judged-jurisdiction";

Expand All @@ -23,7 +23,7 @@ public class K8sLabelBasedServiceLocator implements ServiceLocator {
private final Predicate<Namespace> excludedNamespacesFilter;
private final Predicate<Namespace> includedNamespacesFilter;

public K8sLabelBasedServiceLocator(
K8sLabelBasedServiceLocator(
KubernetesClient kubernetesClient,
String requiredLabel,
Set<String> excludedNamespaces,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package com.hltech.judged.agent

import com.github.tomakehurst.wiremock.junit.WireMockRule
import io.fabric8.kubernetes.api.model.*
import io.fabric8.kubernetes.api.model.ContainerBuilder
import io.fabric8.kubernetes.api.model.NamespaceListBuilder
import io.fabric8.kubernetes.api.model.PodBuilder
import io.fabric8.kubernetes.api.model.PodList
import io.fabric8.kubernetes.api.model.PodListBuilder
import io.fabric8.kubernetes.api.model.PodSpecBuilder
import io.fabric8.kubernetes.api.model.PodStatusBuilder
import io.fabric8.kubernetes.client.Config
import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer
import org.springframework.beans.factory.annotation.Value
Expand All @@ -12,7 +18,11 @@ import spock.lang.Specification

import java.util.concurrent.TimeUnit

import static com.github.tomakehurst.wiremock.client.WireMock.*
import static com.github.tomakehurst.wiremock.client.WireMock.equalToJson
import static com.github.tomakehurst.wiremock.client.WireMock.ok
import static com.github.tomakehurst.wiremock.client.WireMock.put
import static com.github.tomakehurst.wiremock.client.WireMock.putRequestedFor
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching
import static org.awaitility.Awaitility.await

@SpringBootTest
Expand Down Expand Up @@ -45,37 +55,37 @@ class K8sServiceLocatorFT extends Specification {

def 'should send update services message with expected names and versions'() {
given: 'expected namespaces'
k8sServer.expect().get().withPath('/api/v1/namespaces')
.andReturn(200, new NamespaceListBuilder()
.addNewItem()
.withNewMetadata()
.withName('firstnamespace')
.withLabels([:])
.endMetadata()
.and()
.addNewItem()
.withNewMetadata()
.withName('secondnamespace')
.withLabels([:])
.endMetadata()
.and()
.build())
.always()
k8sServer.expect().get().withPath('/api/v1/namespaces')
.andReturn(200, new NamespaceListBuilder()
.addNewItem()
.withNewMetadata()
.withName('firstnamespace')
.withLabels([:])
.endMetadata()
.and()
.addNewItem()
.withNewMetadata()
.withName('secondnamespace')
.withLabels([:])
.endMetadata()
.and()
.build())
.always()

and: 'expected pods'
PodList expectedPodList = new PodListBuilder()
.withItems(
buildPod('firstnamespace', 1, '78d588f9cc'),
buildPod('firstnamespace', 2, '54d576f9dh'),
buildPod('secondnamespace', 3, '98d500g9df')
).build()
PodList expectedPodList = new PodListBuilder()
.withItems(
buildPod('firstnamespace', 1, '78d588f9cc'),
buildPod('firstnamespace', 2, '54d576f9dh'),
buildPod('secondnamespace', 3, '98d500g9df')
).build()

k8sServer.expect().get().withPath('/api/v1/pods?labelSelector=app')
.andReturn(HttpURLConnection.HTTP_OK, expectedPodList)
.always()
k8sServer.expect().get().withPath('/api/v1/pods?labelSelector=app')
.andReturn(HttpURLConnection.HTTP_OK, expectedPodList)
.always()

and: 'expected message to publish'
def expectedRequestBody = '''
def expectedRequestBody = '''
[
{
"name": "test_app_1",
Expand All @@ -92,16 +102,16 @@ class K8sServiceLocatorFT extends Specification {
]
'''

wireMockRule.stubFor(
put(urlPathMatching('/environments/test'))
.withRequestBody(equalToJson(expectedRequestBody, true, false))
.willReturn(ok())
)
wireMockRule.stubFor(
put(urlPathMatching('/environments/test'))
.withRequestBody(equalToJson(expectedRequestBody, true, false))
.willReturn(ok())
)


expect: 'update services message is sent with expected services names and versions'
await().atMost(updateServicesInitialDelay + 5000, TimeUnit.MILLISECONDS).until({ wireMockRule.getServeEvents().requests.size() == 1 })
wireMockRule.verify(1, putRequestedFor(urlPathMatching('/environments/test')).withRequestBody(equalToJson(expectedRequestBody, true, false)))
await().atMost(updateServicesInitialDelay + 5000, TimeUnit.MILLISECONDS).until({ wireMockRule.getServeEvents().requests.size() == 1 })
wireMockRule.verify(1, putRequestedFor(urlPathMatching('/environments/test')).withRequestBody(equalToJson(expectedRequestBody, true, false)))
}

def buildPod(String namespace, int index, String version) {
Expand Down
Loading

0 comments on commit ed75f20

Please sign in to comment.