Skip to content

Commit

Permalink
build(mage): improve tasks for launching build
Browse files Browse the repository at this point in the history
  • Loading branch information
sheldonhull authored Oct 2, 2024
1 parent 35b01cf commit 8237c7c
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 150 deletions.
3 changes: 3 additions & 0 deletions .changes/unreleased/ci-build-20241002-234044.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
kind: ci-build
body: Improve mage tasks and linting with fixes. Less issues with err output on cleanup now, and also load the docker version of images into minikube setup proactively.
time: 2024-10-02T23:40:44.31027171Z
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ linters:
- funlen #OVERRIDE: ok using for bot, lots of quick long commands i worked on
- cyclop #OVERRIDE: ok using for bot, lots of quick long commands i worked on
- gocognit #OVERRIDE: ok using for bot, lots of quick long commands i worked on

- perfsprint
run:
timeout: 5m
build-tags:
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ require (
golang.org/x/term v0.23.0 // indirect
golang.org/x/text v0.17.0 // indirect
golang.org/x/time v0.6.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down
135 changes: 13 additions & 122 deletions go.sum

Large diffs are not rendered by default.

33 changes: 31 additions & 2 deletions magefiles/helm/helm.mage.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
package helm

import (
"bytes"
"fmt"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
Expand Down Expand Up @@ -128,6 +130,19 @@ func (Helm) Install() error {
return nil
}

// invokeHelmCaptureStdErr runs a Helm command and returns the error if any.
func invokeHelmCaptureStdErr(args ...string) error {
cmd := exec.Command("helm", args...)
var stderr bytes.Buffer
cmd.Stderr = &stderr // Capture stderr
err := cmd.Run()
if err != nil {
return err
}

return nil
}

// Uninstall uninstalls all the charts listed in constants.HelmChartsList.
func (Helm) Uninstall() {
magetoolsutils.CheckPtermDebug()
Expand All @@ -136,12 +151,26 @@ func (Helm) Uninstall() {
}
for _, chart := range constants.HelmChartsList {
pterm.Info.Printfln("Uninstalling: %s", chart.ReleaseName)
if err := invokeHelm("uninstall",

// Check if the release exists
err := invokeHelmCaptureStdErr("status", chart.ReleaseName)
if err != nil {
pterm.Warning.Printfln("%v", err)
if strings.Contains(err.Error(), "not found") {
pterm.Warning.Printfln("release '%s' does not exist, skipping uninstall.", chart.ReleaseName)
continue
}
pterm.Error.Printfln("Failed to check status of release '%s': %v", chart.ReleaseName, err)
continue
}

// Proceed to uninstall if it exists
if err := invokeHelmCaptureStdErr("uninstall",
chart.ReleaseName,
"--wait", // waits, those atomic already runs this
"--debug", // enable verbose output
); err != nil {
pterm.Warning.Printfln("failed to uninstall: %s, err: %v", chart.ReleaseName, err)
pterm.Warning.Printfln("Failed to uninstall: %s, err: %v", chart.ReleaseName, err)
} else {
pterm.Success.Printfln("Successfully uninstalled: %s", chart.ReleaseName)
}
Expand Down
72 changes: 49 additions & 23 deletions magefiles/minikube/minikube.mage.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
package minikube

import (
"bytes"
"fmt"
"os"
"os/exec"
"strings"
"time"

"github.com/DelineaXPM/dsv-k8s/v2/magefiles/constants"
"github.com/DelineaXPM/dsv-k8s/v2/magefiles/helm"

"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
Expand Down Expand Up @@ -41,6 +43,20 @@ func createCluster() error {
return nil
}

// invokeMinikubeCaptureStdErr runs a minikube command and returns the error if any.
func invokeMinikubeCaptureStdErr(args ...string) error {
cmd := exec.Command("minikube", args...)
var stderr bytes.Buffer
var stdout bytes.Buffer
cmd.Stderr = &stderr // Capture stderr
cmd.Stdout = &stdout
err := cmd.Run()
if err != nil {
return err
}
return nil
}

func updateKubeconfig() error {
mtu.CheckPtermDebug()
if _, err := os.Stat(constants.Kubeconfig); os.IsNotExist(err) {
Expand Down Expand Up @@ -130,59 +146,69 @@ func (Minikube) LoadImages() {
"image", "load",
"--overwrite", // minikube CLI docs causing strife, wasting time in my life.... ensure this is here or problems ensure in your local testing :-)
fmt.Sprintf("%s:latest", constants.DockerImageNameLocal),
); err != nil {
pterm.Warning.Printfln("unable to load image into minikube: %v", err)
} else {
pterm.Success.Printfln("image loaded into minikube: %s", constants.DockerImageNameLocal)
}

if err := invokeMinikubeCaptureStdErr(
"--profile", constants.KindClusterName,
"image", "load",
"--overwrite", // minikube CLI docs causing strife, wasting time in my life.... ensure this is here or problems ensure in your local testing :-)
fmt.Sprintf("%s:latest", constants.DockerImageQualified),
); err != nil {
pterm.Error.Printfln("unable to load image into minikube: %v", err)
} else {
pterm.Success.Printfln("image loaded into minikube: %s", constants.DockerImageQualified)
}
pterm.Success.Printfln("image loaded into minikube: %s", constants.DockerImageNameLocal)
// }
}

// 💾 RemoveImages removes the images both local and docker registered from the minikube cluster.
func (Minikube) RemoveImages() {
mtu.CheckPtermDebug()
mg.SerialDeps(
helm.Helm{}.Uninstall,
Minikube{}.ListImages,
)
var output string
// var err error
var elapsed time.Duration

for {
for _, image := range []string{constants.DockerImageNameLocal, constants.DockerImageQualified} {
// Run the docker rmi command and capture the output

simpleImageName := strings.ReplaceAll(image, "docker.io/", "")
cmd := exec.Command( //nolint:gosec // this is a local command being built
"minikube",
"image",
"rm",
"--profile", constants.KindClusterName,
fmt.Sprintf("%s:latest", constants.DockerImageNameLocal),
simpleImageName,
)

out, err := cmd.CombinedOutput()
output = string(out)
if err != nil {
pterm.Error.Printfln("image not rm from minikube: %v", err)
}
// Check if the output contains the image name
if !strings.Contains(output, constants.DockerImageNameLocal) ||
strings.Contains(output, fmt.Sprintf("No such image: %s", constants.DockerImageNameLocal)) {
pterm.Success.Printfln("image unloaded")
break
}

if strings.Contains(output, fmt.Sprintf("No such image: %s", simpleImageName)) {
pterm.Debug.Println("no such image detected:", simpleImageName)
pterm.Success.Println("image unloaded:", simpleImageName)
continue
} else if strings.Contains(output, simpleImageName) {
pterm.Debug.Println(output)
pterm.Info.Printfln("waiting for image [%s] to unload (elapsed time: %s)", simpleImageName, elapsed.Round(time.Second))
}
// If the image is still being unloaded, print a progress message
pterm.Info.Printf("Still waiting for image [%s] to unload (elapsed time: %s)\n", constants.DockerImageNameLocal, elapsed.Round(time.Second))

// Wait for 3 seconds before trying again
time.Sleep(3 * time.Second) //nolint:gomnd // no need to make a constant
elapsed += 3 * time.Second
}

// for _, chart := range constants.HelmChartsList {
// Load image into minikube
// debug output "--logtostderr",
// NOTE: removed this as we don't need to remove those images regularly as it's an upstream version infrequently changed and causes confusing output.
// if err := sh.Run("minikube", "image", "rm", "--profile", constants.KindClusterName, constants.DockerImageQualified); err != nil {
// pterm.Warning.Printfln("image not rm from minikube: %v", err)
// }
pterm.Success.Printfln("image removed from minikube: %s", constants.DockerImageNameLocal)
// }
mg.SerialDeps(
Minikube{}.ListImages,
)
}

// 🔍 ListImages provides a list of the minikube loaded images
Expand All @@ -198,7 +224,7 @@ func (Minikube) ListImages() {
// 🗑️ Destroy tears down the Kind cluster.
func (Minikube) Destroy() error {
mtu.CheckPtermDebug()
if err := sh.Run("minikube", "delete", "--profile", constants.KindClusterName); err != nil {
if err := invokeMinikubeCaptureStdErr("delete", "--profile", constants.KindClusterName); err != nil {
pterm.Error.Printfln("minikube delete error: %v", err)
return err
}
Expand Down
2 changes: 1 addition & 1 deletion magefiles/vault/vault.mage.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (DSV) SetupDSV() error {
}
logger := pterm.DefaultLogger.WithLevel(pterm.LogLevelInfo).WithCaller(true)
if _, err := os.Stat(clientcredfile); err == nil {
logger.Error("client credentials already exist, please run `mage vault:destroy` to remove and try again")
logger.Error("client credentials already exist, please run `mage dsv:destroy` to remove and try again")
return fmt.Errorf("tear down existing test credentials before recreate to avoid conflicts")
}
// dsv role create
Expand Down

0 comments on commit 8237c7c

Please sign in to comment.