Check for null in labels and annotations. Fixes #613

This commit is contained in:
Ryan Baxter
2020-11-24 14:00:31 -05:00
parent e616bfd949
commit e877e34146
2 changed files with 40 additions and 15 deletions

View File

@@ -105,15 +105,20 @@ public class KubernetesServiceInstanceMapper {
}
private boolean isSecure(Service service, ServicePort port) {
final String securedLabelValue = service.getMetadata().getLabels()
.getOrDefault("secured", "false");
if (securedLabelValue.equals("true")) {
return true;
if (service.getMetadata().getLabels() != null) {
final String securedLabelValue = service.getMetadata().getLabels()
.getOrDefault("secured", "false");
if (securedLabelValue.equals("true")) {
return true;
}
}
final String securedAnnotationValue = service.getMetadata().getAnnotations()
.getOrDefault("secured", "false");
if (securedAnnotationValue.equals("true")) {
return true;
if (service.getMetadata().getAnnotations() != null) {
final String securedAnnotationValue = service.getMetadata().getAnnotations()
.getOrDefault("secured", "false");
if (securedAnnotationValue.equals("true")) {
return true;
}
}
return (port.getName() != null && port.getName().endsWith("https"))
|| port.getPort().toString().endsWith("443");

View File

@@ -76,6 +76,21 @@ class KubernetesServiceInstanceMapperTests {
Assertions.assertTrue(instance.isSecure());
}
@Test
void testMapperSecureNullLabelsAndAnnotations() {
KubernetesLoadBalancerProperties properties = new KubernetesLoadBalancerProperties();
KubernetesDiscoveryProperties discoveryProperties = new KubernetesDiscoveryProperties();
List<ServicePort> ports = new ArrayList<>();
ports.add(new ServicePortBuilder().withPort(443).build());
Service service = buildService("test", "abc", ports, null, null);
KubernetesServiceInstance instance = new KubernetesServiceInstanceMapper(
properties, discoveryProperties).map(service);
Assertions.assertNotNull(instance);
Assertions.assertEquals("test", instance.getServiceId());
Assertions.assertEquals("abc", instance.getInstanceId());
Assertions.assertTrue(instance.isSecure());
}
@Test
void testMapperSecureWithLabels() {
KubernetesLoadBalancerProperties properties = new KubernetesLoadBalancerProperties();
@@ -93,13 +108,6 @@ class KubernetesServiceInstanceMapperTests {
Assertions.assertEquals(2, instance.getMetadata().keySet().size());
}
private Service buildService(String name, String uid, List<ServicePort> ports,
Map<String, String> labels) {
return new ServiceBuilder().withNewMetadata().withName(name).withNewUid(uid)
.addToLabels(labels).addToAnnotations(new HashMap<>(0)).endMetadata()
.withNewSpec().addAllToPorts(ports).endSpec().build();
}
private Service buildService(String name, String uid, int port, String portName,
Map<String, String> labels) {
ServicePort servicePort = new ServicePortBuilder().withPort(port)
@@ -107,4 +115,16 @@ class KubernetesServiceInstanceMapperTests {
return buildService(name, uid, Collections.singletonList(servicePort), labels);
}
private Service buildService(String name, String uid, List<ServicePort> ports,
Map<String, String> labels, Map<String, String> annotations) {
return new ServiceBuilder().withNewMetadata().withName(name).withNewUid(uid)
.addToLabels(labels).withAnnotations(annotations).endMetadata()
.withNewSpec().addAllToPorts(ports).endSpec().build();
}
private Service buildService(String name, String uid, List<ServicePort> ports,
Map<String, String> labels) {
return buildService(name, uid, ports, labels, new HashMap<>(0));
}
}