Kubernetes info and health clean-up + tests (#707)
* initial commit * 7 properties, not 6 * more changes to tests/info/health
This commit is contained in:
@@ -16,43 +16,44 @@
|
||||
|
||||
package org.springframework.cloud.kubernetes.client;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
import io.kubernetes.client.openapi.models.V1Pod;
|
||||
|
||||
import org.springframework.cloud.kubernetes.commons.AbstractKubernetesHealthIndicator;
|
||||
import org.springframework.cloud.kubernetes.commons.PodUtils;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
/**
|
||||
* @author Ryan Baxter
|
||||
*/
|
||||
public class KubernetesClientHealthIndicator extends AbstractKubernetesHealthIndicator {
|
||||
|
||||
private PodUtils<V1Pod> utils;
|
||||
private final PodUtils<V1Pod> utils;
|
||||
|
||||
public KubernetesClientHealthIndicator(PodUtils<V1Pod> utils) {
|
||||
this.utils = utils;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Map<String, Object> getDetails() throws Exception {
|
||||
Map<String, Object> details = new HashMap<>();
|
||||
protected Map<String, Object> getDetails() {
|
||||
V1Pod current = this.utils.currentPod().get();
|
||||
if (current != null) {
|
||||
Map<String, Object> details = CollectionUtils.newHashMap(8);
|
||||
details.put(INSIDE, true);
|
||||
details.put(NAMESPACE, current.getMetadata().getNamespace());
|
||||
details.put(POD_NAME, current.getMetadata().getName());
|
||||
details.put(LABELS, current.getMetadata().getLabels());
|
||||
details.put(POD_IP, current.getStatus().getPodIP());
|
||||
details.put(HOST_IP, current.getStatus().getHostIP());
|
||||
details.put(SERVICE_ACCOUNT, current.getSpec().getServiceAccountName());
|
||||
details.put(NODE_NAME, current.getSpec().getNodeName());
|
||||
details.put(HOST_IP, current.getStatus().getHostIP());
|
||||
details.put(LABELS, current.getMetadata().getLabels());
|
||||
return details;
|
||||
}
|
||||
else {
|
||||
details.put(INSIDE, false);
|
||||
return Collections.singletonMap(INSIDE, false);
|
||||
}
|
||||
return details;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,20 +16,21 @@
|
||||
|
||||
package org.springframework.cloud.kubernetes.client;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
import io.kubernetes.client.openapi.models.V1Pod;
|
||||
|
||||
import org.springframework.cloud.kubernetes.commons.AbstractKubernetesInfoContributor;
|
||||
import org.springframework.cloud.kubernetes.commons.PodUtils;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
/**
|
||||
* @author Ryan Baxter
|
||||
*/
|
||||
public class KubernetesClientInfoContributor extends AbstractKubernetesInfoContributor {
|
||||
|
||||
private PodUtils<V1Pod> utils;
|
||||
private final PodUtils<V1Pod> utils;
|
||||
|
||||
public KubernetesClientInfoContributor(PodUtils<V1Pod> utils) {
|
||||
this.utils = utils;
|
||||
@@ -38,18 +39,18 @@ public class KubernetesClientInfoContributor extends AbstractKubernetesInfoContr
|
||||
@Override
|
||||
public Map<String, Object> getDetails() {
|
||||
V1Pod current = this.utils.currentPod().get();
|
||||
Map<String, Object> details = new HashMap<>();
|
||||
boolean inside = current != null;
|
||||
details.put(INSIDE, inside);
|
||||
if (inside) {
|
||||
if (current != null) {
|
||||
Map<String, Object> details = CollectionUtils.newHashMap(7);
|
||||
details.put(INSIDE, true);
|
||||
details.put(NAMESPACE, current.getMetadata().getNamespace());
|
||||
details.put(POD_NAME, current.getMetadata().getName());
|
||||
details.put(POD_IP, current.getStatus().getPodIP());
|
||||
details.put(SERVICE_ACCOUNT, current.getSpec().getServiceAccountName());
|
||||
details.put(NODE_NAME, current.getSpec().getNodeName());
|
||||
details.put(POD_IP, current.getStatus().getPodIP());
|
||||
details.put(HOST_IP, current.getStatus().getHostIP());
|
||||
return details;
|
||||
}
|
||||
return details;
|
||||
return Collections.singletonMap(INSIDE, false);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.cloud.kubernetes.client.example.App;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* @author wind57
|
||||
*/
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = App.class)
|
||||
public class KubernetesClientHealthIndicatorInsideTests {
|
||||
|
||||
@Autowired
|
||||
private ApplicationContext context;
|
||||
|
||||
// test that the bean responsible for health is present.
|
||||
// the actual fields it must provide are tested in
|
||||
// KubernetesClientHealthIndicatorTests.
|
||||
@Test
|
||||
public void test() {
|
||||
assertThat(context.getBean(KubernetesClientHealthIndicator.class)).isNotNull();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.cloud.kubernetes.client.example.App;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
/**
|
||||
* @author wind57
|
||||
*/
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = App.class,
|
||||
properties = { "management.health.kubernetes.enabled=false" })
|
||||
public class KubernetesClientHealthIndicatorNotInsideTests {
|
||||
|
||||
@Autowired
|
||||
private ApplicationContext context;
|
||||
|
||||
// test that the bean responsible for info contribution is NOT present.
|
||||
@Test
|
||||
public void test() {
|
||||
assertThatThrownBy(() -> context.getBean(KubernetesClientHealthIndicator.class))
|
||||
.isInstanceOf(NoSuchBeanDefinitionException.class);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -16,13 +16,9 @@
|
||||
|
||||
package org.springframework.cloud.kubernetes.client;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import io.kubernetes.client.openapi.models.V1ObjectMeta;
|
||||
import io.kubernetes.client.openapi.models.V1Pod;
|
||||
import io.kubernetes.client.openapi.models.V1PodSpec;
|
||||
import io.kubernetes.client.openapi.models.V1PodStatus;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
@@ -32,6 +28,21 @@ import org.springframework.cloud.kubernetes.commons.PodUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.springframework.cloud.kubernetes.client.KubernetesClientHealthIndicator.HOST_IP;
|
||||
import static org.springframework.cloud.kubernetes.client.KubernetesClientHealthIndicator.INSIDE;
|
||||
import static org.springframework.cloud.kubernetes.client.KubernetesClientHealthIndicator.LABELS;
|
||||
import static org.springframework.cloud.kubernetes.client.KubernetesClientHealthIndicator.NAMESPACE;
|
||||
import static org.springframework.cloud.kubernetes.client.KubernetesClientHealthIndicator.NODE_NAME;
|
||||
import static org.springframework.cloud.kubernetes.client.KubernetesClientHealthIndicator.POD_IP;
|
||||
import static org.springframework.cloud.kubernetes.client.KubernetesClientHealthIndicator.POD_NAME;
|
||||
import static org.springframework.cloud.kubernetes.client.KubernetesClientHealthIndicator.SERVICE_ACCOUNT;
|
||||
import static org.springframework.cloud.kubernetes.client.StubProvider.STUB_HOST_IP;
|
||||
import static org.springframework.cloud.kubernetes.client.StubProvider.STUB_LABELS;
|
||||
import static org.springframework.cloud.kubernetes.client.StubProvider.STUB_NAMESPACE;
|
||||
import static org.springframework.cloud.kubernetes.client.StubProvider.STUB_NODE_NAME;
|
||||
import static org.springframework.cloud.kubernetes.client.StubProvider.STUB_POD_IP;
|
||||
import static org.springframework.cloud.kubernetes.client.StubProvider.STUB_POD_NAME;
|
||||
import static org.springframework.cloud.kubernetes.client.StubProvider.STUB_SERVICE_ACCOUNT;
|
||||
|
||||
/**
|
||||
* @author Ryan Baxter
|
||||
@@ -43,48 +54,32 @@ class KubernetesClientHealthIndicatorTests {
|
||||
private PodUtils<V1Pod> utils;
|
||||
|
||||
@Test
|
||||
void getDetailsNotInsideTest() throws Exception {
|
||||
void getDetailsNotInsideTest() {
|
||||
when(utils.currentPod()).thenReturn(() -> null);
|
||||
KubernetesClientHealthIndicator healthIndicator = new KubernetesClientHealthIndicator(utils);
|
||||
assertThat(healthIndicator.getDetails().containsKey(KubernetesClientHealthIndicator.INSIDE)).isTrue();
|
||||
assertThat(healthIndicator.getDetails().get(KubernetesClientHealthIndicator.INSIDE)).isEqualTo(false);
|
||||
Map<String, Object> details = healthIndicator.getDetails();
|
||||
|
||||
assertThat(details.containsKey(INSIDE)).isTrue();
|
||||
assertThat(details.get(INSIDE)).isEqualTo(false);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getDetailsInsideTest() throws Exception {
|
||||
Map<String, String> labels = new HashMap<>();
|
||||
labels.put("spring", "cloud");
|
||||
V1ObjectMeta metaData = new V1ObjectMeta();
|
||||
metaData.setLabels(labels);
|
||||
metaData.setName("mypod");
|
||||
metaData.setNamespace("default");
|
||||
void getDetailsInsideTest() {
|
||||
|
||||
V1PodStatus status = new V1PodStatus();
|
||||
status.setPodIP("127.0.0.1");
|
||||
status.setHostIP("123.456.789.1");
|
||||
|
||||
V1PodSpec spec = new V1PodSpec();
|
||||
spec.setNodeName("nodeName");
|
||||
spec.setServiceAccountName("serviceAccount");
|
||||
|
||||
V1Pod pod = new V1Pod();
|
||||
pod.setMetadata(metaData);
|
||||
pod.setStatus(status);
|
||||
pod.setSpec(spec);
|
||||
|
||||
when(utils.currentPod()).thenReturn(() -> pod);
|
||||
when(utils.currentPod()).thenReturn(StubProvider::stubPod);
|
||||
KubernetesClientHealthIndicator healthIndicator = new KubernetesClientHealthIndicator(utils);
|
||||
assertThat(healthIndicator.getDetails().containsKey(KubernetesClientHealthIndicator.INSIDE)).isTrue();
|
||||
assertThat(healthIndicator.getDetails().get(KubernetesClientHealthIndicator.INSIDE)).isEqualTo(true);
|
||||
assertThat(healthIndicator.getDetails().get(KubernetesClientHealthIndicator.HOST_IP))
|
||||
.isEqualTo("123.456.789.1");
|
||||
assertThat(healthIndicator.getDetails().get(KubernetesClientHealthIndicator.POD_IP)).isEqualTo("127.0.0.1");
|
||||
assertThat(healthIndicator.getDetails().get(KubernetesClientHealthIndicator.NODE_NAME)).isEqualTo("nodeName");
|
||||
assertThat(healthIndicator.getDetails().get(KubernetesClientHealthIndicator.SERVICE_ACCOUNT))
|
||||
.isEqualTo("serviceAccount");
|
||||
assertThat(healthIndicator.getDetails().get(KubernetesClientHealthIndicator.POD_NAME)).isEqualTo("mypod");
|
||||
assertThat(healthIndicator.getDetails().get(KubernetesClientHealthIndicator.NAMESPACE)).isEqualTo("default");
|
||||
assertThat(healthIndicator.getDetails().get(KubernetesClientHealthIndicator.LABELS)).isEqualTo(labels);
|
||||
Map<String, Object> details = healthIndicator.getDetails();
|
||||
|
||||
assertThat(details.containsKey(INSIDE)).isTrue();
|
||||
assertThat(details.get(INSIDE)).isEqualTo(true);
|
||||
|
||||
assertThat(details.get(HOST_IP)).isEqualTo(STUB_HOST_IP);
|
||||
assertThat(details.get(POD_IP)).isEqualTo(STUB_POD_IP);
|
||||
assertThat(details.get(NODE_NAME)).isEqualTo(STUB_NODE_NAME);
|
||||
assertThat(details.get(SERVICE_ACCOUNT)).isEqualTo(STUB_SERVICE_ACCOUNT);
|
||||
assertThat(details.get(POD_NAME)).isEqualTo(STUB_POD_NAME);
|
||||
assertThat(details.get(NAMESPACE)).isEqualTo(STUB_NAMESPACE);
|
||||
assertThat(details.get(LABELS)).isEqualTo(STUB_LABELS);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.cloud.kubernetes.client.example.App;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* @author wind57
|
||||
*/
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = App.class)
|
||||
public class KubernetesClientInfoContributorInsideTests {
|
||||
|
||||
@Autowired
|
||||
private ApplicationContext context;
|
||||
|
||||
// test that the bean responsible for info contribution is present.
|
||||
// the actual fields it must provide are tested in
|
||||
// KubernetesClientInfoContributorTests.
|
||||
@Test
|
||||
public void test() {
|
||||
assertThat(context.getBean(KubernetesClientInfoContributor.class)).isNotNull();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.cloud.kubernetes.client.example.App;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
/**
|
||||
* @author wind57
|
||||
*/
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = App.class,
|
||||
properties = { "management.info.kubernetes.enabled=false" })
|
||||
public class KubernetesClientInfoContributorNotInsideTests {
|
||||
|
||||
@Autowired
|
||||
private ApplicationContext context;
|
||||
|
||||
// test that the bean responsible for info contribution is NOT present.
|
||||
@Test
|
||||
public void test() {
|
||||
assertThatThrownBy(() -> context.getBean(KubernetesClientInfoContributor.class))
|
||||
.isInstanceOf(NoSuchBeanDefinitionException.class);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -16,13 +16,9 @@
|
||||
|
||||
package org.springframework.cloud.kubernetes.client;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import io.kubernetes.client.openapi.models.V1ObjectMeta;
|
||||
import io.kubernetes.client.openapi.models.V1Pod;
|
||||
import io.kubernetes.client.openapi.models.V1PodSpec;
|
||||
import io.kubernetes.client.openapi.models.V1PodStatus;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
@@ -32,6 +28,19 @@ import org.springframework.cloud.kubernetes.commons.PodUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.springframework.cloud.kubernetes.client.KubernetesClientHealthIndicator.HOST_IP;
|
||||
import static org.springframework.cloud.kubernetes.client.KubernetesClientHealthIndicator.INSIDE;
|
||||
import static org.springframework.cloud.kubernetes.client.KubernetesClientHealthIndicator.NAMESPACE;
|
||||
import static org.springframework.cloud.kubernetes.client.KubernetesClientHealthIndicator.NODE_NAME;
|
||||
import static org.springframework.cloud.kubernetes.client.KubernetesClientHealthIndicator.POD_IP;
|
||||
import static org.springframework.cloud.kubernetes.client.KubernetesClientHealthIndicator.POD_NAME;
|
||||
import static org.springframework.cloud.kubernetes.client.KubernetesClientHealthIndicator.SERVICE_ACCOUNT;
|
||||
import static org.springframework.cloud.kubernetes.client.StubProvider.STUB_HOST_IP;
|
||||
import static org.springframework.cloud.kubernetes.client.StubProvider.STUB_NAMESPACE;
|
||||
import static org.springframework.cloud.kubernetes.client.StubProvider.STUB_NODE_NAME;
|
||||
import static org.springframework.cloud.kubernetes.client.StubProvider.STUB_POD_IP;
|
||||
import static org.springframework.cloud.kubernetes.client.StubProvider.STUB_POD_NAME;
|
||||
import static org.springframework.cloud.kubernetes.client.StubProvider.STUB_SERVICE_ACCOUNT;
|
||||
|
||||
/**
|
||||
* @author Ryan Baxter
|
||||
@@ -46,44 +55,28 @@ class KubernetesClientInfoContributorTests {
|
||||
void getDetailsIsNotInside() {
|
||||
when(utils.currentPod()).thenReturn(() -> null);
|
||||
KubernetesClientInfoContributor infoContributor = new KubernetesClientInfoContributor(utils);
|
||||
assertThat(infoContributor.getDetails().containsKey(KubernetesClientHealthIndicator.INSIDE)).isTrue();
|
||||
assertThat(infoContributor.getDetails().get(KubernetesClientHealthIndicator.INSIDE)).isEqualTo(false);
|
||||
Map<String, Object> details = infoContributor.getDetails();
|
||||
|
||||
assertThat(details.containsKey(INSIDE)).isTrue();
|
||||
assertThat(details.get(INSIDE)).isEqualTo(false);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getDetailsInside() throws Exception {
|
||||
Map<String, String> labels = new HashMap<>();
|
||||
labels.put("spring", "cloud");
|
||||
V1ObjectMeta metaData = new V1ObjectMeta();
|
||||
metaData.setLabels(labels);
|
||||
metaData.setName("mypod");
|
||||
metaData.setNamespace("default");
|
||||
void getDetailsInside() {
|
||||
|
||||
V1PodStatus status = new V1PodStatus();
|
||||
status.setPodIP("127.0.0.1");
|
||||
status.setHostIP("123.456.789.1");
|
||||
|
||||
V1PodSpec spec = new V1PodSpec();
|
||||
spec.setNodeName("nodeName");
|
||||
spec.setServiceAccountName("serviceAccount");
|
||||
|
||||
V1Pod pod = new V1Pod();
|
||||
pod.setMetadata(metaData);
|
||||
pod.setStatus(status);
|
||||
pod.setSpec(spec);
|
||||
|
||||
when(utils.currentPod()).thenReturn(() -> pod);
|
||||
when(utils.currentPod()).thenReturn(StubProvider::stubPod);
|
||||
KubernetesClientInfoContributor infoContributor = new KubernetesClientInfoContributor(utils);
|
||||
assertThat(infoContributor.getDetails().containsKey(KubernetesClientHealthIndicator.INSIDE)).isTrue();
|
||||
assertThat(infoContributor.getDetails().get(KubernetesClientHealthIndicator.INSIDE)).isEqualTo(true);
|
||||
assertThat(infoContributor.getDetails().get(KubernetesClientHealthIndicator.HOST_IP))
|
||||
.isEqualTo("123.456.789.1");
|
||||
assertThat(infoContributor.getDetails().get(KubernetesClientHealthIndicator.POD_IP)).isEqualTo("127.0.0.1");
|
||||
assertThat(infoContributor.getDetails().get(KubernetesClientHealthIndicator.NODE_NAME)).isEqualTo("nodeName");
|
||||
assertThat(infoContributor.getDetails().get(KubernetesClientHealthIndicator.SERVICE_ACCOUNT))
|
||||
.isEqualTo("serviceAccount");
|
||||
assertThat(infoContributor.getDetails().get(KubernetesClientHealthIndicator.POD_NAME)).isEqualTo("mypod");
|
||||
assertThat(infoContributor.getDetails().get(KubernetesClientHealthIndicator.NAMESPACE)).isEqualTo("default");
|
||||
Map<String, Object> details = infoContributor.getDetails();
|
||||
|
||||
assertThat(details.containsKey(INSIDE)).isTrue();
|
||||
assertThat(details.get(INSIDE)).isEqualTo(true);
|
||||
|
||||
assertThat(details.get(HOST_IP)).isEqualTo(STUB_HOST_IP);
|
||||
assertThat(details.get(POD_IP)).isEqualTo(STUB_POD_IP);
|
||||
assertThat(details.get(NODE_NAME)).isEqualTo(STUB_NODE_NAME);
|
||||
assertThat(details.get(SERVICE_ACCOUNT)).isEqualTo(STUB_SERVICE_ACCOUNT);
|
||||
assertThat(details.get(POD_NAME)).isEqualTo(STUB_POD_NAME);
|
||||
assertThat(details.get(NAMESPACE)).isEqualTo(STUB_NAMESPACE);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
import io.kubernetes.client.openapi.models.V1ObjectMeta;
|
||||
import io.kubernetes.client.openapi.models.V1Pod;
|
||||
import io.kubernetes.client.openapi.models.V1PodSpec;
|
||||
import io.kubernetes.client.openapi.models.V1PodStatus;
|
||||
|
||||
/**
|
||||
* @author wind57
|
||||
*/
|
||||
final class StubProvider {
|
||||
|
||||
static final String STUB_POD_IP = "127.0.0.1";
|
||||
|
||||
static final String STUB_HOST_IP = "123.456.789.1";
|
||||
|
||||
static final String STUB_NODE_NAME = "nodeName";
|
||||
|
||||
static final String STUB_SERVICE_ACCOUNT = "serviceAccount";
|
||||
|
||||
static final String STUB_POD_NAME = "mypod";
|
||||
|
||||
static final String STUB_NAMESPACE = "default";
|
||||
|
||||
static final Map<String, String> STUB_LABELS = Collections.singletonMap("spring", "cloud");
|
||||
|
||||
private StubProvider() {
|
||||
}
|
||||
|
||||
static V1Pod stubPod() {
|
||||
|
||||
V1ObjectMeta metaData = new V1ObjectMeta().labels(STUB_LABELS).name(STUB_POD_NAME).namespace(STUB_NAMESPACE);
|
||||
V1PodStatus status = new V1PodStatus().podIP(STUB_POD_IP).hostIP(STUB_HOST_IP);
|
||||
V1PodSpec spec = new V1PodSpec().nodeName(STUB_NODE_NAME).serviceAccountName(STUB_SERVICE_ACCOUNT);
|
||||
|
||||
return new V1Pod().metadata(metaData).status(status).spec(spec);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* 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.example;
|
||||
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
/**
|
||||
* @author wind57
|
||||
*/
|
||||
@SpringBootApplication
|
||||
public class App {
|
||||
|
||||
}
|
||||
@@ -16,14 +16,11 @@
|
||||
|
||||
package org.springframework.cloud.kubernetes.client.profile;
|
||||
|
||||
import io.kubernetes.client.openapi.ApiClient;
|
||||
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.cloud.kubernetes.client.example.App;
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
@@ -32,27 +29,15 @@ import static org.springframework.cloud.kubernetes.commons.profile.AbstractKuber
|
||||
/**
|
||||
* @author Ryan Baxter
|
||||
*/
|
||||
@SpringBootTest(classes = { KubernetesClientProfileEnvironmentPostProcessorNoProfileTests.App.class })
|
||||
@SpringBootTest(classes = { App.class })
|
||||
class KubernetesClientProfileEnvironmentPostProcessorNoProfileTests {
|
||||
|
||||
@Autowired
|
||||
Environment environment;
|
||||
|
||||
@MockBean
|
||||
CoreV1Api coreV1Api;
|
||||
|
||||
@MockBean
|
||||
ApiClient apiClient;
|
||||
private Environment environment;
|
||||
|
||||
@Test
|
||||
void whenNoKubernetesEnvironmentAndNoApiAccessThenNoProfileEnabled() {
|
||||
|
||||
assertThat(environment.getActiveProfiles()).doesNotContain(KUBERNETES_PROFILE);
|
||||
}
|
||||
|
||||
@SpringBootApplication
|
||||
static class App {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,14 +16,11 @@
|
||||
|
||||
package org.springframework.cloud.kubernetes.client.profile;
|
||||
|
||||
import io.kubernetes.client.openapi.ApiClient;
|
||||
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.cloud.kubernetes.client.example.App;
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
import static io.kubernetes.client.util.Config.ENV_SERVICE_HOST;
|
||||
@@ -34,27 +31,15 @@ import static org.springframework.cloud.kubernetes.commons.profile.AbstractKuber
|
||||
/**
|
||||
* @author Thomas Vitale
|
||||
*/
|
||||
@SpringBootTest(properties = { ENV_SERVICE_HOST + "=10.0.0.1", ENV_SERVICE_PORT + "=80" },
|
||||
classes = { KubernetesClientProfileEnvironmentPostProcessorTests.App.class })
|
||||
@SpringBootTest(properties = { ENV_SERVICE_HOST + "=10.0.0.1", ENV_SERVICE_PORT + "=80" }, classes = { App.class })
|
||||
class KubernetesClientProfileEnvironmentPostProcessorTests {
|
||||
|
||||
@Autowired
|
||||
Environment environment;
|
||||
|
||||
@MockBean
|
||||
CoreV1Api coreV1Api;
|
||||
|
||||
@MockBean
|
||||
ApiClient apiClient;
|
||||
private Environment environment;
|
||||
|
||||
@Test
|
||||
void whenKubernetesEnvironmentAndNoApiAccessThenProfileEnabled() {
|
||||
assertThat(environment.getActiveProfiles()).contains(KUBERNETES_PROFILE);
|
||||
}
|
||||
|
||||
@SpringBootApplication
|
||||
static class App {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user