-
Notifications
You must be signed in to change notification settings - Fork 7
/
git.go
411 lines (365 loc) · 10.7 KB
/
git.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
package main
import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
"time"
"github.com/go-git/go-billy/v5"
"github.com/go-git/go-billy/v5/memfs"
"github.com/go-git/go-billy/v5/osfs"
git "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/cache"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/go-git/go-git/v5/plumbing/transport"
"github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/go-git/go-git/v5/storage"
"github.com/go-git/go-git/v5/storage/filesystem"
"github.com/go-git/go-git/v5/storage/memory"
"golang.org/x/xerrors"
yaml "gopkg.in/yaml.v2"
"sigs.k8s.io/kustomize/api/types"
)
// GitOperator is our wrapper aroud go-git to do GitOps, and
// tagging commits to correlate them with the container image tags.
type GitOperator struct {
auth transport.AuthMethod
// repo is the remote repository that contains the gitops config
// we are going to modify, or the kanvas config we are going to use for deployment.
//
// It needs to be in the form of "https://github.com/owner/repo.git",
// not "owner/repo" or "repo".
repo string
repository *git.Repository
username string
defaultBranch string
// gitRoot is the root of the local git repository, used to
// clone and checkout the remote repository that contains the gitops config
// or the kustomize config we are going to modify.
// If empty, we will use in-memory filesystem.
gitRoot string
}
func CreateGitOperatorInstance(username, token, repo, defaultBranch, gitRoot string) (g GitOperator) {
g.auth = &http.BasicAuth{
Username: username, // yes, this can be anything except an empty string
Password: token,
}
g.repo = repo
g.username = username
g.defaultBranch = defaultBranch
g.gitRoot = gitRoot
if err := g.Clone(); err != nil {
fmt.Println("[ERROR] Failed to Clone: ", xerrors.New(err.Error()))
}
return
}
// getLocalRepoRoot returns the path from the gocat's current working directory
// to the root of the local git repository.
//
// The caller needs to be aware that the returned path can be either an absolute path
// or a relative path.
//
// The returned path is relative to the gocat's current working directory,
// if gitRoot is not an absolute path.
//
// In case you run gocat like this:
//
// GOCAT_GITROOT=path/to/gitroot gocat ...
//
// it results in getLocalRepoRoot() returning "path/to/gitroot/$host/$owner/$repo".
//
// In case you run gocat like this:
//
// GOCAT_GITROOT=/path/to/gitroot gocat ...
//
// it returns "/path/to/gitroot/$host/$owner/$repo".
//
// If gitRoot is empty, which means we are using in-memory filesystem,
// we will return an empty string.
func (g *GitOperator) getLocalRepoRoot() string {
if g.gitRoot != "" {
// Without this modification, we will end up with a nested directory structure like this:
//
// - $GOCAT_GITROOT
// - https:
// - github.com
// - zaiminc
// - gocat.git
//
// which is not what we want.
//
// Instead, we want:
//
// - $GOCAT_GITROOT
// - github.com
// - zaiminc
// - gocat
repo := strings.ReplaceAll(g.repo, "https://", "")
repo = strings.ReplaceAll(repo, "/", string(os.PathSeparator))
repo = strings.TrimSuffix(repo, ".git")
return filepath.Join(g.gitRoot, repo)
}
return ""
}
func (g *GitOperator) Clone() error {
var (
storage storage.Storer
fs billy.Filesystem
)
if g.gitRoot != "" {
repoRoot := g.getLocalRepoRoot()
fs = osfs.New(repoRoot)
storage = filesystem.NewStorage(
osfs.New(filepath.Join(repoRoot, ".git")),
cache.NewObjectLRUDefault(),
)
} else {
storage = memory.NewStorage()
fs = memfs.New()
}
r, err := git.Clone(storage, fs, &git.CloneOptions{
URL: g.Repo(),
Auth: g.auth,
})
g.repository = r
return err
}
func (g GitOperator) Clean() error {
if p := g.getLocalRepoRoot(); p != "" {
// Do our best not to delete unrelated and unintended files!
//
// If there's a .git directory, it is more likely a git repository created gocat,
// so we can safely delete it.
dotGit := filepath.Join(p, ".git")
if _, err := os.Stat(dotGit); err != nil {
return fmt.Errorf("unable to stat %s: %w", dotGit, err)
}
if err := os.RemoveAll(p); err != nil {
return fmt.Errorf("unable to remove %s: %w", p, err)
}
}
return nil
}
func (g GitOperator) Repo() string {
return g.repo
}
func (g GitOperator) DeleteBranch(branch string) (err error) {
return g.repository.Storer.RemoveReference(plumbing.ReferenceName(branch))
}
func (g GitOperator) PushDockerImageTag(id string, phase DeployPhase, tag string, targetTag string) (branch string, err error) {
branch = fmt.Sprintf("bot/docker-image-tag-%s-%s-%s", id, phase.Name, tag)
w, err := g.createAndCheckoutNewBranch(branch)
if err != nil {
return "", err
}
err = g.commit(w, phase.Path, KustomizationOverWrite{tag, targetTag})
if err != nil {
fmt.Println("[ERROR] Failed to Marshal kustomize.yaml: ", xerrors.New(err.Error()))
return
}
err = g.commit(w, strings.Replace(phase.Path, "kustomization.yaml", "configmap.yaml", -1), MemcachedOverWrite{})
if err != nil {
fmt.Println("[ERROR] Failed to Write MEMCACHED_PREFIX \\n: ", xerrors.New(err.Error()))
return
}
err = g.verify(w)
if err != nil {
return
}
hash, _ := w.Commit(
fmt.Sprintf("Change docker image tag. target: %s, phase: %s, tag: %s.", phase.Path, phase.Name, tag),
&git.CommitOptions{
Author: &object.Signature{
Name: g.username,
Email: "",
When: time.Now(),
},
})
if err := g.repository.Storer.SetReference(plumbing.NewReferenceFromStrings(branch, hash.String())); err != nil {
fmt.Println("[ERROR] Failed to SetReference: ", xerrors.New(err.Error()))
return "", err
}
// push
remote, err := g.repository.Remote("origin")
if err != nil {
fmt.Println("[ERROR] Failed to Add remote origin: ", xerrors.New(err.Error()))
return
}
err = remote.Push(&git.PushOptions{
Progress: os.Stdout,
RefSpecs: []config.RefSpec{
config.RefSpec(plumbing.ReferenceName(branch) + ":" + plumbing.ReferenceName(fmt.Sprintf("refs/heads/%s", branch))),
},
Auth: g.auth,
})
if err != nil {
fmt.Println("[ERROR] Failed to Push origin: ", xerrors.New(err.Error()))
}
return
}
func (g GitOperator) checkoutMainBranch() (*git.Worktree, error) {
w, err := g.repository.Worktree()
if err != nil {
return nil, err
}
refName := plumbing.Master
if g.defaultBranch != "" {
refName = plumbing.ReferenceName(g.defaultBranch)
}
if err := w.Checkout(&git.CheckoutOptions{
Create: false,
Branch: refName,
}); err != nil {
fmt.Println("[ERROR] Failed to Checkout master: ", xerrors.New(err.Error()))
return nil, err
}
if err := w.Pull(&git.PullOptions{RemoteName: "origin", Auth: g.auth}); err != nil && err != git.NoErrAlreadyUpToDate {
fmt.Println("[ERROR] Failed to Pull origin/master: ", xerrors.New(err.Error()))
fmt.Println("[INFO] Running Clone to see if it fixes the issue")
if err := g.Clone(); err != nil {
fmt.Println("[ERROR] Failed to Clone: ", xerrors.New(err.Error()))
}
}
return w, nil
}
func (g GitOperator) createAndCheckoutNewBranch(branch string) (*git.Worktree, error) {
if err := g.DeleteBranch(branch); err != nil {
fmt.Println("[ERROR] Failed to DeleteBranch: ", xerrors.New(err.Error()))
}
// checkout
w, err := g.checkoutMainBranch()
if err != nil {
return nil, err
}
err = w.Checkout(&git.CheckoutOptions{
Create: true,
Branch: plumbing.ReferenceName(branch),
})
if err != nil {
fmt.Println("[ERROR] Failed to Checkout workbranch: ", xerrors.New(err.Error()))
return nil, err
}
return w, nil
}
type ConfigMap struct {
APIVersion string `yaml:"apiVersion"`
Kind string `yaml:"kind"`
Metadata map[string]string `yaml:"metadata"`
Data map[string]string `yaml:"data"`
}
type OverWrite interface {
Update([]byte) (interface{}, error)
}
func (g GitOperator) verify(w *git.Worktree) (err error) {
status, err := w.Status()
if err != nil {
fmt.Println("[ERROR] Failed to get status: ", xerrors.New(err.Error()))
return
}
for path, status := range status {
if status.Staging != git.Modified {
fmt.Printf("[ERROR] There are some extra file updates. File: %v %s", status, path)
return xerrors.New("There are some extra file updates")
}
}
return nil
}
func (g GitOperator) commit(w *git.Worktree, targetFilePath string, o OverWrite) (err error) {
_, err = w.Filesystem.Stat(targetFilePath)
if err != nil {
fmt.Println("[INFO] The file does not exist: ", xerrors.New(err.Error()))
return nil
}
file, err := w.Filesystem.Open(targetFilePath)
if err != nil {
fmt.Println("[ERROR] Failed to Open file: ", xerrors.New(err.Error()))
return
}
b, err := io.ReadAll(file)
if err != nil {
fmt.Println("[ERROR] Failed to ReadAll file: ", xerrors.New(err.Error()))
return
}
err = file.Close()
if err != nil {
fmt.Println("[ERROR] Failed to Close file: ", xerrors.New(err.Error()))
return
}
obj, err := o.Update(b)
if err != nil {
return
}
rb, err := yaml.Marshal(&obj)
if err != nil {
fmt.Println("[ERROR] Failed to Marshal kustomize.yaml: ", xerrors.New(err.Error()))
return
}
err = w.Filesystem.Remove(targetFilePath)
if err != nil {
fmt.Println("[ERROR] Failed to Remove kustomize.yaml: ", xerrors.New(err.Error()))
return
}
file, err = w.Filesystem.OpenFile(targetFilePath, os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
fmt.Println("[ERROR] Failed to Open kustomize.yaml: ", xerrors.New(err.Error()))
return
}
_, err = file.Write(rb)
if err != nil {
fmt.Println("[ERROR] Failed to Write kustomize.yaml: ", xerrors.New(err.Error()))
return
}
_, err = file.Write([]byte("\n"))
if err != nil {
fmt.Println("[ERROR] Failed to Write \\n: ", xerrors.New(err.Error()))
return
}
// git add
_, err = w.Add(targetFilePath)
if err != nil {
fmt.Println("[ERROR] Failed to Add file to Worktree: ", xerrors.New(err.Error()))
return
}
return
}
type KustomizationOverWrite struct {
tag string
targetTag string
}
func (o KustomizationOverWrite) Update(b []byte) (interface{}, error) {
obj := types.Kustomization{}
err := yaml.Unmarshal([]byte(b), &obj)
if err != nil {
return nil, err
}
updated := false
for i, image := range obj.Images {
if image.Name == o.targetTag {
obj.Images[i].NewTag = o.tag
updated = true
}
}
if !updated {
obj.Images = append(obj.Images, types.Image{
Name: o.targetTag,
NewTag: o.tag,
})
}
return obj, nil
}
type MemcachedOverWrite struct {
}
func (o MemcachedOverWrite) Update(b []byte) (interface{}, error) {
obj := ConfigMap{}
err := yaml.Unmarshal([]byte(b), &obj)
if err != nil {
return nil, err
}
if _, ok := obj.Data["MEMCACHED_PREFIX"]; ok {
obj.Data["MEMCACHED_PREFIX"] = time.Now().Format("2006-01-02T15:04:05")
}
return obj, nil
}