Merge branch '1.1.x'

This commit is contained in:
spencergibb
2020-08-28 12:19:12 -04:00
5 changed files with 100 additions and 1 deletions

View File

@@ -130,7 +130,7 @@ public class KubernetesAutoConfiguration {
base.getHttpsProxy()))
.withProxyUsername(or(kubernetesClientProperties.getProxyUsername(),
base.getProxyUsername()))
.withPassword(or(kubernetesClientProperties.getProxyPassword(),
.withProxyPassword(or(kubernetesClientProperties.getProxyPassword(),
base.getProxyPassword()))
.withNoProxy(
or(kubernetesClientProperties.getNoProxy(), base.getNoProxy()))

View File

@@ -0,0 +1,77 @@
/*
* Copyright 2013-2019 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;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.server.mock.KubernetesServer;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.kubernetes.example.App;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = App.class, properties = {
"spring.cloud.kubernetes.client.password=mypassword",
"spring.cloud.kubernetes.client.proxy-password=myproxypassword" })
public class KubernetesAutoConfigurationTests {
@ClassRule
public static KubernetesServer server = new KubernetesServer();
@Autowired
ConfigurableApplicationContext context;
@BeforeClass
public static void setUpBeforeClass() {
KubernetesClient mockClient = server.getClient();
// Configure the kubernetes master url to point to the mock server
System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY,
mockClient.getConfiguration().getMasterUrl());
System.setProperty(Config.KUBERNETES_TRUST_CERT_SYSTEM_PROPERTY, "true");
System.setProperty(Config.KUBERNETES_AUTH_TRYKUBECONFIG_SYSTEM_PROPERTY, "false");
System.setProperty(Config.KUBERNETES_AUTH_TRYSERVICEACCOUNT_SYSTEM_PROPERTY,
"false");
System.setProperty(Config.KUBERNETES_NAMESPACE_SYSTEM_PROPERTY, "test");
System.setProperty(Config.KUBERNETES_HTTP2_DISABLE, "true");
}
@Test
public void beansAreCreated() {
assertThat(context.getBeanNamesForType(Config.class)).hasSize(1);
assertThat(context.getBeanNamesForType(KubernetesClient.class)).hasSize(1);
assertThat(context.getBeanNamesForType(StandardPodUtils.class)).hasSize(1);
assertThat(context.getBeanNamesForType(KubernetesHealthIndicator.class))
.hasSize(1);
assertThat(context.getBeanNamesForType(KubernetesInfoContributor.class))
.hasSize(1);
Config config = context.getBean(Config.class);
assertThat(config.getPassword()).isEqualTo("mypassword");
assertThat(config.getProxyPassword()).isEqualTo("myproxypassword");
}
}

View File

@@ -41,6 +41,7 @@ import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import static java.util.stream.Collectors.toMap;
import static org.springframework.cloud.kubernetes.discovery.KubernetesServiceInstance.NAMESPACE_METADATA_KEY;
/**
* Kubeneretes implementation of {@link DiscoveryClient}.
@@ -153,6 +154,10 @@ public class KubernetesDiscoveryClient implements DiscoveryClient {
endpointMetadata.putAll(portMetadata);
}
if (this.properties.isAllNamespaces()) {
endpointMetadata.put(NAMESPACE_METADATA_KEY, namespace);
}
List<EndpointAddress> addresses = s.getAddresses();
for (EndpointAddress endpointAddress : addresses) {
String instanceId = null;

View File

@@ -28,6 +28,11 @@ import org.springframework.cloud.client.ServiceInstance;
*/
public class KubernetesServiceInstance implements ServiceInstance {
/**
* Key of the namespace metadata.
*/
public static final String NAMESPACE_METADATA_KEY = "k8s_namespace";
private static final String HTTP_PREFIX = "http";
private static final String HTTPS_PREFIX = "https";
@@ -115,4 +120,8 @@ public class KubernetesServiceInstance implements ServiceInstance {
return URI.create(sb.toString());
}
public String getNamespace() {
return this.metadata != null ? this.metadata.get(NAMESPACE_METADATA_KEY) : null;
}
}

View File

@@ -377,6 +377,14 @@ public class KubernetesDiscoveryClientTest {
.hasSize(1);
assertThat(instances).filteredOn(s -> s.getHost().equals("ip2") && !s.isSecure())
.hasSize(1);
assertThat(instances)
.filteredOn(s -> s.getServiceId().contains("endpoint")
&& ((KubernetesServiceInstance) s).getNamespace().equals("test"))
.hasSize(1);
assertThat(instances)
.filteredOn(s -> s.getServiceId().contains("endpoint")
&& ((KubernetesServiceInstance) s).getNamespace().equals("test2"))
.hasSize(1);
assertThat(instances).filteredOn(s -> s.getInstanceId().equals("60")).hasSize(1);
assertThat(instances).filteredOn(s -> s.getInstanceId().equals("70")).hasSize(1);
}