Merge remote-tracking branch 'origin/1.1.x'
This commit is contained in:
@@ -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 {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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");
|
||||
|
||||
@@ -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,14 +108,19 @@ 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).withName(portName).build();
|
||||
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));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user