diff --git a/spring-cloud-kubernetes-client-autoconfig/src/test/java/org/springframework/cloud/kubernetes/client/profile/KubernetesClientProfileEnvironmentPostProcessorNoProfileTests.java b/spring-cloud-kubernetes-client-autoconfig/src/test/java/org/springframework/cloud/kubernetes/client/profile/KubernetesClientProfileEnvironmentPostProcessorNoProfileTests.java new file mode 100644 index 00000000..e820703e --- /dev/null +++ b/spring-cloud-kubernetes-client-autoconfig/src/test/java/org/springframework/cloud/kubernetes/client/profile/KubernetesClientProfileEnvironmentPostProcessorNoProfileTests.java @@ -0,0 +1,54 @@ +/* + * Copyright 2013-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.kubernetes.client.profile; + +import io.kubernetes.client.openapi.apis.CoreV1Api; +import org.junit.jupiter.api.Test; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.core.env.Environment; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.cloud.kubernetes.commons.profile.AbstractKubernetesProfileEnvironmentPostProcessor.KUBERNETES_PROFILE; + +/** + * @author Ryan Baxter + */ +@SpringBootTest(classes = { KubernetesClientProfileEnvironmentPostProcessorNoProfileTests.App.class }) +class KubernetesClientProfileEnvironmentPostProcessorNoProfileTests { + + @Autowired + Environment environment; + + @MockBean + CoreV1Api coreV1Api; + + @Test + void whenNoKubernetesEnvironmentAndNoApiAccessThenNoProfileEnabled() { + + assertThat(environment.getActiveProfiles()).doesNotContain(KUBERNETES_PROFILE); + } + + @SpringBootApplication + static class App { + + } + +} diff --git a/spring-cloud-kubernetes-client-autoconfig/src/test/java/org/springframework/cloud/kubernetes/client/profile/KubernetesClientProfileEnvironmentPostProcessorTests.java b/spring-cloud-kubernetes-client-autoconfig/src/test/java/org/springframework/cloud/kubernetes/client/profile/KubernetesClientProfileEnvironmentPostProcessorTests.java index 0c5c7523..3953dead 100644 --- a/spring-cloud-kubernetes-client-autoconfig/src/test/java/org/springframework/cloud/kubernetes/client/profile/KubernetesClientProfileEnvironmentPostProcessorTests.java +++ b/spring-cloud-kubernetes-client-autoconfig/src/test/java/org/springframework/cloud/kubernetes/client/profile/KubernetesClientProfileEnvironmentPostProcessorTests.java @@ -16,40 +16,41 @@ package org.springframework.cloud.kubernetes.client.profile; +import io.kubernetes.client.openapi.apis.CoreV1Api; import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.boot.builder.SpringApplicationBuilder; -import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.core.env.Environment; +import static io.kubernetes.client.util.Config.ENV_SERVICE_HOST; +import static io.kubernetes.client.util.Config.ENV_SERVICE_PORT; import static org.assertj.core.api.Assertions.assertThat; import static org.springframework.cloud.kubernetes.commons.profile.AbstractKubernetesProfileEnvironmentPostProcessor.KUBERNETES_PROFILE; /** * @author Thomas Vitale */ +@SpringBootTest(properties = { ENV_SERVICE_HOST + "=10.0.0.1", ENV_SERVICE_PORT + "=80" }, + classes = { KubernetesClientProfileEnvironmentPostProcessorTests.App.class }) class KubernetesClientProfileEnvironmentPostProcessorTests { + @Autowired + Environment environment; + + @MockBean + CoreV1Api coreV1Api; + @Test void whenKubernetesEnvironmentAndNoApiAccessThenProfileEnabled() { - ConfigurableApplicationContext context = new SpringApplicationBuilder(App.class) - .web(org.springframework.boot.WebApplicationType.NONE) - .properties("KUBERNETES_SERVICE_HOST=10.0.0.1") - .run(); - - assertThat(context.getEnvironment().getActiveProfiles()).contains(KUBERNETES_PROFILE); - } - - @Test - void whenNoKubernetesEnvironmentAndNoApiAccessThenNoProfileEnabled() { - ConfigurableApplicationContext context = new SpringApplicationBuilder(App.class) - .web(org.springframework.boot.WebApplicationType.NONE) - .run(); - - assertThat(context.getEnvironment().getActiveProfiles()).doesNotContain(KUBERNETES_PROFILE); + assertThat(environment.getActiveProfiles()).contains(KUBERNETES_PROFILE); } @SpringBootApplication static class App { + } + } diff --git a/spring-cloud-kubernetes-fabric8-autoconfig/src/test/java/org/springframework/cloud/kubernetes/fabric8/profile/Fabric8ProfileEnvironmentPostProcessorTests.java b/spring-cloud-kubernetes-fabric8-autoconfig/src/test/java/org/springframework/cloud/kubernetes/fabric8/profile/Fabric8ProfileEnvironmentPostProcessorTests.java index b2ddc012..703075f3 100644 --- a/spring-cloud-kubernetes-fabric8-autoconfig/src/test/java/org/springframework/cloud/kubernetes/fabric8/profile/Fabric8ProfileEnvironmentPostProcessorTests.java +++ b/spring-cloud-kubernetes-fabric8-autoconfig/src/test/java/org/springframework/cloud/kubernetes/fabric8/profile/Fabric8ProfileEnvironmentPostProcessorTests.java @@ -33,9 +33,8 @@ class Fabric8ProfileEnvironmentPostProcessorTests { @Test void whenKubernetesEnvironmentAndNoApiAccessThenProfileEnabled() { ConfigurableApplicationContext context = new SpringApplicationBuilder(App.class) - .web(org.springframework.boot.WebApplicationType.NONE) - .properties("KUBERNETES_SERVICE_HOST=10.0.0.1") - .run(); + .web(org.springframework.boot.WebApplicationType.NONE).properties("KUBERNETES_SERVICE_HOST=10.0.0.1") + .run(); assertThat(context.getEnvironment().getActiveProfiles()).contains(KUBERNETES_PROFILE); } @@ -43,8 +42,9 @@ class Fabric8ProfileEnvironmentPostProcessorTests { @Test void whenNoKubernetesEnvironmentAndNoApiAccessThenNoProfileEnabled() { ConfigurableApplicationContext context = new SpringApplicationBuilder(App.class) - .web(org.springframework.boot.WebApplicationType.NONE).run(); + .web(org.springframework.boot.WebApplicationType.NONE).run(); assertThat(context.getEnvironment().getActiveProfiles()).doesNotContain(KUBERNETES_PROFILE); } + } diff --git a/spring-cloud-kubernetes-loadbalancer/src/main/java/org/springframework/cloud/kubernetes/loadbalancer/KubernetesServiceInstanceMapper.java b/spring-cloud-kubernetes-loadbalancer/src/main/java/org/springframework/cloud/kubernetes/loadbalancer/KubernetesServiceInstanceMapper.java index 32f43266..b0993316 100644 --- a/spring-cloud-kubernetes-loadbalancer/src/main/java/org/springframework/cloud/kubernetes/loadbalancer/KubernetesServiceInstanceMapper.java +++ b/spring-cloud-kubernetes-loadbalancer/src/main/java/org/springframework/cloud/kubernetes/loadbalancer/KubernetesServiceInstanceMapper.java @@ -100,13 +100,19 @@ 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"); diff --git a/spring-cloud-kubernetes-loadbalancer/src/test/java/org/springframework/cloud/kubernetes/loadbalancer/KubernetesServiceInstanceMapperTests.java b/spring-cloud-kubernetes-loadbalancer/src/test/java/org/springframework/cloud/kubernetes/loadbalancer/KubernetesServiceInstanceMapperTests.java index 5fb1368b..99f68058 100644 --- a/spring-cloud-kubernetes-loadbalancer/src/test/java/org/springframework/cloud/kubernetes/loadbalancer/KubernetesServiceInstanceMapperTests.java +++ b/spring-cloud-kubernetes-loadbalancer/src/test/java/org/springframework/cloud/kubernetes/loadbalancer/KubernetesServiceInstanceMapperTests.java @@ -76,6 +76,21 @@ class KubernetesServiceInstanceMapperTests { Assertions.assertTrue(instance.isSecure()); } + @Test + void testMapperSecureNullLabelsAndAnnotations() { + KubernetesLoadBalancerProperties properties = new KubernetesLoadBalancerProperties(); + KubernetesDiscoveryProperties discoveryProperties = new KubernetesDiscoveryProperties(); + List 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,14 +108,19 @@ class KubernetesServiceInstanceMapperTests { Assertions.assertEquals(2, instance.getMetadata().keySet().size()); } - private Service buildService(String name, String uid, List ports, Map 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 labels) { ServicePort servicePort = new ServicePortBuilder().withPort(port).withName(portName).build(); return buildService(name, uid, Collections.singletonList(servicePort), labels); } + private Service buildService(String name, String uid, List ports, Map labels, + Map 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 ports, Map labels) { + return buildService(name, uid, ports, labels, new HashMap<>(0)); + } + }