glusterfs_util.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. Copyright 2015 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 glusterfs
  14. import (
  15. "bufio"
  16. "fmt"
  17. "os"
  18. "k8s.io/klog"
  19. )
  20. // readGlusterLog will take the last 2 lines of the log file
  21. // on failure of gluster SetUp and return those so kubelet can
  22. // properly expose them
  23. // return error on any failure
  24. func readGlusterLog(path string, podName string) error {
  25. var line1 string
  26. var line2 string
  27. linecount := 0
  28. klog.Infof("failure, now attempting to read the gluster log for pod %s", podName)
  29. // Check and make sure path exists
  30. if len(path) == 0 {
  31. return fmt.Errorf("log file does not exist for pod %s", podName)
  32. }
  33. // open the log file
  34. file, err := os.Open(path)
  35. if err != nil {
  36. return fmt.Errorf("could not open log file for pod %s", podName)
  37. }
  38. defer file.Close()
  39. // read in and scan the file using scanner
  40. // from stdlib
  41. fscan := bufio.NewScanner(file)
  42. // rather than guessing on bytes or using Seek
  43. // going to scan entire file and take the last two lines
  44. // generally the file should be small since it is pod specific
  45. for fscan.Scan() {
  46. if linecount > 0 {
  47. line1 = line2
  48. }
  49. line2 = "\n" + fscan.Text()
  50. linecount++
  51. }
  52. if linecount > 0 {
  53. return fmt.Errorf("%v", line1+line2+"\n")
  54. }
  55. return nil
  56. }