-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
cell.go
187 lines (153 loc) · 5.07 KB
/
cell.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package envoy
import (
"context"
"fmt"
"path/filepath"
"runtime/pprof"
"github.com/sirupsen/logrus"
"github.com/cilium/cilium/pkg/hive/cell"
"github.com/cilium/cilium/pkg/hive/job"
"github.com/cilium/cilium/pkg/ipcache"
"github.com/cilium/cilium/pkg/option"
"github.com/cilium/cilium/pkg/proxy/endpoint"
"github.com/cilium/cilium/pkg/time"
)
// Cell initializes and manages the Envoy proxy and its control-plane components like xDS- and accesslog server.
// It is used to provide support for Ingress, GatewayAPI and L7 network policies (e.g. HTTP).
var Cell = cell.Module(
"envoy-proxy",
"Envoy proxy and control-plane",
cell.Provide(newEnvoyXDSServer),
cell.Provide(newEnvoyAdminClient),
cell.ProvidePrivate(newEnvoyAccessLogServer),
cell.ProvidePrivate(newLocalEndpointStore),
cell.ProvidePrivate(newArtifactCopier),
cell.Invoke(registerEnvoyVersionCheck),
)
type xdsServerParams struct {
cell.In
Lifecycle cell.Lifecycle
IPCache *ipcache.IPCache
LocalEndpointStore *LocalEndpointStore
// Depend on access log server to enforce init order.
// This ensures that the access log server is ready before it gets used by the
// Cilium Envoy filter after receiving the resources via xDS server.
AccessLogServer *AccessLogServer
// Depend on ArtifactCopier to enforce init order and ensure that the additional artifacts are copied
// before starting the xDS server (and starting to configure Envoy).
ArtifactCopier *ArtifactCopier
}
func newEnvoyXDSServer(params xdsServerParams) (XDSServer, error) {
xdsServer, err := newXDSServer(GetSocketDir(option.Config.RunDir), params.IPCache, params.LocalEndpointStore)
if err != nil {
return nil, fmt.Errorf("failed to create Envoy xDS server: %w", err)
}
if !option.Config.EnableL7Proxy {
log.Debug("L7 proxies are disabled - not starting Envoy xDS server")
return xdsServer, nil
}
params.Lifecycle.Append(cell.Hook{
OnStart: func(startContext cell.HookContext) error {
if err := xdsServer.start(); err != nil {
return fmt.Errorf("failed to start Envoy xDS server: %w", err)
}
return nil
},
OnStop: func(stopContext cell.HookContext) error {
xdsServer.stop()
return nil
},
})
if !option.Config.ExternalEnvoyProxy {
return &onDemandXdsStarter{
XDSServer: xdsServer,
runDir: option.Config.RunDir,
}, nil
}
return xdsServer, nil
}
func newEnvoyAdminClient() *EnvoyAdminClient {
return NewEnvoyAdminClientForSocket(GetSocketDir(option.Config.RunDir))
}
type accessLogServerParams struct {
cell.In
Lifecycle cell.Lifecycle
LocalEndpointStore *LocalEndpointStore
}
func newEnvoyAccessLogServer(params accessLogServerParams) *AccessLogServer {
if !option.Config.EnableL7Proxy {
log.Debug("L7 proxies are disabled - not starting Envoy AccessLog server")
return nil
}
accessLogServer := newAccessLogServer(GetSocketDir(option.Config.RunDir), params.LocalEndpointStore)
params.Lifecycle.Append(cell.Hook{
OnStart: func(startContext cell.HookContext) error {
if err := accessLogServer.start(); err != nil {
return fmt.Errorf("failed to start Envoy AccessLog server: %w", err)
}
return nil
},
OnStop: func(stopContext cell.HookContext) error {
accessLogServer.stop()
return nil
},
})
return accessLogServer
}
type versionCheckParams struct {
cell.In
Lifecycle cell.Lifecycle
Logger logrus.FieldLogger
JobRegistry job.Registry
Scope cell.Scope
EnvoyAdminClient *EnvoyAdminClient
}
func registerEnvoyVersionCheck(params versionCheckParams) {
if !option.Config.EnableL7Proxy || option.Config.DisableEnvoyVersionCheck {
return
}
envoyVersionFunc := func() (string, error) {
return getRemoteEnvoyVersion(params.EnvoyAdminClient)
}
if !option.Config.ExternalEnvoyProxy {
envoyVersionFunc = getEmbeddedEnvoyVersion
}
jobGroup := params.JobRegistry.NewGroup(
params.Scope,
job.WithLogger(params.Logger),
job.WithPprofLabels(pprof.Labels("cell", "envoy")),
)
params.Lifecycle.Append(jobGroup)
// To prevent agent restarts in case the Envoy DaemonSet isn't ready yet,
// version check is performed periodically and any errors are logged
// and reported via health reporter.
jobGroup.Add(job.Timer("version-check", func(_ context.Context) error {
if err := checkEnvoyVersion(envoyVersionFunc); err != nil {
params.Logger.WithError(err).Error("Envoy: Version check failed")
return err
}
return nil
}, 5*time.Minute))
}
func newLocalEndpointStore() *LocalEndpointStore {
return &LocalEndpointStore{
networkPolicyEndpoints: make(map[string]endpoint.EndpointUpdater),
}
}
func newArtifactCopier(lifecycle cell.Lifecycle) *ArtifactCopier {
artifactCopier := &ArtifactCopier{
sourcePath: "/envoy-artifacts",
targetPath: filepath.Join(option.Config.RunDir, "envoy", "artifacts"),
}
lifecycle.Append(cell.Hook{
OnStart: func(startContext cell.HookContext) error {
if err := artifactCopier.Copy(); err != nil {
return fmt.Errorf("failed to copy artifacts to envoy: %w", err)
}
return nil
},
})
return artifactCopier
}