diff --git a/packages/by-name/k8s-log-collector/package.nix b/packages/by-name/k8s-log-collector/package.nix index b2713c5e0..5a81ccc85 100644 --- a/packages/by-name/k8s-log-collector/package.nix +++ b/packages/by-name/k8s-log-collector/package.nix @@ -4,8 +4,13 @@ { dockerTools, writeShellApplication, + buildEnv, inotify-tools, - busybox + coreutils, + findutils, + bash, + gnutar, + gzip }: let @@ -13,7 +18,8 @@ let name = "collect-logs"; runtimeInputs = [ inotify-tools - busybox + coreutils + findutils ]; text = '' set -euo pipefail @@ -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 | @@ -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" = {}; }; diff --git a/packages/log-collector.yaml b/packages/log-collector.yaml index 548cb4b7a..01845eb26 100644 --- a/packages/log-collector.yaml +++ b/packages/log-collector.yaml @@ -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 @@ -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 diff --git a/packages/scripts.nix b/packages/scripts.nix index 090e86a42..a2a6a8fcd 100644 --- a/packages/scripts.nix +++ b/packages/scripts.nix @@ -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