Skip to content

Commit

Permalink
utilise new image + adjust script
Browse files Browse the repository at this point in the history
  • Loading branch information
miampf committed Nov 7, 2024
1 parent 70142e1 commit e46a38c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 28 deletions.
24 changes: 20 additions & 4 deletions packages/by-name/k8s-log-collector/package.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,22 @@
{
dockerTools,
writeShellApplication,
buildEnv,
inotify-tools,
busybox
coreutils,
findutils,
bash,
gnutar,
gzip
}:

let
collection-script = writeShellApplication {
name = "collect-logs";
runtimeInputs = [
inotify-tools
busybox
coreutils
findutils
];
text = ''
set -euo pipefail
Expand All @@ -23,7 +29,7 @@ let
while read -r file; do
if [[ -f "$file" && "$file" == *"$POD_NAMESPACE"* ]]; then
mkdir -p "/export$(dirname "$file")"
tail --follow=name "$file" > "export$file" &
tail --follow=name "$file" >"/export$file" &
fi
done
inotifywait -m /logs -r -e create -e moved_to |
Expand All @@ -37,9 +43,19 @@ let
'';
};
in
dockerTools.buildLayeredImage {
dockerTools.buildImage {
name = "k8s-log-collector";
tag = "0.1.0";
copyToRoot = buildEnv {
name = "bin";
paths = [
bash
coreutils
gnutar
gzip
];
pathsToLink = "/bin";
};
config = {
Cmd = [ "${collection-script}/bin/collect-logs" ];
Volumes = { "/logs" = {}; };
Expand Down
24 changes: 1 addition & 23 deletions packages/log-collector.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ spec:
effect: NoSchedule
containers:
- name: log-collector
image: docker.io/ubuntu:oracular # TODO(miampf): Replace with non docker.io image
image: "ghcr.io/miampf/k8s-log-collector:latest"
volumeMounts:
- mountPath: /logs
name: log-volume
Expand All @@ -35,28 +35,6 @@ spec:
valueFrom:
fieldRef:
fieldPath: metadata.namespace
command:
- /usr/bin/bash
- -c
- |
mkdir /export
# TODO(miampf): Prepare an image that already has required tools installed
apt-get update && apt-get install -y inotify-tools
# collect all logs that may have been missed during startup
for file in $(find /logs -name *.log); do
if [[ -f "$file" && "$file" == *"$POD_NAMESPACE"* ]]; then
mkdir -p "/export$(dirname "$file")"
cp "$file" "/export$file"
fi
done
inotifywait -m /logs -r -e create -e moved_to |
while read path action file; do
filepath="$path$file"
if [[ -f "$filepath" && "$filepath" == *"$POD_NAMESPACE"* ]]; then
mkdir -p "/export$path"
tail --follow=name "$filepath" >"/export$filepath" &
fi
done
volumes:
- name: log-volume
# mount the nodes logs to the container
Expand Down
2 changes: 1 addition & 1 deletion packages/scripts.nix
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@
kubectl wait --for=condition=Ready -n "$namespace" "pod/$pod" 1>/dev/null 2>/dev/null
# Download and extract the logs every 3 seconds
while true; do
kubectl exec -n "$namespace" "$pod" -- bash -c "rm -f /exported-logs.tar.gz; tar zcvf /exported-logs.tar.gz /export" 1>/dev/null 2>/dev/null
kubectl exec -n "$namespace" "$pod" -- /bin/bash -c "rm -f /exported-logs.tar.gz; tar zcvf /exported-logs.tar.gz /export" 1>/dev/null 2>/dev/null
rm -f ./workspace/logs/exported-logs.tar.gz
kubectl cp -n "$namespace" "$pod:/exported-logs.tar.gz" ./workspace/logs/exported-logs.tar.gz 1>/dev/null 2>/dev/null
tar xzvf ./workspace/logs/exported-logs.tar.gz --directory ./workspace/logs 1>/dev/null 2>/dev/null
Expand Down

0 comments on commit e46a38c

Please sign in to comment.