k8s.io/kubernetes@v1.29.3/test/e2e_node/services/logs.go (about) 1 /* 2 Copyright 2017 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package services 18 19 import ( 20 "encoding/json" 21 "flag" 22 "fmt" 23 ) 24 25 // LogFileData holds data about logfiles to fetch with a journalctl command or 26 // file from a node's file system. 27 type LogFileData struct { 28 // Name of the log file. 29 Name string `json:"name"` 30 // Files are possible absolute paths of the log file. 31 Files []string `json:"files"` 32 // JournalctlCommand is the journalctl command to get log. 33 JournalctlCommand []string `json:"journalctl"` 34 } 35 36 // logFiles are the type used to collect all log files. The key is the expected 37 // name of the log file after collected. 38 type logFiles map[string]LogFileData 39 40 // String function of flag.Value 41 func (l *logFiles) String() string { 42 return fmt.Sprint(*l) 43 } 44 45 // Set function of flag.Value 46 func (l *logFiles) Set(value string) error { 47 var log LogFileData 48 if err := json.Unmarshal([]byte(value), &log); err != nil { 49 return err 50 } 51 // Note that we assume all white space in flag string is separating fields 52 logs := *l 53 logs[log.Name] = log 54 return nil 55 } 56 57 // extraLogs is the extra logs specified by the test runner. 58 var extraLogs = make(logFiles) 59 60 func init() { 61 flag.Var(&extraLogs, "extra-log", "Extra log to collect after test in the json format of LogFile.") 62 } 63 64 // requiredLogs is the required logs to collect after the test. 65 var requiredLogs = []LogFileData{ 66 { 67 Name: "kern.log", 68 Files: []string{"/var/log/kern.log"}, 69 JournalctlCommand: []string{"-k"}, 70 }, 71 { 72 Name: "cloud-init.log", 73 Files: []string{"/var/log/cloud-init.log", "/var/log/cloud-init-output.log"}, 74 JournalctlCommand: []string{"-u", "cloud*"}, 75 }, 76 { 77 Name: "containerd.log", 78 Files: []string{"/var/log/containerd.log"}, 79 JournalctlCommand: []string{"-u", "containerd"}, 80 }, 81 { 82 Name: "containerd-installation.log", 83 JournalctlCommand: []string{"-u", "containerd-installation"}, 84 }, 85 } 86 87 // getLogFiles get all logs to collect after the test. 88 func getLogFiles() logFiles { 89 logs := make(logFiles) 90 for _, l := range requiredLogs { 91 logs[l.Name] = l 92 } 93 for _, l := range extraLogs { 94 logs[l.Name] = l 95 } 96 return logs 97 }