Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add generate config command #328

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions cmd/k3s_release/generate_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package main

import (
"github.com/rancher/ecm-distro-tools/release/k3s"
"github.com/urfave/cli/v2"
)

func generateConfigCommand() *cli.Command {
return &cli.Command{
Name: "generate-config",
Usage: "Generate a k3s release configuration file",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "version",
Aliases: []string{"v"},
Usage: "version to generate the config for",
Required: true,
},
&cli.StringFlag{
Name: "ssh-path",
Aliases: []string{"s"},
Usage: "ssh key path",
Required: true,
},
&cli.StringFlag{
Name: "generate-path",
Aliases: []string{"g"},
Usage: "path to generate the configuration in",
Value: ".",
Required: false,
},
&cli.StringFlag{
Name: "workspace",
Aliases: []string{"w"},
Usage: "directory to clone repositories and create files",
Required: true,
},
&cli.StringFlag{
Name: "handler",
Aliases: []string{"r"},
Usage: "your github handler",
Required: true,
},
},
Action: generateConfg,
}
}

func generateConfg(c *cli.Context) error {
version := c.String("version")
sshPath := c.String("ssh-path")
generatePath := c.String("generate-path")
workspace := c.String("workspace")
handler := c.String("handler")
return k3s.GenerateConfig(version, sshPath, generatePath, workspace, handler)
}
1 change: 1 addition & 0 deletions cmd/k3s_release/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func main() {
modifyK3SCommand(),
tagRCReleaseCommand(),
tagReleaseCommand(),
generateConfigCommand(),
}
app.Flags = rootFlags

Expand Down
127 changes: 123 additions & 4 deletions release/k3s/k3s.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,23 @@ import (
"encoding/json"
"errors"
"fmt"
"net/http"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"text/template"
"time"

"github.com/Masterminds/semver/v3"
"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"
ecmExec "github.com/rancher/ecm-distro-tools/exec"
ecmHttp "github.com/rancher/ecm-distro-tools/http"
"github.com/rancher/ecm-distro-tools/release"
"github.com/rancher/ecm-distro-tools/repository"
"github.com/sirupsen/logrus"
Expand All @@ -27,6 +31,7 @@ import (
)

const (
channelsURL = "https://update.k3s.io/v1-release/channels"
k3sRepo = "k3s"
k8sUpstreamURL = "https://github.com/kubernetes/kubernetes"
rancherRemote = "k3s-io"
Expand Down Expand Up @@ -101,14 +106,35 @@ type Release struct {
NewGoVersion string `json:"-"`
ReleaseBranch string `json:"release_branch"`
Workspace string `json:"workspace"`
K3sRemote string `json:"k3s_remote"`
K3sRemote string `json:"k3s_remote,omitempty"`
Handler string `json:"handler"`
Email string `json:"email"`
GithubToken string `json:"-"`
K8sRancherURL string `json:"k8s_rancher_url"`
K3sUpstreamURL string `json:"k3s_upstream_url"`
K8sRancherURL string `json:"k8s_rancher_url,omitempty"`
K3sUpstreamURL string `json:"k3s_upstream_url,omitempty"`
SSHKeyPath string `json:"ssh_key_path"`
DryRun bool `json:"dry_run"`
DryRun bool `json:"dry_run,omitempty"`
}

type Channels struct {
Type string `json:"type"`
Links struct {
Self string `json:"self"`
} `json:"links"`
Actions struct {
} `json:"actions"`
ResourceType string `json:"resourceType"`
Data []struct {
ID string `json:"id"`
Type string `json:"type"`
Links struct {
Self string `json:"self"`
} `json:"links"`
Name string `json:"name"`
Latest string `json:"latest"`
LatestRegexp string `json:"latestRegexp,omitempty"`
ExcludeRegexp string `json:"excludeRegexp,omitempty"`
} `json:"data"`
}

func NewRelease(configPath string) (*Release, error) {
Expand Down Expand Up @@ -753,3 +779,96 @@ func (r *Release) CreateRelease(ctx context.Context, client *github.Client, rc b

return nil
}

func GenerateConfig(version, sshPath, generatePath, workspace, handler string) error {
var releaseBranch string
var lastVersion string
var configFile string

channels, err := getK3sChannels(channelsURL)
if err != nil {
return err
}
ver, err := semver.NewVersion(version)
if err != nil {
return err
}
releaseBranch = fmt.Sprintf("release-%d.%d", ver.Major(), ver.Minor())
for _, channel := range channels.Data {
if channel.ID == "latest" {
if strings.Contains(channel.Latest, version) {
releaseBranch = "master"
}
}
if channel.Name == "v"+version {
lastVersion = channel.Latest
break
}
}
lastVer, err := semver.NewVersion(lastVersion)
if err != nil {
return err
}
oldK8sVersion := fmt.Sprintf("%d.%d.%d", lastVer.Major(), lastVer.Minor(), lastVer.Patch())
newK8sVersion := fmt.Sprintf("%d.%d.%d", lastVer.Major(), lastVer.Minor(), lastVer.Patch()+1)
oldK8sClient := fmt.Sprintf("0.%d.%d", lastVer.Minor(), lastVer.Patch())
newK8sClient := fmt.Sprintf("0.%d.%d", lastVer.Minor(), lastVer.Patch()+1)

workspace, err = filepath.Abs(workspace)
if err != nil {
return err
}
sshPath, err = filepath.Abs(sshPath)
if err != nil {
return err
}

output, err := ecmExec.RunCommand(workspace, "/bin/sh", "-c", "git config --list | grep user.email=")
if err != nil {
return err
}
email := strings.TrimPrefix(output, "user.email=")
email = strings.TrimSuffix(email, "\n")
config := Release{
OldK8SVersion: oldK8sVersion,
NewK8SVersion: newK8sVersion,
OldK8SClient: oldK8sClient,
NewK8SClient: newK8sClient,
OldK3SSuffix: lastVer.Metadata(),
NewK3SSuffix: "k3s1",
Handler: handler,
Email: email,
ReleaseBranch: releaseBranch,
Workspace: workspace,
SSHKeyPath: sshPath,
}
configFile, err = filepath.Abs(generatePath)
if err != nil {
return err
}
configFile = filepath.Join(configFile, "config.json")
file, err := json.MarshalIndent(config, "", " ")
if err != nil {
return err
}
return os.WriteFile(configFile, file, 0644)
}

func getK3sChannels(url string) (*Channels, error) {
client := ecmHttp.NewClient(time.Second * 15)
res, err := client.Get(url)
if err != nil {
return nil, err
}

if res.StatusCode != http.StatusOK {
return nil, errors.New("failed to get channels file")
}

var channels Channels
if err := json.NewDecoder(res.Body).Decode(&channels); err != nil {
return nil, err
}

return &channels, nil
}
29 changes: 29 additions & 0 deletions release/k3s/k3s_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package k3s

import (
"net/http"
"net/http/httptest"
"testing"
)

func TestGetK3sChannels(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"type":"collection","links":{"self":"…/v1-release/channels"},"actions":{},"resourceType":"channels","data":[{"id":"latest","type":"channel","links":{"self":"…/v1-release/channels/latest"},"name":"latest","latest":"v1.29.0+k3s1","latestRegexp":".*","excludeRegexp":"(^[^+]+-|v1\\.25\\.5\\+k3s1|v1\\.26\\.0\\+k3s1)"},{"id":"v1.29","type":"channel","links":{"self":"…/v1-release/channels/v1.29"},"name":"v1.29","latest":"v1.29.0+k3s1","latestRegexp":"v1\\.29\\..*","excludeRegexp":"^[^+]+-"}]}`))
}))
defer server.Close()

channels, err := getK3sChannels(server.URL)
if err != nil {
t.Error(err)
}
if channels.Data[0].ID != "latest" {
t.Error("first entry should be latest")
}
if channels.Data[1].ID != "v1.29" {
t.Error("second entry should be v1.29")
}
if channels.Data[1].Latest != "v1.29.0+k3s1" {
t.Error("v1.29 latest version should be v1.29.0+k3s1")
}
}