forked from cisco-app-networking/networkservicemesh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
100 lines (79 loc) · 3.05 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
package main
import (
"context"
"net"
"os"
"strings"
"time"
"github.com/adodon2go/networkservicemesh/utils"
"github.com/adodon2go/networkservicemesh/pkg/probes/health"
"github.com/adodon2go/networkservicemesh/pkg/tools/jaeger"
"github.com/adodon2go/networkservicemesh/pkg/probes"
"github.com/adodon2go/networkservicemesh/controlplane/pkg/nsmd"
"github.com/adodon2go/networkservicemesh/controlplane/pkg/serviceregistry"
"github.com/adodon2go/networkservicemesh/sdk/monitor/remote"
"github.com/sirupsen/logrus"
"github.com/adodon2go/networkservicemesh/controlplane/api/connection"
unified "github.com/adodon2go/networkservicemesh/controlplane/api/networkservice"
proxynetworkserviceserver "github.com/adodon2go/networkservicemesh/controlplane/pkg/remote/proxy_network_service_server"
"github.com/adodon2go/networkservicemesh/pkg/tools"
)
var version string
// Default values and environment variables of proxy connection
const (
ProxyNsmdAPIAddressEnv = "PROXY_NSMD_API_ADDRESS"
ProxyNsmdAPIAddressDefaults = ":5006"
)
func main() {
logrus.Info("Starting proxy nsmd...")
logrus.Infof("Version: %v", version)
utils.PrintAllEnv(logrus.StandardLogger())
start := time.Now()
// Capture signals to cleanup before exiting
c := tools.NewOSSignalChannel()
closer := jaeger.InitJaeger("proxy-nsmd")
defer func() { _ = closer.Close() }()
goals := &proxyNsmdProbeGoals{}
nsmdProbes := probes.New("Prxoy NSMD liveness/readiness healthcheck", goals)
nsmdProbes.BeginHealthCheck()
apiRegistry := nsmd.NewApiRegistry()
serviceRegistry := nsmd.NewServiceRegistry()
defer serviceRegistry.Stop()
// Choose a public API listener
sock, err := apiRegistry.NewPublicListener(getProxyNSMDAPIAddress())
if err != nil {
logrus.Errorf("Failed to start Public API server...")
return
}
logrus.Info("Public listener is ready")
goals.SetPublicListenerReady()
startAPIServerAt(sock, serviceRegistry, nsmdProbes)
logrus.Info("API server is ready")
goals.SetServerAPIReady()
elapsed := time.Since(start)
logrus.Debugf("Starting Proxy NSMD took: %s", elapsed)
<-c
}
func getProxyNSMDAPIAddress() string {
result := os.Getenv(ProxyNsmdAPIAddressEnv)
if strings.TrimSpace(result) == "" {
result = ProxyNsmdAPIAddressDefaults
}
return result
}
// StartAPIServerAt starts GRPC API server at sock
func startAPIServerAt(sock net.Listener, serviceRegistry serviceregistry.ServiceRegistry, probes probes.Probes) {
grpcServer := tools.NewServer(context.Background())
remoteConnectionMonitor := remote.NewProxyMonitorServer()
connection.RegisterMonitorConnectionServer(grpcServer, remoteConnectionMonitor)
probes.Append(health.NewGrpcHealth(grpcServer, sock.Addr(), time.Minute))
// Register Remote NetworkServiceManager
remoteServer := proxynetworkserviceserver.NewProxyNetworkServiceServer(serviceRegistry)
unified.RegisterNetworkServiceServer(grpcServer, remoteServer)
go func() {
if err := grpcServer.Serve(sock); err != nil {
logrus.Fatalf("Failed to start NSM API server %+v", err)
}
}()
logrus.Infof("NSM gRPC API Server: %s is operational", sock.Addr().String())
}