-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
281 lines (237 loc) · 8.38 KB
/
main.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
277
278
279
280
281
package main
import (
"context"
"encoding/json"
"fmt"
"net/http"
"os"
"os/signal"
"path/filepath"
"syscall"
"time"
"github.com/sirupsen/logrus"
flag "github.com/spf13/pflag"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
deviceagent "github.com/nais/device/internal/device-agent"
"github.com/nais/device/internal/device-agent/config"
"github.com/nais/device/internal/device-agent/filesystem"
"github.com/nais/device/internal/device-agent/runtimeconfig"
"github.com/nais/device/internal/logger"
"github.com/nais/device/internal/notify"
"github.com/nais/device/internal/otel"
"github.com/nais/device/internal/pb"
"github.com/nais/device/internal/unixsocket"
"github.com/nais/device/internal/version"
)
const (
healthCheckInterval = 20 * time.Second // how often to healthcheck gateways
versionCheckInterval = 1 * time.Hour // how often to check for a new version of naisdevice
)
func handleSignals(log *logrus.Entry, cancel context.CancelFunc) {
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
go func() {
sig := <-signals
log.Infof("signal handler: got signal '%s', canceling main context", sig)
cancel()
log.Info("signal handler: allowing 1s to stop gracefully...", sig)
// normally cancelling the context will result in the program returning before the next lines are evaluated
time.Sleep(1 * time.Second)
log.Info("signal handler: force-exiting")
os.Exit(0)
}()
}
func main() {
cfg, err := config.DefaultConfig()
if err != nil {
logrus.StandardLogger().Errorf("unable to read default configuration: %v", err)
os.Exit(1)
}
flag.StringVar(&cfg.ConfigDir, "config-dir", cfg.ConfigDir, "path to agent config directory")
flag.StringVar(&cfg.Interface, "interface", cfg.Interface, "name of tunnel interface")
flag.StringVar(&cfg.LogLevel, "log-level", cfg.LogLevel, "which log level to output")
flag.StringVar(&cfg.GrpcAddress, "grpc-address", cfg.GrpcAddress, "unix socket for gRPC server")
flag.StringVar(&cfg.DeviceAgentHelperAddress, "device-agent-helper-address", cfg.DeviceAgentHelperAddress, "device-agent-helper unix socket")
flag.StringVar(&cfg.GoogleAuthServerAddress, "google-auth-server-address", cfg.GoogleAuthServerAddress, "Google auth-server address")
flag.Parse()
cfg.SetDefaults()
logDir := filepath.Join(cfg.ConfigDir, logger.LogDir)
log := logger.SetupLogger(cfg.LogLevel, logDir, logger.Agent).WithField("component", "main")
defer logger.CapturePanic(log)
programContext, programCancel := context.WithCancel(context.Background())
handleSignals(log, programCancel)
cfg.PopulateAgentConfiguration(log)
log.Infof("naisdevice-agent %s starting up", version.Version)
log.Infof("configuration: %+v", cfg)
notifier := notify.New(log)
err = run(programContext, log, cfg, notifier)
if err != nil {
notifier.Errorf(err.Error())
log.Errorf("naisdevice-agent terminated with error.")
os.Exit(1)
}
log.Infof("naisdevice-agent shutting down.")
}
func run(ctx context.Context, log *logrus.Entry, cfg *config.Config, notifier notify.Notifier) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
otelCancel, err := otel.SetupOTelSDK(ctx, "naisdevice-agent", log)
if err != nil {
return fmt.Errorf("setup OTel SDK: %s", err)
}
defer func() {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
if err := otelCancel(ctx); err != nil {
log.Errorf("shutdown OTel SDK: %s", err)
}
cancel()
}()
if err := filesystem.EnsurePrerequisites(cfg); err != nil {
return fmt.Errorf("missing prerequisites: %s", err)
}
rc, err := runtimeconfig.New(log.WithField("component", "runtimeconfig"), cfg)
if err != nil {
log.Errorf("instantiate runtime config: %v", err)
return fmt.Errorf("unable to start naisdevice-agent, check logs for details")
}
if cfg.AgentConfiguration.ILoveNinetiesBoybands {
err := rc.PopulateTenants(ctx)
if err != nil {
return fmt.Errorf("populate tenants from bucket: %w", err)
}
}
log.Infof("naisdevice-helper connection on unix socket %s", cfg.DeviceAgentHelperAddress)
connection, err := grpc.Dial(
"unix:"+cfg.DeviceAgentHelperAddress,
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithIdleTimeout(10*time.Hour),
grpc.WithStatsHandler(otel.NewGRPCClientHandler(pb.DeviceHelper_Ping_FullMethodName)),
)
if err != nil {
return fmt.Errorf("connect to naisdevice-helper: %v", err)
}
client := pb.NewDeviceHelperClient(connection)
defer connection.Close()
go func() {
var helperCheckErrors []error
for ctx.Err() == nil {
select {
case <-ctx.Done():
return
case <-time.After(healthCheckInterval):
err = helperHealthCheck(ctx, client)
if err != nil {
helperCheckErrors = append(helperCheckErrors, err)
if len(helperCheckErrors) < 3 {
break
}
log.WithField("errors", helperCheckErrors).Errorf("Unable to communicate with helper. Agent shutting down")
notifier.Errorf("Unable to communicate with helper. Agent shutting down.")
cancel()
}
// healthcheck successful, reset errors
helperCheckErrors = nil
}
}
}()
listener, err := unixsocket.ListenWithFileMode(cfg.GrpcAddress, 0o666)
if err != nil {
return err
}
log.Infof("accepting network connections on unix socket %s", cfg.GrpcAddress)
statusChannel := make(chan *pb.AgentStatus, 32)
stateMachine := deviceagent.NewStateMachine(ctx, rc, *cfg, notifier, client, statusChannel, log.WithField("component", "statemachine"))
grpcServer := grpc.NewServer(
grpc.StatsHandler(otelgrpc.NewServerHandler()),
)
das := deviceagent.NewServer(ctx, log.WithField("component", "device-agent-server"), cfg, rc, notifier, stateMachine.SendEvent)
pb.RegisterDeviceAgentServer(grpcServer, das)
newVersionChannel := make(chan bool, 1)
go versionChecker(ctx, newVersionChannel, notifier, log)
go func() {
// This routine forwards status updates from the state machine to the device agent server
newVersionAvailable := false
for ctx.Err() == nil {
select {
case newVersionAvailable = <-newVersionChannel:
case s := <-statusChannel:
s.NewVersionAvailable = newVersionAvailable
s.Tenants = rc.Tenants()
das.UpdateAgentStatus(s)
case <-ctx.Done():
}
}
}()
go func() {
stateMachine.Run(ctx)
// after state machine is done, stop the grpcServer
grpcServer.Stop()
}()
err = grpcServer.Serve(listener)
if err != nil {
return fmt.Errorf("failed to start gRPC server: %v", err)
}
log.Infof("gRPC server shut down.")
return nil
}
func versionChecker(ctx context.Context, newVersionChannel chan<- bool, notifier notify.Notifier, logger logrus.FieldLogger) {
versionCheckTimer := time.NewTimer(versionCheckInterval)
for ctx.Err() == nil {
select {
case <-ctx.Done():
return
case <-versionCheckTimer.C:
newVersionAvailable, err := checkNewVersionAvailable(ctx)
if err != nil {
logger.Infof("check for new version: %s", err)
break
}
newVersionChannel <- newVersionAvailable
if newVersionAvailable {
notifier.Infof("New version of device agent available: https://doc.nais.io/how-to-guides/naisdevice/update")
versionCheckTimer.Stop()
} else {
versionCheckTimer.Reset(versionCheckInterval)
}
}
}
}
func helperHealthCheck(ctx context.Context, client pb.DeviceHelperClient) error {
helperHealthCheckCtx, helperHealthCheckCancel := context.WithTimeout(ctx, 5*time.Second)
defer helperHealthCheckCancel()
if _, err := client.Ping(helperHealthCheckCtx, &pb.PingRequest{}); err != nil {
return err
}
return nil
}
func checkNewVersionAvailable(ctx context.Context) (bool, error) {
type response struct {
Tag string `json:"tag_name"`
}
ctx, span := otel.Start(ctx, "CheckNewVersionAvailable")
defer span.End()
req, err := http.NewRequestWithContext(ctx, "GET", "https://api.github.com/repos/nais/device/releases/latest", nil)
if err != nil {
span.RecordError(err)
return false, err
}
resp, err := otelhttp.DefaultClient.Do(req)
if err != nil {
return false, fmt.Errorf("retrieve current release version: %s", err)
}
defer resp.Body.Close()
res := &response{}
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(res)
if err != nil {
span.RecordError(err)
return false, fmt.Errorf("unmarshal response: %s", err)
}
if version.Version != res.Tag {
return true, nil
}
return false, nil
}