2 Copyright 2016 The Kubernetes Authors.
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
29 "k8s.io/apimachinery/pkg/runtime"
30 "k8s.io/apimachinery/pkg/util/diff"
33 var mutationDetectionEnabled = false
36 mutationDetectionEnabled, _ = strconv.ParseBool(os.Getenv("KUBE_CACHE_MUTATION_DETECTOR"))
39 type CacheMutationDetector interface {
40 AddObject(obj interface{})
41 Run(stopCh <-chan struct{})
44 func NewCacheMutationDetector(name string) CacheMutationDetector {
45 if !mutationDetectionEnabled {
46 return dummyMutationDetector{}
48 klog.Warningln("Mutation detector is enabled, this will result in memory leakage.")
49 return &defaultCacheMutationDetector{name: name, period: 1 * time.Second}
52 type dummyMutationDetector struct{}
54 func (dummyMutationDetector) Run(stopCh <-chan struct{}) {
56 func (dummyMutationDetector) AddObject(obj interface{}) {
59 // defaultCacheMutationDetector gives a way to detect if a cached object has been mutated
60 // It has a list of cached objects and their copies. I haven't thought of a way
61 // to see WHO is mutating it, just that it's getting mutated.
62 type defaultCacheMutationDetector struct {
69 // failureFunc is injectable for unit testing. If you don't have it, the process will panic.
70 // This panic is intentional, since turning on this detection indicates you want a strong
71 // failure signal. This failure is effectively a p0 bug and you can't trust process results
72 // after a mutation anyway.
73 failureFunc func(message string)
76 // cacheObj holds the actual object and a copy
77 type cacheObj struct {
82 func (d *defaultCacheMutationDetector) Run(stopCh <-chan struct{}) {
83 // we DON'T want protection from panics. If we're running this code, we want to die
90 case <-time.After(d.period):
95 // AddObject makes a deep copy of the object for later comparison. It only works on runtime.Object
96 // but that covers the vast majority of our cached objects
97 func (d *defaultCacheMutationDetector) AddObject(obj interface{}) {
98 if _, ok := obj.(DeletedFinalStateUnknown); ok {
101 if obj, ok := obj.(runtime.Object); ok {
102 copiedObj := obj.DeepCopyObject()
105 defer d.lock.Unlock()
106 d.cachedObjs = append(d.cachedObjs, cacheObj{cached: obj, copied: copiedObj})
110 func (d *defaultCacheMutationDetector) CompareObjects() {
112 defer d.lock.Unlock()
115 for i, obj := range d.cachedObjs {
116 if !reflect.DeepEqual(obj.cached, obj.copied) {
117 fmt.Printf("CACHE %s[%d] ALTERED!\n%v\n", d.name, i, diff.ObjectDiff(obj.cached, obj.copied))
123 msg := fmt.Sprintf("cache %s modified", d.name)
124 if d.failureFunc != nil {