-
Notifications
You must be signed in to change notification settings - Fork 141
/
cloudrun.go
155 lines (133 loc) · 5.2 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
// Copyright 2020 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"
"io"
"time"
"go.uber.org/zap"
provider "github.com/pipe-cd/pipecd/pkg/app/piped/cloudprovider/cloudrun"
"github.com/pipe-cd/pipecd/pkg/app/piped/planner"
"github.com/pipe-cd/pipecd/pkg/model"
)
// Planner plans the deployment pipeline for CloudRun application.
type Planner struct {
}
type registerer interface {
Register(k model.ApplicationKind, p planner.Planner) error
}
// Register registers this planner into the given registerer.
func Register(r registerer) {
r.Register(model.ApplicationKind_CLOUDRUN, &Planner{})
}
// Plan decides which pipeline should be used for the given input.
func (p *Planner) Plan(ctx context.Context, in planner.Input) (out planner.Output, err error) {
ds, err := in.TargetDSP.Get(ctx, io.Discard)
if err != nil {
err = fmt.Errorf("error while preparing deploy source data (%v)", err)
return
}
cfg := ds.ApplicationConfig.CloudRunApplicationSpec
if cfg == nil {
err = fmt.Errorf("missing CloudRunApplicationSpec in application configuration")
return
}
// Determine application version from the manifest.
if version, e := p.determineVersion(ds.AppDir, cfg.Input.ServiceManifestFile); e == nil {
out.Version = version
} else {
out.Version = "unknown"
in.Logger.Warn("unable to determine target version", zap.Error(e))
}
out.Versions, err = p.determineVersions(ds.AppDir, cfg.Input.ServiceManifestFile)
if err != nil {
in.Logger.Warn("unable to determine target versions", zap.Error(err))
out.Versions = []*model.ArtifactVersion{
{
Kind: model.ArtifactVersion_UNKNOWN,
Version: "unknown",
},
}
}
autoRollback := *cfg.Input.AutoRollback
// In case the strategy has been decided by trigger.
// For example: user triggered the deployment via web console.
switch in.Trigger.SyncStrategy {
case model.SyncStrategy_QUICK_SYNC:
out.SyncStrategy = model.SyncStrategy_QUICK_SYNC
out.Stages = buildQuickSyncPipeline(autoRollback, time.Now())
out.Summary = in.Trigger.StrategySummary
return
case model.SyncStrategy_PIPELINE:
if cfg.Pipeline == nil {
err = fmt.Errorf("unable to force sync with pipeline because no pipeline was specified")
return
}
out.SyncStrategy = model.SyncStrategy_PIPELINE
out.Stages = buildProgressivePipeline(cfg.Pipeline, autoRollback, time.Now())
out.Summary = in.Trigger.StrategySummary
return
}
// When no pipeline was configured, do the quick sync.
if cfg.Pipeline == nil || len(cfg.Pipeline.Stages) == 0 {
out.SyncStrategy = model.SyncStrategy_QUICK_SYNC
out.Stages = buildQuickSyncPipeline(autoRollback, time.Now())
out.Summary = fmt.Sprintf("Quick sync to deploy image %s and configure all traffic to it (pipeline was not configured)", out.Version)
return
}
// Force to use pipeline when the alwaysUsePipeline field was configured.
if cfg.Planner.AlwaysUsePipeline {
out.SyncStrategy = model.SyncStrategy_PIPELINE
out.Stages = buildProgressivePipeline(cfg.Pipeline, autoRollback, time.Now())
out.Summary = "Sync with the specified pipeline (alwaysUsePipeline was set)"
return
}
// This is the first time to deploy this application or it was unable to retrieve that value.
// We just do the quick sync.
if in.MostRecentSuccessfulCommitHash == "" {
out.SyncStrategy = model.SyncStrategy_QUICK_SYNC
out.Stages = buildQuickSyncPipeline(autoRollback, time.Now())
out.Summary = fmt.Sprintf("Quick sync to deploy image %s and configure all traffic to it (it seems this is the first deployment)", out.Version)
return
}
// Load service manifest at the last deployed commit to decide running version.
ds, err = in.RunningDSP.Get(ctx, io.Discard)
if err == nil {
if lastVersion, e := p.determineVersion(ds.AppDir, cfg.Input.ServiceManifestFile); e == nil {
out.SyncStrategy = model.SyncStrategy_PIPELINE
out.Stages = buildProgressivePipeline(cfg.Pipeline, autoRollback, time.Now())
out.Summary = fmt.Sprintf("Sync with pipeline to update image from %s to %s", lastVersion, out.Version)
return
}
}
out.SyncStrategy = model.SyncStrategy_PIPELINE
out.Stages = buildProgressivePipeline(cfg.Pipeline, autoRollback, time.Now())
out.Summary = "Sync with the specified pipeline"
return
}
func (p *Planner) determineVersion(appDir, serviceManifestFile string) (string, error) {
sm, err := provider.LoadServiceManifest(appDir, serviceManifestFile)
if err != nil {
return "", err
}
return provider.FindImageTag(sm)
}
func (p *Planner) determineVersions(appDir, serviceManifestFile string) ([]*model.ArtifactVersion, error) {
sm, err := provider.LoadServiceManifest(appDir, serviceManifestFile)
if err != nil {
return nil, err
}
return provider.FindArtifactVersions(sm)
}