-
Notifications
You must be signed in to change notification settings - Fork 153
/
cloudrun.go
94 lines (79 loc) · 3.3 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
// 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"
"time"
"go.uber.org/zap"
provider "github.com/pipe-cd/pipe/pkg/app/piped/cloudprovider/cloudrun"
"github.com/pipe-cd/pipe/pkg/app/piped/planner"
"github.com/pipe-cd/pipe/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) {
cfg := in.DeploymentConfig.CloudRunDeploymentSpec
if cfg == nil {
err = fmt.Errorf("missing CloudRunDeploymentSpec in deployment configuration")
return
}
// Determine application version from the manifest.
if version, e := p.determineVersion(in.AppDir, cfg.Input.ServiceManifestFile); e == nil {
out.Version = version
} else {
out.Version = "unknown"
in.Logger.Warn("unable to determine target version", zap.Error(e))
}
// 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.Stages = buildQuickSyncPipeline(cfg.Input.AutoRollback, time.Now())
out.Summary = "Quick sync by deploying the new version and configuring all traffic to it because it seems this is the first deployment"
return
}
// When no pipeline was configured, do the quick sync.
if cfg.Pipeline == nil || len(cfg.Pipeline.Stages) == 0 {
out.Stages = buildQuickSyncPipeline(cfg.Input.AutoRollback, time.Now())
out.Summary = "Quick sync by deploying the new version and configuring all traffic to it because no pipeline was configured"
return
}
// Load service manifest at the last deployed commit to decide running version.
if err = in.Repo.Checkout(ctx, in.MostRecentSuccessfulCommitHash); err == nil {
if lastVersion, e := p.determineVersion(in.AppDir, cfg.Input.ServiceManifestFile); e == nil {
out.Stages = buildProgressivePipeline(cfg.Pipeline, cfg.Input.AutoRollback, time.Now())
out.Summary = fmt.Sprintf("Sync progressively because of updating image from %s to %s", lastVersion, out.Version)
return
}
}
out.Stages = buildProgressivePipeline(cfg.Pipeline, cfg.Input.AutoRollback, time.Now())
out.Summary = "Sync progressively 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)
}