-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipeline.go
379 lines (320 loc) · 13.9 KB
/
pipeline.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
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
package types
import (
"encoding/json"
"net/http"
"slices"
"sort"
"strings"
"time"
fluentbitconfig "github.com/calyptia/go-fluentbit-config/v2"
)
const DefaultHealthCheckPipelinePort = 2020
type DeploymentStrategy string
const (
DeploymentStrategyRecreate DeploymentStrategy = "recreate"
DeploymentStrategyHotReload DeploymentStrategy = "hotReload"
)
const (
SectionKindServiceOrdinal int = iota + 1
SectionKindInputOrdinal
SectionKindFilterOrdinal
SectionKindOutputOrdinal
)
var (
DefaultDeploymentStrategy = DeploymentStrategyRecreate
AllValidDeploymentStrategies = [...]DeploymentStrategy{
DeploymentStrategyRecreate,
DeploymentStrategyHotReload,
}
)
// Pipeline model.
type Pipeline struct {
ID string `json:"id" yaml:"id"`
Name string `json:"name" yaml:"name"`
Kind PipelineKind `json:"kind" yaml:"kind"`
Config PipelineConfig `json:"config" yaml:"config"`
ConfigSections []ConfigSection `json:"configSections" yaml:"configSections"`
Image *string `json:"image" yaml:"image"`
Status PipelineStatus `json:"status" yaml:"status"`
ResourceProfile ResourceProfile `json:"resourceProfile" yaml:"resourceProfile"`
DeploymentStrategy DeploymentStrategy `json:"deploymentStrategy,omitempty" yaml:"deploymentStrategy,omitempty"`
TracingEnabled bool `json:"tracingEnabled" yaml:"tracingEnabled"`
WaitForChecksBeforeDeploying bool `json:"waitForChecksBeforeDeploying" yaml:"waitForChecksBeforeDeploying"`
ReplicasCount uint `json:"replicasCount" yaml:"replicasCount"`
ReplicasCountPrev uint `json:"replicasCountPrev" yaml:"replicasCountPrev"`
Tags []string `json:"tags" yaml:"tags"`
Metadata *json.RawMessage `json:"metadata" yaml:"metadata"`
ChecksTotal uint `json:"checksTotal" yaml:"checksTotal"`
ChecksOK uint `json:"checksOK" yaml:"checksOK"`
ChecksRunning uint `json:"checksRunning" yaml:"checksRunning"`
CreatedAt time.Time `json:"createdAt" yaml:"createdAt"`
UpdatedAt time.Time `json:"updatedAt" yaml:"updatedAt"`
Ports []PipelinePort `json:"ports,omitempty" yaml:"ports,omitempty"`
Files []PipelineFile `json:"files,omitempty" yaml:"files,omitempty"`
Secrets []PipelineSecret `json:"secrets,omitempty" yaml:"secrets,omitempty"`
}
func (p *Pipeline) sortSections() {
// Have to name these constants or the lint job won't pass
sectionKindOrdering := map[ConfigSectionKind]int{
SectionKindService: SectionKindServiceOrdinal,
SectionKindInput: SectionKindInputOrdinal,
SectionKindFilter: SectionKindFilterOrdinal,
SectionKindOutput: SectionKindOutputOrdinal,
}
sort.SliceStable(p.ConfigSections, func(i, j int) bool {
iSection := p.ConfigSections[i]
jSection := p.ConfigSections[j]
if sectionKindOrdering[iSection.Kind] < sectionKindOrdering[jSection.Kind] {
// Put filters next to each other in the section slice following the
// ordering defined in "sectionKindOrdering"
return true
}
// this code can only be reached if "i" and "j" are of the same kind, don't
// reorder if they are not filters
if iSection.Kind != SectionKindFilter {
return false
}
// If the filter name is "kubernetes", then it should be less
return iSection.Name() == "kubernetes"
})
}
func (p *Pipeline) ApplyConfigSections() error {
if len(p.ConfigSections) == 0 {
return nil
}
format := fluentbitconfig.Format(p.Config.ConfigFormat)
c, err := fluentbitconfig.ParseAs(p.Config.RawConfig, format)
if err != nil {
return err
}
p.sortSections()
for _, section := range p.ConfigSections {
c.AddSection(fluentbitconfig.SectionKind(section.Kind), section.Properties.AsProperties())
}
raw, err := c.DumpAs(format)
if err != nil {
return err
}
p.Config.RawConfig = raw
p.Status.Config.RawConfig = raw
return nil
}
type PipelineKind string
const (
PipelineKindDaemonSet PipelineKind = "daemonSet"
PipelineKindDeployment PipelineKind = "deployment"
)
// AllPipelineKindTypes all valid pipeline kinds.
var AllPipelineKindTypes = [...]PipelineKind{
PipelineKindDaemonSet,
PipelineKindDeployment,
}
// CreatePipeline request payload for creating a new pipeline.
type CreatePipeline struct {
Name string `json:"name"`
Kind PipelineKind `json:"kind"`
ReplicasCount uint `json:"replicasCount"`
RawConfig string `json:"rawConfig"`
ConfigFormat ConfigFormat `json:"configFormat"`
DeploymentStrategy DeploymentStrategy `json:"deploymentStrategy"`
Secrets []CreatePipelineSecret `json:"secrets"`
Files []CreatePipelineFile `json:"files"`
ResourceProfileName string `json:"resourceProfile"`
Image *string `json:"image"`
SkipConfigValidation bool `json:"skipConfigValidation"`
Metadata *json.RawMessage `json:"metadata"`
Tags []string `json:"tags"`
// Deprecated: in favor of NoAutoCreateEndpointsFromConfig
AutoCreatePortsFromConfig bool `json:"autoCreatePortsFromConfig"`
// Deprecated: in favor of NoAutoCreateChecksFromConfig
AutoCreateChecksFromConfig bool `json:"autoCreateChecksFromConfig"`
// no automatically create endpoints from config
NoAutoCreateEndpointsFromConfig bool `json:"noAutoCreateEndpointsFromConfig"`
// no automatically create checks based on the output configuration.
NoAutoCreateChecksFromConfig bool `json:"noAutoCreateChecksFromConfig"`
// WaitForChecksBeforeDeploying is a conditional variable that defines behavior on the
// pipeline deployment
//
// If set to true:
//
// If all checks associated with the pipeline run successfully, the status of
// the pipeline will be switched to CHECKS_OK and the deployment will be executed.
//
// If any of the checks associated with the pipeline fails, the status of
// the pipeline will be switched to CHECKS_FAILED and the deployment of the pipeline
// will be blocked.
//
// If set to false (default):
//
// If all checks associated with the pipeline run successfully, the status of
// the pipeline will be switched to CHECKS_OK and the deployment will be executed.
//
// If any of the checks associated with the pipeline fails, the status of
// the pipeline will be switched to CHECKS_FAILED and the deployment of the pipeline
// will be executed.
WaitForChecksBeforeDeploying bool `json:"waitForChecksBeforeDeploying"`
// The default portKind to be used for input ports that belongs to this pipeline.
PortKind PipelinePortKind `json:"portKind"`
status PipelineStatusKind
// Internal denotes that this pipeline was created by the system.
// That's the case for the "health-check-*" automated pipeline
// with each new core instance.
// We use it to not take these into account for project quotas.
internal bool
// ClusterLogging denotes that this pipeline is internal.
// There should be only one cluster-logging pipeline in the system.
clusterLogging bool
}
func (in *CreatePipeline) SetStatus(status PipelineStatusKind) {
in.status = status
}
func (in *CreatePipeline) SetInternal(internal bool) {
in.internal = internal
}
func (in *CreatePipeline) SetClusterLogging(clusterLogging bool) {
in.clusterLogging = clusterLogging
}
func (in CreatePipeline) Status() PipelineStatusKind {
return in.status
}
func (in CreatePipeline) Internal() bool {
return in.internal
}
func (in CreatePipeline) ClusterLogging() bool {
return in.clusterLogging
}
// CreatedPipeline response payload after creating a pipeline successfully.
type CreatedPipeline struct {
ID string `json:"id"`
Name string `json:"name"`
Kind PipelineKind `json:"kind"`
Config PipelineConfig `json:"config"`
DeploymentStrategy DeploymentStrategy `json:"deploymentStrategy"`
Secrets []PipelineSecret `json:"secrets"`
Files []PipelineFile `json:"files"`
Status PipelineStatus `json:"status"`
ResourceProfile ResourceProfile `json:"resourceProfile"`
Checks []PipelineCheck `json:"checks"`
ReplicasCount uint `json:"replicasCount"`
WaitForChecksBeforeDeploying bool `json:"waitForChecksBeforeDeploying"`
CreatedAt time.Time `json:"createdAt"`
}
// PipelinesParams represents the request payload for querying pipelines.
type PipelinesParams struct {
ProjectID *string
CoreInstanceID *string
Last *uint
Before *string
Kind *PipelineKind
Name *string
TagsQuery *string
ConfigFormat *ConfigFormat
IncludeObjects *PipelineObjectsParams
RenderWithConfigSections bool
}
func (p PipelinesParams) Tags() *[]string {
if p.TagsQuery == nil {
return nil
}
tags := strings.Split(*p.TagsQuery, " AND ")
for i, tag := range tags {
tags[i] = strings.TrimSpace(tag)
}
return &tags
}
// PipelineObjectsParams represents the options for including different types of pipeline objects in the response.
type PipelineObjectsParams struct {
Files bool // include files in the response.
Secrets bool // include secrets in the response.
Ports bool // include ports in the response.
}
// NewPipelineObjectsParams creates and returns a new PipelineObjectsParams object based on the "include" parameter in the given request.
func NewPipelineObjectsParams(r *http.Request) *PipelineObjectsParams {
include := strings.Split(r.URL.Query().Get("include"), ",")
return &PipelineObjectsParams{
Files: slices.Contains(include, "files"),
Secrets: slices.Contains(include, "secrets"),
Ports: slices.Contains(include, "ports"),
}
}
// Pipelines paginated list.
type Pipelines struct {
Items []Pipeline `json:"items"`
EndCursor *string `json:"endCursor"`
Count int `json:"count"`
}
// UpdatePipeline request payload for updating a pipeline.
type UpdatePipeline struct {
Name *string `json:"name"`
Kind *PipelineKind `json:"kind"`
Status *PipelineStatusKind `json:"status"`
ConfigFormat *ConfigFormat `json:"configFormat"`
DeploymentStrategy *DeploymentStrategy `json:"deploymentStrategy"`
PortKind *PipelinePortKind `json:"portKind"`
ReplicasCount *uint `json:"replicasCount"`
RawConfig *string `json:"rawConfig"`
ResourceProfile *string `json:"resourceProfile"`
Image *string `json:"image"`
// Deprecated: in favor of NoAutoCreateEndpointsFromConfig
AutoCreatePortsFromConfig *bool `json:"autoCreatePortsFromConfig"`
// Deprecated: in favor of NoAutoCreateChecksFromConfig
AutoCreateChecksFromConfig *bool `json:"autoCreateChecksFromConfig"`
// no automatically create endpoints from config
NoAutoCreateEndpointsFromConfig bool `json:"noAutoCreateEndpointsFromConfig"`
// no automatically create checks based on the output configuration.
NoAutoCreateChecksFromConfig bool `json:"noAutoCreateChecksFromConfig"`
// this defines behavior; await for checks to complete before reporting the status back.
WaitForChecksBeforeDeploying *bool `json:"waitForChecksBeforeDeploying"`
SkipConfigValidation bool `json:"skipConfigValidation"`
Metadata *json.RawMessage `json:"metadata"`
Secrets []UpdatePipelineSecret `json:"secrets"`
Files []UpdatePipelineFile `json:"files"`
Events []PipelineEvent `json:"events"`
clusterLogging *bool
resourceProfileID *string
}
func (in *UpdatePipeline) SetClusterLogging(clusterLogging bool) {
in.clusterLogging = &clusterLogging
}
func (in *UpdatePipeline) SetResourceProfileID(resourceProfileID string) {
in.resourceProfileID = &resourceProfileID
}
func (in UpdatePipeline) ClusterLogging() *bool {
return in.clusterLogging
}
func (in UpdatePipeline) ResourceProfileID() *string {
return in.resourceProfileID
}
// PipelineParams request payload for querying a single pipeline.
type PipelineParams struct {
ConfigFormat *ConfigFormat
RenderWithConfigSections bool
}
// UpdatedPipeline response payload after updating a pipeline successfully.
type UpdatedPipeline struct {
AddedPorts []PipelinePort `json:"addedPorts"`
RemovedPorts []PipelinePort `json:"removedPorts"`
// Pipeline checks that have been created/updated based on AutoCreatePreChecksFromConfig changes.
AddedChecks []PipelineCheck `json:"addedChecks"`
RemovedChecks []PipelineCheck `json:"removedChecks"`
}
// UpdatePipelineClusterObjects update cluster objects associated to a pipeline.
type UpdatePipelineClusterObjects struct {
ClusterObjectsIDs []string `json:"clusterObjectsIDs"`
}
// PipelineClusterObjectsParams request payload to filter cluster objects belonging to a pipeline.
type PipelineClusterObjectsParams struct {
Last *uint
Before *string
}
// PipelineMetadata is the default metadata format for a pipeline.
type PipelineMetadata map[string]any
// UpdatePipelineMetadata request payload to store a key on the metadata field with the given value (json serializable).
type UpdatePipelineMetadata struct {
Key *string `json:"key"`
Value *json.RawMessage `json:"value"`
}
// PipelineMetadataParams request payload for listing metadata from keys.
type PipelineMetadataParams struct {
Keys *[]string
}