From 729d166b6a03b84e4728b7d913ff13ec62eef7c0 Mon Sep 17 00:00:00 2001 From: Strongest Number 9 <16169054+StrongestNumber9@users.noreply.github.com> Date: Mon, 14 Aug 2023 11:18:15 +0300 Subject: [PATCH] Use env variables for kubernetes url, no need for config (#110) --- README.adoc | 4 ++-- example/combined.yaml | 8 +++----- example/config/k8s_01/config.json | 1 - example/pods/k8s_01.json | 4 ++-- k8s_01/values.yaml | 2 -- rpm/config.json | 1 - .../java/com/teragrep/k8s_01/K8SConsumer.java | 7 +++++-- .../teragrep/k8s_01/K8SConsumerSupplier.java | 9 +++++---- .../k8s_01/KubernetesCachingAPIClient.java | 6 +++--- .../teragrep/k8s_01/KubernetesLogReader.java | 14 +++++++++++--- .../k8s_01/config/AppConfigKubernetes.java | 19 ------------------- 11 files changed, 31 insertions(+), 44 deletions(-) diff --git a/README.adoc b/README.adoc index ea3873d..e95ab8f 100644 --- a/README.adoc +++ b/README.adoc @@ -2,6 +2,8 @@ A container that will read mounted container logfiles, then enriches them using Kubernetes API server, and forwards them with RELP to wanted target server. +It uses KUBERNETES_SERVICE_HOST and KUBERNETES_SERVICE_PORT environment variables to connect to the Kubernetes API. + ## Example Helm Chart usage See `k8s_01/` directory for more information @@ -14,8 +16,6 @@ Currently supported: - `K8S_01_RELP_PORT` overwrites `.relp.port` - - `K8S_01_KUBERNETES_URL` overwrites `.kubernetes.url` - ## Example test cluster usage You can find example cluster in `/example` directory diff --git a/example/combined.yaml b/example/combined.yaml index 644cc76..e269a38 100644 --- a/example/combined.yaml +++ b/example/combined.yaml @@ -41,7 +41,6 @@ data: }, "kubernetes": { "logdir": "/var/log/containers", - "url": "https://127.0.0.1:8443", "timezone": "Europe/Helsinki", "cacheExpireInterval": 300, "cacheMaxEntries": 4096, @@ -109,7 +108,7 @@ data: kind: ConfigMap metadata: - name: app-config-5tmb2cmh92 + name: app-config-674ff8b7bd --- apiVersion: v1 data: @@ -293,12 +292,11 @@ spec: - args: - -c - export K8S_01_RELP_TARGET="${RELP_SERVICE_SERVICE_HOST}"; export K8S_01_RELP_PORT="${RELP_SERVICE_SERVICE_PORT}"; - export K8S_01_KUBERNETES_URL="https://${KUBERNETES_SERVICE_HOST}:${KUBERNETES_SERVICE_PORT}"; cd /opt/teragrep/k8s_01 || exit 1; exec /usr/bin/java -Dlog4j2.configurationFile=file:etc/log4j2.xml -jar lib/k8s_01.jar; command: - /usr/bin/bash - image: ghcr.io/teragrep/k8s_01/app:latest + image: ghcr.io/teragrep/k8s_01:latest imagePullPolicy: IfNotPresent name: kubelogreader volumeMounts: @@ -326,7 +324,7 @@ spec: terminationGracePeriodSeconds: 30 volumes: - configMap: - name: app-config-5tmb2cmh92 + name: app-config-674ff8b7bd name: app-config - hostPath: path: /var/log/containers diff --git a/example/config/k8s_01/config.json b/example/config/k8s_01/config.json index df26f55..19a0f25 100644 --- a/example/config/k8s_01/config.json +++ b/example/config/k8s_01/config.json @@ -4,7 +4,6 @@ }, "kubernetes": { "logdir": "/var/log/containers", - "url": "https://127.0.0.1:8443", "timezone": "Europe/Helsinki", "cacheExpireInterval": 300, "cacheMaxEntries": 4096, diff --git a/example/pods/k8s_01.json b/example/pods/k8s_01.json index 4da7604..75a9039 100644 --- a/example/pods/k8s_01.json +++ b/example/pods/k8s_01.json @@ -54,7 +54,7 @@ ], "containers": [ { - "image": "ghcr.io/teragrep/k8s_01/app:latest", + "image": "ghcr.io/teragrep/k8s_01:latest", "imagePullPolicy": "IfNotPresent", "name": "kubelogreader", "volumeMounts": [ @@ -88,7 +88,7 @@ ], "args": [ "-c", - "export K8S_01_RELP_TARGET=\"${RELP_SERVICE_SERVICE_HOST}\"; export K8S_01_RELP_PORT=\"${RELP_SERVICE_SERVICE_PORT}\"; export K8S_01_KUBERNETES_URL=\"https://${KUBERNETES_SERVICE_HOST}:${KUBERNETES_SERVICE_PORT}\"; cd /opt/teragrep/k8s_01 || exit 1; exec /usr/bin/java -Dlog4j2.configurationFile=file:etc/log4j2.xml -jar lib/k8s_01.jar;" + "export K8S_01_RELP_TARGET=\"${RELP_SERVICE_SERVICE_HOST}\"; export K8S_01_RELP_PORT=\"${RELP_SERVICE_SERVICE_PORT}\"; cd /opt/teragrep/k8s_01 || exit 1; exec /usr/bin/java -Dlog4j2.configurationFile=file:etc/log4j2.xml -jar lib/k8s_01.jar;" ] } ], diff --git a/k8s_01/values.yaml b/k8s_01/values.yaml index 675e3bf..e3bd000 100644 --- a/k8s_01/values.yaml +++ b/k8s_01/values.yaml @@ -12,8 +12,6 @@ config: kubernetes: # Base path where logs are read from. Uses {logdir}/{logfiles} logdir: /var/log/containers - # Kubernetes API url where metadata is asked from. Can be dynamically set with "K8S_01_KUBERNETES_URL" environment variable. - url: https://127.0.0.1:8443 # Timezone used by the logs timezone: Europe/Helsinki # How long API responses are cached diff --git a/rpm/config.json b/rpm/config.json index d6339dd..d86044d 100644 --- a/rpm/config.json +++ b/rpm/config.json @@ -4,7 +4,6 @@ }, "kubernetes": { "logdir": "/var/log/containers", - "url": "https://127.0.0.1:8443", "timezone": "Europe/Helsinki", "cacheExpireInterval": 900, "cacheMaxEntries": 4096, diff --git a/src/main/java/com/teragrep/k8s_01/K8SConsumer.java b/src/main/java/com/teragrep/k8s_01/K8SConsumer.java index 67b1200..4b4dd4c 100644 --- a/src/main/java/com/teragrep/k8s_01/K8SConsumer.java +++ b/src/main/java/com/teragrep/k8s_01/K8SConsumer.java @@ -61,14 +61,17 @@ public class K8SConsumer implements Consumer { private static final Pattern appNamePattern = Pattern.compile("^[\\x21-\\x7e]+$"); // DEC 33 - DEC 126 as specified in RFC5424 private final boolean discardEnabled; private final String discardLabel; + private final String apiUrl; K8SConsumer( AppConfig appConfig, KubernetesCachingAPIClient cacheClient, - BlockingQueue relpOutputPool + BlockingQueue relpOutputPool, + String apiUrl ) { this.appConfig = appConfig; this.cacheClient = cacheClient; this.relpOutputPool = relpOutputPool; + this.apiUrl = apiUrl; this.timezoneId = ZoneId.of(appConfig.getKubernetes().getTimezone()); this.discardEnabled = appConfig.getKubernetes().getLabels().getDiscard().isEnabled(); this.discardLabel = appConfig.getKubernetes().getLabels().getDiscard().getLabel(); @@ -207,7 +210,7 @@ public void accept(FileRecord record) { namespaceMetadataContainer, podMetadataContainer, ContainerInfo.getContainerName(record.getFilename()), - appConfig.getKubernetes().getUrl() + apiUrl ); JsonObject dockerMetadata = new JsonObject(); dockerMetadata.addProperty("container_id", containerId); diff --git a/src/main/java/com/teragrep/k8s_01/K8SConsumerSupplier.java b/src/main/java/com/teragrep/k8s_01/K8SConsumerSupplier.java index 90305b3..a354974 100644 --- a/src/main/java/com/teragrep/k8s_01/K8SConsumerSupplier.java +++ b/src/main/java/com/teragrep/k8s_01/K8SConsumerSupplier.java @@ -19,8 +19,6 @@ import com.teragrep.k8s_01.config.AppConfig; import com.teragrep.rlo_13.FileRecord; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import java.util.concurrent.BlockingQueue; import java.util.function.Consumer; @@ -30,19 +28,22 @@ public class K8SConsumerSupplier implements Supplier> { private final AppConfig appConfig; private final KubernetesCachingAPIClient cacheClient; private final BlockingQueue relpOutputPool; + private final String apiUrl; K8SConsumerSupplier( AppConfig appConfig, KubernetesCachingAPIClient cacheClient, - BlockingQueue relpOutputPool + BlockingQueue relpOutputPool, + String apiUrl ) { this.appConfig = appConfig; this.cacheClient = cacheClient; this.relpOutputPool = relpOutputPool; + this.apiUrl = apiUrl; } @Override public Consumer get() { - return new K8SConsumer(appConfig, cacheClient, relpOutputPool); + return new K8SConsumer(appConfig, cacheClient, relpOutputPool, apiUrl); } } diff --git a/src/main/java/com/teragrep/k8s_01/KubernetesCachingAPIClient.java b/src/main/java/com/teragrep/k8s_01/KubernetesCachingAPIClient.java index 5bd43d2..12883ec 100644 --- a/src/main/java/com/teragrep/k8s_01/KubernetesCachingAPIClient.java +++ b/src/main/java/com/teragrep/k8s_01/KubernetesCachingAPIClient.java @@ -43,11 +43,11 @@ class KubernetesCachingAPIClient { private LoadingCache namespaceCache; private final AppConfigKubernetes config; - KubernetesCachingAPIClient(AppConfigKubernetes appConfigKubernetes) throws IOException { + KubernetesCachingAPIClient(AppConfigKubernetes appConfigKubernetes, String apiUrl) throws IOException { config = appConfigKubernetes; LOGGER.info( "Starting Caching API Client using {}", - config.getUrl() + apiUrl ); LOGGER.info( "Keeping up to {} cached entries for {} seconds before evicting.", @@ -60,7 +60,7 @@ class KubernetesCachingAPIClient { ); try { ApiClient client = Config.fromCluster(); - client.setBasePath(config.getUrl()); + client.setBasePath(apiUrl); Configuration.setDefaultApiClient(client); api = new CoreV1Api(); } catch (Exception e) { diff --git a/src/main/java/com/teragrep/k8s_01/KubernetesLogReader.java b/src/main/java/com/teragrep/k8s_01/KubernetesLogReader.java index ed0dc30..a6fab88 100644 --- a/src/main/java/com/teragrep/k8s_01/KubernetesLogReader.java +++ b/src/main/java/com/teragrep/k8s_01/KubernetesLogReader.java @@ -75,7 +75,6 @@ public static void main(String[] args) throws IOException, InterruptedException return; } try { - appConfig.getKubernetes().handleOverrides(); appConfig.getRelp().handleOverrides(); appConfig.validate(); } @@ -86,13 +85,22 @@ public static void main(String[] args) throws IOException, InterruptedException ); return; } + + String apiAddress = System.getenv("KUBERNETES_SERVICE_HOST"); + String apiPort = System.getenv("KUBERNETES_SERVICE_PORT"); + if(apiAddress == null || apiPort == null) { + LOGGER.error("Can't resolve KUBERNETES_SERVICE_HOST and/or KUBERNETES_SERVICE_PORT environment variables, can't continue."); + return; + } + String apiUrl = String.format("https://%s:%s", apiAddress, apiPort); + if(appConfig.getKubernetes().getLabels().getDiscard().isEnabled()) { LOGGER.warn( "WARNING: Event discarding is enabled. This will lose any data from pods that has label <[{}]> that is equal to \"true\".", appConfig.getKubernetes().getLabels().getDiscard().getLabel() ); } - KubernetesCachingAPIClient cacheClient = new KubernetesCachingAPIClient(appConfig.getKubernetes()); + KubernetesCachingAPIClient cacheClient = new KubernetesCachingAPIClient(appConfig.getKubernetes(), apiUrl); PrometheusMetrics prometheusMetrics = new PrometheusMetrics(appConfig.getMetrics().getPort()); // Pool of Relp output threads to be shared by every consumer @@ -124,7 +132,7 @@ public static void main(String[] args) throws IOException, InterruptedException } // consumer supplier, returns always the same instance - K8SConsumerSupplier consumerSupplier = new K8SConsumerSupplier(appConfig, cacheClient, relpOutputPool); + K8SConsumerSupplier consumerSupplier = new K8SConsumerSupplier(appConfig, cacheClient, relpOutputPool, apiUrl); String[] logfiles = appConfig.getKubernetes().getLogfiles(); LOGGER.debug( "Monitored logfiles: {}", diff --git a/src/main/java/com/teragrep/k8s_01/config/AppConfigKubernetes.java b/src/main/java/com/teragrep/k8s_01/config/AppConfigKubernetes.java index cc75b7e..d3f7640 100644 --- a/src/main/java/com/teragrep/k8s_01/config/AppConfigKubernetes.java +++ b/src/main/java/com/teragrep/k8s_01/config/AppConfigKubernetes.java @@ -25,22 +25,11 @@ /* POJO representing the .kubernetes part of config.json */ public class AppConfigKubernetes implements BaseConfig { private static final Logger LOGGER = LoggerFactory.getLogger(AppConfigKubernetes.class); - public void handleOverrides() { - String kubernetesUrl = System.getenv("K8S_01_KUBERNETES_URL"); - if(kubernetesUrl != null) { - LOGGER.info( - "Found K8S_01_KUBERNETES_URL environment variable <[{}]>, using it as kubernetes url.", - kubernetesUrl - ); - this.url = kubernetesUrl; - } - } private Integer cacheExpireInterval; private Integer cacheMaxEntries; private String logdir; private AppConfigLabels labels; private String[] logfiles; - private String url; private String timezone; private Integer maxLogReadingThreads; @@ -59,10 +48,6 @@ public AppConfigLabels getLabels() { public String[] getLogfiles() { return logfiles; } - - public String getUrl() { - return url; - } public Integer getCacheMaxEntries() { return cacheMaxEntries; } @@ -102,10 +87,6 @@ public void validate() throws InvalidConfigurationException { } } - if(url == null) { - throw new InvalidConfigurationException("url not found or is null in kubernetes config object"); - } - if(timezone == null) { throw new InvalidConfigurationException("timezone not found or is null in kubernetes config object"); }