forked from argoproj/argo-cd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repository.go
810 lines (756 loc) · 24.2 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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
package repository
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/url"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"github.com/TomOnTime/utfutil"
"github.com/ghodss/yaml"
"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"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
executil "github.com/argoproj/argo-cd/util/exec"
"github.com/argoproj/argo-cd/util/security"
"github.com/argoproj/argo-cd/common"
"github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"
"github.com/argoproj/argo-cd/reposerver/apiclient"
"github.com/argoproj/argo-cd/reposerver/metrics"
"github.com/argoproj/argo-cd/util"
"github.com/argoproj/argo-cd/util/app/discovery"
argopath "github.com/argoproj/argo-cd/util/app/path"
"github.com/argoproj/argo-cd/util/cache"
"github.com/argoproj/argo-cd/util/git"
"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"
"github.com/argoproj/argo-cd/util/text"
)
const (
PluginEnvAppName = "ARGOCD_APP_NAME"
PluginEnvAppNamespace = "ARGOCD_APP_NAMESPACE"
)
// Service implements ManifestService interface
type Service struct {
repoLock *util.KeyLock
cache *cache.Cache
parallelismLimitSemaphore *semaphore.Weighted
metricsServer *metrics.MetricsServer
newGitClient func(rawRepoURL string, creds git.Creds, insecure bool, enableLfs bool) (git.Client, error)
newHelmClient func(repoURL string, creds helm.Creds) helm.Client
}
// NewService returns a new instance of the Manifest service
func NewService(metricsServer *metrics.MetricsServer, cache *cache.Cache, parallelismLimit int64) *Service {
var parallelismLimitSemaphore *semaphore.Weighted
if parallelismLimit > 0 {
parallelismLimitSemaphore = semaphore.NewWeighted(parallelismLimit)
}
repoLock := util.NewKeyLock()
return &Service{
parallelismLimitSemaphore: parallelismLimitSemaphore,
repoLock: repoLock,
cache: cache,
metricsServer: metricsServer,
newGitClient: git.NewClient,
newHelmClient: func(repoURL string, creds helm.Creds) helm.Client {
return helm.NewClientWithLock(repoURL, creds, repoLock)
},
}
}
// ListDir lists the contents of a GitHub repo
func (s *Service) ListApps(ctx context.Context, q *apiclient.ListAppsRequest) (*apiclient.AppList, error) {
gitClient, commitSHA, err := s.newClientResolveRevision(q.Repo, q.Revision)
if err != nil {
return nil, err
}
if apps, err := s.cache.ListApps(q.Repo.Repo, commitSHA); err == nil {
log.Infof("cache hit: %s/%s", q.Repo.Repo, q.Revision)
return &apiclient.AppList{Apps: apps}, nil
}
s.repoLock.Lock(gitClient.Root())
defer s.repoLock.Unlock(gitClient.Root())
commitSHA, err = checkoutRevision(gitClient, commitSHA)
if err != nil {
return nil, err
}
apps, err := discovery.Discover(gitClient.Root())
if err != nil {
return nil, err
}
err = s.cache.SetApps(q.Repo.Repo, commitSHA, apps)
if err != nil {
log.Warnf("cache set error %s/%s: %v", q.Repo.Repo, commitSHA, err)
}
res := apiclient.AppList{Apps: apps}
return &res, nil
}
type operationSettings struct {
sem *semaphore.Weighted
noCache bool
}
// runRepoOperation downloads either git folder or helm chart and executes specified operation
func (s *Service) runRepoOperation(
ctx context.Context,
revision string,
repo *v1alpha1.Repository,
source *v1alpha1.ApplicationSource,
getCached func(revision string) bool,
operation func(appPath string, revision string) error,
settings operationSettings) error {
var gitClient git.Client
var err error
revision = util.FirstNonEmpty(revision, source.TargetRevision)
if !source.IsHelm() {
gitClient, revision, err = s.newClientResolveRevision(repo, revision)
if err != nil {
return err
}
}
if !settings.noCache && getCached(revision) {
return nil
}
s.metricsServer.IncPendingRepoRequest(repo.Repo)
defer s.metricsServer.DecPendingRepoRequest(repo.Repo)
if settings.sem != nil {
err = settings.sem.Acquire(ctx, 1)
if err != nil {
return err
}
defer settings.sem.Release(1)
}
if source.IsHelm() {
helmClient := s.newHelmClient(repo.Repo, repo.GetHelmCreds())
if settings.noCache {
err = helmClient.CleanChartCache(source.Chart, revision)
if err != nil {
return err
}
}
chartPath, closer, err := helmClient.ExtractChart(source.Chart, revision)
if err != nil {
return err
}
defer util.Close(closer)
return operation(chartPath, revision)
} else {
s.repoLock.Lock(gitClient.Root())
defer s.repoLock.Unlock(gitClient.Root())
// double-check locking
if !settings.noCache && getCached(revision) {
return nil
}
revision, err = checkoutRevision(gitClient, revision)
if err != nil {
return err
}
appPath, err := argopath.Path(gitClient.Root(), source.Path)
if err != nil {
return err
}
return operation(appPath, revision)
}
}
func (s *Service) GenerateManifest(ctx context.Context, q *apiclient.ManifestRequest) (*apiclient.ManifestResponse, error) {
res := &apiclient.ManifestResponse{}
getCached := func(revision string) bool {
err := s.cache.GetManifests(revision, q.ApplicationSource, q.Namespace, q.AppLabelKey, q.AppLabelValue, &res)
if err == nil {
log.Infof("manifest cache hit: %s/%s", q.ApplicationSource.String(), revision)
return true
}
if err != cache.ErrCacheMiss {
log.Warnf("manifest cache error %s: %v", q.ApplicationSource.String(), err)
} else {
log.Infof("manifest cache miss: %s/%s", q.ApplicationSource.String(), revision)
}
return false
}
err := s.runRepoOperation(ctx, q.Revision, q.Repo, q.ApplicationSource, getCached, func(appPath string, revision string) error {
var err error
res, err = GenerateManifests(appPath, q)
if err != nil {
return err
}
res.Revision = revision
err = s.cache.SetManifests(revision, q.ApplicationSource, q.Namespace, q.AppLabelKey, q.AppLabelValue, &res)
if err != nil {
log.Warnf("manifest cache set error %s/%s: %v", q.ApplicationSource.String(), revision, err)
}
return nil
}, operationSettings{sem: s.parallelismLimitSemaphore, noCache: q.NoCache})
return res, err
}
func getHelmRepos(repositories []*v1alpha1.Repository) []helm.HelmRepository {
repos := make([]helm.HelmRepository, 0)
for _, repo := range repositories {
repos = append(repos, helm.HelmRepository{Name: repo.Name, Repo: repo.Repo, Creds: repo.GetHelmCreds()})
}
return repos
}
func helmTemplate(appPath string, q *apiclient.ManifestRequest) ([]*unstructured.Unstructured, error) {
templateOpts := &helm.TemplateOpts{
Name: q.AppLabelValue,
Namespace: q.Namespace,
KubeVersion: text.SemVer(q.KubeVersion),
Set: map[string]string{},
SetString: map[string]string{},
}
appHelm := q.ApplicationSource.Helm
if appHelm != nil {
if appHelm.ReleaseName != "" {
templateOpts.Name = appHelm.ReleaseName
}
for _, val := range appHelm.ValueFiles {
// If val is not a URL, run it against the directory enforcer. If it is a URL, use it without checking
if _, err := url.ParseRequestURI(val); err != nil {
baseDirectoryPath, err := security.SubtractRelativeFromAbsolutePath(appPath, q.ApplicationSource.Path)
if err != nil {
return nil, err
}
absBaseDir, err := filepath.Abs(baseDirectoryPath)
if err != nil {
return nil, err
}
if !filepath.IsAbs(val) {
absWorkDir, err := filepath.Abs(appPath)
if err != nil {
return nil, err
}
val = filepath.Join(absWorkDir, val)
}
_, err = security.EnforceToCurrentRoot(absBaseDir, val)
if err != nil {
return nil, err
}
}
templateOpts.Values = append(templateOpts.Values, val)
}
if appHelm.Values != "" {
file, err := ioutil.TempFile("", "values-*.yaml")
if err != nil {
return nil, err
}
p := file.Name()
defer func() { _ = os.RemoveAll(p) }()
err = ioutil.WriteFile(p, []byte(appHelm.Values), 0644)
if err != nil {
return nil, err
}
templateOpts.Values = append(templateOpts.Values, p)
}
for _, p := range appHelm.Parameters {
if p.ForceString {
templateOpts.SetString[p.Name] = p.Value
} else {
templateOpts.Set[p.Name] = p.Value
}
}
}
if templateOpts.Name == "" {
templateOpts.Name = q.AppLabelValue
}
h, err := helm.NewHelmApp(appPath, getHelmRepos(q.Repos))
if err != nil {
return nil, err
}
defer h.Dispose()
err = h.Init()
if err != nil {
return nil, err
}
out, err := h.Template(templateOpts)
if err != nil {
if !helm.IsMissingDependencyErr(err) {
return nil, err
}
err = h.DependencyBuild()
if err != nil {
return nil, err
}
out, err = h.Template(templateOpts)
if err != nil {
return nil, err
}
}
return kube.SplitYAML(out)
}
// GenerateManifests generates manifests from a path
func GenerateManifests(appPath string, q *apiclient.ManifestRequest) (*apiclient.ManifestResponse, error) {
var targetObjs []*unstructured.Unstructured
var dest *v1alpha1.ApplicationDestination
appSourceType, err := GetAppSourceType(q.ApplicationSource, appPath)
repoURL := ""
if q.Repo != nil {
repoURL = q.Repo.Repo
}
switch appSourceType {
case v1alpha1.ApplicationSourceTypeKsonnet:
targetObjs, dest, err = ksShow(q.AppLabelKey, appPath, q.ApplicationSource.Ksonnet)
case v1alpha1.ApplicationSourceTypeHelm:
targetObjs, err = helmTemplate(appPath, q)
case v1alpha1.ApplicationSourceTypeKustomize:
k := kustomize.NewKustomizeApp(appPath, q.Repo.GetGitCreds(), repoURL)
targetObjs, _, err = k.Build(q.ApplicationSource.Kustomize, q.KustomizeOptions)
case v1alpha1.ApplicationSourceTypePlugin:
targetObjs, err = runConfigManagementPlugin(appPath, q, q.Repo.GetGitCreds())
case v1alpha1.ApplicationSourceTypeDirectory:
var directory *v1alpha1.ApplicationSourceDirectory
if directory = q.ApplicationSource.Directory; directory == nil {
directory = &v1alpha1.ApplicationSourceDirectory{}
}
targetObjs, err = findManifests(appPath, *directory)
}
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 := apiclient.ManifestResponse{
Manifests: manifests,
SourceType: string(appSourceType),
}
if dest != nil {
res.Namespace = dest.Namespace
res.Server = dest.Server
}
return &res, nil
}
// GetAppSourceType returns explicit application source type or examines a directory and determines its application source type
func GetAppSourceType(source *v1alpha1.ApplicationSource, path string) (v1alpha1.ApplicationSourceType, error) {
appSourceType, err := source.ExplicitType()
if err != nil {
return "", err
}
if appSourceType != nil {
return *appSourceType, nil
}
appType, err := discovery.AppType(path)
if err != nil {
return "", err
}
return v1alpha1.ApplicationSourceType(appType), nil
}
// 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
}
// ksShow runs `ks show` in an app directory after setting any component parameter overrides
func ksShow(appLabelKey, appPath string, ksonnetOpts *v1alpha1.ApplicationSourceKsonnet) ([]*unstructured.Unstructured, *v1alpha1.ApplicationDestination, error) {
ksApp, err := ksonnet.NewKsonnetApp(appPath)
if err != nil {
return nil, nil, status.Errorf(codes.FailedPrecondition, "unable to load application from %s: %v", appPath, err)
}
if ksonnetOpts == nil {
return nil, nil, status.Errorf(codes.InvalidArgument, "Ksonnet environment not set")
}
for _, override := range ksonnetOpts.Parameters {
err = ksApp.SetComponentParams(ksonnetOpts.Environment, override.Component, override.Name, override.Value)
if err != nil {
return nil, nil, err
}
}
dest, err := ksApp.Destination(ksonnetOpts.Environment)
if err != nil {
return nil, nil, status.Errorf(codes.InvalidArgument, err.Error())
}
targetObjs, err := ksApp.Show(ksonnetOpts.Environment)
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, err
}
return targetObjs, 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, directory v1alpha1.ApplicationSourceDirectory) ([]*unstructured.Unstructured, error) {
var objs []*unstructured.Unstructured
err := filepath.Walk(appPath, func(path string, f os.FileInfo, err error) error {
if err != nil {
return err
}
if f.IsDir() {
if path != appPath && !directory.Recurse {
return filepath.SkipDir
} else {
return nil
}
}
if !manifestFile.MatchString(f.Name()) {
return nil
}
out, err := utfutil.ReadFile(path, utfutil.UTF8)
if err != nil {
return err
}
if strings.HasSuffix(f.Name(), ".json") {
var obj unstructured.Unstructured
err = json.Unmarshal(out, &obj)
if err != nil {
return status.Errorf(codes.FailedPrecondition, "Failed to unmarshal %q: %v", f.Name(), err)
}
objs = append(objs, &obj)
} else if strings.HasSuffix(f.Name(), ".jsonnet") {
vm := makeJsonnetVm(directory.Jsonnet)
vm.Importer(&jsonnet.FileImporter{
JPaths: []string{appPath},
})
jsonStr, err := vm.EvaluateSnippet(f.Name(), string(out))
if err != nil {
return 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 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 status.Errorf(codes.FailedPrecondition, "Failed to unmarshal %q: %v", f.Name(), err)
}
// Otherwise, it might be a unrelated YAML file which we will ignore
return nil
}
objs = append(objs, yamlObjs...)
}
return nil
})
if err != nil {
return nil, err
}
return objs, nil
}
func makeJsonnetVm(sourceJsonnet v1alpha1.ApplicationSourceJsonnet) *jsonnet.VM {
vm := jsonnet.MakeVM()
for _, arg := range sourceJsonnet.TLAs {
if arg.Code {
vm.TLACode(arg.Name, arg.Value)
} else {
vm.TLAVar(arg.Name, arg.Value)
}
}
for _, extVar := range sourceJsonnet.ExtVars {
if extVar.Code {
vm.ExtCode(extVar.Name, extVar.Value)
} else {
vm.ExtVar(extVar.Name, extVar.Value)
}
}
return vm
}
func runCommand(command v1alpha1.Command, path string, env []string) (string, error) {
if len(command.Command) == 0 {
return "", fmt.Errorf("Command is empty")
}
cmd := exec.Command(command.Command[0], append(command.Command[1:], command.Args...)...)
cmd.Env = env
cmd.Dir = path
return executil.Run(cmd)
}
func findPlugin(plugins []*v1alpha1.ConfigManagementPlugin, name string) *v1alpha1.ConfigManagementPlugin {
for _, plugin := range plugins {
if plugin.Name == name {
return plugin
}
}
return nil
}
func runConfigManagementPlugin(appPath string, q *apiclient.ManifestRequest, creds git.Creds) ([]*unstructured.Unstructured, error) {
plugin := findPlugin(q.Plugins, q.ApplicationSource.Plugin.Name)
if plugin == nil {
return nil, fmt.Errorf("Config management plugin with name '%s' is not supported.", q.ApplicationSource.Plugin.Name)
}
env := append(os.Environ(), fmt.Sprintf("%s=%s", PluginEnvAppName, q.AppLabelValue), fmt.Sprintf("%s=%s", PluginEnvAppNamespace, q.Namespace))
if creds != nil {
closer, environ, err := creds.Environ()
if err != nil {
return nil, err
}
defer func() { _ = closer.Close() }()
env = append(env, environ...)
}
env = append(env, q.ApplicationSource.Plugin.Env.Environ()...)
if plugin.Init != nil {
_, err := runCommand(*plugin.Init, appPath, env)
if err != nil {
return nil, err
}
}
out, err := runCommand(plugin.Generate, appPath, env)
if err != nil {
return nil, err
}
return kube.SplitYAML(out)
}
func (s *Service) GetAppDetails(ctx context.Context, q *apiclient.RepoServerAppDetailsQuery) (*apiclient.RepoAppDetailsResponse, error) {
res := &apiclient.RepoAppDetailsResponse{}
getCached := func(revision string) bool {
err := s.cache.GetAppDetails(revision, q.Source, &res)
if err == nil {
log.Infof("app details cache hit: %s/%s", revision, q.Source.Path)
return true
} else {
if err != cache.ErrCacheMiss {
log.Warnf("app details cache error %s: %v", revision, q.Source)
} else {
log.Infof("app details cache miss: %s/%s", revision, q.Source)
}
}
return false
}
err := s.runRepoOperation(ctx, q.Source.TargetRevision, q.Repo, q.Source, getCached, func(appPath string, revision string) error {
appSourceType, err := GetAppSourceType(q.Source, appPath)
if err != nil {
return err
}
res.Type = string(appSourceType)
switch appSourceType {
case v1alpha1.ApplicationSourceTypeKsonnet:
var ksonnetAppSpec apiclient.KsonnetAppSpec
data, err := ioutil.ReadFile(filepath.Join(appPath, "app.yaml"))
if err != nil {
return err
}
err = yaml.Unmarshal(data, &ksonnetAppSpec)
if err != nil {
return err
}
ksApp, err := ksonnet.NewKsonnetApp(appPath)
if err != nil {
return status.Errorf(codes.FailedPrecondition, "unable to load application from %s: %v", appPath, err)
}
env := ""
if q.Source.Ksonnet != nil {
env = q.Source.Ksonnet.Environment
}
params, err := ksApp.ListParams(env)
if err != nil {
return err
}
ksonnetAppSpec.Parameters = params
res.Ksonnet = &ksonnetAppSpec
case v1alpha1.ApplicationSourceTypeHelm:
res.Helm = &apiclient.HelmAppSpec{}
files, err := ioutil.ReadDir(appPath)
if err != nil {
return err
}
for _, f := range files {
if f.IsDir() {
continue
}
fName := f.Name()
if strings.Contains(fName, "values") && (filepath.Ext(fName) == ".yaml" || filepath.Ext(fName) == ".yml") {
res.Helm.ValueFiles = append(res.Helm.ValueFiles, fName)
}
}
h, err := helm.NewHelmApp(appPath, getHelmRepos(q.Repos))
if err != nil {
return err
}
defer h.Dispose()
err = h.Init()
if err != nil {
return err
}
valuesPath := filepath.Join(appPath, "values.yaml")
info, err := os.Stat(valuesPath)
if err == nil && !info.IsDir() {
bytes, err := ioutil.ReadFile(valuesPath)
if err != nil {
return err
}
res.Helm.Values = string(bytes)
}
params, err := h.GetParameters(valueFiles(q))
if err != nil {
return err
}
for k, v := range params {
res.Helm.Parameters = append(res.Helm.Parameters, &v1alpha1.HelmParameter{
Name: k,
Value: v,
})
}
case v1alpha1.ApplicationSourceTypeKustomize:
res.Kustomize = &apiclient.KustomizeAppSpec{}
k := kustomize.NewKustomizeApp(appPath, q.Repo.GetGitCreds(), q.Repo.Repo)
_, images, err := k.Build(nil, q.KustomizeOptions)
if err != nil {
return err
}
res.Kustomize.Images = images
}
_ = s.cache.SetAppDetails(revision, q.Source, res)
return nil
}, operationSettings{})
return res, err
}
func (s *Service) GetRevisionMetadata(ctx context.Context, q *apiclient.RepoServerRevisionMetadataRequest) (*v1alpha1.RevisionMetadata, error) {
if !git.IsCommitSHA(q.Revision) {
return nil, fmt.Errorf("revision %s must be resolved", q.Revision)
}
metadata, err := s.cache.GetRevisionMetadata(q.Repo.Repo, q.Revision)
if err == nil {
log.Infof("revision metadata cache hit: %s/%s", q.Repo.Repo, q.Revision)
return metadata, nil
} else {
if err != cache.ErrCacheMiss {
log.Warnf("revision metadata cache error %s/%s: %v", q.Repo.Repo, q.Revision, err)
} else {
log.Infof("revision metadata cache miss: %s/%s", q.Repo.Repo, q.Revision)
}
}
gitClient, _, err := s.newClientResolveRevision(q.Repo, q.Revision)
if err != nil {
return nil, err
}
_, err = checkoutRevision(gitClient, q.Revision)
if err != nil {
return nil, err
}
m, err := gitClient.RevisionMetadata(q.Revision)
if err != nil {
return nil, err
}
// discard anything after the first new line and then truncate to 64 chars
message := text.Trunc(strings.SplitN(m.Message, "\n", 2)[0], 64)
metadata = &v1alpha1.RevisionMetadata{Author: m.Author, Date: metav1.Time{Time: m.Date}, Tags: m.Tags, Message: message}
_ = s.cache.SetRevisionMetadata(q.Repo.Repo, q.Revision, metadata)
return metadata, nil
}
func valueFiles(q *apiclient.RepoServerAppDetailsQuery) []string {
if q.Source.Helm == nil {
return nil
}
return q.Source.Helm.ValueFiles
}
func (s *Service) newClient(repo *v1alpha1.Repository) (git.Client, error) {
gitClient, err := s.newGitClient(repo.Repo, repo.GetGitCreds(), repo.IsInsecure(), repo.EnableLFS)
if err != nil {
return nil, err
}
return metrics.WrapGitClient(repo.Repo, s.metricsServer, gitClient), nil
}
// 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) {
gitClient, err := s.newClient(repo)
if err != nil {
return nil, "", err
}
commitSHA, err := gitClient.LsRemote(revision)
if err != nil {
return nil, "", err
}
return gitClient, commitSHA, 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 (s *Service) GetHelmCharts(ctx context.Context, q *apiclient.HelmChartsRequest) (*apiclient.HelmChartsResponse, error) {
index, err := s.newHelmClient(q.Repo.Repo, q.Repo.GetHelmCreds()).GetIndex()
if err != nil {
return nil, err
}
res := apiclient.HelmChartsResponse{}
for chartName, entries := range index.Entries {
chart := apiclient.HelmChart{
Name: chartName,
}
for _, entry := range entries {
chart.Versions = append(chart.Versions, entry.Version)
}
res.Items = append(res.Items, &chart)
}
return &res, nil
}