Skip to content

Commit

Permalink
Merge pull request #50 from elmiko/limit-log-line-length
Browse files Browse the repository at this point in the history
add logic to truncate long log files
  • Loading branch information
elmiko authored May 11, 2024
2 parents 585a067 + 93a6d6e commit e8ede62
Show file tree
Hide file tree
Showing 5 changed files with 246 additions and 36 deletions.
40 changes: 38 additions & 2 deletions src/mustgather.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ use crate::resources::*;
use std::fs;
use std::path::{Path, PathBuf};

// MAX_LOG_LINES defines the maximum number of log lines that will be included in the output
const MAX_LOG_LINES: usize = 99;

pub struct MustGather {
pub title: String,
pub version: String,
Expand Down Expand Up @@ -290,14 +293,29 @@ fn get_pod(pod_dir: &PathBuf) -> Option<Pod> {
current_log_filename.push("current.log");
if current_log_filename.exists() {
// if it exists open and read into a new string
let raw = match fs::read_to_string(current_log_filename.as_path()) {
let raw: String = match fs::read_to_string(current_log_filename.as_path()) {
Ok(contents) => contents,
Err(_) => continue,
};
let mut logoutput = String::new();
let mut revlines = raw.lines().rev();
if revlines.clone().count() > MAX_LOG_LINES {
for _ in 0..MAX_LOG_LINES {
logoutput = match revlines.next() {
Some(l) => l.to_owned() + "\n" + &logoutput,
None => logoutput,
};
}

logoutput = String::from("camgi warning: log file has been truncated to end, see full contents in the must-gather\n") + &logoutput;
} else {
logoutput = raw;
}

// create a Container and add it to the Pod
pod.push_container(Container {
name: container_name.to_string(),
current_log: raw,
current_log: logoutput,
});
}
}
Expand Down Expand Up @@ -427,6 +445,24 @@ mod tests {
assert_eq!(pod.containers.len(), 7)
}

#[test]
fn test_get_pod_log_file() {
let path = PathBuf::from("testdata/must-gather-valid/sample-openshift-release/namespaces/openshift-machine-api/pods/cluster-baremetal-operator-6955c869bf-9ccgk");
let pod = get_pod(&path).unwrap();
assert_eq!(pod.containers.len(), 1);
let expected = include_str!("../testdata/must-gather-valid/sample-openshift-release/namespaces/openshift-machine-api/pods/cluster-baremetal-operator-6955c869bf-9ccgk/cluster-baremetal-operator/cluster-baremetal-operator/logs/current.log");
assert_eq!(pod.containers[0].current_log.as_str(), expected);
}

#[test]
fn test_get_pod_log_file_truncated() {
let path = PathBuf::from("testdata/must-gather-valid/sample-openshift-release/namespaces/openshift-machine-api/pods/cluster-autoscaler-default-f548ffc66-bck7p");
let pod = get_pod(&path).unwrap();
assert_eq!(pod.containers.len(), 1);
let expected = include_str!("../testdata/truncated-log-file.txt");
assert_eq!(pod.containers[0].current_log.as_str(), expected);
}

#[test]
fn test_get_pods_success() {
let path = PathBuf::from("testdata/must-gather-valid/sample-openshift-release");
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,109 @@
cluster-autoscaler-default-f548ffc66-bck7p cluster-autoscaler logs
line 2
line 3
line 4
line 5
line 6
line 7
line 8
line 9
line 10
line 11
line 12
line 13
line 14
line 15
line 16
line 17
line 18
line 19
line 20
line 21
line 22
line 23
line 24
line 25
line 26
line 27
line 28
line 29
line 30
line 31
line 32
line 33
line 34
line 35
line 36
line 37
line 38
line 39
line 40
line 41
line 42
line 43
line 44
line 45
line 46
line 47
line 48
line 49
line 50
line 51
line 52
line 53
line 54
line 55
line 56
line 57
line 58
line 59
line 60
line 61
line 62
line 63
line 64
line 65
line 66
line 67
line 68
line 69
line 70
line 71
line 72
line 73
line 74
line 75
line 76
line 77
line 78
line 79
line 80
line 81
line 82
line 83
line 84
line 85
line 86
line 87
line 88
line 89
line 90
line 91
line 92
line 93
line 94
line 95
line 96
line 97
line 98
line 99
line 100
line 101
line 102
line 103
line 104
line 105
line 106
line 107
line 108
line 109
Original file line number Diff line number Diff line change
Expand Up @@ -60,39 +60,6 @@ spec:
- mountPath: /var/run/secrets/kubernetes.io/serviceaccount
name: kube-api-access-n8nw4
readOnly: true
- args:
- --secure-listen-address=0.0.0.0:8443
- --upstream=http://localhost:8080/
- --tls-cert-file=/etc/tls/private/tls.crt
- --tls-private-key-file=/etc/tls/private/tls.key
- --config-file=/etc/kube-rbac-proxy/config-file.yaml
- --logtostderr=true
- --v=10
image: quay.io/openshift-release-dev/ocp-v4.0-fake
imagePullPolicy: IfNotPresent
name: kube-rbac-proxy
ports:
- containerPort: 8443
name: https
protocol: TCP
resources:
requests:
cpu: 10m
memory: 20Mi
securityContext:
capabilities:
drop:
- MKNOD
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /etc/kube-rbac-proxy
name: config
- mountPath: /etc/tls/private
name: cluster-baremetal-operator-tls
- mountPath: /var/run/secrets/kubernetes.io/serviceaccount
name: kube-api-access-n8nw4
readOnly: true
dnsPolicy: ClusterFirst
enableServiceLinks: true
nodeName: ip-10-0-0-1.control.plane
Expand Down

This file was deleted.

100 changes: 100 additions & 0 deletions testdata/truncated-log-file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
camgi warning: log file has been truncated to end, see full contents in the must-gather
line 11
line 12
line 13
line 14
line 15
line 16
line 17
line 18
line 19
line 20
line 21
line 22
line 23
line 24
line 25
line 26
line 27
line 28
line 29
line 30
line 31
line 32
line 33
line 34
line 35
line 36
line 37
line 38
line 39
line 40
line 41
line 42
line 43
line 44
line 45
line 46
line 47
line 48
line 49
line 50
line 51
line 52
line 53
line 54
line 55
line 56
line 57
line 58
line 59
line 60
line 61
line 62
line 63
line 64
line 65
line 66
line 67
line 68
line 69
line 70
line 71
line 72
line 73
line 74
line 75
line 76
line 77
line 78
line 79
line 80
line 81
line 82
line 83
line 84
line 85
line 86
line 87
line 88
line 89
line 90
line 91
line 92
line 93
line 94
line 95
line 96
line 97
line 98
line 99
line 100
line 101
line 102
line 103
line 104
line 105
line 106
line 107
line 108
line 109

0 comments on commit e8ede62

Please sign in to comment.