-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
xds_server_ondemand.go
67 lines (51 loc) · 2.29 KB
/
xds_server_ondemand.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package envoy
import (
"context"
"sync"
"github.com/cilium/cilium/pkg/completion"
"github.com/cilium/cilium/pkg/logging/logfields"
"github.com/cilium/cilium/pkg/option"
"github.com/cilium/cilium/pkg/policy"
)
type onDemandXdsStarter struct {
XDSServer
runDir string
envoyOnce sync.Once
}
var _ XDSServer = &onDemandXdsStarter{}
func (o *onDemandXdsStarter) AddListener(name string, kind policy.L7ParserType, port uint16, isIngress bool, mayUseOriginalSourceAddr bool, wg *completion.WaitGroup) {
if err := o.startEmbeddedEnvoy(nil); err != nil {
log.WithError(err).Error("Envoy: Failed to start embedded Envoy proxy on demand")
}
o.XDSServer.AddListener(name, kind, port, isIngress, mayUseOriginalSourceAddr, wg)
}
func (o *onDemandXdsStarter) UpsertEnvoyResources(ctx context.Context, resources Resources, portAllocator PortAllocator) error {
if err := o.startEmbeddedEnvoy(nil); err != nil {
log.WithError(err).Error("Envoy: Failed to start embedded Envoy proxy on demand")
}
return o.XDSServer.UpsertEnvoyResources(ctx, resources, portAllocator)
}
func (o *onDemandXdsStarter) UpdateEnvoyResources(ctx context.Context, old, new Resources, portAllocator PortAllocator) error {
if err := o.startEmbeddedEnvoy(nil); err != nil {
log.WithError(err).Error("Envoy: Failed to start embedded Envoy proxy on demand")
}
return o.XDSServer.UpdateEnvoyResources(ctx, old, new, portAllocator)
}
func (o *onDemandXdsStarter) startEmbeddedEnvoy(wg *completion.WaitGroup) error {
var startErr error
o.envoyOnce.Do(func() {
// Start embedded Envoy on first invocation
_, startErr = startEmbeddedEnvoy(o.runDir, option.Config.EnvoyLogPath, 0)
// Add Prometheus listener if the port is (properly) configured
if option.Config.ProxyPrometheusPort < 0 || option.Config.ProxyPrometheusPort > 65535 {
log.WithField(logfields.Port, option.Config.ProxyPrometheusPort).Error("Envoy: Invalid configured proxy-prometheus-port")
} else if option.Config.ProxyPrometheusPort != 0 {
// We could do this in the bootstrap config as with the Envoy DaemonSet,
// but then a failure to bind to the configured port would fail starting Envoy.
o.XDSServer.AddMetricsListener(uint16(option.Config.ProxyPrometheusPort), wg)
}
})
return startErr
}