This repository has been archived by the owner on May 12, 2021. It is now read-only.
/
vc.go
276 lines (220 loc) · 6.16 KB
/
vc.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
//
// Copyright (c) 2017-2018 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
package main
import (
"errors"
"flag"
"fmt"
"os"
"os/signal"
"path"
"path/filepath"
"sync"
"syscall"
"time"
"github.com/fsnotify/fsnotify"
"github.com/kata-containers/ksm-throttler/pkg/client"
ksig "github.com/kata-containers/ksm-throttler/pkg/signals"
"github.com/sirupsen/logrus"
)
// DefaultURI is populated at link time with the value of:
// ${locatestatedir}/run/ksm-throttler/ksm.sock
var DefaultURI string
// ArgURI is populated at runtime from the option -uri
var ArgURI = flag.String("uri", "", "KSM throttler gRPC URI")
var triggerLog = logrus.WithFields(logrus.Fields{
"source": "throttler-trigger",
"name": "vc",
"pid": os.Getpid(),
})
const (
defaultgRPCSocket = "/var/run/ksm-throttler/ksm.sock"
// In linux the max socket path is 108 including null character
// see http://man7.org/linux/man-pages/man7/unix.7.html
socketPathMaxLength = 107
)
// getSocketPath computes the path of the KSM throttler socket.
// Note that when socket activated, the socket path is specified
// in the systemd socket file but the same value is set in
// DefaultURI at link time.
func getSocketPath() (string, error) {
// Invoking "go build" without any linker option will not
// populate DefaultURI, so fallback to a reasonable
// path. People should really use the Makefile though.
if DefaultURI == "" {
DefaultURI = defaultgRPCSocket
}
socketURI := DefaultURI
if len(*ArgURI) != 0 {
socketURI = *ArgURI
}
if len(socketURI) > socketPathMaxLength {
return "", fmt.Errorf("socket path too long %d (max %d)",
len(socketURI), socketPathMaxLength)
}
return socketURI, nil
}
func waitForDirectory(dir string) error {
if dir == "" || !path.IsAbs(dir) {
return fmt.Errorf("Can not wait for empty or relative directory")
}
syncCh := make(chan error)
logger := triggerLog.WithField("directory", dir)
watcher, err := fsnotify.NewWatcher()
if err != nil {
logger.WithError(err).Error("could not create new watcher")
return err
}
defer watcher.Close()
logger.Debug("Waiting for directory")
go func() {
for {
select {
case event := <-watcher.Events:
logger.WithField("event", event).Debug("Got event")
if event.Op&fsnotify.Create != fsnotify.Create ||
event.Name != dir {
continue
}
syncCh <- nil
return
case err := <-watcher.Errors:
logger.WithError(err).Error("Directory monitoring failed")
syncCh <- err
return
}
}
}()
if err := watcher.Add(filepath.Dir(dir)); err != nil {
logger.WithError(err).Error("Could not monitor directory")
return err
}
syncErr := <-syncCh
if syncErr == nil {
logger.Debug("directory created")
}
return err
}
func monitorPods(vcRunRoot, throttler string) error {
var wg sync.WaitGroup
watcher, err := fsnotify.NewWatcher()
if err != nil {
triggerLog.WithError(err).Error("could not create new watcher")
return err
}
defer watcher.Close()
logger := triggerLog.WithFields(logrus.Fields{
"vc-root": vcRunRoot,
"throttler": throttler,
})
// Wait for vc root if it does not exist
if _, err := os.Stat(vcRunRoot); os.IsNotExist(err) {
if err := waitForDirectory(vcRunRoot); err != nil {
logger.WithError(err).Error("Could not monitor virtcontainers base directory")
return err
}
}
// Wait for the pods path if it does not exist
podsPath := filepath.Join(vcRunRoot, "sbs")
logger = logger.WithField("pods-path", podsPath)
if _, err := os.Stat(podsPath); os.IsNotExist(err) {
if err := waitForDirectory(podsPath); err != nil {
logger.WithError(err).Error("Could not monitor virtcontainers pods")
return err
}
// First pod created, we should kick the throttler
if err := client.Kick(throttler); err != nil {
logger.WithError(err).Error("Could not kick the throttler")
return err
}
}
wg.Add(1)
logger.Debug("Monitoring virtcontainers events")
go func() {
for {
select {
case event := <-watcher.Events:
logger.WithField("event", event).Debug("Virtcontainers monitoring event")
if event.Op&fsnotify.Create != fsnotify.Create {
continue
}
logger.Debug("Kicking KSM throttler")
if err := client.Kick(throttler); err != nil {
logger.WithError(err).Error("Could not kick the throttler")
continue
}
case err := <-watcher.Errors:
logger.WithError(err).Error("Virtcontainers monitoring error")
wg.Done()
return
}
}
}()
if err := watcher.Add(podsPath); err != nil {
logger.WithError(err).Error("Could not monitor virtcontainers root")
return err
}
wg.Wait()
return nil
}
func setLoggingLevel(l string) error {
levelStr := l
level, err := logrus.ParseLevel(levelStr)
if err != nil {
return err
}
triggerLog.Logger.SetLevel(level)
triggerLog.Logger.Formatter = &logrus.TextFormatter{TimestampFormat: time.RFC3339Nano}
return nil
}
func setupSignalHandler() {
sigCh := make(chan os.Signal, 8)
for _, sig := range ksig.HandledSignals() {
signal.Notify(sigCh, sig)
}
var debug = true
go func() {
for {
sig := <-sigCh
nativeSignal, ok := sig.(syscall.Signal)
if !ok {
err := errors.New("unknown signal")
triggerLog.WithError(err).WithField("signal", sig.String()).Error()
continue
}
if ksig.FatalSignal(nativeSignal) {
triggerLog.WithField("signal", sig).Error("received fatal signal")
ksig.Die()
} else if ksig.NonFatalSignal(nativeSignal) {
if debug {
triggerLog.WithField("signal", sig).Debug("handling signal")
ksig.Backtrace()
}
}
}
}()
}
func main() {
vcRoot := flag.String("root", "/run/vc", "Virtcontainers root directory")
logLevel := flag.String("log", "warn",
"log messages above specified level; one of debug, warn, error, fatal or panic")
flag.Parse()
if err := setLoggingLevel(*logLevel); err != nil {
fmt.Fprintf(os.Stderr, "Could not set logging level %s: %v", *logLevel, err)
os.Exit(1)
}
ksig.SetLogger(triggerLog)
uri, err := getSocketPath()
if err != nil {
logrus.WithError(err).Error("Could net get service socket URI")
os.Exit(1)
}
setupSignalHandler()
if err := monitorPods(*vcRoot, uri); err != nil {
logrus.WithError(err).Error("Could not monitor pods")
os.Exit(1)
}
}