forked from argoproj/argo-cd
/
repository.go
522 lines (481 loc) · 15.3 KB
/
repository.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
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
package repository
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
"time"
jsonnet "github.com/google/go-jsonnet"
log "github.com/sirupsen/logrus"
"golang.org/x/sync/semaphore"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"github.com/argoproj/argo-cd/common"
"github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"
"github.com/argoproj/argo-cd/util"
"github.com/argoproj/argo-cd/util/cache"
"github.com/argoproj/argo-cd/util/git"
"github.com/argoproj/argo-cd/util/hash"
"github.com/argoproj/argo-cd/util/helm"
"github.com/argoproj/argo-cd/util/ksonnet"
"github.com/argoproj/argo-cd/util/kube"
"github.com/argoproj/argo-cd/util/kustomize"
)
const (
// DefaultRepoCacheExpiration is the duration for items to live in the repo cache
DefaultRepoCacheExpiration = 24 * time.Hour
)
// Service implements ManifestService interface
type Service struct {
repoLock *util.KeyLock
gitFactory git.ClientFactory
cache cache.Cache
parallelismLimitSemaphore *semaphore.Weighted
}
// NewService returns a new instance of the Manifest service
func NewService(gitFactory git.ClientFactory, cache cache.Cache, parallelismLimit int64) *Service {
var parallelismLimitSemaphore *semaphore.Weighted
if parallelismLimit > 0 {
parallelismLimitSemaphore = semaphore.NewWeighted(parallelismLimit)
}
return &Service{
parallelismLimitSemaphore: parallelismLimitSemaphore,
repoLock: util.NewKeyLock(),
gitFactory: gitFactory,
cache: cache,
}
}
// ListDir lists the contents of a GitHub repo
func (s *Service) ListDir(ctx context.Context, q *ListDirRequest) (*FileList, error) {
gitClient, commitSHA, err := s.newClientResolveRevision(q.Repo, q.Revision)
if err != nil {
return nil, err
}
cacheKey := listDirCacheKey(commitSHA, q)
var res FileList
err = s.cache.Get(cacheKey, &res)
if err == nil {
log.Infof("listdir cache hit: %s", cacheKey)
return &res, nil
}
s.repoLock.Lock(gitClient.Root())
defer s.repoLock.Unlock(gitClient.Root())
commitSHA, err = checkoutRevision(gitClient, commitSHA)
if err != nil {
return nil, err
}
lsFiles, err := gitClient.LsFiles(q.Path)
if err != nil {
return nil, err
}
res = FileList{
Items: lsFiles,
}
err = s.cache.Set(&cache.Item{
Key: listDirCacheKey(commitSHA, q),
Object: &res,
Expiration: DefaultRepoCacheExpiration,
})
if err != nil {
log.Warnf("listdir cache set error %s: %v", cacheKey, err)
}
return &res, nil
}
func (s *Service) GetFile(ctx context.Context, q *GetFileRequest) (*GetFileResponse, error) {
gitClient, commitSHA, err := s.newClientResolveRevision(q.Repo, q.Revision)
if err != nil {
return nil, err
}
cacheKey := getFileCacheKey(commitSHA, q)
var res GetFileResponse
err = s.cache.Get(cacheKey, &res)
if err == nil {
log.Infof("getfile cache hit: %s", cacheKey)
return &res, nil
}
s.repoLock.Lock(gitClient.Root())
defer s.repoLock.Unlock(gitClient.Root())
commitSHA, err = checkoutRevision(gitClient, commitSHA)
if err != nil {
return nil, err
}
data, err := ioutil.ReadFile(filepath.Join(gitClient.Root(), q.Path))
if err != nil {
return nil, err
}
res = GetFileResponse{
Data: data,
}
err = s.cache.Set(&cache.Item{
Key: getFileCacheKey(commitSHA, q),
Object: &res,
Expiration: DefaultRepoCacheExpiration,
})
if err != nil {
log.Warnf("getfile cache set error %s: %v", cacheKey, err)
}
return &res, nil
}
func (s *Service) GenerateManifest(c context.Context, q *ManifestRequest) (*ManifestResponse, error) {
gitClient, commitSHA, err := s.newClientResolveRevision(q.Repo, q.Revision)
if err != nil {
return nil, err
}
cacheKey := manifestCacheKey(commitSHA, q)
getCached := func() *ManifestResponse {
var res ManifestResponse
if !q.NoCache {
err = s.cache.Get(cacheKey, &res)
if err == nil {
log.Infof("manifest cache hit: %s", cacheKey)
return &res
}
if err != cache.ErrCacheMiss {
log.Warnf("manifest cache error %s: %v", cacheKey, err)
} else {
log.Infof("manifest cache miss: %s", cacheKey)
}
}
return nil
}
cached := getCached()
if cached != nil {
return cached, nil
}
s.repoLock.Lock(gitClient.Root())
defer s.repoLock.Unlock(gitClient.Root())
cached = getCached()
if cached != nil {
return cached, nil
}
if s.parallelismLimitSemaphore != nil {
err = s.parallelismLimitSemaphore.Acquire(c, 1)
if err != nil {
return nil, err
}
defer s.parallelismLimitSemaphore.Release(1)
}
commitSHA, err = checkoutRevision(gitClient, commitSHA)
if err != nil {
return nil, err
}
appPath := filepath.Join(gitClient.Root(), q.ApplicationSource.Path)
genRes, err := generateManifests(appPath, q)
if err != nil {
return nil, err
}
res := *genRes
res.Revision = commitSHA
err = s.cache.Set(&cache.Item{
Key: manifestCacheKey(commitSHA, q),
Object: res,
Expiration: DefaultRepoCacheExpiration,
})
if err != nil {
log.Warnf("manifest cache set error %s: %v", cacheKey, err)
}
return &res, nil
}
// helper to formulate helm template options from a manifest request
func helmOpts(q *ManifestRequest) helm.HelmTemplateOpts {
opts := helm.HelmTemplateOpts{
Namespace: q.Namespace,
}
valueFiles := v1alpha1.HelmValueFiles(q.ApplicationSource)
if q.ApplicationSource.Helm != nil {
opts.ValueFiles = valueFiles
}
return opts
}
func kustomizeOpts(q *ManifestRequest) kustomize.KustomizeBuildOpts {
opts := kustomize.KustomizeBuildOpts{}
if q.ApplicationSource.Kustomize != nil {
opts.NamePrefix = q.ApplicationSource.Kustomize.NamePrefix
}
return opts
}
// generateManifests generates manifests from a path
func generateManifests(appPath string, q *ManifestRequest) (*ManifestResponse, error) {
var targetObjs []*unstructured.Unstructured
var params []*v1alpha1.ComponentParameter
var dest *v1alpha1.ApplicationDestination
var err error
appSourceType := IdentifyAppSourceTypeByAppDir(appPath)
switch appSourceType {
case v1alpha1.ApplicationSourceTypeKsonnet:
env := v1alpha1.KsonnetEnv(q.ApplicationSource)
targetObjs, params, dest, err = ksShow(q.AppLabelKey, appPath, env, q.ComponentParameterOverrides)
case v1alpha1.ApplicationSourceTypeHelm:
h := helm.NewHelmApp(appPath, q.HelmRepos)
err := h.Init()
if err != nil {
return nil, err
}
opts := helmOpts(q)
targetObjs, err = h.Template(q.AppLabelValue, opts, q.ComponentParameterOverrides)
if err != nil {
if !helm.IsMissingDependencyErr(err) {
return nil, err
}
err = h.DependencyBuild()
if err != nil {
return nil, err
}
targetObjs, err = h.Template(q.AppLabelValue, opts, q.ComponentParameterOverrides)
if err != nil {
return nil, err
}
}
params, err = h.GetParameters(opts.ValueFiles)
if err != nil {
return nil, err
}
case v1alpha1.ApplicationSourceTypeKustomize:
k := kustomize.NewKustomizeApp(appPath)
opts := kustomizeOpts(q)
targetObjs, params, err = k.Build(opts, q.ComponentParameterOverrides)
case v1alpha1.ApplicationSourceTypeDirectory:
targetObjs, err = FindManifests(appPath)
}
if err != nil {
return nil, err
}
manifests := make([]string, 0)
for _, obj := range targetObjs {
var targets []*unstructured.Unstructured
if obj.IsList() {
err = obj.EachListItem(func(object runtime.Object) error {
unstructuredObj, ok := object.(*unstructured.Unstructured)
if ok {
targets = append(targets, unstructuredObj)
return nil
}
return fmt.Errorf("resource list item has unexpected type")
})
if err != nil {
return nil, err
}
} else if isNullList(obj) {
// noop
} else {
targets = []*unstructured.Unstructured{obj}
}
for _, target := range targets {
if q.AppLabelKey != "" && q.AppLabelValue != "" && !kube.IsCRD(target) {
err = kube.SetAppInstanceLabel(target, q.AppLabelKey, q.AppLabelValue)
if err != nil {
return nil, err
}
}
manifestStr, err := json.Marshal(target.Object)
if err != nil {
return nil, err
}
manifests = append(manifests, string(manifestStr))
}
}
res := ManifestResponse{
Manifests: manifests,
Params: params,
}
if dest != nil {
res.Namespace = dest.Namespace
res.Server = dest.Server
}
return &res, nil
}
// tempRepoPath returns a formulated temporary directory location to clone a repository
func tempRepoPath(repo string) string {
return filepath.Join(os.TempDir(), strings.Replace(repo, "/", "_", -1))
}
// IdentifyAppSourceTypeByAppDir examines a directory and determines its application source type
func IdentifyAppSourceTypeByAppDir(appDirPath string) v1alpha1.ApplicationSourceType {
if pathExists(appDirPath, "app.yaml") {
return v1alpha1.ApplicationSourceTypeKsonnet
}
if pathExists(appDirPath, "Chart.yaml") {
return v1alpha1.ApplicationSourceTypeHelm
}
if pathExists(appDirPath, "kustomization.yaml") {
return v1alpha1.ApplicationSourceTypeKustomize
}
return v1alpha1.ApplicationSourceTypeDirectory
}
// isNullList checks if the object is a "List" type where items is null instead of an empty list.
// Handles a corner case where obj.IsList() returns false when a manifest is like:
// ---
// apiVersion: v1
// items: null
// kind: ConfigMapList
func isNullList(obj *unstructured.Unstructured) bool {
if _, ok := obj.Object["spec"]; ok {
return false
}
if _, ok := obj.Object["status"]; ok {
return false
}
field, ok := obj.Object["items"]
if !ok {
return false
}
return field == nil
}
// checkoutRevision is a convenience function to initialize a repo, fetch, and checkout a revision
// Returns the 40 character commit SHA after the checkout has been performed
func checkoutRevision(gitClient git.Client, commitSHA string) (string, error) {
err := gitClient.Init()
if err != nil {
return "", status.Errorf(codes.Internal, "Failed to initialize git repo: %v", err)
}
err = gitClient.Fetch()
if err != nil {
return "", status.Errorf(codes.Internal, "Failed to fetch git repo: %v", err)
}
err = gitClient.Checkout(commitSHA)
if err != nil {
return "", status.Errorf(codes.Internal, "Failed to checkout %s: %v", commitSHA, err)
}
return gitClient.CommitSHA()
}
func manifestCacheKey(commitSHA string, q *ManifestRequest) string {
appSrc := q.ApplicationSource.DeepCopy()
appSrc.RepoURL = "" // superceded by commitSHA
appSrc.TargetRevision = "" // superceded by commitSHA
appSrcStr, _ := json.Marshal(appSrc)
pStr, _ := json.Marshal(q.ComponentParameterOverrides)
fnva := hash.FNVa(string(appSrcStr) + string(pStr))
return fmt.Sprintf("mfst|%s|%s|%s|%s|%d", q.AppLabelKey, q.AppLabelValue, commitSHA, q.Namespace, fnva)
}
func listDirCacheKey(commitSHA string, q *ListDirRequest) string {
return fmt.Sprintf("ldir|%s|%s", q.Path, commitSHA)
}
func getFileCacheKey(commitSHA string, q *GetFileRequest) string {
return fmt.Sprintf("gfile|%s|%s", q.Path, commitSHA)
}
// ksShow runs `ks show` in an app directory after setting any component parameter overrides
func ksShow(appLabelKey, appPath, envName string, overrides []*v1alpha1.ComponentParameter) ([]*unstructured.Unstructured, []*v1alpha1.ComponentParameter, *v1alpha1.ApplicationDestination, error) {
ksApp, err := ksonnet.NewKsonnetApp(appPath)
if err != nil {
return nil, nil, nil, status.Errorf(codes.FailedPrecondition, "unable to load application from %s: %v", appPath, err)
}
params, err := ksApp.ListEnvParams(envName)
if err != nil {
return nil, nil, nil, status.Errorf(codes.InvalidArgument, "Failed to list ksonnet app params: %v", err)
}
if overrides != nil {
for _, override := range overrides {
err = ksApp.SetComponentParams(envName, override.Component, override.Name, override.Value)
if err != nil {
return nil, nil, nil, err
}
}
}
dest, err := ksApp.Destination(envName)
if err != nil {
return nil, nil, nil, status.Errorf(codes.InvalidArgument, err.Error())
}
targetObjs, err := ksApp.Show(envName)
if err == nil && appLabelKey == common.LabelKeyLegacyApplicationName {
// Address https://github.com/ksonnet/ksonnet/issues/707
for _, d := range targetObjs {
kube.UnsetLabel(d, "ksonnet.io/component")
}
}
if err != nil {
return nil, nil, nil, err
}
return targetObjs, params, dest, nil
}
var manifestFile = regexp.MustCompile(`^.*\.(yaml|yml|json|jsonnet)$`)
// FindManifests looks at all yaml files in a directory and unmarshals them into a list of unstructured objects
func FindManifests(appPath string) ([]*unstructured.Unstructured, error) {
files, err := ioutil.ReadDir(appPath)
if err != nil {
return nil, status.Errorf(codes.FailedPrecondition, "Failed to read dir %s: %v", appPath, err)
}
var objs []*unstructured.Unstructured
for _, f := range files {
if f.IsDir() || !manifestFile.MatchString(f.Name()) {
continue
}
out, err := ioutil.ReadFile(filepath.Join(appPath, f.Name()))
if err != nil {
return nil, err
}
if strings.HasSuffix(f.Name(), ".json") {
var obj unstructured.Unstructured
err = json.Unmarshal(out, &obj)
if err != nil {
return nil, status.Errorf(codes.FailedPrecondition, "Failed to unmarshal %q: %v", f.Name(), err)
}
objs = append(objs, &obj)
} else if strings.HasSuffix(f.Name(), ".jsonnet") {
vm := jsonnet.MakeVM()
vm.Importer(&jsonnet.FileImporter{
JPaths: []string{appPath},
})
jsonStr, err := vm.EvaluateSnippet(f.Name(), string(out))
if err != nil {
return nil, status.Errorf(codes.FailedPrecondition, "Failed to evaluate jsonnet %q: %v", f.Name(), err)
}
// attempt to unmarshal either array or single object
var jsonObjs []*unstructured.Unstructured
err = json.Unmarshal([]byte(jsonStr), &jsonObjs)
if err == nil {
objs = append(objs, jsonObjs...)
} else {
var jsonObj unstructured.Unstructured
err = json.Unmarshal([]byte(jsonStr), &jsonObj)
if err != nil {
return nil, status.Errorf(codes.FailedPrecondition, "Failed to unmarshal generated json %q: %v", f.Name(), err)
}
objs = append(objs, &jsonObj)
}
} else {
yamlObjs, err := kube.SplitYAML(string(out))
if err != nil {
if len(yamlObjs) > 0 {
// If we get here, we had a multiple objects in a single YAML file which had some
// valid k8s objects, but errors parsing others (within the same file). It's very
// likely the user messed up a portion of the YAML, so report on that.
return nil, status.Errorf(codes.FailedPrecondition, "Failed to unmarshal %q: %v", f.Name(), err)
}
// Otherwise, it might be a unrelated YAML file which we will ignore
continue
}
objs = append(objs, yamlObjs...)
}
}
return objs, nil
}
// pathExists reports whether the file or directory at the named concatenation of paths exists.
func pathExists(ss ...string) bool {
name := filepath.Join(ss...)
if _, err := os.Stat(name); err != nil {
if os.IsNotExist(err) {
return false
}
}
return true
}
// newClientResolveRevision is a helper to perform the common task of instantiating a git client
// and resolving a revision to a commit SHA
func (s *Service) newClientResolveRevision(repo *v1alpha1.Repository, revision string) (git.Client, string, error) {
repoURL := git.NormalizeGitURL(repo.Repo)
appRepoPath := tempRepoPath(repoURL)
gitClient, err := s.gitFactory.NewClient(repoURL, appRepoPath, repo.Username, repo.Password, repo.SSHPrivateKey)
if err != nil {
return nil, "", err
}
commitSHA, err := gitClient.LsRemote(revision)
if err != nil {
return nil, "", err
}
return gitClient, commitSHA, nil
}