Skip to content

Commit

Permalink
Use env variables for kubernetes url, no need for config (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
StrongestNumber9 authored Aug 14, 2023
1 parent addf222 commit 729d166
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 44 deletions.
4 changes: 2 additions & 2 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
8 changes: 3 additions & 5 deletions example/combined.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ data:
},
"kubernetes": {
"logdir": "/var/log/containers",
"url": "https://127.0.0.1:8443",
"timezone": "Europe/Helsinki",
"cacheExpireInterval": 300,
"cacheMaxEntries": 4096,
Expand Down Expand Up @@ -109,7 +108,7 @@ data:
</Configuration>
kind: ConfigMap
metadata:
name: app-config-5tmb2cmh92
name: app-config-674ff8b7bd
---
apiVersion: v1
data:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion example/config/k8s_01/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
},
"kubernetes": {
"logdir": "/var/log/containers",
"url": "https://127.0.0.1:8443",
"timezone": "Europe/Helsinki",
"cacheExpireInterval": 300,
"cacheMaxEntries": 4096,
Expand Down
4 changes: 2 additions & 2 deletions example/pods/k8s_01.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down Expand Up @@ -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;"
]
}
],
Expand Down
2 changes: 0 additions & 2 deletions k8s_01/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion rpm/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
},
"kubernetes": {
"logdir": "/var/log/containers",
"url": "https://127.0.0.1:8443",
"timezone": "Europe/Helsinki",
"cacheExpireInterval": 900,
"cacheMaxEntries": 4096,
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/com/teragrep/k8s_01/K8SConsumer.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,17 @@ public class K8SConsumer implements Consumer<FileRecord> {
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<RelpOutput> relpOutputPool
BlockingQueue<RelpOutput> 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();
Expand Down Expand Up @@ -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);
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/com/teragrep/k8s_01/K8SConsumerSupplier.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,19 +28,22 @@ public class K8SConsumerSupplier implements Supplier<Consumer<FileRecord>> {
private final AppConfig appConfig;
private final KubernetesCachingAPIClient cacheClient;
private final BlockingQueue<RelpOutput> relpOutputPool;
private final String apiUrl;

K8SConsumerSupplier(
AppConfig appConfig,
KubernetesCachingAPIClient cacheClient,
BlockingQueue<RelpOutput> relpOutputPool
BlockingQueue<RelpOutput> relpOutputPool,
String apiUrl
) {
this.appConfig = appConfig;
this.cacheClient = cacheClient;
this.relpOutputPool = relpOutputPool;
this.apiUrl = apiUrl;
}

@Override
public Consumer<FileRecord> get() {
return new K8SConsumer(appConfig, cacheClient, relpOutputPool);
return new K8SConsumer(appConfig, cacheClient, relpOutputPool, apiUrl);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ class KubernetesCachingAPIClient {
private LoadingCache<String, NamespaceMetadataContainer> 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.",
Expand All @@ -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) {
Expand Down
14 changes: 11 additions & 3 deletions src/main/java/com/teragrep/k8s_01/KubernetesLogReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ public static void main(String[] args) throws IOException, InterruptedException
return;
}
try {
appConfig.getKubernetes().handleOverrides();
appConfig.getRelp().handleOverrides();
appConfig.validate();
}
Expand All @@ -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
Expand Down Expand Up @@ -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: {}",
Expand Down
19 changes: 0 additions & 19 deletions src/main/java/com/teragrep/k8s_01/config/AppConfigKubernetes.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -59,10 +48,6 @@ public AppConfigLabels getLabels() {
public String[] getLogfiles() {
return logfiles;
}

public String getUrl() {
return url;
}
public Integer getCacheMaxEntries() {
return cacheMaxEntries;
}
Expand Down Expand Up @@ -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");
}
Expand Down

0 comments on commit 729d166

Please sign in to comment.