[WIP] Try to pass command line options for upgrade as kairos config

because it's actually configuration and it's better than using viper to
pass things around.

The `upgrade:` block seems to be ignored early in the process so even if
the user specifies an image there, the latest release will be looked up.

Signed-off-by: Dimitris Karakasilis <dimitris@karakasilis.me>
This commit is contained in:
Dimitris Karakasilis
2023-09-27 17:38:55 +03:00
parent 7d18e6504a
commit 3482e88aa3
3 changed files with 108 additions and 52 deletions

View File

@@ -5,8 +5,7 @@ import (
"encoding/json"
"fmt"
"sort"
"github.com/spf13/viper"
"strings"
hook "github.com/kairos-io/kairos-agent/v2/internal/agent/hooks"
@@ -55,6 +54,10 @@ func Upgrade(
version, source string, force, strictValidations bool, dirs []string, preReleases, upgradeRecovery bool) error {
bus.Manager.Initialize()
// TODO: Before we check for empy source,
// shouldn't we read the `upgrade:` block from the config and check if something
// is defined there?
if version == "" && source == "" {
fmt.Println("Searching for releases")
if preReleases {
@@ -93,13 +96,11 @@ func Upgrade(
}
}
// Set this here with viper help so we can use it while creating the upgrade spec
// And its properly set since creation without having to modify it later
// This should be binded somehow but the current cli doesnt allow us to bind flags to values
viper.Set("upgradeSource", img)
viper.Set("upgradeRecovery", upgradeRecovery)
upgradeConf := generateUpgradeConf(img, upgradeRecovery)
c, err := config.Scan(collector.Directories(dirs...), collector.StrictValidation(strictValidations))
c, err := config.Scan(collector.Directories(dirs...),
collector.Readers(strings.NewReader(upgradeConf)),
collector.StrictValidation(strictValidations))
if err != nil {
return err
}
@@ -162,3 +163,48 @@ func determineUpgradeImage(version string) (string, error) {
return fmt.Sprintf("%s:%s", registry, version), nil
}
// generateUpgradeConf creates a kairos configuration for upgrade to be
// added to the rest of the configurations.
func generateUpgradeConf(source string, upgradeRecovery bool) string {
conf := ""
if source == "" {
return conf
}
// TODO: Do the same for recovery upgrade
conf = fmt.Sprintf(`
upgrade:
recovery-system:
uri: %s`, source)
// source := viper.GetString("upgradeSource")
// recoveryUpgrade := viper.GetBool("upgradeRecovery")
// if source != "" {
// imgSource, err := v1.NewSrcFromURI(source)
// // TODO: Don't hide the error here!
// if err == nil {
// if recoveryUpgrade {
// spec.RecoveryUpgrade = recoveryUpgrade
// spec.Recovery.Source = imgSource
// } else {
// spec.Active.Source = imgSource
// }
// size, err := GetSourceSize(cfg, imgSource)
// if err != nil {
// cfg.Logger.Warnf("Failed to infer size for images: %s", err.Error())
// } else {
// cfg.Logger.Infof("Setting image size to %dMb", size)
// // On upgrade only the active or recovery will be upgraded, so we dont need to override passive
// if recoveryUpgrade {
// spec.Recovery.Size = uint(size)
// } else {
// spec.Active.Size = uint(size)
// }
// }
// }
//}
return conf
}

View File

@@ -27,7 +27,7 @@ import (
"github.com/kairos-io/kairos-agent/v2/internal/common"
"github.com/kairos-io/kairos-agent/v2/pkg/constants"
v1 "github.com/kairos-io/kairos-agent/v2/pkg/types/v1"
"github.com/kairos-io/kairos-agent/v2/pkg/utils/fs"
fsutils "github.com/kairos-io/kairos-agent/v2/pkg/utils/fs"
"github.com/kairos-io/kairos-agent/v2/pkg/utils/partitions"
"github.com/mitchellh/mapstructure"
"github.com/sanity-io/litter"
@@ -265,31 +265,32 @@ func NewUpgradeSpec(cfg *Config) (*v1.UpgradeSpec, error) {
State: installState,
}
source := viper.GetString("upgradeSource")
recoveryUpgrade := viper.GetBool("upgradeRecovery")
if source != "" {
imgSource, err := v1.NewSrcFromURI(source)
if err == nil {
if recoveryUpgrade {
spec.RecoveryUpgrade = recoveryUpgrade
spec.Recovery.Source = imgSource
} else {
spec.Active.Source = imgSource
}
size, err := GetSourceSize(cfg, imgSource)
if err != nil {
cfg.Logger.Warnf("Failed to infer size for images: %s", err.Error())
} else {
cfg.Logger.Infof("Setting image size to %dMb", size)
// On upgrade only the active or recovery will be upgraded, so we dont need to override passive
if recoveryUpgrade {
spec.Recovery.Size = uint(size)
} else {
spec.Active.Size = uint(size)
}
}
}
}
// source := viper.GetString("upgradeSource")
// recoveryUpgrade := viper.GetBool("upgradeRecovery")
// if source != "" {
// imgSource, err := v1.NewSrcFromURI(source)
// // TODO: Don't hide the error here!
// if err == nil {
// if recoveryUpgrade {
// spec.RecoveryUpgrade = recoveryUpgrade
// spec.Recovery.Source = imgSource
// } else {
// spec.Active.Source = imgSource
// }
// size, err := GetSourceSize(cfg, imgSource)
// if err != nil {
// cfg.Logger.Warnf("Failed to infer size for images: %s", err.Error())
// } else {
// cfg.Logger.Infof("Setting image size to %dMb", size)
// // On upgrade only the active or recovery will be upgraded, so we dont need to override passive
// if recoveryUpgrade {
// spec.Recovery.Size = uint(size)
// } else {
// spec.Active.Size = uint(size)
// }
// }
// }
//}
return spec, nil
}
@@ -543,6 +544,8 @@ func ReadUpgradeSpecFromConfig(c *Config) (*v1.UpgradeSpec, error) {
// ReadSpecFromCloudConfig returns a v1.Spec for the given spec
func ReadSpecFromCloudConfig(r *Config, spec string) (v1.Spec, error) {
fmt.Printf("litter.Sdump(r) before = %+v\n", litter.Sdump(r))
var sp v1.Spec
var err error
@@ -560,6 +563,8 @@ func ReadSpecFromCloudConfig(r *Config, spec string) (v1.Spec, error) {
return nil, fmt.Errorf("failed initializing spec: %v", err)
}
fmt.Printf("litter.Sdump(sp) before = %+v\n", litter.Sdump(sp))
// Load the config into viper from the raw cloud config string
ccString, err := r.String()
if err != nil {
@@ -576,6 +581,11 @@ func ReadSpecFromCloudConfig(r *Config, spec string) (v1.Spec, error) {
if err != nil {
r.Logger.Warnf("error unmarshalling %s Spec: %s", spec, err)
}
fmt.Println("---------------------------------")
fmt.Printf("litter.Sdump(sp) after = %+v\n", litter.Sdump(sp))
fmt.Println("---------------------------------")
err = sp.Sanitize()
if err != nil {
r.Logger.Warnf("Error sanitizing the % spec: %s", spec, err)

View File

@@ -35,31 +35,31 @@ const (
// ImageSource represents the source from where an image is created for easy identification
type ImageSource struct {
source string
srcType string
Source string
SrcType string
}
func (i ImageSource) Value() string {
return i.source
return i.Source
}
func (i ImageSource) IsDocker() bool {
return i.srcType == oci
return i.SrcType == oci
}
func (i ImageSource) IsDir() bool {
return i.srcType == dir
return i.SrcType == dir
}
func (i ImageSource) IsFile() bool {
return i.srcType == file
return i.SrcType == file
}
func (i ImageSource) IsEmpty() bool {
if i.srcType == "" {
if i.SrcType == "" {
return true
}
if i.source == "" {
if i.Source == "" {
return true
}
return false
@@ -69,7 +69,7 @@ func (i ImageSource) String() string {
if i.IsEmpty() {
return ""
}
return fmt.Sprintf("%s://%s", i.srcType, i.source)
return fmt.Sprintf("%s://%s", i.SrcType, i.Source)
}
func (i ImageSource) MarshalYAML() (interface{}, error) {
@@ -103,11 +103,11 @@ func (i *ImageSource) updateFromURI(uri string) error {
case oci, docker:
return i.parseImageReference(value)
case dir:
i.srcType = dir
i.source = value
i.SrcType = dir
i.Source = value
case file:
i.srcType = file
i.source = value
i.SrcType = file
i.Source = value
default:
return i.parseImageReference(uri)
}
@@ -121,8 +121,8 @@ func (i *ImageSource) parseImageReference(ref string) error {
} else if reference.IsNameOnly(n) {
ref += ":latest"
}
i.srcType = oci
i.source = ref
i.SrcType = oci
i.Source = ref
return nil
}
@@ -137,13 +137,13 @@ func NewEmptySrc() *ImageSource {
}
func NewDockerSrc(src string) *ImageSource {
return &ImageSource{source: src, srcType: oci}
return &ImageSource{Source: src, SrcType: oci}
}
func NewFileSrc(src string) *ImageSource {
return &ImageSource{source: src, srcType: file}
return &ImageSource{Source: src, SrcType: file}
}
func NewDirSrc(src string) *ImageSource {
return &ImageSource{source: src, srcType: dir}
return &ImageSource{Source: src, SrcType: dir}
}