diff --git a/cmd/buildtools/k0s.go b/cmd/buildtools/k0s.go index 930ecf909..aacfd2814 100644 --- a/cmd/buildtools/k0s.go +++ b/cmd/buildtools/k0s.go @@ -4,9 +4,7 @@ import ( "bufio" "bytes" "fmt" - "os" "os/exec" - "path/filepath" "strings" "github.com/sirupsen/logrus" @@ -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() @@ -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) } @@ -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 { @@ -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() -} diff --git a/cmd/buildtools/utils.go b/cmd/buildtools/utils.go index 5c2bda214..b72b3cc62 100644 --- a/cmd/buildtools/utils.go +++ b/cmd/buildtools/utils.go @@ -10,6 +10,7 @@ import ( "io" "net/http" "os" + "os/exec" "path/filepath" "regexp" "sort" @@ -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 @@ -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) @@ -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() +}