Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
Signed-off-by: Dimitris Karakasilis <dimitris@spectrocloud.com>
  • Loading branch information
mauromorales authored and Dimitris Karakasilis committed Jun 30, 2023
1 parent 609c274 commit 62eee9f
Showing 1 changed file with 31 additions and 18 deletions.
49 changes: 31 additions & 18 deletions internal/agent/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"

"github.com/Masterminds/semver/v3"
events "github.com/kairos-io/kairos-sdk/bus"
"github.com/kairos-io/kairos-sdk/collector"
Expand Down Expand Up @@ -80,28 +81,13 @@ func Upgrade(
}

img := source

if img == "" {
bus.Manager.Response(events.EventVersionImage, func(p *pluggable.Plugin, r *pluggable.EventResponse) {
img = r.Data
})
}

_, err := bus.Manager.Publish(events.EventVersionImage, &events.VersionImagePayload{
Version: version,
})
if err != nil {
return err
}

var err error
if img == "" {
registry, err := utils.OSRelease("IMAGE_REPO")
img, err = determineUpgradeImage(version)
if err != nil {
fmt.Printf("Can't find IMAGE_REPO key under /etc/os-release\n")
fmt.Println(err.Error())
return err
}

img = fmt.Sprintf("%s:%s", registry, version)
}

if debug {
Expand Down Expand Up @@ -145,3 +131,30 @@ func Upgrade(

return upgradeAction.Run()
}

// determineUpgradeImage asks the provider plugin for an image or constructs
// it using version and data from /etc/os-release
func determineUpgradeImage(version string) (string, error) {
var img string
bus.Manager.Response(events.EventVersionImage, func(p *pluggable.Plugin, r *pluggable.EventResponse) {
img = r.Data
})

_, err := bus.Manager.Publish(events.EventVersionImage, &events.VersionImagePayload{
Version: version,
})
if err != nil {
return "", err
}

if img != "" {
return img, nil
}

registry, err := utils.OSRelease("IMAGE_REPO")
if err != nil {
return "", fmt.Errorf("can't find IMAGE_REPO key under /etc/os-release %w", err)
}

return fmt.Sprintf("%s:%s", registry, version), nil
}

0 comments on commit 62eee9f

Please sign in to comment.