-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsourceControllerService.go
More file actions
404 lines (357 loc) · 12.9 KB
/
Copy pathsourceControllerService.go
File metadata and controls
404 lines (357 loc) · 12.9 KB
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
package main
import (
"bytes"
"context"
"fmt"
"github.com/caarlos0/env"
"github.com/devtron-labs/devtron/pkg/sourceController/bean"
"github.com/devtron-labs/devtron/pkg/sourceController/oci"
repository "github.com/devtron-labs/devtron/pkg/sourceController/sql/repo"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/crane"
"github.com/google/go-containerregistry/pkg/name"
gcrv1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/remote"
"go.uber.org/zap"
"k8s.io/apimachinery/pkg/util/json"
kuberecorder "k8s.io/client-go/tools/record"
"net/http"
"sigs.k8s.io/controller-runtime/pkg/client"
"strconv"
"strings"
)
type SourceControllerService interface {
CallExternalCIWebHook(digest, tag string) error
ReconcileSource(ctx context.Context) (bean.Result, error)
ReconcileSourceWrapper()
}
type SourceControllerServiceImpl struct {
logger *zap.SugaredLogger
SCSconfig *SourceControllerConfig
ciArtifactRepository repository.CiArtifactRepository
client.Client
kuberecorder.EventRecorder
}
type SourceControllerConfig struct {
ImageShowCount int `env:"IMAGE_COUNT_FROM_REPO" envDefault:"20"`
ExternalCiId int `env:"EXTERNAL_CI_ID" envDefault:"6"`
RepoName string `env:"REPO_NAME_EXTERNAL_CI" envDefault:"stefanprodan/manifests/podinfo"`
RegistryURL string `env:"REGISTRY_URL_EXTERNAL_CI" envDefault:"ghcr.io"`
Insecure bool `env:"INSECURE_EXTERNAL_CI" envDefault:"true"`
ApiToken string `env:"API_TOKEN_EXTERNAL_CI" envDefault:""`
}
var UserAgent = "flux/v2"
type invalidOCIURLError struct {
err error
}
func (e invalidOCIURLError) Error() string {
return e.err.Error()
}
func NewSourceControllerServiceImpl(logger *zap.SugaredLogger,
cfg *SourceControllerConfig,
ciArtifactRepository repository.CiArtifactRepository) *SourceControllerServiceImpl {
sourceControllerServiceimpl := &SourceControllerServiceImpl{
logger: logger,
SCSconfig: cfg,
ciArtifactRepository: ciArtifactRepository,
}
return sourceControllerServiceimpl
}
func GetSourceControllerConfig() (*SourceControllerConfig, error) {
cfg := &SourceControllerConfig{}
err := env.Parse(cfg)
if err != nil {
fmt.Println("failed to parse server cluster status config: " + err.Error())
return nil, err
}
return cfg, err
}
type ExternalCI struct {
DockerImage string `json:"dockerImage" validate:"required,image-validator"`
Digest string `json:"digest"`
DataSource string `json:"dataSource"`
MaterialType string `json:"materialType"`
}
//type CiCompleteEvent struct {
// CiProjectDetails []pipeline.CiProjectDetails `json:"ciProjectDetails"`
// DockerImage string `json:"dockerImage" validate:"required,image-validator"`
// Digest string `json:"digest"`
// PipelineId int `json:"pipelineId"`
// WorkflowId *int `json:"workflowId"`
// TriggeredBy int32 `json:"triggeredBy"`
// PipelineName string `json:"pipelineName"`
// DataSource string `json:"dataSource"`
// MaterialType string `json:"materialType"`
// Metrics util.CIMetrics `json:"metrics"`
// AppName string `json:"appName"`
// IsArtifactUploaded bool `json:"isArtifactUploaded"`
// FailureReason string `json:"failureReason"`
//}
func getPayloadForExternalCi(image, digest string) *ExternalCI {
payload := &ExternalCI{
DockerImage: image,
Digest: digest,
DataSource: bean.External,
MaterialType: bean.MaterialTypeGit,
}
return payload
}
func (impl *SourceControllerServiceImpl) ReconcileSourceWrapper() {
result, err := impl.ReconcileSource(context.Background())
if err != nil {
impl.logger.Errorw("error in reconciling sources", "err", err, "result", result)
}
}
func (impl *SourceControllerServiceImpl) ReconcileSource(ctx context.Context) (bean.Result, error) {
var auth authn.Authenticator
keychain := oci.Anonymous{}
transport := remote.DefaultTransport.(*http.Transport).Clone()
opts := makeRemoteOptions(ctx, transport, keychain, auth, impl.SCSconfig.Insecure)
url, err := parseRepositoryURLInValidFormat(impl.SCSconfig.RegistryURL, impl.SCSconfig.RepoName)
if err != nil {
impl.logger.Errorw("error in parsung repository utl in valid format", "err", err)
return bean.ResultEmpty, invalidOCIURLError{err}
}
tags, err := getAllTags(url, opts.craneOpts)
if err != nil {
impl.logger.Errorw("error in getting all tags ", "err", err, "url", url)
return bean.ResultEmpty, err
}
digests := make([]string, 0, len(tags))
digestTagMap := make(map[string]string)
for i := 0; i < len(tags) && i < impl.SCSconfig.ImageShowCount; i++ {
tag := tags[i]
// Determine which artifact revision to pull
tagUrl, err := getArtifactURLForTag(url, tag)
if err != nil {
impl.logger.Errorw("error in getting artifact url", "err", err, "tag", tag)
return bean.ResultEmpty, err
}
digest, err := crane.Digest(tagUrl, opts.craneOpts...)
if err != nil {
fmt.Errorf("error")
}
digestTagMap[digest] = tag
digests = append(digests, digest)
}
err = impl.filterAlreadyPresentArtifacts(digests, digestTagMap)
if err != nil {
impl.logger.Errorw("error in filtering artifacts", "err", err)
return bean.ResultEmpty, err
}
for digest, tag := range digestTagMap {
impl.CallExternalCIWebHook(digest, tag)
}
return bean.ResultSuccess, err
}
// CallingExternalCiWebhook
func (impl *SourceControllerServiceImpl) CallExternalCIWebHook(digest, tag string) error {
host := impl.SCSconfig.RegistryURL
repoName := impl.SCSconfig.RepoName
image := fmt.Sprintf("%s/%s:%s", host, repoName, tag)
url := bean.WebHookHostUrl + strconv.Itoa(impl.SCSconfig.ExternalCiId)
payload := getPayloadForExternalCi(image, digest)
b, err := json.Marshal(payload)
if err != nil {
impl.logger.Errorw("error in marshalling golang struct", "err", err)
return err
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(b))
req.Header.Set("api-token", impl.SCSconfig.ApiToken)
req.Header.Add("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
impl.logger.Errorw("error in hitting http request to web hook", "err", err)
return err
}
defer resp.Body.Close()
return nil
}
func (impl *SourceControllerServiceImpl) filterAlreadyPresentArtifacts(imageDigests []string, digestTagMap map[string]string) error {
ciArtifacts, err := impl.ciArtifactRepository.GetByImageDigests(imageDigests)
if err != nil {
impl.logger.Errorw("error in getting ci artifact by image digests ", "err", err)
return err
}
for _, ciArtifact := range ciArtifacts {
delete(digestTagMap, ciArtifact.ImageDigest)
}
return nil
}
// getAllTags call the remote container registry, fetches all the tags from the repository
func getAllTags(url string, options []crane.Option) ([]string, error) {
tags, err := crane.ListTags(url, options...)
if err != nil {
return nil, err
}
return tags, nil
}
// getArtifactURL determines which tag or revision should be used and returns the OCI artifact FQN.
func getArtifactURLForTag(tag, url string) (string, error) {
if tag != "" {
return fmt.Sprintf("%s:%s", tag, url), nil
}
return url, nil
}
// parseRepositoryURL validates and extracts the repository URL.
func parseRepositoryURLInValidFormat(registryUrl, repo string) (string, error) {
url := fmt.Sprintf("%s/%s", registryUrl, repo)
ref, err := name.ParseReference(url)
if err != nil {
return "", err
}
imageName := strings.TrimPrefix(url, ref.Context().RegistryStr())
if s := strings.Split(imageName, ":"); len(s) > 1 {
return "", fmt.Errorf("URL must not contain a tag; remove ':%s'", s[1])
}
return ref.Context().Name(), nil
}
// getRevision fetches the upstream digest, returning the revision in the
// format '<tag>@<digest>'.
func (r *SourceControllerServiceImpl) getRevision(url string, options []crane.Option) (string, error) {
ref, err := name.ParseReference(url)
if err != nil {
return "", err
}
repoTag := ""
repoName := strings.TrimPrefix(url, ref.Context().RegistryStr())
if s := strings.Split(repoName, ":"); len(s) == 2 && !strings.Contains(repoName, "@") {
repoTag = s[1]
}
if repoTag == "" && !strings.Contains(repoName, "@") {
repoTag = "latest"
}
digest, err := crane.Digest(url, options...)
if err != nil {
return "", err
}
digestHash, err := gcrv1.NewHash(digest)
if err != nil {
return "", err
}
revision := digestHash.String()
if repoTag != "" {
revision = fmt.Sprintf("%s@%s", repoTag, revision)
}
return revision, nil
}
//// getArtifactURL determines which tag or revision should be used and returns the OCI artifact FQN.
//func (r *SourceControllerServiceImpl) getArtifactURL(obj *OCIRepository, options []crane.Option) (string, error) {
// url, err := r.parseRepositoryURL(obj)
// if err != nil {
// return "", invalidOCIURLError{err}
// }
//
// if obj.Spec.Reference != nil {
// if obj.Spec.Reference.Digest != "" {
// return fmt.Sprintf("%s@%s", url, obj.Spec.Reference.Digest), nil
// }
//
// if obj.Spec.Reference.SemVer != "" {
// tag, err := r.getTagBySemver(url, obj.Spec.Reference.SemVer, options)
// if err != nil {
// return "", err
// }
// return fmt.Sprintf("%s:%s", url, tag), nil
// }
//
// if obj.Spec.Reference.Tag != "" {
// return fmt.Sprintf("%s:%s", url, obj.Spec.Reference.Tag), nil
// }
// }
//
// return url, nil
//}
//// getTagBySemver call the remote container registry, fetches all the tags from the repository,
//// and returns the latest tag according to the semver expression.
//func (r *SourceControllerServiceImpl) getTagBySemver(url, exp string, options []crane.Option) (string, error) {
// tags, err := crane.ListTags(url, options...)
// if err != nil {
// return "", err
// }
//
// constraint, err := semver.NewConstraint(exp)
// if err != nil {
// return "", fmt.Errorf("semver '%s' parse error: %w", exp, err)
// }
//
// var matchingVersions []*semver.Version
// for _, t := range tags {
// v, err := bean.ParseVersion(t)
// if err != nil {
// continue
// }
//
// if constraint.Check(v) {
// matchingVersions = append(matchingVersions, v)
// }
// }
//
// if len(matchingVersions) == 0 {
// return "", fmt.Errorf("no match found for semver: %s", exp)
// }
//
// sort.Sort(sort.Reverse(semver.Collection(matchingVersions)))
// return matchingVersions[0].Original(), nil
//}
//// parseRepositoryURL validates and extracts the repository URL.
//func (r *SourceControllerServiceImpl) parseRepositoryURL(obj *OCIRepository) (string, error) {
// if !strings.HasPrefix(obj.Spec.URL, oci.OCIRepositoryPrefix) {
// return "", fmt.Errorf("URL must be in format 'oci://<domain>/<org>/<repo>'")
// }
//
// url := strings.TrimPrefix(obj.Spec.URL, oci.OCIRepositoryPrefix)
// ref, err := name.ParseReference(url)
// if err != nil {
// return "", err
// }
//
// imageName := strings.TrimPrefix(url, ref.Context().RegistryStr())
// if s := strings.Split(imageName, ":"); len(s) > 1 {
// return "", fmt.Errorf("URL must not contain a tag; remove ':%s'", s[1])
// }
//
// return ref.Context().Name(), nil
//}
// remoteOptions contains the options to interact with a remote registry.
// It can be used to pass options to go-containerregistry based libraries.
type remoteOptions struct {
craneOpts []crane.Option
verifyOpts []remote.Option
}
// makeRemoteOptions returns a remoteOptions struct with the authentication and transport options set.
// The returned struct can be used to interact with a remote registry using go-containerregistry based libraries.
func makeRemoteOptions(ctxTimeout context.Context, transport http.RoundTripper, keychain authn.Keychain, auth authn.Authenticator, insecure bool) remoteOptions {
// have to make it configurable insecure in future iterations
o := remoteOptions{
craneOpts: craneOptions(ctxTimeout, insecure),
verifyOpts: []remote.Option{},
}
if transport != nil {
o.craneOpts = append(o.craneOpts, crane.WithTransport(transport))
o.verifyOpts = append(o.verifyOpts, remote.WithTransport(transport))
}
if auth != nil {
// auth take precedence over keychain here as we expect the caller to set
// the auth only if it is required.
o.verifyOpts = append(o.verifyOpts, remote.WithAuth(auth))
o.craneOpts = append(o.craneOpts, crane.WithAuth(auth))
return o
}
o.verifyOpts = append(o.verifyOpts, remote.WithAuthFromKeychain(keychain))
o.craneOpts = append(o.craneOpts, crane.WithAuthFromKeychain(keychain))
return o
}
// craneOptions sets the auth headers, timeout and user agent
// for all operations against remote container registries.
func craneOptions(ctx context.Context, insecure bool) []crane.Option {
options := []crane.Option{
crane.WithContext(ctx),
crane.WithUserAgent(UserAgent),
}
if insecure {
options = append(options, crane.Insecure)
}
return options
}