-
Notifications
You must be signed in to change notification settings - Fork 28
/
setup.go
91 lines (75 loc) · 2.48 KB
/
setup.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
// Copyright (c) 2022 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.
package queue
import (
"fmt"
"strings"
"time"
"github.com/go-vela/server/queue/redis"
"github.com/go-vela/types/constants"
"github.com/sirupsen/logrus"
)
// Setup represents the configuration necessary for
// creating a Vela service capable of integrating
// with a configured queue environment.
type Setup struct {
// Queue Configuration
// specifies the driver to use for the queue client
Driver string
// specifies the address to use for the queue client
Address string
// enables the queue client to integrate with a queue cluster
Cluster bool
// specifies a list of routes (channels/topics) for managing builds for the queue client
Routes []string
// specifies the timeout for pop requests for the queue client
Timeout time.Duration
}
// Redis creates and returns a Vela service capable
// of integrating with a Redis queue.
func (s *Setup) Redis() (Service, error) {
logrus.Trace("creating redis queue client from setup")
// create new Redis queue service
//
// https://pkg.go.dev/github.com/go-vela/server/queue/redis?tab=doc#New
return redis.New(
redis.WithAddress(s.Address),
redis.WithChannels(s.Routes...),
redis.WithCluster(s.Cluster),
redis.WithTimeout(s.Timeout),
)
}
// Kafka creates and returns a Vela service capable
// of integrating with a Kafka queue.
func (s *Setup) Kafka() (Service, error) {
logrus.Trace("creating kafka queue client from setup")
return nil, fmt.Errorf("unsupported queue driver: %s", constants.DriverKafka)
}
// Validate verifies the necessary fields for the
// provided configuration are populated correctly.
func (s *Setup) Validate() error {
logrus.Trace("validating queue setup for client")
// verify a queue driver was provided
if len(s.Driver) == 0 {
return fmt.Errorf("no queue driver provided")
}
// verify a queue address was provided
if len(s.Address) == 0 {
return fmt.Errorf("no queue address provided")
}
// check if the queue address has a scheme
if !strings.Contains(s.Address, "://") {
return fmt.Errorf("queue address must be fully qualified (<scheme>://<host>)")
}
// check if the queue address has a trailing slash
if strings.HasSuffix(s.Address, "/") {
return fmt.Errorf("queue address must not have trailing slash")
}
// verify queue routes were provided
if len(s.Routes) == 0 {
return fmt.Errorf("no queue routes provided")
}
// setup is valid
return nil
}