-
Notifications
You must be signed in to change notification settings - Fork 6
/
server.go
165 lines (136 loc) · 3.48 KB
/
server.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package server
import (
"net/http"
"os"
"path/filepath"
"sync/atomic"
"time"
"github.com/fsnotify/fsnotify"
"github.com/rs/zerolog/log"
"github.com/bluebrown/kobold/kobold/config"
)
type muxGenerator interface {
Generate(conf *config.NormalizedConfig) (http.Handler, error)
}
type Server struct {
atomicHandler *atomic.Value
generator muxGenerator
}
type Options struct {
Watch bool
ConfigPath string
Config *config.NormalizedConfig
Datapath string
UseK8sChain bool
muxGenerator muxGenerator
defaultRegistry string
imagerefTemplate string
debounce time.Duration
}
type Option func(*Options)
func NewOrDie(options ...Option) *Server {
opts := &Options{}
for _, o := range options {
o(opts)
}
if opts.ConfigPath != "" && opts.Config != nil {
panic("configPath and config are mutually exclusive")
}
if opts.ConfigPath != "" {
var err error
opts.Config, err = config.ReadPath(opts.ConfigPath)
if err != nil {
panic(err)
}
}
if opts.Config == nil {
opts.Config = &config.NormalizedConfig{}
opts.Config.Defaults()
}
if opts.Datapath == "" {
opts.Datapath = filepath.Join(os.TempDir(), "kobold")
}
if opts.muxGenerator == nil {
opts.muxGenerator = generator{
dataDir: opts.Datapath,
useK8sChain: opts.UseK8sChain,
defaultRegistry: opts.defaultRegistry,
imagerefTemplate: opts.imagerefTemplate,
debounceTime: opts.debounce,
}
}
s := &Server{
generator: opts.muxGenerator,
atomicHandler: &atomic.Value{},
}
mux, err := s.generator.Generate(opts.Config)
if err != nil {
panic(err)
}
s.atomicHandler.Store(mux)
if opts.Watch && opts.ConfigPath != "" {
go WatchConfigOrDie(opts.ConfigPath, func(c *config.NormalizedConfig) {
log.Info().Msg("reloading config")
m, err := s.generator.Generate(c)
if err != nil {
log.Error().Err(err).Msg("could not reload config")
return
}
s.Reload(m)
})
}
return s
}
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.atomicHandler.Load().(http.Handler).ServeHTTP(w, r)
}
func (s *Server) Reload(handler http.Handler) {
s.atomicHandler.Store(handler)
}
func WatchConfigOrDie(path string, onChange func(c *config.NormalizedConfig)) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
panic(err)
}
defer watcher.Close()
err = watcher.Add(path)
if err != nil {
panic(err)
}
log.Debug().Str("path", path).Msg("watching config")
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
log.Trace().Str("op", event.Op.String()).Msg("inotify event received")
// if its not an event that modifies the file, ignore it
if !(event.Op.Has(fsnotify.Write) || event.Op.Has(fsnotify.Create) || event.Op.Has(fsnotify.Remove)) {
continue
}
// if the file has been removed, rewatch it. This helps with
// scenarios where the file is deleted and moved or symlinked
// to get atomic writes. I.e. in kubernetes this is the case
if event.Op.Has(fsnotify.Remove) {
if err := watcher.Add(event.Name); err != nil {
log.Error().Err(err).Msg("failed to re-watch config")
continue
}
}
// finally load the new config
conf, err := config.ReadPath(path)
if err != nil {
log.Error().Err(err).Msg("failed to config")
continue
}
// call the on change handler
onChange(conf)
case err, ok := <-watcher.Errors:
log.Error().Err(err).Msg("error while watching config file")
if !ok {
return
}
}
}
}