warnings.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. Copyright 2020 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 rest
  14. import (
  15. "fmt"
  16. "io"
  17. "net/http"
  18. "sync"
  19. "k8s.io/klog/v2"
  20. "k8s.io/apimachinery/pkg/util/net"
  21. )
  22. // WarningHandler is an interface for handling warning headers
  23. type WarningHandler interface {
  24. // HandleWarningHeader is called with the warn code, agent, and text when a warning header is countered.
  25. HandleWarningHeader(code int, agent string, text string)
  26. }
  27. var (
  28. defaultWarningHandler WarningHandler = WarningLogger{}
  29. defaultWarningHandlerLock sync.RWMutex
  30. )
  31. // SetDefaultWarningHandler sets the default handler client uses when warning headers are encountered.
  32. // By default, warnings are printed to stderr.
  33. func SetDefaultWarningHandler(l WarningHandler) {
  34. defaultWarningHandlerLock.Lock()
  35. defer defaultWarningHandlerLock.Unlock()
  36. defaultWarningHandler = l
  37. }
  38. func getDefaultWarningHandler() WarningHandler {
  39. defaultWarningHandlerLock.RLock()
  40. defer defaultWarningHandlerLock.RUnlock()
  41. l := defaultWarningHandler
  42. return l
  43. }
  44. // NoWarnings is an implementation of WarningHandler that suppresses warnings.
  45. type NoWarnings struct{}
  46. func (NoWarnings) HandleWarningHeader(code int, agent string, message string) {}
  47. // WarningLogger is an implementation of WarningHandler that logs code 299 warnings
  48. type WarningLogger struct{}
  49. func (WarningLogger) HandleWarningHeader(code int, agent string, message string) {
  50. if code != 299 || len(message) == 0 {
  51. return
  52. }
  53. klog.Warning(message)
  54. }
  55. type warningWriter struct {
  56. // out is the writer to output warnings to
  57. out io.Writer
  58. // opts contains options controlling warning output
  59. opts WarningWriterOptions
  60. // writtenLock guards written and writtenCount
  61. writtenLock sync.Mutex
  62. writtenCount int
  63. written map[string]struct{}
  64. }
  65. // WarningWriterOptions controls the behavior of a WarningHandler constructed using NewWarningWriter()
  66. type WarningWriterOptions struct {
  67. // Deduplicate indicates a given warning message should only be written once.
  68. // Setting this to true in a long-running process handling many warnings can result in increased memory use.
  69. Deduplicate bool
  70. // Color indicates that warning output can include ANSI color codes
  71. Color bool
  72. }
  73. // NewWarningWriter returns an implementation of WarningHandler that outputs code 299 warnings to the specified writer.
  74. func NewWarningWriter(out io.Writer, opts WarningWriterOptions) *warningWriter {
  75. h := &warningWriter{out: out, opts: opts}
  76. if opts.Deduplicate {
  77. h.written = map[string]struct{}{}
  78. }
  79. return h
  80. }
  81. const (
  82. yellowColor = "\u001b[33;1m"
  83. resetColor = "\u001b[0m"
  84. )
  85. // HandleWarningHeader prints warnings with code=299 to the configured writer.
  86. func (w *warningWriter) HandleWarningHeader(code int, agent string, message string) {
  87. if code != 299 || len(message) == 0 {
  88. return
  89. }
  90. w.writtenLock.Lock()
  91. defer w.writtenLock.Unlock()
  92. if w.opts.Deduplicate {
  93. if _, alreadyWritten := w.written[message]; alreadyWritten {
  94. return
  95. }
  96. w.written[message] = struct{}{}
  97. }
  98. w.writtenCount++
  99. if w.opts.Color {
  100. fmt.Fprintf(w.out, "%sWarning:%s %s\n", yellowColor, resetColor, message)
  101. } else {
  102. fmt.Fprintf(w.out, "Warning: %s\n", message)
  103. }
  104. }
  105. func (w *warningWriter) WarningCount() int {
  106. w.writtenLock.Lock()
  107. defer w.writtenLock.Unlock()
  108. return w.writtenCount
  109. }
  110. func handleWarnings(headers http.Header, handler WarningHandler) []net.WarningHeader {
  111. if handler == nil {
  112. handler = getDefaultWarningHandler()
  113. }
  114. warnings, _ := net.ParseWarningHeaders(headers["Warning"])
  115. for _, warning := range warnings {
  116. handler.HandleWarningHeader(warning.Code, warning.Agent, warning.Text)
  117. }
  118. return warnings
  119. }