-
Notifications
You must be signed in to change notification settings - Fork 141
/
cloudrun.go
215 lines (185 loc) · 5.63 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
// 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"
"errors"
"fmt"
"path/filepath"
"strconv"
"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")
)
type (
Service run.Service
Revision run.Revision
)
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.CloudProviderCloudRunConfig, 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.CloudProviderCloudRunConfig, 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) HealthStatus() (model.CloudRunResourceState_HealthStatus, string) {
var (
status = s.Status
errMessage = "Unexpected error while calculating: %s"
)
if status == nil {
return model.CloudRunResourceState_UNKNOWN, fmt.Sprintf(errMessage, "unable to find status")
}
conds := status.Conditions
for _, c := range conds {
isHealthy, err := strconv.ParseBool(c.Status)
if err != nil {
return model.CloudRunResourceState_UNKNOWN, fmt.Sprintf(errMessage, "unable to parse status: %s", c.Status)
}
if !isHealthy {
return model.CloudRunResourceState_OTHER, c.Message
}
}
return model.CloudRunResourceState_HEALTHY, ""
}
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) HealthStatus() (model.CloudRunResourceState_HealthStatus, string) {
var (
status = r.Status
errMessage = "Unexpected error while calculating: %s"
)
if status == nil {
return model.CloudRunResourceState_UNKNOWN, fmt.Sprintf(errMessage, "unable to find status")
}
conds := status.Conditions
for _, c := range conds {
isHealthy, err := strconv.ParseBool(c.Status)
if err != nil {
return model.CloudRunResourceState_UNKNOWN, fmt.Sprintf(errMessage, "unable to parse status: %s", c.Status)
}
if !isHealthy {
return model.CloudRunResourceState_OTHER, c.Message
}
}
return model.CloudRunResourceState_HEALTHY, ""
}