-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
repository.go
247 lines (221 loc) · 6.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
package repository
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
"strings"
"time"
log "github.com/sirupsen/logrus"
"github.com/argoproj/argo-cd/common"
"github.com/argoproj/argo-cd/util"
"github.com/argoproj/argo-cd/util/cache"
"github.com/argoproj/argo-cd/util/git"
ksutil "github.com/argoproj/argo-cd/util/ksonnet"
"github.com/argoproj/argo-cd/util/kube"
)
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
}
// NewService returns a new instance of the Manifest service
func NewService(gitFactory git.ClientFactory, cache cache.Cache) *Service {
return &Service{
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) {
appRepoPath := tempRepoPath(q.Repo.Repo)
s.repoLock.Lock(appRepoPath)
defer s.repoLock.Unlock(appRepoPath)
gitClient := s.gitFactory.NewClient(q.Repo.Repo, appRepoPath, q.Repo.Username, q.Repo.Password, q.Repo.SSHPrivateKey)
err := gitClient.Init()
if err != nil {
return nil, err
}
commitSHA, err := gitClient.LsRemote(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
}
err = checkoutRevision(gitClient, q.Revision)
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: cacheKey,
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) {
appRepoPath := tempRepoPath(q.Repo.Repo)
s.repoLock.Lock(appRepoPath)
defer s.repoLock.Unlock(appRepoPath)
gitClient := s.gitFactory.NewClient(q.Repo.Repo, appRepoPath, q.Repo.Username, q.Repo.Password, q.Repo.SSHPrivateKey)
err := gitClient.Init()
if err != nil {
return nil, err
}
err = checkoutRevision(gitClient, q.Revision)
if err != nil {
return nil, err
}
data, err := ioutil.ReadFile(path.Join(gitClient.Root(), q.Path))
if err != nil {
return nil, err
}
res := GetFileResponse{
Data: data,
}
return &res, nil
}
func (s *Service) GenerateManifest(c context.Context, q *ManifestRequest) (*ManifestResponse, error) {
var res ManifestResponse
if git.IsCommitSHA(q.Revision) {
cacheKey := manifestCacheKey(q.Revision, q)
err := s.cache.Get(cacheKey, &res)
if err == nil {
log.Infof("manifest cache hit: %s", cacheKey)
return &res, nil
}
}
appRepoPath := tempRepoPath(q.Repo.Repo)
s.repoLock.Lock(appRepoPath)
defer s.repoLock.Unlock(appRepoPath)
gitClient := s.gitFactory.NewClient(q.Repo.Repo, appRepoPath, q.Repo.Username, q.Repo.Password, q.Repo.SSHPrivateKey)
err := gitClient.Init()
if err != nil {
return nil, err
}
commitSHA, err := gitClient.LsRemote(q.Revision)
if err != nil {
return nil, err
}
cacheKey := manifestCacheKey(commitSHA, q)
err = s.cache.Get(cacheKey, &res)
if err == nil {
log.Infof("manifest cache hit: %s", cacheKey)
return &res, nil
}
if err != cache.ErrCacheMiss {
log.Warnf("manifest cache error %s: %v", cacheKey, err)
} else {
log.Infof("manifest cache miss: %s", cacheKey)
}
err = checkoutRevision(gitClient, q.Revision)
if err != nil {
return nil, err
}
appPath := path.Join(appRepoPath, q.Path)
ksApp, err := ksutil.NewKsonnetApp(appPath)
if err != nil {
return nil, fmt.Errorf("unable to load application from %s: %v", appPath, err)
}
params, err := ksApp.ListEnvParams(q.Environment)
if err != nil {
return nil, fmt.Errorf("Failed to list ksonnet app params: %v", err)
}
if q.ComponentParameterOverrides != nil {
for _, override := range q.ComponentParameterOverrides {
err = ksApp.SetComponentParams(q.Environment, override.Component, override.Name, override.Value)
if err != nil {
return nil, err
}
}
}
appSpec := ksApp.App()
env, err := appSpec.Environment(q.Environment)
if err != nil {
return nil, fmt.Errorf("environment '%s' does not exist in ksonnet app", q.Environment)
}
targetObjs, err := ksApp.Show(q.Environment)
if err != nil {
return nil, err
}
manifests := make([]string, len(targetObjs))
for i, target := range targetObjs {
if q.AppLabel != "" {
err = kube.SetLabel(target, common.LabelApplicationName, q.AppLabel)
if err != nil {
return nil, err
}
}
manifestStr, err := json.Marshal(target.Object)
if err != nil {
return nil, err
}
manifests[i] = string(manifestStr)
}
res = ManifestResponse{
Revision: commitSHA,
Manifests: manifests,
Namespace: env.Destination.Namespace,
Server: env.Destination.Server,
Params: params,
}
err = s.cache.Set(&cache.Item{
Key: cacheKey,
Object: &res,
Expiration: DefaultRepoCacheExpiration,
})
if err != nil {
log.Warnf("manifest cache set error %s: %v", cacheKey, err)
}
return &res, nil
}
// tempRepoPath returns a formulated temporary directory location to clone a repository
func tempRepoPath(repo string) string {
return path.Join(os.TempDir(), strings.Replace(repo, "/", "_", -1))
}
// checkoutRevision is a convenience function to initialize a repo, fetch, and checkout a revision
func checkoutRevision(gitClient git.Client, revision string) error {
err := gitClient.Fetch()
if err != nil {
return err
}
err = gitClient.Reset()
if err != nil {
log.Warn(err)
}
err = gitClient.Checkout(revision)
if err != nil {
return err
}
return nil
}
func manifestCacheKey(commitSHA string, q *ManifestRequest) string {
pStr, _ := json.Marshal(q.ComponentParameterOverrides)
return fmt.Sprintf("mfst|%s|%s|%s|%s", q.Path, q.Environment, commitSHA, string(pStr))
}
func listDirCacheKey(commitSHA string, q *ListDirRequest) string {
return fmt.Sprintf("ldir|%s|%s", q.Path, commitSHA)
}