-
Notifications
You must be signed in to change notification settings - Fork 11
/
versions.go
276 lines (248 loc) · 7.74 KB
/
versions.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
/*This file is part of kuberpult.
Kuberpult is free software: you can redistribute it and/or modify
it under the terms of the Expat(MIT) License as published by
the Free Software Foundation.
Kuberpult is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
MIT License for more details.
You should have received a copy of the MIT License
along with kuberpult. If not, see <https://directory.fsf.org/wiki/License:Expat>.
Copyright 2023 freiheit.com*/
package versions
import (
"context"
"fmt"
"strconv"
"time"
"github.com/argoproj/argo-cd/v2/pkg/apiclient/application"
"github.com/freiheit-com/kuberpult/services/rollout-service/pkg/argo"
"github.com/argoproj/argo-cd/v2/util/grpc"
api "github.com/freiheit-com/kuberpult/pkg/api/v1"
"github.com/freiheit-com/kuberpult/pkg/auth"
"github.com/freiheit-com/kuberpult/pkg/logger"
"github.com/freiheit-com/kuberpult/pkg/setup"
"go.uber.org/zap"
"google.golang.org/grpc/codes"
"k8s.io/utils/lru"
)
// This is a the user that the rollout service uses to query the versions.
// It is not written to the repository.
var RolloutServiceUser auth.User = auth.User{
Email: "kuberpult-rollout-service@local",
Name: "kuberpult-rollout-service",
}
type VersionClient interface {
GetVersion(ctx context.Context, revision, environment, application string) (*VersionInfo, error)
ConsumeEvents(ctx context.Context, processor VersionEventProcessor, hr *setup.HealthReporter) error
GetArgoProcessor() *argo.ArgoAppProcessor
}
type versionClient struct {
overviewClient api.OverviewServiceClient
versionClient api.VersionServiceClient
cache *lru.Cache
manageArgoAppsEnabled bool
manageArgoAppsFilter string
ArgoProcessor argo.ArgoAppProcessor
}
type VersionInfo struct {
Version uint64
SourceCommitId string
DeployedAt time.Time
}
func (v *VersionInfo) Equal(w *VersionInfo) bool {
if v == nil {
return w == nil
}
if w == nil {
return false
}
return v.Version == w.Version
}
var ErrNotFound error = fmt.Errorf("not found")
var ZeroVersion VersionInfo
// GetVersion implements VersionClient
func (v *versionClient) GetVersion(ctx context.Context, revision, environment, application string) (*VersionInfo, error) {
ctx = auth.WriteUserToGrpcContext(ctx, RolloutServiceUser)
tr, err := v.tryGetVersion(ctx, revision, environment, application)
if err == nil {
return tr, nil
}
info, err := v.versionClient.GetVersion(ctx, &api.GetVersionRequest{
GitRevision: revision,
Environment: environment,
Application: application,
})
if err != nil {
return nil, err
}
return &VersionInfo{
Version: info.Version,
SourceCommitId: info.SourceCommitId,
DeployedAt: info.DeployedAt.AsTime(),
}, nil
}
// Tries getting the version from cache
func (v *versionClient) tryGetVersion(ctx context.Context, revision, environment, application string) (*VersionInfo, error) {
var overview *api.GetOverviewResponse
entry, ok := v.cache.Get(revision)
if !ok {
return nil, ErrNotFound
}
overview = entry.(*api.GetOverviewResponse)
for _, group := range overview.GetEnvironmentGroups() {
for _, env := range group.GetEnvironments() {
if env.Name == environment {
app := env.Applications[application]
if app == nil {
return &ZeroVersion, nil
}
return &VersionInfo{
Version: app.Version,
SourceCommitId: sourceCommitId(overview, app),
DeployedAt: deployedAt(app),
}, nil
}
}
}
return &ZeroVersion, nil
}
func deployedAt(app *api.Environment_Application) time.Time {
if app.DeploymentMetaData == nil {
return time.Time{}
}
deployTime := app.DeploymentMetaData.DeployTime
if deployTime != "" {
dt, err := strconv.ParseInt(deployTime, 10, 64)
if err != nil {
return time.Time{}
}
return time.Unix(dt, 0).UTC()
}
return time.Time{}
}
func team(overview *api.GetOverviewResponse, app string) string {
a := overview.Applications[app]
if a == nil {
return ""
}
return a.Team
}
func sourceCommitId(overview *api.GetOverviewResponse, app *api.Environment_Application) string {
a := overview.Applications[app.Name]
if a == nil {
return ""
}
for _, rel := range a.Releases {
if rel.Version == app.Version {
return rel.SourceCommitId
}
}
return ""
}
type KuberpultEvent struct {
Environment string
Application string
EnvironmentGroup string
IsProduction bool
Team string
Version *VersionInfo
}
type VersionEventProcessor interface {
ProcessKuberpultEvent(ctx context.Context, ev KuberpultEvent)
}
type key struct {
Environment string
Application string
}
func (v *versionClient) ConsumeEvents(ctx context.Context, processor VersionEventProcessor, hr *setup.HealthReporter) error {
ctx = auth.WriteUserToGrpcContext(ctx, RolloutServiceUser)
versions := map[key]uint64{}
environmentGroups := map[key]string{}
teams := map[key]string{}
return hr.Retry(ctx, func() error {
client, err := v.overviewClient.StreamOverview(ctx, &api.GetOverviewRequest{})
if err != nil {
return fmt.Errorf("overview.connect: %w", err)
}
hr.ReportReady("consuming")
for {
select {
case <-ctx.Done():
return nil
default:
}
overview, err := client.Recv()
if err != nil {
grpcErr := grpc.UnwrapGRPCStatus(err)
if grpcErr != nil {
if grpcErr.Code() == codes.Canceled {
return nil
}
}
return fmt.Errorf("overview.recv: %w", err)
}
l := logger.FromContext(ctx).With(zap.String("git.revision", overview.GitRevision))
v.cache.Add(overview.GitRevision, overview)
l.Info("overview.get")
seen := make(map[key]uint64, len(versions))
for _, envGroup := range overview.EnvironmentGroups {
for _, env := range envGroup.Environments {
for _, app := range env.Applications {
dt := deployedAt(app)
sc := sourceCommitId(overview, app)
tm := team(overview, app.Name)
l.Info("version.process", zap.String("application", app.Name), zap.String("environment", env.Name), zap.Uint64("version", app.Version), zap.Time("deployedAt", dt))
k := key{env.Name, app.Name}
seen[k] = app.Version
environmentGroups[k] = envGroup.EnvironmentGroupName
teams[k] = tm
if versions[k] == app.Version {
continue
}
processor.ProcessKuberpultEvent(ctx, KuberpultEvent{
Application: app.Name,
Environment: env.Name,
EnvironmentGroup: envGroup.EnvironmentGroupName,
Team: tm,
IsProduction: env.Priority == api.Priority_PROD,
Version: &VersionInfo{
Version: app.Version,
SourceCommitId: sc,
DeployedAt: dt,
},
})
}
}
}
l.Info("version.push")
v.ArgoProcessor.Push(ctx, overview)
// Send events with version 0 for deleted applications so that we can react
// to apps getting deleted.
for k := range versions {
if seen[k] == 0 {
processor.ProcessKuberpultEvent(ctx, KuberpultEvent{
Application: k.Application,
Environment: k.Environment,
EnvironmentGroup: environmentGroups[k],
Team: teams[k],
Version: &VersionInfo{},
})
}
}
versions = seen
}
})
}
func New(oclient api.OverviewServiceClient, vclient api.VersionServiceClient, appClient application.ApplicationServiceClient, manageArgoApplicationEnabled bool, manageArgoApplicationFilter []string) VersionClient {
result := &versionClient{
cache: lru.New(20),
overviewClient: oclient,
versionClient: vclient,
ArgoProcessor: argo.New(appClient, manageArgoApplicationEnabled, manageArgoApplicationFilter),
}
return result
}
func (v *versionClient) GetArgoProcessor() *argo.ArgoAppProcessor {
return &v.ArgoProcessor
}