Add InfoContributor reporting pod details (#295)

Fixes gh-128
This commit is contained in:
Mark Anderson
2018-12-18 19:43:45 +00:00
committed by Spencer Gibb
parent a5baf013f6
commit 135bf6f8eb
3 changed files with 141 additions and 0 deletions

View File

@@ -127,6 +127,11 @@ public class KubernetesAutoConfiguration {
public KubernetesHealthIndicator kubernetesHealthIndicator(PodUtils podUtils) {
return new KubernetesHealthIndicator(podUtils);
}
@Bean
public KubernetesInfoContributor kubernetesInfoContributor(PodUtils podUtils) {
return new KubernetesInfoContributor(podUtils);
}
}
private static <D> D or(D dis, D dat) {

View File

@@ -0,0 +1,63 @@
/*
* Copyright 2013-2018 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
*
* http://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.api.model.Pod;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.boot.actuate.info.Info.Builder;
import org.springframework.boot.actuate.info.InfoContributor;
public class KubernetesInfoContributor implements InfoContributor {
private static final Log LOG = LogFactory.getLog(KubernetesInfoContributor.class);
private PodUtils utils;
public KubernetesInfoContributor(PodUtils utils) {
this.utils = utils;
}
@Override
public void contribute(Builder builder) {
try {
Pod current = utils.currentPod().get();
Map<String, Object> details = new HashMap<>();
if (current != null) {
details.put("inside", true);
details.put("namespace", current.getMetadata().getNamespace());
details.put("podName", current.getMetadata().getName());
details.put("podIp", current.getStatus().getPodIP());
details.put("serviceAccount", current.getSpec().getServiceAccountName());
details.put("nodeName", current.getSpec().getNodeName());
details.put("hostIp", current.getStatus().getHostIP());
}
else {
details.put("inside", false);
}
builder.withDetail("kubernetes", details);
}
catch (Exception e) {
LOG.warn("Failed to get pod details", e);
}
}
}

View File

@@ -0,0 +1,73 @@
/*
* Copyright 2013-2018 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
*
* http://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 static io.restassured.RestAssured.given;
import static org.hamcrest.core.StringContains.containsString;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.server.mock.KubernetesServer;
import io.restassured.RestAssured;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.kubernetes.example.App;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = App.class)
public class InfoContributorTest {
@ClassRule
public static KubernetesServer server = new KubernetesServer();
private static KubernetesClient mockClient;
@Value("${local.server.port}")
private int port;
@BeforeClass
public static void setUpBeforeClass() {
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");
}
@Test
public void infoEndpointShouldContainKubernetes() {
RestAssured.baseURI = String.format("http://localhost:%d/actuator/info", port);
given()
.contentType("application/json")
.get()
.then()
.statusCode(200)
.body(containsString("kubernetes"));
}
}