forked from flyteorg/flyte
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.go
More file actions
220 lines (191 loc) · 8.41 KB
/
Copy pathsetup.go
File metadata and controls
220 lines (191 loc) · 8.41 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
package executor
import (
"context"
"crypto/tls"
"fmt"
"net/http"
"os"
"connectrpc.com/connect"
"connectrpc.com/otelconnect"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/kubernetes"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/metrics/filters"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
"sigs.k8s.io/controller-runtime/pkg/webhook"
flyteorgv1 "github.com/flyteorg/flyte/v2/executor/api/v1"
"github.com/flyteorg/flyte/v2/executor/pkg/config"
"github.com/flyteorg/flyte/v2/executor/pkg/controller"
"github.com/flyteorg/flyte/v2/executor/pkg/plugin"
webhookPkg "github.com/flyteorg/flyte/v2/executor/pkg/webhook"
"github.com/flyteorg/flyte/v2/flyteplugins/go/tasks/pluginmachinery"
"github.com/flyteorg/flyte/v2/flyteplugins/go/tasks/pluginmachinery/catalog"
cachecatalog "github.com/flyteorg/flyte/v2/flyteplugins/go/tasks/pluginmachinery/catalog/cache_service"
webhookConfig "github.com/flyteorg/flyte/v2/flyteplugins/go/tasks/pluginmachinery/secret/config"
connectorplugin "github.com/flyteorg/flyte/v2/flyteplugins/go/tasks/plugins/webapi/connector"
"github.com/flyteorg/flyte/v2/flytestdlib/app"
"github.com/flyteorg/flyte/v2/flytestdlib/otelutils"
"github.com/flyteorg/flyte/v2/flytestdlib/promutils"
"github.com/flyteorg/flyte/v2/flytestdlib/storage"
"github.com/flyteorg/flyte/v2/gen/go/flyteidl2/workflow/workflowconnect"
_ "github.com/flyteorg/flyte/v2/executor/plugins"
_ "github.com/flyteorg/flyte/v2/flyteplugins/go/tasks/plugins/k8s/clustered"
_ "github.com/flyteorg/flyte/v2/flyteplugins/go/tasks/plugins/k8s/pod"
)
var scheme = runtime.NewScheme()
const otelServiceName = "executor"
func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(flyteorgv1.AddToScheme(scheme))
}
// Scheme returns the runtime.Scheme with executor CRDs registered.
// Useful for callers that need to pass the scheme to InitKubernetesClient.
func Scheme() *runtime.Scheme {
return scheme
}
// Setup registers the executor as a background worker on the SetupContext.
// Requires sc.K8sConfig and sc.DataStore to be set.
func Setup(ctx context.Context, sc *app.SetupContext) error {
ctrl.SetLogger(zap.New(zap.UseDevMode(true)))
cfg := config.GetConfig()
for _, reg := range pluginmachinery.PluginRegistry().GetSchemeRegisters() {
utilruntime.Must(reg.AddToScheme(scheme))
}
// Register the connector (webapi) backend plugin so task types backed by an external connector
// service are routed to it. This must run before plugin.NewRegistry below, which snapshots the
// core plugins once.
connectorplugin.RegisterConnectorPlugin(&connectorplugin.ConnectorService{})
var tlsOpts []func(*tls.Config)
if !cfg.EnableHTTP2 {
tlsOpts = append(tlsOpts, func(c *tls.Config) {
c.NextProtos = []string{"http/1.1"}
})
}
wCfg := webhookConfig.GetConfig()
webhookServerOptions := webhook.Options{TLSOpts: tlsOpts}
webhookServerOptions.CertDir = wCfg.ExpandCertDir()
webhookServerOptions.CertName = webhookPkg.ServerCertKey
webhookServerOptions.KeyName = webhookPkg.ServerCertPrivateKey
webhookServerOptions.Port = wCfg.ListenPort
metricsServerOptions := metricsserver.Options{
BindAddress: cfg.MetricsBindAddress,
SecureServing: cfg.MetricsSecure,
TLSOpts: tlsOpts,
}
if cfg.MetricsSecure {
metricsServerOptions.FilterProvider = filters.WithAuthenticationAndAuthorization
}
if len(cfg.MetricsCertPath) > 0 {
metricsServerOptions.CertDir = cfg.MetricsCertPath
metricsServerOptions.CertName = cfg.MetricsCertName
metricsServerOptions.KeyName = cfg.MetricsCertKey
}
mgr, err := ctrl.NewManager(sc.K8sConfig, ctrl.Options{
Scheme: scheme,
Metrics: metricsServerOptions,
WebhookServer: webhook.NewServer(webhookServerOptions),
HealthProbeBindAddress: cfg.HealthProbeBindAddress,
LeaderElection: cfg.LeaderElect,
LeaderElectionID: "abf369a8.flyte.org",
})
if err != nil {
return fmt.Errorf("executor: failed to create controller manager: %w", err)
}
sc.K8sCache = mgr.GetCache()
kubeClient, err := kubernetes.NewForConfig(sc.K8sConfig)
if err != nil {
return fmt.Errorf("executor: failed to create kubernetes client for webhook: %w", err)
}
podNamespace := os.Getenv(webhookPkg.PodNamespaceEnvVar)
if podNamespace == "" {
podNamespace = sc.Namespace
}
executorScope := promutils.NewScope("executor")
if err := webhookPkg.Setup(ctx, kubeClient, wCfg, podNamespace, executorScope.NewSubScope("webhook"), mgr); err != nil {
return fmt.Errorf("executor: webhook setup failed: %w", err)
}
dataStore, err := storage.NewDataStore(storage.GetConfig(), promutils.NewScope("executor:storage"))
if err != nil {
return fmt.Errorf("executor: failed to create data store: %w", err)
}
setupCtx := plugin.NewSetupContext(
mgr, nil, plugin.NewNoopResourceRegistrar(), nil, nil,
"TaskAction",
executorScope.NewSubScope("plugin"),
)
registry := plugin.NewRegistry(setupCtx, pluginmachinery.PluginRegistry())
if err := registry.Initialize(ctx); err != nil {
return fmt.Errorf("executor: failed to initialize plugin registry: %w", err)
}
otelCfg := otelutils.GetConfig()
if err := otelutils.RegisterProvidersWithContext(ctx, otelServiceName, otelCfg); err != nil {
return fmt.Errorf("registering otel providers: %w", err)
}
otelInterceptor, err := otelconnect.NewInterceptor(
otelconnect.WithTracerProvider(otelutils.GetTracerProvider(otelServiceName)),
otelconnect.WithMeterProvider(otelutils.GetMeterProvider(otelServiceName)),
otelconnect.WithoutServerPeerAttributes(),
)
if err != nil {
return fmt.Errorf("creating otel interceptor: %w", err)
}
eventsServiceURL := sc.BaseURL
if eventsServiceURL == "" {
eventsServiceURL = cfg.EventsServiceURL
}
eventsClient := workflowconnect.NewEventsProxyServiceClient(http.DefaultClient, eventsServiceURL, connect.WithInterceptors(otelInterceptor))
catalogCfg := catalog.GetConfig()
cacheServiceURL := sc.BaseURL
if cacheServiceURL == "" {
cacheServiceURL = cfg.CacheServiceURL
}
cacheClient := cachecatalog.NewHTTPClient(dataStore, cacheServiceURL, catalogCfg.MaxCacheAge.Duration, connect.WithInterceptors(otelInterceptor))
asyncCatalogClient, err := catalog.NewAsyncClient(cacheClient, *catalogCfg, promutils.NewScope("executor:catalog"))
if err != nil {
return fmt.Errorf("executor: failed to create catalog cache client: %w", err)
}
if err := asyncCatalogClient.Start(ctx); err != nil {
return fmt.Errorf("executor: failed to start catalog cache client: %w", err)
}
reconciler := controller.NewTaskActionReconciler(
mgr.GetClient(), mgr.GetScheme(), registry, dataStore, eventsClient, cfg.Cluster,
otelutils.GetMeterProvider(otelServiceName), mgr.GetCache(),
)
reconciler.CatalogClient = asyncCatalogClient
reconciler.Catalog = cacheClient
reconciler.Recorder = mgr.GetEventRecorder("taskaction-controller")
// Supply a ResourceManager for the webapi allocation-token path, used by connector-backed task
// types that declare ResourceQuotas. It grants every allocation by default, matching
// FlytePropeller with no quota backend. Swap in a real one to enforce quotas.
reconciler.ResourceManager = plugin.NewNoopResourceManager()
if cfg.MaxSystemFailures < 0 {
return fmt.Errorf("executor: maxSystemFailures must be non-negative, got %d", cfg.MaxSystemFailures)
}
reconciler.MaxSystemFailures = uint32(cfg.MaxSystemFailures)
if err := reconciler.SetupWithManager(mgr); err != nil {
return fmt.Errorf("executor: failed to setup controller: %w", err)
}
if cfg.GC.Interval.Duration > 0 {
if cfg.GC.MaxTTL.Duration <= 0 {
return fmt.Errorf("executor: gc.maxTTL must be positive when gc is enabled, got %v", cfg.GC.MaxTTL.Duration)
}
gc := controller.NewGarbageCollector(mgr.GetClient(), cfg.GC.Interval.Duration, cfg.GC.MaxTTL.Duration)
if err := mgr.Add(gc); err != nil {
return fmt.Errorf("executor: failed to add garbage collector: %w", err)
}
}
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
return fmt.Errorf("executor: failed to add health check: %w", err)
}
if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil {
return fmt.Errorf("executor: failed to add ready check: %w", err)
}
sc.AddWorker("executor", func(ctx context.Context) error {
return mgr.Start(ctx)
})
return nil
}