-
Notifications
You must be signed in to change notification settings - Fork 4
/
client.go
178 lines (150 loc) · 5.07 KB
/
client.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
package pubsubapi
import (
"context"
"fmt"
"strings"
"time"
stdpubsub "cloud.google.com/go/pubsub"
"github.com/estafette/estafette-ci-api/pkg/api"
manifest "github.com/estafette/estafette-ci-manifest"
"github.com/rs/zerolog/log"
)
// Client is the interface for communicating with the pubsub apis
//go:generate mockgen -package=pubsubapi -destination ./mock.go -source=client.go
type Client interface {
SubscriptionForTopic(ctx context.Context, message PubSubPushMessage) (event *manifest.EstafettePubSubEvent, err error)
SubscribeToTopic(ctx context.Context, projectID, topicID string) (err error)
SubscribeToPubsubTriggers(ctx context.Context, manifestString string) (err error)
}
// NewClient returns a new pubsub.Client
func NewClient(config *api.APIConfig, pubsubClient *stdpubsub.Client) Client {
if config == nil || config.Integrations == nil || config.Integrations.Pubsub == nil || !config.Integrations.Pubsub.Enable {
return &client{
enabled: false,
}
}
return &client{
enabled: true,
config: config,
pubsubClient: pubsubClient,
}
}
type client struct {
enabled bool
config *api.APIConfig
pubsubClient *stdpubsub.Client
}
func (c *client) SubscriptionForTopic(ctx context.Context, message PubSubPushMessage) (*manifest.EstafettePubSubEvent, error) {
if !c.enabled {
return nil, nil
}
projectID := message.GetProject()
subscriptionName := message.GetSubscription()
if strings.HasSuffix(subscriptionName, c.config.Integrations.Pubsub.SubscriptionNameSuffix) {
return &manifest.EstafettePubSubEvent{
Project: projectID,
Topic: strings.TrimSuffix(subscriptionName, c.config.Integrations.Pubsub.SubscriptionNameSuffix),
}, nil
}
subscription := c.pubsubClient.SubscriptionInProject(subscriptionName, projectID)
if subscription == nil {
return nil, fmt.Errorf("Can't find subscription %v in project %v", subscriptionName, projectID)
}
subscriptionConfig, err := subscription.Config(ctx)
if err != nil {
return nil, err
}
return &manifest.EstafettePubSubEvent{
Project: projectID,
Topic: subscriptionConfig.Topic.ID(),
Message: message.Message,
}, nil
}
func (c *client) SubscribeToTopic(ctx context.Context, projectID, topicID string) error {
if !c.enabled {
return nil
}
// check if topic exists
topic := c.pubsubClient.TopicInProject(topicID, projectID)
topicExists, err := topic.Exists(ctx)
if err != nil {
return err
}
if !topicExists {
return fmt.Errorf("Pub/Sub topic %v does not exist in project %v, cannot subscribe to it", topicID, projectID)
}
// check if subscription already exists
subscriptionName := c.getSubscriptionName(topicID)
subscription := c.pubsubClient.SubscriptionInProject(subscriptionName, projectID)
log.Info().Msgf("Checking if subscription %v for topic %v in project %v exists...", subscriptionName, topicID, projectID)
subscriptionExists, err := subscription.Exists(ctx)
if err != nil {
return err
}
if subscriptionExists {
// already exists, no need to do anything
return nil
}
// create a subscription to the topic
log.Info().Msgf("Creating subscription %v for topic %v in project %v...", subscriptionName, topicID, projectID)
_, err = c.pubsubClient.CreateSubscription(ctx, subscriptionName, stdpubsub.SubscriptionConfig{
Topic: topic,
PushConfig: stdpubsub.PushConfig{
Endpoint: c.config.Integrations.Pubsub.Endpoint,
AuthenticationMethod: &stdpubsub.OIDCToken{
Audience: c.config.Integrations.Pubsub.Audience,
ServiceAccountEmail: c.config.Integrations.Pubsub.ServiceAccountEmail,
},
},
AckDeadline: 20 * time.Second,
RetentionDuration: 3 * time.Hour,
ExpirationPolicy: time.Duration(c.config.Integrations.Pubsub.SubscriptionIdleExpirationDays) * 24 * time.Hour,
})
if err != nil {
return err
}
log.Info().Msgf("Created subscription %v in project %v", subscriptionName, projectID)
return nil
}
func (c *client) getSubscriptionName(topicName string) string {
// it must start with a letter, and contain only letters ([A-Za-z]), numbers ([0-9]), dashes (-), underscores (_), periods (.), tildes (~), plus (+) or percent signs (%). It must be between 3 and 255 characters in length, and must not start with "goog".
return topicName + c.config.Integrations.Pubsub.SubscriptionNameSuffix
}
func (c *client) SubscribeToPubsubTriggers(ctx context.Context, manifestString string) error {
if !c.enabled {
return nil
}
mft, err := manifest.ReadManifest(c.config.ManifestPreferences, manifestString, false)
if err != nil {
return err
}
for _, t := range mft.Triggers {
if t.PubSub != nil {
err := c.SubscribeToTopic(ctx, t.PubSub.Project, t.PubSub.Topic)
if err != nil {
return err
}
}
}
for _, r := range mft.Releases {
for _, t := range r.Triggers {
if t.PubSub != nil {
err := c.SubscribeToTopic(ctx, t.PubSub.Project, t.PubSub.Topic)
if err != nil {
return err
}
}
}
}
for _, b := range mft.Bots {
for _, t := range b.Triggers {
if t.PubSub != nil {
err := c.SubscribeToTopic(ctx, t.PubSub.Project, t.PubSub.Topic)
if err != nil {
return err
}
}
}
}
return nil
}