mutation_detector.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. Copyright 2016 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 cache
  14. import (
  15. "fmt"
  16. "os"
  17. "reflect"
  18. "strconv"
  19. "sync"
  20. "time"
  21. "k8s.io/apimachinery/pkg/runtime"
  22. "k8s.io/apimachinery/pkg/util/diff"
  23. "k8s.io/client-go/pkg/api"
  24. )
  25. var mutationDetectionEnabled = false
  26. func init() {
  27. mutationDetectionEnabled, _ = strconv.ParseBool(os.Getenv("KUBE_CACHE_MUTATION_DETECTOR"))
  28. }
  29. type CacheMutationDetector interface {
  30. AddObject(obj interface{})
  31. Run(stopCh <-chan struct{})
  32. }
  33. func NewCacheMutationDetector(name string) CacheMutationDetector {
  34. if !mutationDetectionEnabled {
  35. return dummyMutationDetector{}
  36. }
  37. return &defaultCacheMutationDetector{name: name, period: 1 * time.Second}
  38. }
  39. type dummyMutationDetector struct{}
  40. func (dummyMutationDetector) Run(stopCh <-chan struct{}) {
  41. }
  42. func (dummyMutationDetector) AddObject(obj interface{}) {
  43. }
  44. // defaultCacheMutationDetector gives a way to detect if a cached object has been mutated
  45. // It has a list of cached objects and their copies. I haven't thought of a way
  46. // to see WHO is mutating it, just that it's getting mutated.
  47. type defaultCacheMutationDetector struct {
  48. name string
  49. period time.Duration
  50. lock sync.Mutex
  51. cachedObjs []cacheObj
  52. // failureFunc is injectable for unit testing. If you don't have it, the process will panic.
  53. // This panic is intentional, since turning on this detection indicates you want a strong
  54. // failure signal. This failure is effectively a p0 bug and you can't trust process results
  55. // after a mutation anyway.
  56. failureFunc func(message string)
  57. }
  58. // cacheObj holds the actual object and a copy
  59. type cacheObj struct {
  60. cached interface{}
  61. copied interface{}
  62. }
  63. func (d *defaultCacheMutationDetector) Run(stopCh <-chan struct{}) {
  64. // we DON'T want protection from panics. If we're running this code, we want to die
  65. go func() {
  66. for {
  67. d.CompareObjects()
  68. select {
  69. case <-stopCh:
  70. return
  71. case <-time.After(d.period):
  72. }
  73. }
  74. }()
  75. }
  76. // AddObject makes a deep copy of the object for later comparison. It only works on runtime.Object
  77. // but that covers the vast majority of our cached objects
  78. func (d *defaultCacheMutationDetector) AddObject(obj interface{}) {
  79. if _, ok := obj.(DeletedFinalStateUnknown); ok {
  80. return
  81. }
  82. if _, ok := obj.(runtime.Object); !ok {
  83. return
  84. }
  85. copiedObj, err := api.Scheme.Copy(obj.(runtime.Object))
  86. if err != nil {
  87. return
  88. }
  89. d.lock.Lock()
  90. defer d.lock.Unlock()
  91. d.cachedObjs = append(d.cachedObjs, cacheObj{cached: obj, copied: copiedObj})
  92. }
  93. func (d *defaultCacheMutationDetector) CompareObjects() {
  94. d.lock.Lock()
  95. defer d.lock.Unlock()
  96. altered := false
  97. for i, obj := range d.cachedObjs {
  98. if !reflect.DeepEqual(obj.cached, obj.copied) {
  99. fmt.Printf("CACHE %s[%d] ALTERED!\n%v\n", d.name, i, diff.ObjectDiff(obj.cached, obj.copied))
  100. altered = true
  101. }
  102. }
  103. if altered {
  104. msg := fmt.Sprintf("cache %s modified", d.name)
  105. if d.failureFunc != nil {
  106. d.failureFunc(msg)
  107. return
  108. }
  109. panic(msg)
  110. }
  111. }