-
Notifications
You must be signed in to change notification settings - Fork 148
/
cloudrun.go
255 lines (219 loc) · 7.38 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
// Copyright 2023 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"
"fmt"
"strings"
"time"
"github.com/pipe-cd/pipecd/pkg/app/piped/deploysource"
"github.com/pipe-cd/pipecd/pkg/app/piped/executor"
provider "github.com/pipe-cd/pipecd/pkg/app/piped/platformprovider/cloudrun"
"github.com/pipe-cd/pipecd/pkg/config"
"github.com/pipe-cd/pipecd/pkg/model"
)
type registerer interface {
Register(stage model.Stage, f executor.Factory) error
RegisterRollback(kind model.RollbackKind, f executor.Factory) error
}
func Register(r registerer) {
f := func(in executor.Input) executor.Executor {
return &deployExecutor{
Input: in,
}
}
r.Register(model.StageCloudRunSync, f)
r.Register(model.StageCloudRunPromote, f)
r.RegisterRollback(model.RollbackKind_Rollback_CLOUDRUN, func(in executor.Input) executor.Executor {
return &rollbackExecutor{
Input: in,
}
})
}
func loadServiceManifest(in *executor.Input, serviceManifestFile string, ds *deploysource.DeploySource) (provider.ServiceManifest, bool) {
in.LogPersister.Infof("Loading service manifest at commit %s", ds.Revision)
sm, err := provider.LoadServiceManifest(ds.AppDir, serviceManifestFile)
if err != nil {
in.LogPersister.Errorf("Failed to load service manifest (%v)", err)
return provider.ServiceManifest{}, false
}
in.LogPersister.Infof("Successfully loaded the service manifest at commit %s", ds.Revision)
return sm, true
}
func findPlatformProvider(in *executor.Input) (name string, cfg *config.PlatformProviderCloudRunConfig, found bool) {
name = in.Application.PlatformProvider
if name == "" {
in.LogPersister.Error("Missing the PlatformProvider name in the application configuration")
return
}
cp, ok := in.PipedConfig.FindPlatformProvider(name, model.ApplicationKind_CLOUDRUN)
if !ok {
in.LogPersister.Errorf("The specified platform provider %q was not found in piped configuration", name)
return
}
cfg = cp.CloudRunConfig
found = true
return
}
func decideRevisionName(sm provider.ServiceManifest, commit string, lp executor.LogPersister) (revision string, ok bool) {
var err error
revision, err = provider.DecideRevisionName(sm, commit)
if err != nil {
lp.Errorf("Unable to decide revision name for the commit %s (%v)", commit, err)
return
}
ok = true
return
}
func configureServiceManifest(sm provider.ServiceManifest, revision string, traffics []provider.RevisionTraffic, lp executor.LogPersister) bool {
if revision != "" {
if err := sm.SetRevision(revision); err != nil {
lp.Errorf("Unable to set revision name to service manifest (%v)", err)
return false
}
}
if err := sm.UpdateTraffic(traffics); err != nil {
lp.Errorf("Unable to configure traffic percentages to service manifest (%v)", err)
return false
}
lp.Info("Successfully prepared service manifest with traffic percentages as below:")
for _, t := range traffics {
lp.Infof(" %s: %d", t.RevisionName, t.Percent)
}
return true
}
func apply(ctx context.Context, client provider.Client, sm provider.ServiceManifest, lp executor.LogPersister) bool {
lp.Info("Start applying the service manifest")
_, err := client.Update(ctx, sm)
if err == nil {
lp.Infof("Successfully updated the service %s", sm.Name)
return true
}
if err != provider.ErrServiceNotFound {
lp.Errorf("Failed to update the service %s (%v)", sm.Name, err)
return false
}
lp.Infof("Service %s was not found, a new service will be created", sm.Name)
if _, err := client.Create(ctx, sm); err != nil {
lp.Errorf("Failed to create the service %s (%v)", sm.Name, err)
return false
}
lp.Infof("Successfully created the service %s", sm.Name)
return true
}
func waitRevisionReady(ctx context.Context, client provider.Client, revisionName string, retryDuration, retryTimeout time.Duration, lp executor.LogPersister) error {
shouldCheckConditions := map[string]struct{}{
"Active": struct{}{},
"Ready": struct{}{},
"ConfigurationsReady": struct{}{},
"RoutesReady": struct{}{},
"ContainerHealthy": struct{}{},
"ResourcesAvailable": struct{}{},
}
mustPassConditions := map[string]struct{}{
"Ready": struct{}{},
"Active": struct{}{},
}
doCheck := func() (bool, error) {
rvs, err := client.GetRevision(ctx, revisionName)
// NotFound should be a retriable error.
if err == provider.ErrRevisionNotFound {
return true, err
}
if err != nil {
return false, err
}
var (
trueConds = make(map[string]struct{}, 0)
falseConds = make([]string, 0, len(shouldCheckConditions))
unknownConds = make([]string, 0, len(shouldCheckConditions))
)
if rvs.Status != nil {
for _, cond := range rvs.Status.Conditions {
if _, ok := shouldCheckConditions[cond.Type]; !ok {
continue
}
switch cond.Status {
case "True":
trueConds[cond.Type] = struct{}{}
case "False":
falseConds = append(falseConds, cond.Message)
default:
unknownConds = append(unknownConds, cond.Message)
}
}
}
if len(falseConds) > 0 {
return false, fmt.Errorf("%s", strings.Join(falseConds, "\n"))
}
if len(unknownConds) > 0 {
return true, fmt.Errorf("%s", strings.Join(unknownConds, "\n"))
}
for k := range mustPassConditions {
if _, ok := trueConds[k]; !ok {
return true, fmt.Errorf("could not check status field %q", k)
}
}
return false, nil
}
start := time.Now()
for {
retry, err := doCheck()
if !retry {
if err != nil {
lp.Errorf("Revision %s was not ready: %v", revisionName, err)
return err
}
lp.Infof("Revision %s is ready to receive traffic", revisionName)
return nil
}
if time.Since(start) > retryTimeout {
lp.Errorf("Revision %s was not ready: %v", revisionName, err)
return err
}
lp.Infof("Revision %s is still not ready (%v), will retry after %v", revisionName, err, retryDuration)
time.Sleep(retryDuration)
}
}
func revisionExists(ctx context.Context, client provider.Client, revisionName string, lp executor.LogPersister) (bool, error) {
_, err := client.GetRevision(ctx, revisionName)
if err == nil {
return true, nil
}
if err == provider.ErrRevisionNotFound {
return false, nil
}
lp.Errorf("Failed while checking the existence of revision %s (%v)", revisionName, err)
return false, err
}
func addBuiltinLabels(sm provider.ServiceManifest, hash, pipedID, appID, revisionName string, lp executor.LogPersister) bool {
labels := map[string]string{
provider.LabelManagedBy: provider.ManagedByPiped,
provider.LabelPiped: pipedID,
provider.LabelApplication: appID,
provider.LabelCommitHash: hash,
}
// Set builtinLabels for Service.
sm.AddLabels(labels)
if revisionName == "" {
return true
}
// Set buildinLabels for Revision.
labels[provider.LabelRevisionName] = revisionName
if err := sm.AddRevisionLabels(labels); err != nil {
lp.Errorf("Unable to add revision labels for the service manifest %s (%v)", sm.Name, err)
return false
}
return true
}