Skip to content

Commit

Permalink
refactor owner repo, add list of repos, helper funcs, latest tag, lis…
Browse files Browse the repository at this point in the history
…t tags (#296)
  • Loading branch information
briandowns authored Nov 2, 2023
1 parent 2583848 commit 5c55294
Show file tree
Hide file tree
Showing 12 changed files with 266 additions and 136 deletions.
8 changes: 7 additions & 1 deletion cmd/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
BINARIES = gen_release_notes gen_release_report backport semv standup k3s_release rancher_release test_coverage upstream_go_version rke2_release
ARCHS = amd64 arm64
OSs = linux darwin freebsd

GO_COMPILE = CGO_ENABLED=1 $(GO) build -tags $(TAGS) -v -ldflags "$(LDFLAGS)" -o $@-$${os}-$${arch}
OS := $(shell uname)

ifeq ($(OS),Darwin)
GEN_HASH = shasum -a 256 $@-$${os}-$${arch} >> $(BINDIR)/sha256sums-$(BINARY).txt
else
GEN_HASH = sha256sum $@-$${os}-$${arch} >> $(BINDIR)/sha256sums-$(BINARY).txt
endif

16 changes: 12 additions & 4 deletions cmd/gen_release_notes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,19 @@ Usage: %[2]s [-r repo] [-m milestone] [-p prev milestone]
Options:
-h help
-v show version and exit
-o repository owner/org
-r repo repository that should be used
-m milestone milestone to be used
-p prev milestone previous milestone
-d enable debug logs
Examples:
# generate release notes for RKE2 for milestone v1.21.5
%[2]s -r rke2 -m v1.21.5+rke2r1 -p v1.21.4+rke2r1
%[2]s -o rancher -r rke2 -m v1.21.5+rke2r1 -p v1.21.4+rke2r1
`

var (
vers bool
owner string
repo string
milestone string
prevMilestone string
Expand All @@ -53,6 +55,7 @@ func main() {

flag.BoolVar(&vers, "v", false, "")
flag.BoolVar(&debug, "d", false, "")
flag.StringVar(&owner, "o", "", "")
flag.StringVar(&repo, "r", "", "")
flag.StringVar(&milestone, "m", "", "")
flag.StringVar(&prevMilestone, "p", "", "")
Expand All @@ -73,8 +76,13 @@ func main() {
os.Exit(1)
}

if !repository.IsValidRepo(repo) {
fmt.Println("error: please provide a valid repository")
if owner == "" {
fmt.Println("error: owner flag required")
os.Exit(1)
}

if repo == "" {
fmt.Println("error: repo flag required")
os.Exit(1)
}

Expand All @@ -86,7 +94,7 @@ func main() {
ctx := context.Background()
client := repository.NewGithub(ctx, ghToken)

notes, err := release.GenReleaseNotes(ctx, repo, milestone, prevMilestone, client)
notes, err := release.GenReleaseNotes(ctx, owner, repo, milestone, prevMilestone, client)
if err != nil {
fmt.Println(err)
os.Exit(1)
Expand Down
15 changes: 14 additions & 1 deletion cmd/rke2_release/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,25 @@ Checks if new Golang versions are available and creates new releases in `rancher

**Examples**

```
```sh
export GITHUB_TOKEN={YOUR_GITHUB_TOKEN}

rke2_release image-build-base-release
```

```sh
rke2_release components -l all
image-build-base |v1.21.3b1
image-build-calico |v3.26.1-build20231009
image-build-cni-plugins |v1.2.0-build20231009
...
```

```sh
rke2_release components -l image-build-k8s-metrics-server
image-build-k8s-metrics-server|v0.6.3-build20231009
```

## Contributions

- File Issue with details of the problem, feature request, etc.
Expand Down
78 changes: 78 additions & 0 deletions cmd/rke2_release/components.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package main

import (
"context"
"errors"
"fmt"
"os"
"text/tabwriter"

"github.com/rancher/ecm-distro-tools/repository"
"github.com/urfave/cli/v2"
)

func componentsCommand() *cli.Command {
return &cli.Command{
Name: "components",
Usage: "interact with the RKE2 components",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "github-token",
Aliases: []string{"g"},
EnvVars: []string{"GITHUB_TOKEN"},
Required: true,
},
&cli.StringFlag{
Name: "list",
Aliases: []string{"l"},
Required: false,
},
},
Action: components,
}
}

func components(c *cli.Context) error {
token := c.String("github-token")
if token == "" {
return errors.New("env var GITHUB_TOKEN is required")
}

listContent := c.String("list")

ctx := context.Background()
ghClient := repository.NewGithub(ctx, token)
w := tabwriter.NewWriter(os.Stdout, 0, 0, 0, ' ', tabwriter.Debug)

if listContent == "all" {
for _, repo := range repository.RKE2HardenedImages {
owner, repo, err := repository.SplitOwnerRepo(repo)
if err != nil {
return err
}

tag, err := repository.LatestTag(ctx, ghClient, owner, repo)
if err != nil {
return err
}

fmt.Fprintf(w, "%s\t%+v\n", repo, *tag.Name)
}
} else {
owner, repo, err := repository.SplitOwnerRepo(listContent)
if err != nil {
return err
}

tag, err := repository.LatestTag(ctx, ghClient, owner, repo)
if err != nil {
return err
}

fmt.Fprintf(w, "%s\t%+v\n", repo, *tag.Name)
}

w.Flush()

return nil
}
1 change: 1 addition & 0 deletions cmd/rke2_release/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func main() {
app.UseShortOptionHandling = true
app.Commands = []*cli.Command{
imageBuildBaseReleaseCommand(),
componentsCommand(),
}
app.Flags = rootFlags

Expand Down
File renamed without changes.
19 changes: 15 additions & 4 deletions release/k3s/k3s.go
Original file line number Diff line number Diff line change
Expand Up @@ -658,10 +658,12 @@ func (r *Release) CreateRelease(ctx context.Context, client *github.Client, rc b
rcNum := 1
name := r.NewK8SClient + "+" + r.NewK3SVersion
oldName := r.OldK8SVersion + "+" + r.OldK8SVersion

for {
if rc {
name = r.NewK8SVersion + "-rc" + strconv.Itoa(rcNum) + "+" + r.NewK3SVersion
}

opts := &repository.CreateReleaseOpts{
Repo: k3sRepo,
Name: name,
Expand All @@ -670,27 +672,36 @@ func (r *Release) CreateRelease(ctx context.Context, client *github.Client, rc b
Draft: !rc,
ReleaseNotes: "",
}

if !rc {
latestRc, err := release.LatestRC(ctx, k3sRepo, r.NewK8SVersion, client)
buff, err := release.GenReleaseNotes(ctx, k3sRepo, latestRc, oldName, client)
latestRc, err := release.LatestRC(ctx, "k3s-io", k3sRepo, r.NewK8SVersion, client)
if err != nil {
return err
}

buff, err := release.GenReleaseNotes(ctx, "k3s-io", k3sRepo, latestRc, oldName, client)
if err != nil {
return err
}
opts.ReleaseNotes = buff.String()
}
_, err := repository.CreateRelease(ctx, client, opts)
if err != nil {

if _, err := repository.CreateRelease(ctx, client, opts); err != nil {
githubErr := err.(*github.ErrorResponse)
if strings.Contains(githubErr.Errors[0].Code, "already_exists") {
if !rc {
return err
}

rcNum += 1
continue
}

return err
}

break
}

return nil
}
7 changes: 1 addition & 6 deletions release/rancher/rancher.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,18 +258,13 @@ func SetChartBranchReferences(ctx context.Context, forkPath, rancherBaseBranch,
}

func createPRFromRancher(ctx context.Context, rancherBaseBranch, title, branchName, forkOwner string, ghClient *github.Client) error {
const repo = "rancher"
org, err := repository.OrgFromRepo(repo)
if err != nil {
return err
}
pull := &github.NewPullRequest{
Title: github.String(title),
Base: github.String(rancherBaseBranch),
Head: github.String(forkOwner + ":" + branchName),
MaintainerCanModify: github.Bool(true),
}
_, _, err = ghClient.PullRequests.Create(ctx, org, repo, pull)
_, _, err := ghClient.PullRequests.Create(ctx, "rancher", "rancher", pull)

return err
}
51 changes: 14 additions & 37 deletions release/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func capitalize(s string) string {

// GenReleaseNotes genereates release notes based on the given milestone,
// previous milestone, and repository.
func GenReleaseNotes(ctx context.Context, repo, milestone, prevMilestone string, client *github.Client) (*bytes.Buffer, error) {
func GenReleaseNotes(ctx context.Context, owner, repo, milestone, prevMilestone string, client *github.Client) (*bytes.Buffer, error) {
funcMap := template.FuncMap{
"majMin": majMin,
"trimPeriods": trimPeriods,
Expand All @@ -113,7 +113,7 @@ func GenReleaseNotes(ctx context.Context, repo, milestone, prevMilestone string,
tmpl := template.New(templateName).Funcs(funcMap)
tmpl = template.Must(tmpl.Parse(changelogTemplate))

content, err := repository.RetrieveChangeLogContents(ctx, client, repo, prevMilestone, milestone)
content, err := repository.RetrieveChangeLogContents(ctx, client, owner, repo, prevMilestone, milestone)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -294,16 +294,11 @@ func KubernetesGoVersion(ctx context.Context, client *github.Client, version str
// VerifyAssets checks the number of assets for the
// given release and indicates if the expected number has
// been met.
func VerifyAssets(ctx context.Context, client *github.Client, repo string, tags []string) (map[string]bool, error) {
func VerifyAssets(ctx context.Context, client *github.Client, owner, repo string, tags []string) (map[string]bool, error) {
if len(tags) == 0 {
return nil, errors.New("no tags provided")
}

org, err := repository.OrgFromRepo(repo)
if err != nil {
return nil, err
}

releases := make(map[string]bool, len(tags))

const (
Expand All @@ -317,7 +312,7 @@ func VerifyAssets(ctx context.Context, client *github.Client, repo string, tags
continue
}

release, _, err := client.Repositories.GetReleaseByTag(ctx, org, repo, tag)
release, _, err := client.Repositories.GetReleaseByTag(ctx, owner, repo, tag)
if err != nil {
switch err := err.(type) {
case *github.ErrorResponse:
Expand Down Expand Up @@ -348,17 +343,12 @@ func VerifyAssets(ctx context.Context, client *github.Client, repo string, tags
}

// ListAssets gets all assets associated with the given release.
func ListAssets(ctx context.Context, client *github.Client, repo, tag string) ([]*github.ReleaseAsset, error) {
org, err := repository.OrgFromRepo(repo)
if err != nil {
return nil, err
}

func ListAssets(ctx context.Context, client *github.Client, owner, repo, tag string) ([]*github.ReleaseAsset, error) {
if tag == "" {
return nil, errors.New("invalid tag provided")
}

release, _, err := client.Repositories.GetReleaseByTag(ctx, org, repo, tag)
release, _, err := client.Repositories.GetReleaseByTag(ctx, owner, repo, tag)
if err != nil {
switch err := err.(type) {
case *github.ErrorResponse:
Expand All @@ -374,17 +364,12 @@ func ListAssets(ctx context.Context, client *github.Client, repo, tag string) ([
}

// DeleteAssetsByRelease deletes all release assets for the given release tag.
func DeleteAssetsByRelease(ctx context.Context, client *github.Client, repo, tag string) error {
org, err := repository.OrgFromRepo(repo)
if err != nil {
return err
}

func DeleteAssetsByRelease(ctx context.Context, client *github.Client, owner, repo, tag string) error {
if tag == "" {
return errors.New("invalid tag provided")
}

release, _, err := client.Repositories.GetReleaseByTag(ctx, org, repo, tag)
release, _, err := client.Repositories.GetReleaseByTag(ctx, owner, repo, tag)
if err != nil {
switch err := err.(type) {
case *github.ErrorResponse:
Expand All @@ -397,7 +382,7 @@ func DeleteAssetsByRelease(ctx context.Context, client *github.Client, repo, tag
}

for _, asset := range release.Assets {
if _, err := client.Repositories.DeleteReleaseAsset(ctx, org, repo, asset.GetID()); err != nil {
if _, err := client.Repositories.DeleteReleaseAsset(ctx, owner, repo, asset.GetID()); err != nil {
return err
}
}
Expand All @@ -406,17 +391,12 @@ func DeleteAssetsByRelease(ctx context.Context, client *github.Client, repo, tag
}

// DeleteAssetByID deletes the release asset associated with the given ID.
func DeleteAssetByID(ctx context.Context, client *github.Client, repo, tag string, id int64) error {
org, err := repository.OrgFromRepo(repo)
if err != nil {
return err
}

func DeleteAssetByID(ctx context.Context, client *github.Client, owner, repo, tag string, id int64) error {
if tag == "" {
return errors.New("invalid tag provided")
}

if _, err := client.Repositories.DeleteReleaseAsset(ctx, org, repo, id); err != nil {
if _, err := client.Repositories.DeleteReleaseAsset(ctx, owner, repo, id); err != nil {
return err
}

Expand Down Expand Up @@ -622,13 +602,10 @@ func findInURL(url, regex, str string, checkStatusCode bool) []string {
}

// LatestRC will get the latest rc created for the k8s version in either rke2 or k3s
func LatestRC(ctx context.Context, repo, k8sVersion string, client *github.Client) (string, error) {
func LatestRC(ctx context.Context, owner, repo, k8sVersion string, client *github.Client) (string, error) {
var rcs []*github.RepositoryRelease
org, err := repository.OrgFromRepo(repo)
if err != nil {
return "", err
}
allReleases, _, err := client.Repositories.ListReleases(ctx, org, repo, &github.ListOptions{})

allReleases, _, err := client.Repositories.ListReleases(ctx, owner, repo, &github.ListOptions{})
if err != nil {
return "", err
}
Expand Down
Loading

0 comments on commit 5c55294

Please sign in to comment.