-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.go
More file actions
195 lines (173 loc) · 6.84 KB
/
main.go
File metadata and controls
195 lines (173 loc) · 6.84 KB
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
// SPDX-FileCopyrightText: (C) 2025 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
// Package main implements the Host Manager service.
package main
import (
"context"
"flag"
"net"
"os"
"os/signal"
"sync"
"syscall"
"github.com/open-edge-platform/infra-core/inventory/v2/pkg/client"
"github.com/open-edge-platform/infra-core/inventory/v2/pkg/flags"
"github.com/open-edge-platform/infra-core/inventory/v2/pkg/logging"
"github.com/open-edge-platform/infra-core/inventory/v2/pkg/metrics"
"github.com/open-edge-platform/infra-core/inventory/v2/pkg/oam"
"github.com/open-edge-platform/infra-core/inventory/v2/pkg/policy/rbac"
"github.com/open-edge-platform/infra-core/inventory/v2/pkg/tracing"
"github.com/open-edge-platform/infra-managers/host/internal/hostmgr/handlers"
"github.com/open-edge-platform/infra-managers/host/pkg/config"
"github.com/open-edge-platform/infra-managers/host/pkg/hostmgr"
)
var zlog = logging.GetLogger("HostManagerMain")
var (
RepoURL = "https://github.com/open-edge-platform/infra-managers/host.git"
Version = "<unset>"
Revision = "<unset>"
BuildDate = "<unset>"
)
var (
servaddr = flag.String(flags.ServerAddress, "0.0.0.0:50001", flags.ServerAddressDescription)
invsvcaddr = flag.String(client.InventoryAddress, "localhost:50051", client.InventoryAddressDescription)
oamservaddr = flag.String(oam.OamServerAddress, "", oam.OamServerAddressDescription)
insecureGrpc = flag.Bool(client.InsecureGrpc, true, client.InsecureGrpcDescription)
caCertPath = flag.String(client.CaCertPath, "", client.CaCertPathDescription)
tlsCertPath = flag.String(client.TLSCertPath, "", client.TLSCertPathDescription)
tlsKeyPath = flag.String(client.TLSKeyPath, "", client.TLSKeyPathDescription)
enableTracing = flag.Bool(tracing.EnableTracing, false, tracing.EnableTracingDescription)
traceURL = flag.String(tracing.TraceURL, "", tracing.TraceURLDescription)
allowHostDiscovery = flag.Bool(
hostmgr.AllowHostDiscovery,
hostmgr.AllowHostDiscoveryValue,
hostmgr.AllowHostDiscoveryDescription,
)
disabledProvisioning = flag.Bool(
hostmgr.DisabledProvisioning,
hostmgr.DisabledProvisioningValue,
hostmgr.DisabledProvisioningDescription,
)
enableAuth = flag.Bool(rbac.EnableAuth, true, rbac.EnableAuthDescription)
rbacRules = flag.String(rbac.RbacRules, "/rego/authz.rego", rbac.RbacRulesDescription)
invCacheUUIDEnable = flag.Bool(client.InvCacheUUIDEnable, false, client.InvCacheUUIDEnableDescription)
invCacheStaleTimeout = flag.Duration(
client.InvCacheStaleTimeout, client.InvCacheStaleTimeoutDefault, client.InvCacheStaleTimeoutDescription)
invCacheStaleTimeoutOffset = flag.Uint(
client.InvCacheStaleTimeoutOffset, client.InvCacheStaleTimeoutOffsetDefault, client.InvCacheStaleTimeoutOffsetDescription)
enableMetrics = flag.Bool(metrics.EnableMetrics, false, metrics.EnableMetricsDescription)
metricsAddress = flag.String(metrics.MetricsAddress, metrics.MetricsAddressDefault, metrics.MetricsAddressDescription)
)
var (
wg = sync.WaitGroup{} // waitgroup so main will wait for all go routines to exit cleanly
readyChan = make(chan bool, 1) // channel to signal the readiness.
termChan = make(chan bool, 1) // channel to signal termination of main process.
sigChan = make(chan os.Signal, 1) // channel to handle any interrupt signals
)
func printSummary() {
zlog.Info().Msg("Starting Host Manager")
zlog.Info().Msgf("RepoURL: %s, Version: %s, Revision: %s, BuildDate: %s\n", RepoURL, Version, Revision, BuildDate)
}
func SetTracing(traceURL string) func(context.Context) error {
cleanup, exportErr := tracing.NewTraceExporterHTTP(traceURL, "HostManager", nil)
if exportErr != nil {
zlog.Err(exportErr).Msg("Error creating trace exporter")
}
if cleanup != nil {
zlog.Info().Msgf("Tracing enabled %s", traceURL)
} else {
zlog.Info().Msg("Tracing disabled")
}
return cleanup
}
func setOAM(oamAddress string, termChan, readyChan chan bool, wg *sync.WaitGroup) {
if oamAddress != "" {
// Add oam grpc server
wg.Add(1)
zlog.Info().Msg("initReadinessServer start.")
go func() {
if err := oam.StartOamGrpcServer(termChan, readyChan, wg, oamAddress, false); err != nil {
zlog.InfraSec().InfraErr(err).Msg("Cannot start Host manager OAM gRPC server")
}
}()
}
}
func main() {
flag.Parse()
conf := config.HostMgrConfig{
EnableTracing: *enableTracing,
EnableMetrics: *enableMetrics,
TraceURL: *traceURL,
InventoryAddr: *invsvcaddr,
CACertPath: *caCertPath,
TLSKeyPath: *tlsKeyPath,
TLSCertPath: *tlsCertPath,
InsecureGRPC: *insecureGrpc,
EnableHostDiscovery: *allowHostDiscovery,
DisabledProvisioning: *disabledProvisioning,
EnableUUIDCache: *invCacheUUIDEnable,
UUIDCacheTTL: *invCacheStaleTimeout,
UUIDCacheTTLOffset: int(*invCacheStaleTimeoutOffset),
}
if err := conf.Validate(); err != nil {
zlog.InfraSec().Fatal().Err(err).Msgf("Failed to start due to invalid configuration: %v", conf)
}
zlog.Info().Msgf("Starting Host Manager conf %v", conf)
// Print a summary of the build
printSummary()
if conf.EnableTracing {
cleanup := SetTracing(conf.TraceURL)
if cleanup != nil {
defer func() {
cleanErr := cleanup(context.Background())
if cleanErr != nil {
zlog.Err(cleanErr).Msg("Error in tracing cleanup")
}
}()
}
}
go hostmgr.StartAvailableManager(termChan)
setOAM(*oamservaddr, termChan, readyChan, &wg)
invClient, invEvents, err := hostmgr.StartInvGrpcCli(
&wg,
conf,
)
if err != nil {
zlog.Fatal().Msgf("Couldn't create a NewInventoryClient: %v", err)
}
nbHandler, err := handlers.NewNBHandler(invClient, invEvents)
if err != nil {
zlog.InfraSec().Fatal().Msgf("Failed to create HRM northbound handler: %v", err)
}
err = nbHandler.Start()
if err != nil {
zlog.InfraSec().Fatal().Msgf("Failed to start HRM northbound handler: %v", err)
}
signal.Notify(sigChan, syscall.SIGTERM, syscall.SIGINT)
go func() {
<-sigChan
zlog.InfraSec().Info().Msg("Received Term Signal, will close all grpc connections.")
// stops availability_mgr, oam_server and grpc_server
close(termChan)
// stops northbound handler
nbHandler.Stop()
// stops inventory client
hostmgr.CloseInvGrpcCli()
}()
// Create a listener on TCP port
lc := net.ListenConfig{}
lis, listenErr := lc.Listen(context.Background(), "tcp", *servaddr)
if listenErr != nil {
zlog.InfraSec().Fatal().Err(listenErr).Msgf("Error listening with TCP on %s", *servaddr)
}
// Add host manager grpc server - it sets readyCHan to true
hostmgr.StartGrpcSrv(lis, readyChan, termChan, &wg,
hostmgr.EnableTracing(*enableTracing),
hostmgr.EnableAuth(*enableAuth),
hostmgr.WithRbacRulesPath(*rbacRules),
hostmgr.EnableMetrics(*enableMetrics),
hostmgr.WithMetricsAddress(*metricsAddress),
)
wg.Wait()
}