-
Notifications
You must be signed in to change notification settings - Fork 1
/
watcher.go
146 lines (121 loc) · 2.72 KB
/
watcher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package watcher
import (
"context"
"errors"
"fmt"
"sync"
"time"
"github.com/gardener/gardener/pkg/apis/core/v1beta1"
"github.com/pPrecel/cloudagent/internal/system"
"github.com/pPrecel/cloudagent/pkg/agent"
"github.com/pPrecel/cloudagent/pkg/config"
"github.com/sirupsen/logrus"
)
type Options struct {
Context context.Context
Logger *logrus.Entry
ConfigPath string
}
type watcher struct {
mu sync.Mutex
w agent.ResourceGetter
o *Options
getConfig func(string) (*config.Config, error)
notifyChange func(string) (*system.Notifier, error)
}
func New(o *Options) *watcher {
return &watcher{
o: o,
getConfig: config.Read,
notifyChange: system.NotifyChange,
}
}
func (w *watcher) GetGardenerCache() agent.Cache[*v1beta1.ShootList] {
w.mu.Lock()
defer w.mu.Unlock()
if w.w != nil {
return w.w.GetGardenerCache()
}
return nil
}
func (w *watcher) GetGeneralError() error {
w.mu.Lock()
defer w.mu.Unlock()
if w.w != nil {
return w.w.GetGeneralError()
}
return errors.New("watcher not implemented")
}
func (w *watcher) Start() {
for {
select {
case <-w.o.Context.Done():
w.o.Logger.Warn("watcher context done. Exiting")
return
default:
err := w.start()
if err != nil {
w.o.Logger.Warnf("runned watcher error: %s", err)
}
// wait 1sec to avoid CPU throttling
time.Sleep(time.Second * 1)
}
}
}
type watcherStarter interface {
start(context.Context) error
}
func (w *watcher) start() error {
var err error
starter, err := w.buildWatcher()
if err != nil {
return fmt.Errorf("failed to build watcher: %f", err)
}
ctx, cancel := context.WithCancel(w.o.Context)
defer cancel()
go starter.start(ctx)
w.o.Logger.Info("starting config notifier")
n, err := w.notifyChange(w.o.ConfigPath)
if err != nil {
return err
}
defer n.Stop()
select {
case err := <-n.Errors:
return err
case <-n.IsMotified:
w.o.Logger.Info("configuration midyfication detected")
return nil
}
}
func (w *watcher) buildWatcher() (watcherStarter, error) {
w.mu.Lock()
defer w.mu.Unlock()
cfg, err := w.getConfig(w.o.ConfigPath)
if err != nil {
return nil, fmt.Errorf("failed to read config: %s", err)
}
if cfg.PersistentSpec == "on-demand" {
ondemand := newOnDemand(w.o)
w.w = ondemand
return ondemand, nil
}
w.w = newCache(w.w)
return newCached(w.w, w.o.Logger, w.o.ConfigPath), nil
}
func newCache(actualCache agent.ResourceGetter) agent.ResourceGetter {
if actualCache == nil {
return &agent.ServerCache{
GardenerCache: agent.NewCache[*v1beta1.ShootList](),
}
}
switch actualCache.(type) {
case *agent.ServerCache:
c := actualCache.(*agent.ServerCache)
c.GardenerCache.Clean()
c.GeneralError = nil
return c
default:
return newCache(nil)
}
}