logs.go 2.6 KB

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