-
Notifications
You must be signed in to change notification settings - Fork 53
/
gateway.go
364 lines (315 loc) · 10.5 KB
/
gateway.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
package gateway
import (
"context"
"crypto"
"crypto/tls"
"fmt"
"net"
"github.com/rancher/opni/pkg/auth/challenges"
authv1 "github.com/rancher/opni/pkg/auth/cluster/v1"
authv2 "github.com/rancher/opni/pkg/auth/cluster/v2"
"github.com/rancher/opni/pkg/patch"
"github.com/spf13/afero"
"github.com/hashicorp/go-plugin"
"github.com/prometheus/client_golang/prometheus"
"github.com/samber/lo"
"go.uber.org/zap"
"golang.org/x/exp/slices"
"golang.org/x/mod/module"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/emptypb"
bootstrapv1 "github.com/rancher/opni/pkg/apis/bootstrap/v1"
bootstrapv2 "github.com/rancher/opni/pkg/apis/bootstrap/v2"
capabilityv1 "github.com/rancher/opni/pkg/apis/capability/v1"
controlv1 "github.com/rancher/opni/pkg/apis/control/v1"
corev1 "github.com/rancher/opni/pkg/apis/core/v1"
streamv1 "github.com/rancher/opni/pkg/apis/stream/v1"
"github.com/rancher/opni/pkg/auth/cluster"
"github.com/rancher/opni/pkg/auth/session"
"github.com/rancher/opni/pkg/bootstrap"
"github.com/rancher/opni/pkg/capabilities"
"github.com/rancher/opni/pkg/config"
cfgmeta "github.com/rancher/opni/pkg/config/meta"
"github.com/rancher/opni/pkg/config/v1beta1"
"github.com/rancher/opni/pkg/health"
"github.com/rancher/opni/pkg/keyring"
"github.com/rancher/opni/pkg/logger"
"github.com/rancher/opni/pkg/machinery"
"github.com/rancher/opni/pkg/plugins"
"github.com/rancher/opni/pkg/plugins/hooks"
"github.com/rancher/opni/pkg/plugins/meta"
"github.com/rancher/opni/pkg/plugins/types"
"github.com/rancher/opni/pkg/storage"
"github.com/rancher/opni/pkg/util"
"github.com/rancher/opni/pkg/util/waitctx"
)
type Gateway struct {
GatewayOptions
config *config.GatewayConfig
tlsConfig *tls.Config
logger *zap.SugaredLogger
httpServer *GatewayHTTPServer
grpcServer *GatewayGRPCServer
statusQuerier health.HealthStatusQuerier
storageBackend storage.Backend
capBackendStore capabilities.BackendStore
syncRequester *SyncRequester
}
type GatewayOptions struct {
lifecycler config.Lifecycler
}
type GatewayOption func(*GatewayOptions)
func (o *GatewayOptions) apply(opts ...GatewayOption) {
for _, op := range opts {
op(o)
}
}
func WithLifecycler(lc config.Lifecycler) GatewayOption {
return func(o *GatewayOptions) {
o.lifecycler = lc
}
}
func NewGateway(ctx context.Context, conf *config.GatewayConfig, pl plugins.LoaderInterface, opts ...GatewayOption) *Gateway {
options := GatewayOptions{
lifecycler: config.NewUnavailableLifecycler(cfgmeta.ObjectList{conf}),
}
options.apply(opts...)
lg := logger.New().Named("gateway")
conf.Spec.SetDefaults()
storageBackend, err := machinery.ConfigureStorageBackend(ctx, &conf.Spec.Storage)
if err != nil {
lg.With(
zap.Error(err),
).Panic("failed to configure storage backend")
}
// configure the server-side installer template with the external hostname
// and grpc port which agents will connect to
_, port, err := net.SplitHostPort(conf.Spec.GRPCListenAddress)
if err != nil {
lg.With(
zap.Error(err),
).Panic("failed to parse listen address")
}
capBackendStore := capabilities.NewBackendStore(capabilities.ServerInstallerTemplateSpec{
Address: conf.Spec.Hostname,
Port: fmt.Sprint(port),
}, lg)
// add capabilities from plugins
pl.Hook(hooks.OnLoadM(func(p types.CapabilityBackendPlugin, md meta.PluginMeta) {
info, err := p.Info(ctx, &emptypb.Empty{})
if err != nil {
lg.With(
zap.String("plugin", md.Module),
).Error("failed to get capability info")
return
}
if err := capBackendStore.Add(info.Name, p); err != nil {
lg.With(
zap.String("plugin", md.Module),
zap.Error(err),
).Error("failed to add capability backend")
}
lg.With(
zap.String("plugin", md.Module),
zap.String("capability", info.Name),
).Info("added capability backend")
}))
// serve system plugin kv stores
pl.Hook(hooks.OnLoadM(func(p types.SystemPlugin, md meta.PluginMeta) {
ns := md.Module
if err := module.CheckPath(ns); err != nil {
lg.With(
zap.String("namespace", ns),
zap.Error(err),
).Warn("system plugin module name is invalid")
return
}
store := storageBackend.KeyValueStore(ns)
go p.ServeKeyValueStore(store)
}))
// serve caching provider for plugin RPCs
pl.Hook(hooks.OnLoadM(func(p types.SystemPlugin, md meta.PluginMeta) {
ns := md.Module
if err := module.CheckPath(ns); err != nil {
lg.With(
zap.String("namespace", ns),
zap.Error(err),
).Warn("system plugin module name is invalid")
return
}
go p.ServeCachingProvider()
}))
// set up http server
tlsConfig, pkey, err := loadTLSConfig(&conf.Spec)
if err != nil {
lg.With(
zap.Error(err),
).Panic("failed to load TLS config")
}
httpServer := NewHTTPServer(ctx, &conf.Spec, lg, pl)
// Set up cluster auth
ephemeralKeys, err := machinery.LoadEphemeralKeys(afero.Afero{
Fs: afero.NewOsFs(),
}, conf.Spec.Keyring.EphemeralKeyDirs...)
if err != nil {
lg.With(
zap.Error(err),
).Panic("failed to load ephemeral keys")
}
v1Verifier := challenges.NewKeyringVerifier(storageBackend, authv1.DomainString, lg.Named("authv1"))
v2Verifier := challenges.NewKeyringVerifier(storageBackend, authv2.DomainString, lg.Named("authv2"))
sessionAttrChallenge, err := session.NewServerChallenge(
keyring.New(lo.ToAnySlice(ephemeralKeys)...))
if err != nil {
lg.With(
zap.Error(err),
).Panic("failed to configure authentication")
}
v1Challenge := authv1.NewServerChallenge(streamv1.Stream_Connect_FullMethodName, v1Verifier, lg.Named("authv1"))
v2Challenge := authv2.NewServerChallenge(v2Verifier, lg.Named("authv2"))
clusterAuth := cluster.StreamServerInterceptor(challenges.Chained(
challenges.If(authv2.ShouldEnableIncoming).Then(v2Challenge).Else(v1Challenge),
challenges.If(session.ShouldEnableIncoming).Then(sessionAttrChallenge),
))
// set up plugin sync server
syncServer, err := patch.NewFilesystemPluginSyncServer(conf.Spec.Plugins, lg,
patch.WithPluginSyncFilters(func(pm meta.PluginMeta) bool {
if pm.ExtendedMetadata != nil {
// only sync plugins that have the agent mode set
return slices.Contains(pm.ExtendedMetadata.ModeList.Modes, meta.ModeAgent)
}
return true // default to syncing all plugins
}),
)
if err != nil {
lg.With(
zap.Error(err),
).Panic("failed to create plugin sync server")
}
httpServer.metricsRegisterer.MustRegister(syncServer.Collectors()...)
if err := syncServer.RunGarbageCollection(ctx, storageBackend); err != nil {
lg.With(
zap.Error(err),
).Error("failed to run garbage collection")
}
// set up grpc server
grpcServer := NewGRPCServer(&conf.Spec, lg,
grpc.Creds(credentials.NewTLS(tlsConfig)),
grpc.ChainStreamInterceptor(
clusterAuth,
syncServer.StreamServerInterceptor(),
NewLastKnownDetailsApplier(storageBackend),
),
)
// set up stream server
listener := health.NewListener()
monitor := health.NewMonitor(health.WithLogger(lg.Named("monitor")))
sync := NewSyncRequester(lg)
delegate := NewDelegateServer(storageBackend, lg)
// set up agent connection handlers
agentHandler := MultiConnectionHandler(listener, sync, delegate)
go monitor.Run(ctx, listener)
streamSvc := NewStreamServer(agentHandler, storageBackend, httpServer.metricsRegisterer, lg)
controlv1.RegisterHealthListenerServer(streamSvc, listener)
streamv1.RegisterDelegateServer(streamSvc.InternalServiceRegistrar(), delegate)
streamv1.RegisterStreamServer(grpcServer, streamSvc)
controlv1.RegisterPluginSyncServer(grpcServer, syncServer)
pl.Hook(hooks.OnLoadMC(streamSvc.OnPluginLoad))
// set up bootstrap server
bootstrapServerV1 := bootstrap.NewServer(storageBackend, pkey, capBackendStore)
bootstrapServerV2 := bootstrap.NewServerV2(storageBackend, pkey)
bootstrapv1.RegisterBootstrapServer(grpcServer, bootstrapServerV1)
bootstrapv2.RegisterBootstrapServer(grpcServer, bootstrapServerV2)
g := &Gateway{
GatewayOptions: options,
tlsConfig: tlsConfig,
config: conf,
logger: lg,
storageBackend: storageBackend,
capBackendStore: capBackendStore,
httpServer: httpServer,
grpcServer: grpcServer,
statusQuerier: monitor,
syncRequester: sync,
}
waitctx.Go(ctx, func() {
<-ctx.Done()
lg.Info("shutting down plugins")
plugin.CleanupClients()
lg.Info("all plugins shut down")
})
return g
}
type keyValueStoreServer interface {
ServeKeyValueStore(store storage.KeyValueStore)
}
func (g *Gateway) ListenAndServe(ctx context.Context) error {
lg := g.logger
ctx, ca := context.WithCancel(ctx)
// start http server
e1 := lo.Async(func() error {
err := g.httpServer.ListenAndServe(ctx)
if err != nil {
lg.With(
zap.Error(err),
).Warn("http server exited with error")
}
return err
})
// start grpc server
e2 := lo.Async(func() error {
err := g.grpcServer.ListenAndServe(ctx)
if err != nil {
lg.With(
zap.Error(err),
).Warn("grpc server exited with error")
}
return err
})
return util.WaitAll(ctx, ca, e1, e2)
}
// Implements management.CoreDataSource
func (g *Gateway) StorageBackend() storage.Backend {
return g.storageBackend
}
// Implements management.CoreDataSource
func (g *Gateway) TLSConfig() *tls.Config {
return g.httpServer.tlsConfig
}
// Implements management.CapabilitiesDataSource
func (g *Gateway) CapabilitiesStore() capabilities.BackendStore {
return g.capBackendStore
}
// Implements management.CapabilitiesDataSource
func (g *Gateway) NodeManagerServer() capabilityv1.NodeManagerServer {
return g.syncRequester
}
// Implements management.HealthStatusDataSource
func (g *Gateway) GetClusterHealthStatus(ref *corev1.Reference) (*corev1.HealthStatus, error) {
hs := g.statusQuerier.GetHealthStatus(ref.Id)
if hs.Health == nil && hs.Status == nil {
return nil, status.Error(codes.NotFound, "no health or status has been reported for this cluster yet")
}
return hs, nil
}
// Implements management.HealthStatusDataSource
func (g *Gateway) WatchClusterHealthStatus(ctx context.Context) <-chan *corev1.ClusterHealthStatus {
return g.statusQuerier.WatchHealthStatus(ctx)
}
func (g *Gateway) MustRegisterCollector(collector prometheus.Collector) {
g.httpServer.metricsRegisterer.MustRegister(collector)
}
func loadTLSConfig(cfg *v1beta1.GatewayConfigSpec) (*tls.Config, crypto.Signer, error) {
servingCertBundle, caPool, err := util.LoadServingCertBundle(cfg.Certs)
if err != nil {
return nil, nil, err
}
return &tls.Config{
MinVersion: tls.VersionTLS12,
RootCAs: caPool,
Certificates: []tls.Certificate{*servingCertBundle},
}, servingCertBundle.PrivateKey.(crypto.Signer), nil
}