/
k3s.go
760 lines (642 loc) · 21.6 KB
/
k3s.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
package k3s
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"text/template"
"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/transport/ssh"
"github.com/google/go-github/v39/github"
ecmConfig "github.com/rancher/ecm-distro-tools/cmd/release/config"
ecmExec "github.com/rancher/ecm-distro-tools/exec"
"github.com/rancher/ecm-distro-tools/release"
"github.com/rancher/ecm-distro-tools/repository"
ssh2 "golang.org/x/crypto/ssh"
"golang.org/x/mod/semver"
"gopkg.in/yaml.v2"
)
const (
k3sRepo = "k3s"
k8sUpstreamURL = "https://github.com/kubernetes/kubernetes"
rancherRemote = "k3s-io"
k8sRancherURL = "git@github.com:k3s-io/kubernetes.git"
k8sUserURL = "git@github.com:user/kubernetes.git"
k3sUpstreamRepoURL = "https://github.com/k3s-io/k3s"
gitconfig = `[safe]
directory = /home/go/src/kubernetes
[user]
email = %email%
name = %user%`
dockerDevImage = `FROM %goimage%
RUN apk add --no-cache bash git make tar gzip curl git coreutils rsync alpine-sdk binutils-gold
ARG UID=1000
ARG GID=1000
RUN addgroup -S -g $GID ecmgroup && adduser -S -G ecmgroup -u $UID user
USER user`
updateK3sScriptName = "update_k3s_references.sh"
updateK3sReferencesScript = `#!/bin/bash
set -ex
OS=$(uname -s)
DRY_RUN={{ .K3s.DryRun }}
BRANCH_NAME={{ .K3s.NewK8sVersion }}-{{ .K3s.NewSuffix }}
cd {{ .K3s.Workspace }}
# using ls | grep is not a good idea because it doesn't support non-alphanumeric filenames, but since we're only ever checking 'k3s' it isn't a problem https://www.shellcheck.net/wiki/SC2010
ls | grep -w k3s || git clone "git@github.com:{{ .User.GithubUsername }}/k3s.git"
cd {{ .K3s.Workspace }}/k3s
git remote -v | grep -w upstream || git remote add upstream {{ .K3s.K3sUpstreamURL }}
git fetch upstream
git stash
git branch -D "${BRANCH_NAME}" &>/dev/null || true
git checkout -B "${BRANCH_NAME}" upstream/{{.K3s.ReleaseBranch}}
git clean -xfd
case ${OS} in
Darwin)
sed -Ei '' "\|github.com/k3s-io/kubernetes| s|{{ replaceAll .K3s.OldK8sVersion "." "\\." }}-{{ .K3s.OldSuffix }}|{{ replaceAll .K3s.NewK8sVersion "." "\\." }}-{{ .K3s.NewSuffix }}|" go.mod
sed -Ei '' "s/k8s.io\/kubernetes v\S+/k8s.io\/kubernetes {{ replaceAll .K3s.NewK8sVersion "." "\\." }}/" go.mod
sed -Ei '' "s/{{ replaceAll .K3s.OldK8sClient "." "\\." }}/{{ replaceAll .K3s.NewK8sClient "." "\\." }}/g" go.mod # This should only change ~6 lines in go.mod
sed -Ei '' "s/golang:.*-/golang:{{ .K3s.NewGoVersion }}-/g" Dockerfile.*
sed -Ei '' "s/go-version:.*$/go-version:\ '{{ .K3s.NewGoVersion }}'/g" .github/workflows/integration.yaml .github/workflows/unitcoverage.yaml
;;
Linux)
sed -Ei "\|github.com/k3s-io/kubernetes| s|{{ replaceAll .K3s.OldK8sVersion "." "\\." }}-{{ .K3s.OldSuffix }}|{{ replaceAll .K3s.NewK8sVersion "." "\\." }}-{{ .K3s.NewSuffix }}|" go.mod
sed -Ei "s/k8s.io\/kubernetes v\S+/k8s.io\/kubernetes {{ replaceAll .K3s.NewK8sVersion "." "\\." }}/" go.mod
sed -Ei "s/{{ replaceAll .K3s.OldK8sClient "." "\\." }}/{{ replaceAll .K3s.NewK8sClient "." "\\." }}/g" go.mod # This should only change ~6 lines in go.mod
sed -Ei "s/golang:.*-/golang:{{ .K3s.NewGoVersion }}-/g" Dockerfile.*
sed -Ei "s/go-version:.*$/go-version:\ '{{ .K3s.NewGoVersion }}'/g" .github/workflows/integration.yaml .github/workflows/unitcoverage.yaml
;;
*)
>&2 echo "$(OS) not supported yet"
exit 1
;;
esac
go mod tidy
git add go.mod go.sum Dockerfile.* .github/workflows/integration.yaml .github/workflows/unitcoverage.yaml
git commit --signoff -m "Update to {{ .K3s.NewK8sVersion }}"
if [ "${DRY_RUN}" = false ]; then
git push --set-upstream origin "${BRANCH_NAME}" # run git remote -v for your origin
fi`
)
type UpdateScriptVars struct {
K3s *ecmConfig.K3sRelease
User *ecmConfig.User
}
// GenerateTags will clone the kubernetes repository, rebase it with the k3s-io fork and
// generate tags to be pushed
func GenerateTags(ctx context.Context, ghClient *github.Client, r *ecmConfig.K3sRelease, u *ecmConfig.User, sshKeyPath string) error {
fmt.Println("setting up k8s remotes")
if err := setupK8sRemotes(ghClient, r, u, sshKeyPath); err != nil {
return errors.New("failed to clone and setup remotes for k8s repos: " + err.Error())
}
tagsExists, err := tagsFileExists(r)
if err != nil {
return errors.New("failed to verify if tags file already exists: " + err.Error())
}
if tagsExists {
return errors.New("tag file already exists, skipping rebase and tag")
}
fmt.Println("rebasing and tagging")
tags, err := rebaseAndTag(ghClient, r, u)
if err != nil {
return errors.New("failed to rebase and tag: " + err.Error())
}
fmt.Println("successfully rebased and tagged")
return writeTagsFile(r, tags)
}
func writeTagsFile(r *ecmConfig.K3sRelease, tags []string) error {
tagFile := filepath.Join(r.Workspace, "tags-"+r.NewK8sVersion)
return os.WriteFile(tagFile, []byte(strings.Join(tags, "\n")), 0644)
}
// setupK8sRemotes will clone the kubernetes upstream repo and proceed with setting up remotes
// for rancher and user's forks, then it will fetch branches and tags for all remotes
func setupK8sRemotes(ghClient *github.Client, r *ecmConfig.K3sRelease, u *ecmConfig.User, sshKeyPath string) error {
k8sDir := filepath.Join(r.Workspace, "kubernetes")
fmt.Println("verifying if the k8s dir already exists: " + k8sDir)
if _, err := os.Stat(r.Workspace); err != nil {
if !os.IsNotExist(err) {
return err
}
fmt.Println("dir doesn't exists, creating")
if err := os.MkdirAll(r.Workspace, 0755); err != nil {
return err
}
}
// clone the repo
fmt.Println("cloning the repo")
repo, err := git.PlainClone(k8sDir, false, &git.CloneOptions{
URL: k8sUpstreamURL,
Progress: os.Stdout,
InsecureSkipTLS: true,
})
if err != nil {
if err != git.ErrRepositoryAlreadyExists {
return err
}
fmt.Println("repo already exists, opening it")
repo, err = git.PlainOpen(k8sDir)
if err != nil {
return err
}
}
fmt.Println("getting ssh auth")
gitAuth, err := getAuth(sshKeyPath)
if err != nil {
return err
}
fmt.Println("fetching remote: origin")
if err := repo.Fetch(&git.FetchOptions{
RemoteName: "origin",
Progress: os.Stdout,
Tags: git.AllTags,
InsecureSkipTLS: true,
}); err != nil {
if err != git.NoErrAlreadyUpToDate {
return err
}
}
fmt.Println("creating remote: '" + r.K3sRepoOwner + " " + r.K8sRancherURL + "'")
if _, err := repo.CreateRemote(&config.RemoteConfig{
Name: r.K3sRepoOwner,
URLs: []string{r.K8sRancherURL},
}); err != nil {
if err != git.ErrRemoteExists {
return err
}
}
fmt.Println("fetching remote: " + r.K3sRepoOwner)
if err := repo.Fetch(&git.FetchOptions{
RemoteName: r.K3sRepoOwner,
Progress: os.Stdout,
Tags: git.AllTags,
Auth: gitAuth,
}); err != nil {
if err != git.NoErrAlreadyUpToDate {
return err
}
}
userRemoteURL := strings.Replace(k8sUserURL, "user", u.GithubUsername, -1)
fmt.Println("creating remote: '" + u.GithubUsername + " " + userRemoteURL + "'")
if _, err := repo.CreateRemote(&config.RemoteConfig{
Name: u.GithubUsername,
URLs: []string{userRemoteURL},
}); err != nil {
if err != git.ErrRemoteExists {
return err
}
}
fmt.Println("fetching remote: " + u.GithubUsername)
if err := repo.Fetch(&git.FetchOptions{
RemoteName: u.GithubUsername,
Progress: os.Stdout,
Tags: git.AllTags,
Auth: gitAuth,
}); err != nil {
if err != git.NoErrAlreadyUpToDate {
return err
}
}
return nil
}
func rebaseAndTag(ghClient *github.Client, r *ecmConfig.K3sRelease, u *ecmConfig.User) ([]string, error) {
rebaseOut, err := gitRebaseOnto(r)
if err != nil {
return nil, err
}
fmt.Println(rebaseOut)
wrapperImageTag, err := buildGoWrapper(r)
if err != nil {
return nil, err
}
// setup gitconfig
gitconfigFile, err := setupGitArtifacts(r, u)
if err != nil {
return nil, err
}
// make sure that tag doesnt exist first
tagExists, err := isTagExists(r)
if err != nil {
return nil, err
}
if tagExists {
fmt.Println("tag exists, removing it")
if err := removeExistingTags(r); err != nil {
return nil, err
}
}
out, err := runTagScript(r, gitconfigFile, wrapperImageTag)
if err != nil {
return nil, err
}
tags := tagPushLines(out)
if len(tags) == 0 {
return nil, errors.New("failed to extract tag push lines")
}
return tags, nil
}
// getAuth is a utility function which is used to get the ssh authentication method for connecting to an ssh server.
// the function takes a single parameter, privateKey, which is a string representing the path to a private key file.
// If the privateKey is an empty string, the function uses the default private key located at $HOME/.ssh/id_rsa.
// The function then creates a new ssh.AuthMethod using the ssh.NewPublicKeysFromFile function, passing in the "git" user, the privateKey path, and an empty password.
// If this returns an error, the function returns nil and the error.
// Finally, the function returns the publicKeys variable, which is now an ssh.AuthMethod, and a nil error.
func getAuth(privateKey string) (ssh.AuthMethod, error) {
if privateKey == "" {
privateKey = fmt.Sprintf("%s/.ssh/id_rsa", os.Getenv("HOME"))
}
publicKeys, err := ssh.NewPublicKeysFromFile("git", privateKey, "")
if err != nil {
return nil, err
}
publicKeys.HostKeyCallback = ssh2.InsecureIgnoreHostKey()
return publicKeys, nil
}
func gitRebaseOnto(r *ecmConfig.K3sRelease) (string, error) {
dir := filepath.Join(r.Workspace, "kubernetes")
// clean kubernetes directory before rebase
fmt.Println("cleaning git repo: " + dir)
if err := cleanGitRepo(dir); err != nil {
return "", err
}
commandArgs := strings.Split(fmt.Sprintf("rebase --onto %s %s %s-k3s1~1",
r.NewK8sVersion,
r.OldK8sVersion,
r.OldK8sVersion), " ")
fmt.Println("git ", commandArgs)
return ecmExec.RunCommand(dir, "git", commandArgs...)
}
func goVersion(r *ecmConfig.K3sRelease) (string, error) {
var dep map[string]interface{}
depFile := filepath.Join(r.Workspace, "kubernetes", "build", "dependencies.yaml")
dat, err := os.ReadFile(depFile)
if err != nil {
return "", err
}
if err := yaml.Unmarshal(dat, &dep); err != nil {
return "", err
}
depList := dep["dependencies"].([]interface{})
for _, v := range depList {
item := v.(map[interface{}]interface{})
itemName := item["name"]
if itemName == "golang: upstream version" {
version := item["version"]
return version.(string), nil
}
}
return "", errors.New("can not find Go dependency")
}
func buildGoWrapper(r *ecmConfig.K3sRelease) (string, error) {
fmt.Println("getting go version for k8s")
goVersion, err := goVersion(r)
if err != nil {
return "", err
}
goImageVersion := fmt.Sprintf("golang:%s-alpine", goVersion)
fmt.Println("go image version: " + goImageVersion)
devDockerfile := strings.ReplaceAll(dockerDevImage, "%goimage%", goImageVersion)
fmt.Println("writing dockerfile")
if err := os.WriteFile(filepath.Join(r.Workspace, "dockerfile"), []byte(devDockerfile), 0644); err != nil {
return "", err
}
wrapperImageTag := goImageVersion + "-dev"
fmt.Println("building docker image")
if _, err := ecmExec.RunCommand(r.Workspace, "docker", "build", "-t", wrapperImageTag, "."); err != nil {
return "", err
}
return wrapperImageTag, nil
}
func setupGitArtifacts(r *ecmConfig.K3sRelease, u *ecmConfig.User) (string, error) {
gitconfigFile := filepath.Join(r.Workspace, ".gitconfig")
// setting up username and email for tagging purposes
fmt.Println("updating git config contents")
gitconfigFileContent := strings.ReplaceAll(gitconfig, "%email%", u.Email)
gitconfigFileContent = strings.ReplaceAll(gitconfigFileContent, "%user%", u.GithubUsername)
// disable gpg signing direct in .gitconfig
fmt.Println("disabling gpg signing")
if strings.Contains(gitconfigFileContent, "[commit]") {
gitconfigFileContent = strings.Replace(gitconfigFileContent, "gpgsign = true", "gpgsign = false", 1)
} else {
gitconfigFileContent += "[commit]\n\tgpgsign = false\n"
}
fmt.Println("writing .gitconfig at: " + gitconfigFile)
if err := os.WriteFile(gitconfigFile, []byte(gitconfigFileContent), 0644); err != nil {
return "", err
}
return gitconfigFile, nil
}
func runTagScript(r *ecmConfig.K3sRelease, gitConfigFile, wrapperImageTag string) (string, error) {
const containerK8sPath = "/home/go/src/kubernetes"
const containerGoCachePath = "/home/go/.cache"
uid := strconv.Itoa(os.Getuid())
gid := strconv.Itoa(os.Getgid())
gopath, err := ecmExec.RunCommand(r.Workspace, "go", "env", "GOPATH")
if err != nil {
return "", err
}
gopath = strings.Trim(gopath, "\n")
fmt.Println("gopath: " + gopath)
k8sDir := filepath.Join(r.Workspace, "kubernetes")
// prep the docker run command
args := []string{
"run",
"-u", uid + ":" + gid,
"-v", gopath + ":/home/go:rw",
"-v", gitConfigFile + ":/home/go/.gitconfig:rw",
"-v", k8sDir + ":" + containerK8sPath + ":rw",
"-v", gopath + "/.cache:" + containerGoCachePath + ":rw",
"-e", "HOME=/home/go",
"-e", "GOCACHE=" + containerGoCachePath,
"-w", containerK8sPath,
wrapperImageTag,
"./tag.sh", r.NewK8sVersion + "-k3s1",
}
fmt.Println("running tag script")
return ecmExec.RunCommand(k8sDir, "docker", args...)
}
func tagPushLines(out string) []string {
var tagCmds []string
fmt.Println("getting git push lines")
for _, line := range strings.Split(out, "\n") {
if strings.Contains(line, "git push $REMOTE") {
tagCmds = append(tagCmds, line)
}
}
return tagCmds
}
func tagsCmdsFromFile(r *ecmConfig.K3sRelease) ([]string, error) {
var tagCmds []string
tagFile := filepath.Join(r.Workspace, "tags-"+r.NewK8sVersion)
if _, err := os.Stat(tagFile); err != nil {
return nil, err
}
dat, err := os.ReadFile(tagFile)
if err != nil {
return nil, err
}
for _, line := range strings.Split(string(dat), "\n") {
if strings.Contains(line, "git push $REMOTE") {
tagCmds = append(tagCmds, line)
}
}
return tagCmds, nil
}
func PushTags(ghClient *github.Client, r *ecmConfig.K3sRelease, u *ecmConfig.User, sshKeyPath string) error {
tagsCmds, err := tagsCmdsFromFile(r)
if err != nil {
return errors.New("failed to extract tags from file: " + err.Error())
}
fmt.Println("setting up git artifacts")
gitConfigFile, err := setupGitArtifacts(r, u)
if err != nil {
return err
}
fmt.Println("opening git config file")
file, err := os.Open(gitConfigFile)
if err != nil {
return err
}
defer file.Close()
fmt.Println("reading git config")
cfg, err := config.ReadConfig(file)
if err != nil {
return err
}
fmt.Println("opening kubernetes repo")
repo, err := git.PlainOpen(filepath.Join(r.Workspace, "kubernetes"))
if err != nil {
return err
}
fmt.Println("getting remote: " + u.GithubUsername)
userRemote, err := repo.Remote(u.GithubUsername)
if err != nil {
return err
}
fmt.Println("getting remote: origin")
originRemote, err := repo.Remote("origin")
if err != nil {
return err
}
fmt.Println("getting remote: " + r.K3sRepoOwner)
k3sRemote, err := repo.Remote(r.K3sRepoOwner)
if err != nil {
return errors.New("failed to find remote: '" + r.K3sRepoOwner + "' " + err.Error())
}
cfg.Remotes["origin"] = originRemote.Config()
cfg.Remotes[u.GithubUsername] = userRemote.Config()
cfg.Remotes[r.K3sRepoOwner] = k3sRemote.Config()
fmt.Println("setting remotes in the config")
if err := repo.SetConfig(cfg); err != nil {
return err
}
fmt.Println("getting ssh key auth")
gitAuth, err := getAuth(sshKeyPath)
if err != nil {
return err
}
fmt.Println("pushing tags")
for i, tagCmd := range tagsCmds {
tagCmdStr := tagCmd
tag := strings.Split(tagCmdStr, " ")[3]
fmt.Printf("pushing tag %d/%d: %s\n", i+1, len(tagsCmds), tag)
if r.DryRun {
fmt.Printf("\ndry run, skipping tag creation\n")
continue
}
if err := repo.Push(&git.PushOptions{
RemoteName: r.K3sRepoOwner,
Auth: gitAuth,
Progress: os.Stdout,
RefSpecs: []config.RefSpec{
config.RefSpec("+refs/tags/" + tag + ":refs/tags/" + tag),
},
}); err != nil {
if err != git.NoErrAlreadyUpToDate {
return errors.New("failed to push tag: " + err.Error())
}
}
}
return nil
}
func UpdateK3sReferences(ctx context.Context, ghClient *github.Client, r *ecmConfig.K3sRelease, u *ecmConfig.User) error {
if err := updateK3sReferencesAndPush(r, u); err != nil {
return err
}
if r.DryRun {
fmt.Println("dry run, skipping creating PR")
return nil
}
return createK3sReferencesPR(ctx, ghClient, r, u)
}
func updateK3sReferencesAndPush(r *ecmConfig.K3sRelease, u *ecmConfig.User) error {
fmt.Println("verifying if workspace dir exists")
if _, err := os.Stat(r.Workspace); err != nil {
if !os.IsNotExist(err) {
return err
}
fmt.Println("workspace dir doesn't exists, creating it")
if err := os.MkdirAll(r.Workspace, 0755); err != nil {
return err
}
}
fmt.Println("getting k8s go version")
goVersion, err := goVersion(r)
if err != nil {
return err
}
r.NewGoVersion = goVersion
funcMap := template.FuncMap{"replaceAll": strings.ReplaceAll}
fmt.Println("creating update k3s references script template")
scriptVars := UpdateScriptVars{K3s: r, User: u}
updateScriptOut, err := ecmExec.RunTemplatedScript(r.Workspace, updateK3sScriptName, updateK3sReferencesScript, funcMap, scriptVars)
if err != nil {
return err
}
fmt.Println(updateScriptOut)
return nil
}
func createK3sReferencesPR(ctx context.Context, ghClient *github.Client, r *ecmConfig.K3sRelease, u *ecmConfig.User) error {
const repo = "k3s"
pull := &github.NewPullRequest{
Title: github.String(fmt.Sprintf("Update to %s-%s and Go %s", r.NewK8sVersion, r.NewSuffix, r.NewGoVersion)),
Base: github.String(r.ReleaseBranch),
Head: github.String(u.GithubUsername + ":" + r.NewK8sVersion + "-" + r.NewSuffix),
MaintainerCanModify: github.Bool(true),
}
// creating a pr from your fork branch
_, _, err := ghClient.PullRequests.Create(ctx, r.K3sRepoOwner, repo, pull)
return err
}
func NewGithubClient(ctx context.Context, token string) (*github.Client, error) {
if token == "" {
return nil, errors.New("error: github token required")
}
return repository.NewGithub(ctx, token), nil
}
// tagsFileExists verify if there is a tags file at the release workspace
func tagsFileExists(r *ecmConfig.K3sRelease) (bool, error) {
tagFile := filepath.Join(r.Workspace, "tags-"+r.NewK8sVersion)
fmt.Println("verifying if tags file exists at: " + tagFile)
if _, err := os.Stat(tagFile); err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
return true, nil
}
func isTagExists(r *ecmConfig.K3sRelease) (bool, error) {
dir := filepath.Join(r.Workspace, "kubernetes")
fmt.Println("opening k8s repo: " + dir)
repo, err := git.PlainOpen(dir)
if err != nil {
return false, err
}
tag := r.NewK8sVersion + "-" + r.NewSuffix
fmt.Println("verifying if tag exists: " + tag)
if _, err := repo.Tag(tag); err != nil {
if err == git.ErrTagNotFound {
return false, nil
}
return false, errors.New("invalid tag " + tag + " object: " + err.Error())
}
return true, nil
}
func removeExistingTags(r *ecmConfig.K3sRelease) error {
dir := filepath.Join(r.Workspace, "kubernetes")
fmt.Println("opening k8s repo: " + dir)
repo, err := git.PlainOpen(dir)
if err != nil {
return err
}
fmt.Println("getting repo tags")
tagsIter, err := repo.Tags()
if err != nil {
return err
}
if err := tagsIter.ForEach(func(ref *plumbing.Reference) error {
if strings.Contains(ref.Name().String(), r.NewK8sVersion+"-"+r.NewSuffix) {
tagRefName := ref.Name().Short()
fmt.Println("tag ref found, deleting it: " + tagRefName)
if err := repo.DeleteTag(tagRefName); err != nil {
return err
}
}
return nil
}); err != nil {
return err
}
return nil
}
func cleanGitRepo(dir string) error {
fmt.Println("cleaning _output")
if _, err := ecmExec.RunCommand(dir, "rm", "-rf", "_output"); err != nil {
return err
}
fmt.Println("removing unwanted files")
if _, err := ecmExec.RunCommand(dir, "git", "clean", "-xfd"); err != nil {
return err
}
fmt.Println("git checkout .")
if _, err := ecmExec.RunCommand(dir, "git", "checkout", "."); err != nil {
return err
}
return nil
}
func CreateRelease(ctx context.Context, client *github.Client, r *ecmConfig.K3sRelease, opts *repository.CreateReleaseOpts, rc bool) error {
fmt.Println("validating tag")
if !semver.IsValid(opts.Tag) {
return errors.New("tag isn't a valid semver: " + opts.Tag)
}
name := r.NewK8sVersion + "+" + r.NewSuffix
oldName := r.OldK8sVersion + "+" + r.OldSuffix
latestRC, err := release.LatestRC(ctx, opts.Owner, opts.Repo, r.NewK8sVersion, r.NewSuffix, client)
if err != nil {
return err
}
if latestRC == nil && !rc {
return errors.New("couldn't find the latest RC")
}
if rc {
latestRCNumber := 1
if latestRC != nil {
trimmedRCNumber, _, found := strings.Cut(strings.TrimPrefix(*latestRC, r.NewK8sVersion+"-rc"), "+k3s")
if !found {
return errors.New("failed to parse rc number from " + *latestRC)
}
currentRCNumber, err := strconv.Atoi(trimmedRCNumber)
if err != nil {
return err
}
latestRCNumber = currentRCNumber + 1
}
name = r.NewK8sVersion + "-rc" + strconv.Itoa(latestRCNumber) + "+" + r.NewSuffix
}
opts.Name = name
opts.Tag = name
opts.Prerelease = true
opts.Draft = !rc
opts.ReleaseNotes = ""
fmt.Printf("create release options: %+v\n", *opts)
if !rc && opts.Repo == "k3s" {
buff, err := release.GenReleaseNotes(ctx, opts.Owner, opts.Repo, *latestRC, oldName, client)
if err != nil {
return err
}
opts.ReleaseNotes = buff.String()
}
if r.DryRun {
fmt.Println("dry run, skipping creating release")
return nil
}
createdRelease, err := repository.CreateRelease(ctx, client, opts)
if err != nil {
return err
}
fmt.Println("release created: " + *createdRelease.HTMLURL)
return nil
}