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

Refactor apko related functions and move to utils #800

Merged
merged 3 commits into from
Jul 19, 2024
Merged
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
73 changes: 20 additions & 53 deletions cmd/buildtools/k0s.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import (
"bufio"
"bytes"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -52,31 +50,12 @@ var updateK0sImagesCommand = &cli.Command{
Action: func(c *cli.Context) error {
logrus.Infof("updating k0s images")

k0sVersion := c.String("k0s-version")
if k0sVersion != "" {
if err := runCommand("make", "pkg/goods/bins/k0s", fmt.Sprintf("K0S_VERSION=%s", k0sVersion), "K0S_BINARY_SOURCE_OVERRIDE="); err != nil {
return fmt.Errorf("failed to make k0s: %w", err)
}
} else {
if err := runCommand("make", "pkg/goods/bins/k0s"); err != nil {
return fmt.Errorf("failed to make k0s: %w", err)
}
if err := makeK0s(c.String("k0s-version")); err != nil {
return fmt.Errorf("failed to make k0s: %w", err)
}

if err := runCommand("make", "apko"); err != nil {
return fmt.Errorf("failed to make apko: %w", err)
}

if os.Getenv("REGISTRY_PASS") != "" {
if err := runCommand(
"make",
"apko-login",
fmt.Sprintf("REGISTRY=%s", os.Getenv("REGISTRY_SERVER")),
fmt.Sprintf("USERNAME=%s", os.Getenv("REGISTRY_USER")),
fmt.Sprintf("PASSWORD=%s", os.Getenv("REGISTRY_PASS")),
); err != nil {
return fmt.Errorf("failed to apko login: %w", err)
}
if err := ApkoLogin(); err != nil {
return fmt.Errorf("failed to apko login: %w", err)
}

wolfiAPKIndex, err := GetWolfiAPKIndex()
Expand All @@ -95,17 +74,11 @@ var updateK0sImagesCommand = &cli.Command{
return fmt.Errorf("failed to get package version for %s: %w", component.name, err)
}

if err := runCommand(
"make",
"apko-build-and-publish",
fmt.Sprintf("IMAGE=%s/replicated/ec-%s:%s", os.Getenv("REGISTRY_SERVER"), component.name, packageVersion),
fmt.Sprintf("APKO_CONFIG=%s", filepath.Join("deploy", "images", component.name, "apko.tmpl.yaml")),
fmt.Sprintf("PACKAGE_VERSION=%s", packageVersion),
); err != nil {
return fmt.Errorf("failed to build and publish apko for %s: %w", component.name, err)
if err := ApkoBuildAndPublish(component.name, packageVersion); err != nil {
return fmt.Errorf("failed to apko build and publish for %s: %w", component.name, err)
}

digest, err := getDigestFromBuildFile()
digest, err := GetDigestFromBuildFile()
if err != nil {
return fmt.Errorf("failed to get digest from build file: %w", err)
}
Expand All @@ -119,6 +92,19 @@ var updateK0sImagesCommand = &cli.Command{
},
}

func makeK0s(k0sVersion string) error {
if k0sVersion != "" {
if err := RunCommand("make", "pkg/goods/bins/k0s", fmt.Sprintf("K0S_VERSION=%s", k0sVersion), "K0S_BINARY_SOURCE_OVERRIDE="); err != nil {
return fmt.Errorf("make k0s: %w", err)
}
} else {
if err := RunCommand("make", "pkg/goods/bins/k0s"); err != nil {
return fmt.Errorf("make k0s: %w", err)
}
}
return nil
}

func getUpstreamVersion(name string) (string, error) {
output, err := exec.Command("pkg/goods/bins/k0s", "airgap", "list-images", "--all").Output()
if err != nil {
Expand Down Expand Up @@ -151,22 +137,3 @@ func getUpstreamVersion(name string) (string, error) {
}
return version, nil
}

func getDigestFromBuildFile() (string, error) {
contents, err := os.ReadFile("build/digest")
if err != nil {
return "", fmt.Errorf("read build file: %w", err)
}
parts := strings.Split(string(contents), "@")
if len(parts) != 2 {
return "", fmt.Errorf("incorrect number of parts in build file")
}
return strings.TrimSpace(parts[1]), nil
}

func runCommand(name string, args ...string) error {
cmd := exec.Command(name, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
55 changes: 53 additions & 2 deletions cmd/buildtools/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"io"
"net/http"
"os"
"os/exec"
"path/filepath"
"regexp"
"sort"
Expand Down Expand Up @@ -67,8 +68,8 @@ func GetWolfiPackageVersion(wolfiAPKIndex []byte, pkgName string, pinnedVersion
}
scanner.Scan()
line = scanner.Text()
// filter by package version
if !strings.HasPrefix(line, "V:"+pinnedVersion+"-r") {
// filter by pinned version
if pinnedVersion != "" && !strings.HasPrefix(line, "V:"+pinnedVersion+"-r") {
continue
}
// find the revision number
Expand Down Expand Up @@ -100,6 +101,49 @@ func GetWolfiPackageVersion(wolfiAPKIndex []byte, pkgName string, pinnedVersion
return fmt.Sprintf("%s-r%d", pinnedVersion, revisions[0]), nil
}

func ApkoLogin() error {
if err := RunCommand("make", "apko"); err != nil {
return fmt.Errorf("make apko: %w", err)
}
if os.Getenv("REGISTRY_PASS") != "" {
if err := RunCommand(
"make",
"apko-login",
fmt.Sprintf("REGISTRY=%s", os.Getenv("REGISTRY_SERVER")),
fmt.Sprintf("USERNAME=%s", os.Getenv("REGISTRY_USER")),
fmt.Sprintf("PASSWORD=%s", os.Getenv("REGISTRY_PASS")),
); err != nil {
return err
}
}
return nil
}

func ApkoBuildAndPublish(componentName string, packageVersion string) error {
if err := RunCommand(
"make",
"apko-build-and-publish",
fmt.Sprintf("IMAGE=%s/replicated/ec-%s:%s", os.Getenv("REGISTRY_SERVER"), componentName, packageVersion),
fmt.Sprintf("APKO_CONFIG=%s", filepath.Join("deploy", "images", componentName, "apko.tmpl.yaml")),
fmt.Sprintf("PACKAGE_VERSION=%s", packageVersion),
); err != nil {
return err
}
return nil
}

func GetDigestFromBuildFile() (string, error) {
contents, err := os.ReadFile("build/digest")
if err != nil {
return "", fmt.Errorf("read build file: %w", err)
}
parts := strings.Split(string(contents), "@")
if len(parts) != 2 {
return "", fmt.Errorf("incorrect number of parts in build file")
}
return strings.TrimSpace(parts[1]), nil
}

func GetLatestGitHubRelease(ctx context.Context, owner, repo string) (string, error) {
client := github.NewClient(nil)
release, _, err := client.Repositories.GetLatestRelease(ctx, owner, repo)
Expand Down Expand Up @@ -322,3 +366,10 @@ func ExtractTGZArchive(tgzFile string, destDir string) error {

return nil
}

func RunCommand(name string, args ...string) error {
cmd := exec.Command(name, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
Loading