-
Notifications
You must be signed in to change notification settings - Fork 136
/
application.go
298 lines (269 loc) · 11.6 KB
/
application.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
package executor
import (
"fmt"
"net/http"
"os"
"strings"
"sync"
"time"
"github.com/go-playground/validator/v10"
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
"github.com/prometheus/client_golang/prometheus"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
"google.golang.org/grpc/keepalive"
"k8s.io/apimachinery/pkg/util/clock"
"github.com/armadaproject/armada/internal/common/armadacontext"
"github.com/armadaproject/armada/internal/common/cluster"
"github.com/armadaproject/armada/internal/common/etcdhealth"
"github.com/armadaproject/armada/internal/common/healthmonitor"
common_metrics "github.com/armadaproject/armada/internal/common/metrics"
"github.com/armadaproject/armada/internal/common/task"
"github.com/armadaproject/armada/internal/common/util"
"github.com/armadaproject/armada/internal/executor/configuration"
executor_context "github.com/armadaproject/armada/internal/executor/context"
"github.com/armadaproject/armada/internal/executor/job"
"github.com/armadaproject/armada/internal/executor/job/processors"
"github.com/armadaproject/armada/internal/executor/metrics"
"github.com/armadaproject/armada/internal/executor/metrics/pod_metrics"
"github.com/armadaproject/armada/internal/executor/metrics/runstate"
"github.com/armadaproject/armada/internal/executor/node"
"github.com/armadaproject/armada/internal/executor/podchecks"
"github.com/armadaproject/armada/internal/executor/reporter"
"github.com/armadaproject/armada/internal/executor/service"
"github.com/armadaproject/armada/internal/executor/utilisation"
"github.com/armadaproject/armada/pkg/client"
"github.com/armadaproject/armada/pkg/executorapi"
)
func StartUp(ctx *armadacontext.Context, log *logrus.Entry, config configuration.ExecutorConfiguration) (func(), *sync.WaitGroup) {
err := validateConfig(config)
if err != nil {
log.Errorf("Invalid config: %s", err)
os.Exit(-1)
}
kubernetesClientProvider, err := cluster.NewKubernetesClientProvider(
config.Kubernetes.ImpersonateUsers,
config.Kubernetes.QPS,
config.Kubernetes.Burst,
)
if err != nil {
log.Errorf("Failed to connect to kubernetes because %s", err)
os.Exit(-1)
}
// Create an errgroup to run services in.
g, ctx := armadacontext.ErrGroup(ctx)
// Setup etcd health monitoring.
etcdClusterHealthMonitoringByName := make(map[string]healthmonitor.HealthMonitor, len(config.Kubernetes.Etcd.EtcdClustersHealthMonitoring))
for _, etcdClusterHealthMonitoring := range config.Kubernetes.Etcd.EtcdClustersHealthMonitoring {
etcdReplicaHealthMonitorsByUrl := make(map[string]healthmonitor.HealthMonitor, len(etcdClusterHealthMonitoring.MetricUrls))
for _, metricsUrl := range etcdClusterHealthMonitoring.MetricUrls {
etcdReplicaHealthMonitorsByUrl[metricsUrl] = etcdhealth.NewEtcdReplicaHealthMonitor(
metricsUrl,
etcdClusterHealthMonitoring.FractionOfStorageInUseLimit,
etcdClusterHealthMonitoring.FractionOfStorageLimit,
etcdClusterHealthMonitoring.ReplicaTimeout,
etcdClusterHealthMonitoring.ScrapeInterval,
etcdClusterHealthMonitoring.ScrapeDelayBucketsStart,
etcdClusterHealthMonitoring.ScrapeDelayBucketsFactor,
etcdClusterHealthMonitoring.ScrapeDelayBucketsCount,
common_metrics.NewHttpMetricsProvider(metricsUrl, http.DefaultClient),
).WithMetricsPrefix(metrics.ArmadaExecutorMetricsPrefix)
}
etcdClusterHealthMonitoringByName[etcdClusterHealthMonitoring.Name] = healthmonitor.NewMultiHealthMonitor(
etcdClusterHealthMonitoring.Name,
etcdReplicaHealthMonitorsByUrl,
).WithMinimumReplicasAvailable(
etcdClusterHealthMonitoring.MinimumReplicasAvailable,
).WithMetricsPrefix(
metrics.ArmadaExecutorMetricsPrefix,
)
}
var etcdClustersHealthMonitoring healthmonitor.HealthMonitor
if len(etcdClusterHealthMonitoringByName) > 0 {
log.Info("etcd URLs provided; monitoring etcd health enabled")
etcdClustersHealthMonitoring = healthmonitor.NewMultiHealthMonitor(
"overall_etcd",
etcdClusterHealthMonitoringByName,
).WithMetricsPrefix(
metrics.ArmadaExecutorMetricsPrefix,
)
g.Go(func() error { return etcdClustersHealthMonitoring.Run(ctx, log) })
prometheus.MustRegister(etcdClustersHealthMonitoring)
} else {
log.Info("no etcd URLs provided; etcd health isn't monitored")
}
clusterContext := executor_context.NewClusterContext(
config.Application,
2*time.Minute,
kubernetesClientProvider,
config.Kubernetes.PodKillTimeout,
)
wg := &sync.WaitGroup{}
wg.Add(1)
taskManager := task.NewBackgroundTaskManager(metrics.ArmadaExecutorMetricsPrefix)
taskManager.Register(clusterContext.ProcessPodsToDelete, config.Task.PodDeletionInterval, "pod_deletion")
return StartUpWithContext(log, config, clusterContext, etcdClustersHealthMonitoring, taskManager, wg)
}
func StartUpWithContext(
log *logrus.Entry,
config configuration.ExecutorConfiguration,
clusterContext executor_context.ClusterContext,
clusterHealthMonitor healthmonitor.HealthMonitor,
taskManager *task.BackgroundTaskManager,
wg *sync.WaitGroup,
) (func(), *sync.WaitGroup) {
nodeInfoService := node.NewKubernetesNodeInfoService(clusterContext, config.Kubernetes.ToleratedTaints)
podUtilisationService := utilisation.NewPodUtilisationService(
clusterContext,
nodeInfoService,
config.Metric.CustomUsageMetrics,
&http.Client{Timeout: 15 * time.Second},
)
if config.Kubernetes.PendingPodChecks == nil {
log.Error("Config error: Missing pending pod checks")
os.Exit(-1)
}
pendingPodChecker, err := podchecks.NewPodChecks(*config.Kubernetes.PendingPodChecks)
if err != nil {
log.Errorf("Config error in pending pod checks: %s", err)
os.Exit(-1)
}
stopExecutorApiComponents := setupExecutorApiComponents(log, config, clusterContext, clusterHealthMonitor, taskManager, pendingPodChecker, nodeInfoService, podUtilisationService)
resourceCleanupService := service.NewResourceCleanupService(clusterContext, config.Kubernetes)
taskManager.Register(resourceCleanupService.CleanupResources, config.Task.ResourceCleanupInterval, "resource_cleanup")
if config.Metric.ExposeQueueUsageMetrics {
taskManager.Register(podUtilisationService.RefreshUtilisationData, config.Task.QueueUsageDataRefreshInterval, "pod_usage_data_refresh")
}
return func() {
clusterContext.Stop()
stopExecutorApiComponents()
if taskManager.StopAll(10 * time.Second) {
log.Warnf("Graceful shutdown timed out")
}
log.Infof("Shutdown complete")
wg.Done()
}, wg
}
func setupExecutorApiComponents(
log *logrus.Entry,
config configuration.ExecutorConfiguration,
clusterContext executor_context.ClusterContext,
clusterHealthMonitor healthmonitor.HealthMonitor,
taskManager *task.BackgroundTaskManager,
pendingPodChecker *podchecks.PodChecks,
nodeInfoService node.NodeInfoService,
podUtilisationService utilisation.PodUtilisationService,
) func() {
conn, err := createConnectionToApi(config.ExecutorApiConnection, config.Client.MaxMessageSizeBytes, config.GRPC)
if err != nil {
log.Errorf("Failed to connect to Executor API because: %s", err)
os.Exit(-1)
}
executorApiClient := executorapi.NewExecutorApiClient(conn)
eventSender := reporter.NewExecutorApiEventSender(executorApiClient, 4*1024*1024)
jobRunState := job.NewJobRunStateStore(clusterContext)
clusterUtilisationService := utilisation.NewClusterUtilisationService(
clusterContext,
podUtilisationService,
nodeInfoService,
config.Kubernetes.TrackedNodeLabels,
config.Kubernetes.NodeIdLabel,
config.Kubernetes.MinimumResourcesMarkedAllocatedToNonArmadaPodsPerNode,
config.Kubernetes.MinimumResourcesMarkedAllocatedToNonArmadaPodsPerNodePriority,
)
eventReporter, stopReporter := reporter.NewJobEventReporter(
clusterContext,
jobRunState,
eventSender,
clock.RealClock{},
200)
submitter := job.NewSubmitter(
clusterContext,
config.Kubernetes.PodDefaults,
config.Application.SubmitConcurrencyLimit,
config.Kubernetes.FatalPodSubmissionErrors,
)
leaseRequester := service.NewJobLeaseRequester(
executorApiClient, clusterContext, config.Kubernetes.MinimumJobSize)
preemptRunProcessor := processors.NewRunPreemptedProcessor(clusterContext, jobRunState, eventReporter)
removeRunProcessor := processors.NewRemoveRunProcessor(clusterContext, jobRunState)
jobRequester := service.NewJobRequester(
clusterContext,
eventReporter,
leaseRequester,
jobRunState,
clusterUtilisationService,
config.Kubernetes.PodDefaults,
config.Application.MaxLeasedJobs,
)
clusterAllocationService := service.NewClusterAllocationService(
clusterContext,
eventReporter,
jobRunState,
submitter,
clusterHealthMonitor,
)
podIssueService := service.NewIssueHandler(
jobRunState,
clusterContext,
eventReporter,
config.Kubernetes.StateChecks,
pendingPodChecker,
config.Kubernetes.StuckTerminatingPodExpiry,
)
taskManager.Register(podIssueService.HandlePodIssues, config.Task.PodIssueHandlingInterval, "pod_issue_handling")
taskManager.Register(preemptRunProcessor.Run, config.Task.StateProcessorInterval, "preempt_runs")
taskManager.Register(removeRunProcessor.Run, config.Task.StateProcessorInterval, "remove_runs")
taskManager.Register(jobRequester.RequestJobsRuns, config.Task.AllocateSpareClusterCapacityInterval, "request_runs")
taskManager.Register(clusterAllocationService.AllocateSpareClusterCapacity, config.Task.AllocateSpareClusterCapacityInterval, "submit_runs")
taskManager.Register(eventReporter.ReportMissingJobEvents, config.Task.MissingJobEventReconciliationInterval, "event_reconciliation")
pod_metrics.ExposeClusterContextMetrics(clusterContext, clusterUtilisationService, podUtilisationService, nodeInfoService)
runStateMetricsCollector := runstate.NewJobRunStateStoreMetricsCollector(jobRunState)
prometheus.MustRegister(runStateMetricsCollector)
if config.Metric.ExposeQueueUsageMetrics && config.Task.UtilisationEventReportingInterval > 0 {
podUtilisationReporter := utilisation.NewUtilisationEventReporter(
clusterContext,
podUtilisationService,
eventReporter,
config.Task.UtilisationEventReportingInterval)
taskManager.Register(
podUtilisationReporter.ReportUtilisationEvents,
config.Task.UtilisationEventProcessingInterval,
"pod_utilisation_event_reporting",
)
}
return func() {
stopReporter <- true
conn.Close()
}
}
func createConnectionToApi(connectionDetails client.ApiConnectionDetails, maxMessageSizeBytes int, grpcConfig keepalive.ClientParameters) (*grpc.ClientConn, error) {
grpc_prometheus.EnableClientHandlingTimeHistogram()
return client.CreateApiConnectionWithCallOptions(
&connectionDetails,
[]grpc.CallOption{grpc.MaxCallRecvMsgSize(maxMessageSizeBytes)},
grpc.WithChainUnaryInterceptor(grpc_prometheus.UnaryClientInterceptor),
grpc.WithChainStreamInterceptor(grpc_prometheus.StreamClientInterceptor),
grpc.WithKeepaliveParams(grpcConfig),
)
}
func validateConfig(config configuration.ExecutorConfiguration) error {
validator := validator.New()
if err := validator.Struct(config); err != nil {
return err
}
missing := util.SubtractStringList(config.Kubernetes.AvoidNodeLabelsOnRetry, config.Kubernetes.TrackedNodeLabels)
if len(missing) > 0 {
return fmt.Errorf("These labels were in avoidNodeLabelsOnRetry but not trackedNodeLabels: %s", strings.Join(missing, ", "))
}
if config.Application.SubmitConcurrencyLimit <= 0 {
return fmt.Errorf("SubmitConcurrencyLimit was %d, must be greater or equal to 1", config.Application.SubmitConcurrencyLimit)
}
if config.Application.UpdateConcurrencyLimit <= 0 {
return fmt.Errorf("UpdateConcurrencyLimit was %d, must be greater or equal to 1", config.Application.UpdateConcurrencyLimit)
}
if config.Application.DeleteConcurrencyLimit <= 0 {
return fmt.Errorf("DeleteConcurrencyLimit was %d, must be greater or equal to 1", config.Application.DeleteConcurrencyLimit)
}
return nil
}