/
cloudrun.go
318 lines (273 loc) · 7.94 KB
/
cloudrun.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
// Copyright 2024 The PipeCD Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cloudrun
import (
"context"
"errors"
"fmt"
"path/filepath"
"strings"
"sync"
"go.uber.org/zap"
"golang.org/x/sync/singleflight"
"google.golang.org/api/run/v1"
"github.com/pipe-cd/pipecd/pkg/config"
"github.com/pipe-cd/pipecd/pkg/model"
)
const (
DefaultServiceManifestFilename = "service.yaml"
)
var (
ErrServiceNotFound = errors.New("not found")
ErrRevisionNotFound = errors.New("not found")
)
var (
TypeConditions = map[string]struct{}{
"Active": struct{}{},
"Ready": struct{}{},
"ConfigurationsReady": struct{}{},
"RoutesReady": struct{}{},
"ContainerHealthy": struct{}{},
"ResourcesAvailable": struct{}{},
}
TypeHealthyServiceConditions = map[string]struct{}{
"Ready": struct{}{},
"ConfigurationsReady": struct{}{},
"RoutesReady": struct{}{},
}
TypeHealthyRevisionConditions = map[string]struct{}{
"Ready": struct{}{},
"Active": struct{}{},
"ContainerHealthy": struct{}{},
"ResourcesAvailable": struct{}{},
}
)
// Kind represents the kind of resource.
type Kind string
const (
KindService Kind = "Service"
KindRevision Kind = "Revision"
)
type (
Service run.Service
Revision run.Revision
StatusConditions struct {
Kind Kind
TrueTypes map[string]struct{}
// Eliminate duplicated messages with the same reason.
FalseMessages []string
UnknownMessages []string
}
)
const (
LabelManagedBy = "pipecd-dev-managed-by" // Always be piped.
LabelPiped = "pipecd-dev-piped" // The id of piped handling this application.
LabelApplication = "pipecd-dev-application" // The application this resource belongs to.
LabelCommitHash = "pipecd-dev-commit-hash" // Hash value of the deployed commit.
LabelRevisionName = "pipecd-dev-revision-name" // The name of revision.
ManagedByPiped = "piped"
)
type Client interface {
Create(ctx context.Context, sm ServiceManifest) (*Service, error)
Update(ctx context.Context, sm ServiceManifest) (*Service, error)
List(ctx context.Context, options *ListOptions) ([]*Service, string, error)
GetRevision(ctx context.Context, name string) (*Revision, error)
ListRevisions(ctx context.Context, options *ListRevisionsOptions) ([]*Revision, string, error)
}
type ListOptions struct {
Limit int64
LabelSelector string
Cursor string
}
type ListRevisionsOptions struct {
Limit int64
LabelSelector string
Cursor string
}
type Registry interface {
Client(ctx context.Context, name string, cfg *config.PlatformProviderCloudRunConfig, logger *zap.Logger) (Client, error)
}
func LoadServiceManifest(appDir, serviceFilename string) (ServiceManifest, error) {
if serviceFilename == "" {
serviceFilename = DefaultServiceManifestFilename
}
path := filepath.Join(appDir, serviceFilename)
return loadServiceManifest(path)
}
var defaultRegistry = ®istry{
clients: make(map[string]Client),
newGroup: &singleflight.Group{},
}
func DefaultRegistry() Registry {
return defaultRegistry
}
type registry struct {
clients map[string]Client
mu sync.RWMutex
newGroup *singleflight.Group
}
func (r *registry) Client(ctx context.Context, name string, cfg *config.PlatformProviderCloudRunConfig, logger *zap.Logger) (Client, error) {
r.mu.RLock()
client, ok := r.clients[name]
r.mu.RUnlock()
if ok {
return client, nil
}
c, err, _ := r.newGroup.Do(name, func() (interface{}, error) {
return newClient(ctx, cfg.Project, cfg.Region, cfg.CredentialsFile, logger)
})
if err != nil {
return nil, err
}
client = c.(Client)
r.mu.Lock()
r.clients[name] = client
r.mu.Unlock()
return client, nil
}
func MakeManagedByPipedSelector() string {
return fmt.Sprintf("%s=%s", LabelManagedBy, ManagedByPiped)
}
func MakeRevisionNamesSelector(names []string) string {
return fmt.Sprintf("%s in (%s)", LabelRevisionName, strings.Join(names, ","))
}
func (s *Service) ServiceManifest() (ServiceManifest, error) {
r := (*run.Service)(s)
data, err := r.MarshalJSON()
if err != nil {
return ServiceManifest{}, err
}
return ParseServiceManifest(data)
}
func (s *Service) UID() (string, bool) {
if s.Metadata == nil || s.Metadata.Uid == "" {
return "", false
}
return s.Metadata.Uid, true
}
// ActiveRevisionNames returns all its active revisions which may handle the traffic.
func (s *Service) ActiveRevisionNames() []string {
if s.Status == nil {
return nil
}
tf := s.Status.Traffic
ret := make([]string, len(tf))
for i := range tf {
ret[i] = tf[i].RevisionName
}
return ret
}
func (s *Service) StatusConditions() *StatusConditions {
var (
trueTypes = make(map[string]struct{}, len(TypeConditions))
falseMsgs = make(map[string]string, len(TypeConditions))
unknownMsgs = make(map[string]string, len(TypeConditions))
)
if s.Status == nil {
return nil
}
for _, cond := range s.Status.Conditions {
if _, ok := TypeConditions[cond.Type]; !ok {
continue
}
switch cond.Status {
case "True":
trueTypes[cond.Type] = struct{}{}
case "False":
falseMsgs[cond.Reason] = cond.Message
default:
unknownMsgs[cond.Reason] = cond.Message
}
}
fMsgs := make([]string, 0, len(falseMsgs))
for _, v := range falseMsgs {
fMsgs = append(fMsgs, v)
}
uMsgs := make([]string, 0, len(unknownMsgs))
for _, v := range unknownMsgs {
uMsgs = append(uMsgs, v)
}
return &StatusConditions{
Kind: KindService,
TrueTypes: trueTypes,
FalseMessages: fMsgs,
UnknownMessages: uMsgs,
}
}
func (r *Revision) RevisionManifest() (RevisionManifest, error) {
rev := (*run.Revision)(r)
data, err := rev.MarshalJSON()
if err != nil {
return RevisionManifest{}, err
}
return ParseRevisionManifest(data)
}
func (r *Revision) StatusConditions() *StatusConditions {
var (
trueTypes = make(map[string]struct{}, len(TypeConditions))
falseMsgs = make(map[string]string, len(TypeConditions))
unknownMsgs = make(map[string]string, len(TypeConditions))
)
if r.Status == nil {
return nil
}
for _, cond := range r.Status.Conditions {
if _, ok := TypeConditions[cond.Type]; !ok {
continue
}
switch cond.Status {
case "True":
trueTypes[cond.Type] = struct{}{}
case "False":
falseMsgs[cond.Reason] = cond.Message
default:
unknownMsgs[cond.Reason] = cond.Message
}
}
fMsgs := make([]string, 0, len(falseMsgs))
for _, v := range falseMsgs {
fMsgs = append(fMsgs, v)
}
uMsgs := make([]string, 0, len(unknownMsgs))
for _, v := range unknownMsgs {
uMsgs = append(uMsgs, v)
}
return &StatusConditions{
Kind: KindRevision,
TrueTypes: trueTypes,
FalseMessages: fMsgs,
UnknownMessages: uMsgs,
}
}
func (s *StatusConditions) HealthStatus() (model.CloudRunResourceState_HealthStatus, string) {
if s == nil {
return model.CloudRunResourceState_UNKNOWN, "Unexpected error while calculating: unable to find status"
}
if len(s.FalseMessages) > 0 {
return model.CloudRunResourceState_OTHER, strings.Join(s.FalseMessages, "; ")
}
if len(s.UnknownMessages) > 0 {
return model.CloudRunResourceState_UNKNOWN, strings.Join(s.UnknownMessages, "; ")
}
mustPassConditions := TypeHealthyServiceConditions
if s.Kind == KindRevision {
mustPassConditions = TypeHealthyRevisionConditions
}
for k := range mustPassConditions {
if _, ok := s.TrueTypes[k]; !ok {
return model.CloudRunResourceState_UNKNOWN, fmt.Sprintf("Could not check status field %q", k)
}
}
return model.CloudRunResourceState_HEALTHY, ""
}