获取 Pod 命名空间

This commit is contained in:
zhaoyong 2021-12-29 19:10:58 +08:00
parent 8f0ca59c46
commit b7ccd9317a
3 changed files with 63 additions and 1 deletions

View File

@ -6,7 +6,10 @@
<groupId>cn.axzo.framework</groupId>
<artifactId>common-common</artifactId>
<version>1.1.2</version>
<!-- release -->
<version>1.1.3</version>
<!-- snapshot -->
<!-- <version>1.0.0-SNAPSHOT</version>-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

View File

@ -13,6 +13,7 @@ import java.net.UnknownHostException;
* @since 2021-07-13 10:18
*/
public class CanonicalHostNamePropertyDefiner extends PropertyDefinerBase {
@Override
public String getPropertyValue() {
InetAddress ia;
@ -25,4 +26,5 @@ public class CanonicalHostNamePropertyDefiner extends PropertyDefinerBase {
}
return null;
}
}

View File

@ -0,0 +1,57 @@
package cn.azxo.framework.common.logger.logback;
import ch.qos.logback.core.PropertyDefinerBase;
import java.security.AccessControlException;
import java.util.HashMap;
import java.util.Map;
/**
* 获取 Pod Namespace
*
* @author zhaoyong
* @see PodNamespacePropertyDefiner
* @since 2021-12-29 18:50
*/
public class PodNamespacePropertyDefiner extends PropertyDefinerBase {
@Override
public String getPropertyValue() {
Map<String, Object> environment = new HashMap<>();
Map<String, Object> systemProperties = getSystemProperties();
if(systemProperties != null && systemProperties.size() != 0) {
environment.putAll(systemProperties);
}
Map<String, Object> systemEnvironment = getSystemEnvironment();
if(systemEnvironment != null && systemEnvironment.size() != 0) {
environment.putAll(systemEnvironment);
}
Object podNamespaceObj = environment.get("MY_POD_NAMESPACE");
if(podNamespaceObj != null) {
return podNamespaceObj.toString();
}
return null;
}
public Map<String, Object> getSystemEnvironment() {
try {
return (Map) System.getenv();
}
catch (AccessControlException ex) {
return null;
}
}
public Map<String, Object> getSystemProperties() {
try {
return (Map) System.getProperties();
}
catch (AccessControlException ex) {
return null;
}
}
}