-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathdaemon.go
105 lines (88 loc) · 3.22 KB
/
daemon.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
// Copyright (c) 2025 Tigera, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package daemon
import (
"context"
"sync"
"time"
"github.com/sirupsen/logrus"
"github.com/projectcalico/calico/guardian/pkg/config"
"github.com/projectcalico/calico/guardian/pkg/server"
"github.com/projectcalico/calico/guardian/pkg/tunnel"
)
// Run starts the daemon, which configures and starts the services needed for guardian to run.
func Run(ctx context.Context, cfg config.Config, proxyTargets []server.Target) {
tunnelDialOpts := []tunnel.DialerOption{
tunnel.WithDialerRetryInterval(cfg.TunnelDialRetryInterval),
tunnel.WithDialerTimeout(cfg.TunnelDialTimeout),
tunnel.WithDialerRetryAttempts(cfg.TunnelDialRetryAttempts),
tunnel.WithDialerKeepAliveSettings(cfg.KeepAliveEnable, time.Duration(cfg.KeepAliveInterval)*time.Millisecond),
}
proxyURL, err := cfg.GetHTTPProxyURL()
if err != nil {
logrus.WithError(err).Fatal("Failed to resolve proxy URL.")
} else if proxyURL != nil {
tunnelDialOpts = append(tunnelDialOpts, tunnel.WithDialerHTTPProxyURL(proxyURL))
}
srvOpts := []server.Option{
server.WithProxyTargets(proxyTargets),
server.WithConnectionRetryAttempts(cfg.ConnectionRetryAttempts),
server.WithConnectionRetryInterval(cfg.ConnectionRetryInterval),
}
tlsConfig, cert, err := cfg.TLSConfig()
if err != nil {
logrus.WithError(err).Fatal("Failed to create tls config")
}
logrus.Infof("Using server name %s", tlsConfig.ServerName)
dialer, err := tunnel.NewTLSSessionDialer(cfg.VoltronURL, tlsConfig, tunnelDialOpts...)
if err != nil {
logrus.WithError(err).Fatal("Failed to create session dialer.")
}
srv, err := server.New(ctx, cert, dialer, srvOpts...)
if err != nil {
logrus.WithError(err).Fatal("Failed to create server")
}
health, err := server.NewHealth()
if err != nil {
logrus.WithError(err).Fatal("Failed to create health server")
}
go func() {
// Health checks start, meaning everything before has worked.
if err = health.ListenAndServeHTTP(); err != nil {
logrus.WithError(err).Fatal("Health exited with error")
}
}()
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
// Allow requests to come down from the management cluster.
if err := srv.ListenAndServeManagementCluster(); err != nil {
logrus.WithError(err).Info("Serving the tunnel exited.")
}
}()
// Allow requests from the cluster to be sent up to the management cluster.
if cfg.Listen {
wg.Add(1)
go func() {
defer wg.Done()
if err := srv.ListenAndServeCluster(); err != nil {
logrus.WithError(err).Info("proxy tunnel exited with an error")
}
}()
}
if err := srv.WaitForShutdown(); err != nil {
logrus.WithError(err).Fatal("proxy tunnel exited with an error")
}
}