-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvalidate.go
More file actions
711 lines (582 loc) · 22.6 KB
/
validate.go
File metadata and controls
711 lines (582 loc) · 22.6 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
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
package apiv1
import (
"context"
"errors"
"fmt"
"path/filepath"
"regexp"
"slices"
"strings"
api "github.com/pgEdge/control-plane/api/apiv1/gen/control_plane"
"github.com/pgEdge/control-plane/server/internal/database"
"github.com/pgEdge/control-plane/server/internal/ds"
"github.com/pgEdge/control-plane/server/internal/host"
"github.com/pgEdge/control-plane/server/internal/pgbackrest"
"github.com/pgEdge/control-plane/server/internal/storage"
"github.com/pgEdge/control-plane/server/internal/utils"
)
type validationError struct {
path []string
err error
}
func newValidationError(err error, path []string) *validationError {
return &validationError{
path: path,
err: err,
}
}
func (v *validationError) Unwrap() error {
return v.err
}
func (v *validationError) Error() string {
if len(v.path) == 0 {
return v.err.Error()
}
var path strings.Builder
for i, ele := range v.path {
if i > 0 && !strings.HasPrefix(ele, "[") {
path.WriteString(".")
}
path.WriteString(ele)
}
return fmt.Sprintf("%s: %s", path.String(), v.err.Error())
}
func arrayIndexPath(idx int) string {
return fmt.Sprintf("[%d]", idx)
}
func mapKeyPath(key string) string {
return fmt.Sprintf("[%s]", key)
}
func appendPath(path []string, new ...string) []string {
return append(slices.Clone(path), new...)
}
func validateDatabaseSpec(spec *api.DatabaseSpec) error {
var errs []error
errs = append(errs, validateCPUs(spec.Cpus, []string{"cpus"})...)
errs = append(errs, validateMemory(spec.Memory, []string{"memory"})...)
errs = append(errs, validatePorts(spec.Port, spec.PatroniPort, []string{"port"}))
errs = append(errs, validateUsers(spec.DatabaseUsers, []string{"database_users"})...)
// Track node-name uniqueness and prepare set for cross-node checks.
seenNodeNames := make(ds.Set[string], len(spec.Nodes))
// Track nodes that themselves have a source_node (treated as "new" nodes).
newNodesWithSource := make(ds.Set[string], len(spec.Nodes))
for i, node := range spec.Nodes {
nodePath := []string{"nodes", arrayIndexPath(i)}
if seenNodeNames.Has(node.Name) {
err := errors.New("node names must be unique within a database")
errs = append(errs, newValidationError(err, nodePath))
}
seenNodeNames.Add(node.Name)
// Mark nodes that declare a source_node as "new" nodes.
if utils.FromPointer(node.SourceNode) != "" {
newNodesWithSource.Add(node.Name)
}
// Per-node validation (includes self-ref and restore vs source_node conflict)
errs = append(errs, validateNode(node, nodePath)...)
}
// Cross-node existence check for source_node
for i, node := range spec.Nodes {
src := utils.FromPointer(node.SourceNode)
if src == "" {
continue
}
srcPath := []string{"nodes", arrayIndexPath(i), "source_node"}
if !seenNodeNames.Has(src) {
// Attach error to the specific field path
errs = append(errs, newValidationError(errors.New("source node does not exist"),
srcPath))
continue
}
// prevent using a "new" node (one that has its own source_node)
// as the source for another node.
if newNodesWithSource.Has(src) {
errs = append(errs, newValidationError(
errors.New("source node must refer to an existing node"),
srcPath,
))
}
}
if spec.BackupConfig != nil {
errs = append(errs, validateBackupConfig(spec.BackupConfig, []string{"backup_config"})...)
}
if spec.RestoreConfig != nil {
errs = append(errs, validateRestoreConfig(spec.RestoreConfig, []string{"restore_config"})...)
}
// Validate orchestrator_opts (spec-level)
errs = append(errs, validateOrchestratorOpts(spec.OrchestratorOpts, []string{"orchestrator_opts"})...)
// Validate services
seenServiceIDs := make(ds.Set[string], len(spec.Services))
for i, svc := range spec.Services {
svcPath := []string{"services", arrayIndexPath(i)}
// Check for duplicate service IDs
if seenServiceIDs.Has(string(svc.ServiceID)) {
err := errors.New("service IDs must be unique within a database")
errs = append(errs, newValidationError(err, svcPath))
}
seenServiceIDs.Add(string(svc.ServiceID))
errs = append(errs, validateServiceSpec(svc, svcPath, false, seenNodeNames)...)
}
return errors.Join(errs...)
}
func validateDatabaseUpdate(old *database.Spec, new *api.DatabaseSpec) error {
var errs []error
// Collect names of nodes that already exist in the old spec.
existingNodeNames := make(ds.Set[string], len(old.Nodes))
for _, n := range old.Nodes {
existingNodeNames.Add(n.Name)
}
// For each newly added node, ensure its source_node (if any) refers to an existing node.
for i, n := range new.Nodes {
// Only care about newly added nodes (those NOT in existingNodeNames).
if existingNodeNames.Has(n.Name) {
continue
}
src := utils.FromPointer(n.SourceNode)
if src == "" {
continue // no explicit source_node; auto-selector will handle this later
}
if !existingNodeNames.Has(src) {
// Newly added node is trying to use a new/non-existing node as source.
path := []string{"nodes", arrayIndexPath(i), "source_node"}
errs = append(errs, newValidationError(
errors.New("source node must refer to an existing node"),
path,
))
}
}
// Build the full set of node names from the new spec for cross-validation.
newNodeNames := make(ds.Set[string], len(new.Nodes))
for _, n := range new.Nodes {
newNodeNames.Add(n.Name)
}
// Build a set of service IDs that already exist in the deployment. This is used
// below to distinguish newly added services from existing ones. Currently this
// distinction only affects MCP services, which have bootstrap-only fields
// (init_token, init_users) that may only be set during initial provisioning of
// the service. Because a service can be added to an existing database via
// update-database, "initial provisioning" means "first time this service_id
// appears in the spec" — not "the create-database call was used".
existingServiceIDs := make(ds.Set[string], len(old.Services))
for _, svc := range old.Services {
existingServiceIDs.Add(svc.ServiceID)
}
// Validate each service. Pass isUpdate=false for services being added for the
// first time so that bootstrap-only fields are accepted. For service types that
// have no bootstrap fields (e.g. postgrest) the flag has no effect.
for i, svc := range new.Services {
svcPath := []string{"services", arrayIndexPath(i)}
isExistingService := existingServiceIDs.Has(string(svc.ServiceID))
errs = append(errs, validateServiceSpec(svc, svcPath, isExistingService, newNodeNames)...)
}
return errors.Join(errs...)
}
func validateNode(node *api.DatabaseNodeSpec, path []string) []error {
var errs []error
cpusPath := appendPath(path, "cpus")
errs = append(errs, validateCPUs(node.Cpus, cpusPath)...)
memPath := appendPath(path, "memory")
errs = append(errs, validateMemory(node.Memory, memPath)...)
portPath := appendPath(path, "port")
errs = append(errs, validatePorts(node.Port, node.PatroniPort, portPath))
seenHostIDs := make(ds.Set[string], len(node.HostIds))
for i, h := range node.HostIds {
hostID := string(h)
hostPath := appendPath(path, "host_ids", arrayIndexPath(i))
errs = append(errs, validateIdentifier(hostID, hostPath))
if seenHostIDs.Has(hostID) {
err := errors.New("host IDs must be unique within a node")
errs = append(errs, newValidationError(err, hostPath))
}
seenHostIDs.Add(hostID)
}
// source_node + restore_config validation (field-level)
src := utils.FromPointer(node.SourceNode)
srcPath := appendPath(path, "source_node")
// If restore_config is provided, source_node must be empty
if node.RestoreConfig != nil && src != "" {
errs = append(errs, newValidationError(errors.New("specify either source_node or restore_config"), srcPath))
} else if src != "" {
// Self-reference is invalid
if src == node.Name {
errs = append(errs, newValidationError(errors.New("a node cannot use itself as a source node"), srcPath))
}
}
if node.BackupConfig != nil {
backupConfigPath := appendPath(path, "backup_config")
errs = append(errs, validateBackupConfig(node.BackupConfig, backupConfigPath)...)
}
if node.RestoreConfig != nil {
restoreConfigPath := appendPath(path, "restore_config")
errs = append(errs, validateRestoreConfig(node.RestoreConfig, restoreConfigPath)...)
}
// Validate orchestrator_opts (per-node)
errs = append(errs, validateOrchestratorOpts(node.OrchestratorOpts, appendPath(path, "orchestrator_opts"))...)
return errs
}
func validateServiceSpec(svc *api.ServiceSpec, path []string, isUpdate bool, nodeNames ...ds.Set[string]) []error {
var errs []error
// Validate service_id
serviceIDPath := appendPath(path, "service_id")
errs = append(errs, validateIdentifier(string(svc.ServiceID), serviceIDPath))
// Validate service_type allowlist
supportedServiceTypes := []string{"mcp", "postgrest", "rag"}
if !slices.Contains(supportedServiceTypes, svc.ServiceType) {
err := fmt.Errorf("unsupported service type %q (supported: %s)",
svc.ServiceType, strings.Join(supportedServiceTypes, ", "))
errs = append(errs, newValidationError(err, appendPath(path, "service_type")))
}
// Validate version (semver pattern or "latest")
if svc.Version != "latest" && !semverPattern.MatchString(svc.Version) {
err := errors.New("version must be in semver format (e.g., '1.0.0') or 'latest'")
errs = append(errs, newValidationError(err, appendPath(path, "version")))
}
// Validate host_ids (uniqueness and format)
seenHostIDs := make(ds.Set[string], len(svc.HostIds))
for i, hostID := range svc.HostIds {
hostIDStr := string(hostID)
hostIDPath := appendPath(path, "host_ids", arrayIndexPath(i))
errs = append(errs, validateIdentifier(hostIDStr, hostIDPath))
// may need to relax this if there is a use-case for multiple service instances on the same host
if seenHostIDs.Has(hostIDStr) {
err := errors.New("host IDs must be unique within a service")
errs = append(errs, newValidationError(err, hostIDPath))
}
seenHostIDs.Add(hostIDStr)
}
// Validate config based on service_type
switch svc.ServiceType {
case "mcp":
errs = append(errs, validateMCPServiceConfig(svc.Config, appendPath(path, "config"), isUpdate)...)
case "postgrest":
errs = append(errs, validatePostgRESTServiceConfig(svc.Config, appendPath(path, "config"))...)
case "rag":
errs = append(errs, validateRAGServiceConfig(svc.Config, appendPath(path, "config"), isUpdate)...)
}
// Validate database_connection if provided
if svc.DatabaseConnection != nil {
dcPath := appendPath(path, "database_connection")
var nn ds.Set[string]
if len(nodeNames) > 0 {
nn = nodeNames[0]
}
errs = append(errs, validateDatabaseConnection(svc.DatabaseConnection, dcPath, nn)...)
}
// MCP-specific cross-validation: allow_writes vs target_session_attrs
if svc.ServiceType == "mcp" && svc.DatabaseConnection != nil && svc.DatabaseConnection.TargetSessionAttrs != nil {
if allowWrites, ok := svc.Config["allow_writes"].(bool); ok && allowWrites {
tsa := *svc.DatabaseConnection.TargetSessionAttrs
writeSafe := map[string]bool{database.TargetSessionAttrsPrimary: true, database.TargetSessionAttrsReadWrite: true}
if tsa != "" && !writeSafe[tsa] {
err := fmt.Errorf("allow_writes requires target_session_attrs 'primary' or 'read-write', got '%s'", tsa)
errs = append(errs, newValidationError(err, appendPath(path, "database_connection", "target_session_attrs")))
}
}
}
// Validate cpus if provided
if svc.Cpus != nil {
errs = append(errs, validateCPUs(svc.Cpus, appendPath(path, "cpus"))...)
}
// Validate memory if provided
if svc.Memory != nil {
errs = append(errs, validateMemory(svc.Memory, appendPath(path, "memory"))...)
}
// Validate orchestrator_opts
errs = append(errs, validateOrchestratorOpts(svc.OrchestratorOpts, appendPath(path, "orchestrator_opts"))...)
return errs
}
func validateMCPServiceConfig(config map[string]any, path []string, isUpdate bool) []error {
_, errs := database.ParseMCPServiceConfig(config, isUpdate)
var result []error
for _, err := range errs {
result = append(result, newValidationError(err, path))
}
return result
}
func validatePostgRESTServiceConfig(config map[string]any, path []string) []error {
_, errs := database.ParsePostgRESTServiceConfig(config)
var result []error
for _, err := range errs {
result = append(result, newValidationError(err, path))
}
return result
}
func validateDatabaseConnection(dc *api.DatabaseConnection, path []string, nodeNames ds.Set[string]) []error {
var errs []error
// Validate target_nodes: no duplicates, no empty strings, must exist in spec
if dc.TargetNodes != nil {
seen := make(ds.Set[string], len(dc.TargetNodes))
for i, node := range dc.TargetNodes {
nodePath := appendPath(path, "target_nodes", arrayIndexPath(i))
if node == "" {
errs = append(errs, newValidationError(errors.New("node name must not be empty"), nodePath))
} else if nodeNames != nil && !nodeNames.Has(node) {
errs = append(errs, newValidationError(fmt.Errorf("node %q does not exist in the database spec", node), nodePath))
}
if seen.Has(node) {
errs = append(errs, newValidationError(fmt.Errorf("duplicate node name %q", node), nodePath))
}
seen.Add(node)
}
}
// Validate target_session_attrs enum (belt-and-suspenders — Goa also validates this)
if dc.TargetSessionAttrs != nil && *dc.TargetSessionAttrs != "" {
valid := map[string]bool{
database.TargetSessionAttrsPrimary: true,
database.TargetSessionAttrsPreferStandby: true,
database.TargetSessionAttrsStandby: true,
database.TargetSessionAttrsReadWrite: true,
database.TargetSessionAttrsAny: true,
}
if !valid[*dc.TargetSessionAttrs] {
err := fmt.Errorf("invalid target_session_attrs %q (must be primary, prefer-standby, standby, read-write, or any)", *dc.TargetSessionAttrs)
errs = append(errs, newValidationError(err, appendPath(path, "target_session_attrs")))
}
}
return errs
}
func validateRAGServiceConfig(config map[string]any, path []string, isUpdate bool) []error {
_, errs := database.ParseRAGServiceConfig(config, isUpdate)
var result []error
for _, err := range errs {
result = append(result, newValidationError(err, path))
}
return result
}
func validateCPUs(value *string, path []string) []error {
var errs []error
cpus, err := parseCPUs(value)
if err != nil {
errs = append(errs, newValidationError(err, path))
}
if cpus != 0 && cpus < 0.001 {
err := errors.New("cannot be less than 1 millicpu")
errs = append(errs, newValidationError(err, path))
}
return errs
}
func validateMemory(value *string, path []string) []error {
var errs []error
_, err := parseBytes(value)
if err != nil {
errs = append(errs, newValidationError(err, path))
}
return errs
}
func validatePorts(postgresPort, patroniPort *int, path []string) error {
postgres := utils.FromPointer(postgresPort)
patroni := utils.FromPointer(patroniPort)
if postgres > 0 && postgres == patroni {
return newValidationError(errors.New("postgres and patroni ports must not conflict"), path)
}
return nil
}
func validateUsers(users []*api.DatabaseUserSpec, path []string) []error {
var errs []error
seenNames := ds.NewSet[string]()
var hasOwner bool
for i, user := range users {
userPath := appendPath(path, arrayIndexPath(i))
if seenNames.Has(user.Username) {
err := errors.New("usernames must be unique within a database")
errs = append(errs, newValidationError(err, userPath))
}
if user.DbOwner != nil && *user.DbOwner && hasOwner {
err := errors.New("cannot have multiple users with db_owner = true")
errs = append(errs, newValidationError(err, userPath))
}
seenNames.Add(user.Username)
if user.DbOwner != nil && *user.DbOwner {
hasOwner = true
}
}
return errs
}
func validateBackupConfig(cfg *api.BackupConfigSpec, path []string) []error {
var errs []error
for i, repo := range cfg.Repositories {
repoPath := appendPath(path, "repositories", arrayIndexPath(i))
errs = append(errs, validateBackupRepository(repo, repoPath)...)
}
return errs
}
func validateRestoreConfig(cfg *api.RestoreConfigSpec, path []string) []error {
var errs []error
sourceDbIdPath := appendPath(path, "source_database_id")
errs = append(errs, validateIdentifier(string(cfg.SourceDatabaseID), sourceDbIdPath))
repoPath := appendPath(path, "repository")
errs = append(errs, validateRestoreRepository(cfg.Repository, repoPath)...)
restoreOptsPath := appendPath(path, "restore_options")
errs = append(errs, validatePgBackRestOptions(cfg.RestoreOptions, restoreOptsPath)...)
return errs
}
func validateBackupRepository(cfg *api.BackupRepositorySpec, path []string) []error {
props := repoProperties{
id: cfg.ID,
repoType: cfg.Type,
azureAccount: cfg.AzureAccount,
azureContainer: cfg.AzureContainer,
azureKey: cfg.AzureKey,
basePath: cfg.BasePath,
gcsBucket: cfg.GcsBucket,
s3Bucket: cfg.S3Bucket,
s3Region: cfg.S3Region,
customOptions: cfg.CustomOptions,
}
return validateRepoProperties(props, path)
}
func validateRestoreRepository(cfg *api.RestoreRepositorySpec, path []string) []error {
props := repoProperties{
id: cfg.ID,
repoType: cfg.Type,
azureAccount: cfg.AzureAccount,
azureContainer: cfg.AzureContainer,
azureKey: cfg.AzureKey,
basePath: cfg.BasePath,
gcsBucket: cfg.GcsBucket,
s3Bucket: cfg.S3Bucket,
s3Region: cfg.S3Region,
customOptions: cfg.CustomOptions,
}
return validateRepoProperties(props, path)
}
type repoProperties struct {
id *api.Identifier
repoType string
azureAccount *string
azureContainer *string
azureKey *string
basePath *string
gcsBucket *string
s3Bucket *string
s3Region *string
customOptions map[string]string
}
func validateRepoProperties(props repoProperties, path []string) []error {
var errs []error
id := utils.FromPointer(props.id)
if id != "" {
idPath := appendPath(path, "id")
errs = append(errs, validateIdentifier(string(id), idPath))
}
repoType := pgbackrest.RepositoryType(props.repoType)
switch repoType {
case pgbackrest.RepositoryTypeAzure:
errs = append(errs, validateAzureRepoProperties(props, path)...)
case pgbackrest.RepositoryTypeCifs, pgbackrest.RepositoryTypePosix:
errs = append(errs, validateFSRepoProperties(props, path)...)
case pgbackrest.RepositoryTypeGCS:
errs = append(errs, validateGCSRepoProperties(props, path)...)
case pgbackrest.RepositoryTypeS3:
errs = append(errs, validateS3RepoProperties(props, path)...)
default:
err := newValidationError(
fmt.Errorf("unsupported repo type '%s'", repoType),
appendPath(path, "type"),
)
errs = append(errs, err)
}
customOptsPath := appendPath(path, "custom_options")
errs = append(errs, validatePgBackRestOptions(props.customOptions, customOptsPath)...)
return errs
}
func validateAzureRepoProperties(props repoProperties, path []string) []error {
var errs []error
if utils.FromPointer(props.azureAccount) == "" {
err := errors.New("azure_account is required for azure repositories")
errs = append(errs, newValidationError(err, appendPath(path, "azure_account")))
}
if utils.FromPointer(props.azureContainer) == "" {
err := errors.New("azure_container is required for azure repositories")
errs = append(errs, newValidationError(err, appendPath(path, "azure_container")))
}
if utils.FromPointer(props.azureKey) == "" {
err := errors.New("azure_key is required for azure repositories")
errs = append(errs, newValidationError(err, appendPath(path, "azure_key")))
}
return errs
}
func validateFSRepoProperties(props repoProperties, path []string) []error {
var errs []error
basePath := utils.FromPointer(props.basePath)
if basePath == "" {
err := fmt.Errorf("base_path is required for %s repositories", props.repoType)
errs = append(errs, newValidationError(err, appendPath(path, "base_path")))
} else if !filepath.IsAbs(*props.basePath) {
err := fmt.Errorf("base_path must be absolute for %s repositories", props.repoType)
errs = append(errs, newValidationError(err, appendPath(path, "base_path")))
}
return errs
}
func validateGCSRepoProperties(props repoProperties, path []string) []error {
var errs []error
if utils.FromPointer(props.gcsBucket) == "" {
err := errors.New("gcs_bucket is required for gcs repositories")
errs = append(errs, newValidationError(err, appendPath(path, "gcs_bucket")))
}
return errs
}
func validateS3RepoProperties(props repoProperties, path []string) []error {
var errs []error
if utils.FromPointer(props.s3Bucket) == "" {
err := errors.New("s3_bucket is required for s3 repositories")
errs = append(errs, newValidationError(err, appendPath(path, "s3_bucket")))
}
return errs
}
var pgBackRestOptionPattern = regexp.MustCompile(`^[a-z0-9-]+$`)
var semverPattern = regexp.MustCompile(`^\d+\.\d+(\.\d+)?$`)
// reservedLabelPrefix is the label key prefix reserved for system use.
const reservedLabelPrefix = "pgedge."
func validateOrchestratorOpts(opts *api.OrchestratorOpts, path []string) []error {
if opts == nil || opts.Swarm == nil {
return nil
}
var errs []error
for key := range opts.Swarm.ExtraLabels {
if strings.HasPrefix(key, reservedLabelPrefix) {
labelPath := appendPath(path, "swarm", "extra_labels", mapKeyPath(key))
err := fmt.Errorf("labels starting with %q are reserved for system use", reservedLabelPrefix)
errs = append(errs, newValidationError(err, labelPath))
}
}
return errs
}
func validatePgBackRestOptions(opts map[string]string, path []string) []error {
var errs []error
for key := range opts {
if !pgBackRestOptionPattern.MatchString(key) {
optPath := appendPath(path, mapKeyPath(key))
err := errors.New("invalid option name")
errs = append(errs, newValidationError(err, optPath))
}
}
return errs
}
func validateBackupOptions(opts *api.BackupOptions) error {
var errs []error
optsPath := []string{"backup_options"}
errs = append(errs, validatePgBackRestOptions(opts.BackupOptions, optsPath)...)
return errors.Join(errs...)
}
func validateIdentifier(ident string, path []string) error {
if err := utils.ValidateID(ident); err != nil {
return newValidationError(err, path)
}
return nil
}
// validateHostIDUniqueness checks that the given host ID does not already exist in the cluster.
// Returns an error if the host ID already exists.
func validateHostIDUniqueness(ctx context.Context, hostSvc *host.Service, hostID string) error {
_, err := hostSvc.GetHost(ctx, hostID)
switch {
case err == nil:
// Host already exists - this is a duplicate
return ErrHostAlreadyExistsWithID(hostID)
case errors.Is(err, storage.ErrNotFound):
// Host doesn't exist - good, host ID is unique
return nil
default:
// Other errors (connection failures, permission errors, etc.) should be propagated
return fmt.Errorf("failed to check for existing host: %w", err)
}
}