Skip to content

Commit cdc1557

Browse files
committed
Adding panic-safe ResourceWatcher
1 parent 92416ac commit cdc1557

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

pkg/util/k8sutil/informer.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2018 ArangoDB GmbH, Cologne, Germany
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
//
20+
// Author Ewout Prangsma
21+
//
22+
23+
package k8sutil
24+
25+
import (
26+
"github.com/rs/zerolog"
27+
"k8s.io/apimachinery/pkg/fields"
28+
"k8s.io/apimachinery/pkg/runtime"
29+
"k8s.io/client-go/tools/cache"
30+
)
31+
32+
// ResourceWatcher is a helper to watch for events in a specific type
33+
// of resource. The handler functions are protected from panics.
34+
type ResourceWatcher struct {
35+
informer cache.Controller
36+
}
37+
38+
// NewResourceWatcher creates a helper that watches for changes in a resource of a specific type.
39+
// If wraps the given handler functions, such that panics are caught and logged.
40+
func NewResourceWatcher(log zerolog.Logger, getter cache.Getter, resource, namespace string,
41+
objType runtime.Object, h cache.ResourceEventHandlerFuncs) *ResourceWatcher {
42+
source := cache.NewListWatchFromClient(
43+
getter,
44+
resource,
45+
namespace,
46+
fields.Everything())
47+
48+
_, informer := cache.NewIndexerInformer(source, objType, 0, cache.ResourceEventHandlerFuncs{
49+
AddFunc: func(obj interface{}) {
50+
defer func() {
51+
if err := recover(); err != nil {
52+
log.Error().Interface("error", err).Msg("Recovered from panic")
53+
}
54+
}()
55+
if h.AddFunc != nil {
56+
h.AddFunc(obj)
57+
}
58+
},
59+
UpdateFunc: func(oldObj, newObj interface{}) {
60+
defer func() {
61+
if err := recover(); err != nil {
62+
log.Error().Interface("error", err).Msg("Recovered from panic")
63+
}
64+
}()
65+
if h.UpdateFunc != nil {
66+
h.UpdateFunc(oldObj, newObj)
67+
}
68+
},
69+
DeleteFunc: func(obj interface{}) {
70+
defer func() {
71+
if err := recover(); err != nil {
72+
log.Error().Interface("error", err).Msg("Recovered from panic")
73+
}
74+
}()
75+
if h.DeleteFunc != nil {
76+
h.DeleteFunc(obj)
77+
}
78+
},
79+
}, cache.Indexers{})
80+
81+
return &ResourceWatcher{
82+
informer: informer,
83+
}
84+
}
85+
86+
// Run continues to watch for events on the selected type of resource
87+
// until the given channel is closed.
88+
func (rw *ResourceWatcher) Run(stopCh <-chan struct{}) {
89+
rw.informer.Run(stopCh)
90+
}

0 commit comments

Comments
 (0)