-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathall_in_one.go
More file actions
425 lines (371 loc) · 13.8 KB
/
Copy pathall_in_one.go
File metadata and controls
425 lines (371 loc) · 13.8 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
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
package cmd
import (
"context"
"fmt"
"net"
"net/url"
"os"
"strings"
"time"
validate "github.com/go-playground/validator/v10"
"github.com/spf13/cobra"
"github.com/volatiletech/null/v9"
"golang.org/x/sync/errgroup"
"google.golang.org/protobuf/proto"
"k8s.io/apimachinery/pkg/types"
runtime_ctrl "sigs.k8s.io/controller-runtime"
controllerconfig "sigs.k8s.io/controller-runtime/pkg/config"
"sigs.k8s.io/controller-runtime/pkg/log"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
"github.com/pomerium/ingress-controller/controllers"
"github.com/pomerium/ingress-controller/controllers/gateway"
"github.com/pomerium/ingress-controller/controllers/ingress"
"github.com/pomerium/ingress-controller/controllers/settings"
"github.com/pomerium/ingress-controller/pomerium"
pomerium_ctrl "github.com/pomerium/ingress-controller/pomerium/ctrl"
"github.com/pomerium/ingress-controller/util"
health_ctrl "github.com/pomerium/ingress-controller/util/health"
"github.com/pomerium/pomerium/config"
"github.com/pomerium/pomerium/pkg/grpc/databroker"
"github.com/pomerium/pomerium/pkg/health"
"github.com/pomerium/pomerium/pkg/netutil"
"github.com/pomerium/pomerium/pkg/telemetry/trace"
)
type allCmdOptions struct {
ingressControllerOpts
debug bool
debugDumpConfigDiff bool
debugPomerium bool
debugEnvoy bool
adminBindAddr string
configControllerShutdownTimeout time.Duration
// healthProbeBindAddress must be externally accessible host:port
healthProbeBindAddress string
// metricsBindAddress must be externally accessible host:port
metricsBindAddress string `validate:"required,hostname_port"`
serverAddr string `validate:"required,hostname_port"`
sshAddr string
httpRedirectAddr string `validate:"required,hostname_port"`
deriveTLS string `validate:"required,hostname"`
grpcAddr string `validate:"required,hostname_port"`
services []string `validate:"dive,oneof=all authenticate authorize databroker proxy"`
CertificateControllerOptions certificateControllerOptions
DataBrokerOptions dataBrokerOptions
}
type allCmdParam struct {
settings types.NamespacedName
ingressOpts []ingress.Option
gatewayConfig *gateway.ControllerConfig
updateStatusFromService string
dumpConfigDiff bool
syncAPIURL string
syncAPIToken string
// bootstrapMetricsAddr for bootstrap configuration controller metrics
bootstrapMetricsAddr string
// ingressMetricsAddr for ingress+settings reconciliation controller metrics
ingressMetricsAddr string
// configControllerShutdownTimeout is max time to wait for graceful config (ingress & settings) controller shutdown
// when re-initialization is required. in case it cannot be shut down gracefully, the entire process would exit
// to be re-started by the kubernetes
configControllerShutdownTimeout time.Duration
certificateControllerName string
cfg config.Config
}
type allCmd struct {
allCmdOptions
cobra.Command
}
// AllInOneCommand runs embedded pomerium and controls it according to Settings CRD
func AllInOneCommand() (*cobra.Command, error) {
cmd := allCmd{
Command: cobra.Command{
Use: "all-in-one",
Short: "run ingress controller together with pomerium in all-in-one mode",
},
}
cmd.RunE = cmd.exec
if err := cmd.setupFlags(); err != nil {
return nil, err
}
return &cmd.Command, nil
}
// the below flags are not intended to be used by end users, but rather for development and debugging purposes
// setting them to hidden to avoid confusion, as enabling them may cause sensitive information to be logged or exposed
const (
debug = "debug"
debugPomerium = "debug-pomerium"
debugEnvoy = "debug-envoy"
debugAdminBindAddr = "debug-admin-addr"
debugDumpConfigDiff = "debug-dump-config-diff"
configControllerShutdown = "config-controller-shutdown"
)
var hidden = []string{
debugPomerium,
debugEnvoy,
debugAdminBindAddr,
debugDumpConfigDiff,
}
func (s *allCmd) setupFlags() error {
flags := s.PersistentFlags()
flags.BoolVar(&s.debug, debug, false, "enable debug logging")
flags.BoolVar(&s.debugDumpConfigDiff, debugDumpConfigDiff, false, "development dump of config diff, don't use in production")
flags.BoolVar(&s.debugPomerium, debugPomerium, false, "enable debug logging for pomerium")
flags.BoolVar(&s.debugEnvoy, debugEnvoy, false, "enable debug logging for envoy")
flags.StringVar(&s.metricsBindAddress, metricsBindAddress, "", "host:port for aggregate metrics. host is mandatory")
flags.StringVar(&s.healthProbeBindAddress, healthProbeBindAddress, "127.0.0.1:28080", "host:port for http health probes")
flags.StringVar(&s.adminBindAddr, debugAdminBindAddr, "", "host:port for admin server")
flags.StringVar(&s.serverAddr, "server-addr", ":8443", "the address the HTTPS server would bind to")
flags.StringVar(&s.sshAddr, "ssh-addr", "", "the address the SSH server would bind to")
flags.StringVar(&s.httpRedirectAddr, "http-redirect-addr", ":8080", "the address HTTP redirect would bind to")
flags.StringVar(&s.deriveTLS, "databroker-auto-tls", "", "enable auto TLS and generate server certificate for the domain")
flags.DurationVar(&s.configControllerShutdownTimeout, configControllerShutdown, time.Second*30, "timeout waiting for graceful config controller shutdown")
flags.StringVar(&s.grpcAddr, "grpc-addr", ":5443", "the address the gRPC server would bind to")
flags.StringSliceVar(&s.services, "services", []string{"all"}, "the pomerium services to run")
for _, flag := range hidden {
if err := s.PersistentFlags().MarkHidden(flag); err != nil {
return fmt.Errorf("failed to mark %s flag: %w", flag, err)
}
}
s.ingressControllerOpts.setupFlags(flags)
s.CertificateControllerOptions.setupFlags(flags)
s.DataBrokerOptions.setupFlags(flags)
return viperWalk(flags)
}
func (s *allCmdOptions) Validate() error {
return validate.New().Struct(s)
}
func (s *allCmd) exec(*cobra.Command, []string) error {
if err := s.Validate(); err != nil {
return err
}
setupLogger(s.debug)
ctx := runtime_ctrl.SetupSignalHandler()
param, err := s.getParam()
if err != nil {
return err
}
return param.run(ctx)
}
func (s *allCmdOptions) getParam() (*allCmdParam, error) {
settings, err := util.ParseNamespacedName(s.GlobalSettings, util.WithClusterScope())
if err != nil {
return nil, fmt.Errorf("--%s: %w", globalSettings, err)
}
if err = s.Validate(); err != nil {
return nil, fmt.Errorf("args: %w", err)
}
opts, err := s.getIngressControllerOptions()
if err != nil {
return nil, fmt.Errorf("options: %w", err)
}
gatewayConfig, err := s.getGatewayControllerConfig()
if err != nil {
return nil, fmt.Errorf("options: %w", err)
}
p := &allCmdParam{
settings: *settings,
ingressOpts: opts,
gatewayConfig: gatewayConfig,
updateStatusFromService: s.UpdateStatusFromService,
dumpConfigDiff: s.debugDumpConfigDiff,
configControllerShutdownTimeout: s.configControllerShutdownTimeout,
syncAPIURL: s.SyncAPIURL,
syncAPIToken: s.SyncAPIToken,
certificateControllerName: s.CertificateControllerOptions.Name,
}
if err := p.makeBootstrapConfig(*s); err != nil {
return nil, fmt.Errorf("bootstrap: %w", err)
}
return p, nil
}
func (s *allCmdParam) run(ctx context.Context) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
ctx = trace.NewContext(ctx, trace.NewSyncClient(nil))
ctx = health.Context(
ctx,
health_ctrl.SettingsBootstrapReconciler,
)
eg, ctx := errgroup.WithContext(ctx)
cfgCtl := util.NewRestartOnChange[*config.Config]()
runner, err := pomerium_ctrl.NewPomeriumRunner(s.cfg, cfgCtl.OnConfigUpdated, s.syncAPIURL)
if err != nil {
return fmt.Errorf("preparing to run pomerium: %w", err)
}
eg.Go(func() error { return runner.Run(ctx) })
eg.Go(func() error { return s.runBootstrapConfigController(ctx, runner) })
eg.Go(func() error {
return cfgCtl.Run(log.IntoContext(ctx, log.FromContext(ctx).WithName("config_restarter")),
isBootstrapEqual,
s.runConfigControllers,
s.configControllerShutdownTimeout)
})
eg.Go(func() error {
<-ctx.Done()
if err := trace.ShutdownContext(ctx); err != nil {
log.FromContext(ctx).Error(err, "failed to shutdown trace context")
}
return nil
})
return eg.Wait()
}
func (s *allCmdParam) makeBootstrapConfig(opt allCmdOptions) error {
s.cfg = *config.New(config.NewDefaultOptions())
s.cfg.Options.Services = strings.Join(opt.services, ",")
s.cfg.Options.Addr = opt.serverAddr
s.cfg.Options.GRPCAddr = opt.grpcAddr
s.cfg.Options.HTTPRedirectAddr = opt.httpRedirectAddr
ports, err := netutil.AllocatePorts(9)
if err != nil {
return fmt.Errorf("allocating ports: %w", err)
}
s.cfg.AllocatePorts(*(*[7]string)(ports[:7]))
if opt.deriveTLS != "" {
s.cfg.Options.DeriveInternalDomainCert = &opt.deriveTLS
s.cfg.Options.GRPCInsecure = proto.Bool(false)
}
s.bootstrapMetricsAddr = fmt.Sprintf("localhost:%s", ports[7])
s.ingressMetricsAddr = fmt.Sprintf("localhost:%s", ports[8])
s.cfg.Options.MetricsAddr = opt.metricsBindAddress
s.cfg.Options.HealthCheckAddr = opt.healthProbeBindAddress
s.cfg.MetricsScrapeEndpoints = []config.MetricsScrapeEndpoint{
{
Name: "bootstrap",
URL: url.URL{
Scheme: "http",
Host: s.bootstrapMetricsAddr,
Path: "/metrics",
},
Labels: map[string]string{
"service": "bootstrap-controller",
},
},
{
Name: "ingress",
URL: url.URL{
Scheme: "http",
Host: s.ingressMetricsAddr,
Path: "/metrics",
},
Labels: map[string]string{
"service": "ingress-controller",
},
},
}
if opt.debugPomerium {
s.cfg.Options.LogLevel = "debug"
} else {
s.cfg.Options.LogLevel = "info"
}
// Keep envoy (proxy) logging at info regardless of the core log level, so
// --debug-pomerium raises only Pomerium core logging. --debug-envoy below
// opts envoy into debug explicitly.
s.cfg.Options.ProxyLogLevel = "info"
if opt.debugEnvoy {
s.cfg.Options.ProxyLogLevel = "debug"
s.cfg.Options.LogLevel = "debug"
}
s.cfg.Options.EnvoyAdminAddress = opt.adminBindAddr
s.cfg.Options.HTTP3AdvertisePort = null.NewUint32(443, true)
s.cfg.Options.SSHAddr = opt.sshAddr
opt.DataBrokerOptions.apply(&s.cfg)
return nil
}
// runConfigController runs an integrated Ingress + Settings CRD controller
func (s *allCmdParam) runConfigControllers(ctx context.Context, cfg *config.Config) error {
c, err := s.buildController(ctx, cfg)
if err != nil {
return fmt.Errorf("build controller: %w", err)
}
return c.Run(ctx)
}
func (s *allCmdParam) buildController(ctx context.Context, cfg *config.Config) (*controllers.Controller, error) {
scheme, err := getScheme()
if err != nil {
return nil, fmt.Errorf("get scheme: %w", err)
}
conn, err := getDataBrokerConnection(ctx, cfg)
if err != nil {
return nil, fmt.Errorf("databroker connection: %w", err)
}
client := databroker.NewDataBrokerServiceClient(conn)
var reconciler pomerium.Reconciler
if s.syncAPIURL != "" {
_, port, err := net.SplitHostPort(s.cfg.Options.Addr)
if err != nil {
return nil, fmt.Errorf("couldn't get server address port: %w", err)
}
dialAddressOverride := net.JoinHostPort("localhost", port)
reconciler = pomerium.NewAPIReconciler(
s.syncAPIURL, s.syncAPIToken, s.cfg.Options, dialAddressOverride)
} else {
reconciler = pomerium.NewDataBrokerReconciler(client, s.dumpConfigDiff)
}
c := &controllers.Controller{
Reconciler: reconciler,
DataBrokerServiceClient: client,
MgrOpts: runtime_ctrl.Options{
Scheme: scheme,
Metrics: metricsserver.Options{
BindAddress: s.ingressMetricsAddr,
},
LeaderElection: false,
Controller: controllerconfig.Controller{
SkipNameValidation: new(true),
},
},
IngressCtrlOpts: s.ingressOpts,
GlobalSettings: &s.settings,
GatewayControllerConfig: s.gatewayConfig,
CertificateControllerName: s.certificateControllerName,
}
return c, nil
}
type bootstrapReconciler interface {
pomerium.ConfigReconciler
pomerium.IngressReconciler
}
// runBootstrapConfigController runs a controller that only listens to changes in SettingsCRD
// related to pomerium bootstrap parameters
func (s *allCmdParam) runBootstrapConfigController(ctx context.Context, reconciler bootstrapReconciler) error {
scheme, err := getScheme()
if err != nil {
return err
}
cfg, err := runtime_ctrl.GetConfig()
if err != nil {
return fmt.Errorf("get k8s api config: %w", err)
}
mgr, err := runtime_ctrl.NewManager(cfg, runtime_ctrl.Options{
Scheme: scheme,
Metrics: metricsserver.Options{BindAddress: s.bootstrapMetricsAddr},
LeaderElection: false,
})
if err != nil {
return fmt.Errorf("manager: %w", err)
}
name := settings.ControllerNameBootstrap
if host, err := os.Hostname(); err == nil {
name = fmt.Sprintf("%s pod/%s", name, host)
}
if err := settings.NewSettingsController(mgr, reconciler, s.settings, name, false, health_ctrl.SettingsBootstrapReconciler); err != nil {
return fmt.Errorf("settings controller: %w", err)
}
if s.syncAPIURL != "" {
// When using the sync API in all-in-one mode, also register a bootstrap
// ingress controller, in case we need a route to the sync API itself.
if err := ingress.NewIngressController(mgr, reconciler); err != nil {
return fmt.Errorf("ingress controller: %w", err)
}
}
return mgr.Start(ctx)
}
// isBootstrapEqual returns true if two configs are equal for the purpose of bootstrapping configuration controllers
// right now we only care about sharedkey as it is used for databroker
// and we do not update port allocations
func isBootstrapEqual(prev, next *config.Config) bool {
if prev == nil {
return false
}
return prev.Options.SharedKey == next.Options.SharedKey
}