From 2570a1848bcde35257b31886b38f870462c84533 Mon Sep 17 00:00:00 2001 From: Dimitris Karakasilis Date: Tue, 3 Sep 2024 13:52:12 +0300 Subject: [PATCH] Extract login into a method Signed-off-by: Dimitris Karakasilis --- pkg/action/upgrade_test.go | 1 + pkg/config/spec.go | 45 ++++++++++++++++++++++---------------- 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/pkg/action/upgrade_test.go b/pkg/action/upgrade_test.go index 4dc8740..289fb13 100644 --- a/pkg/action/upgrade_test.go +++ b/pkg/action/upgrade_test.go @@ -164,6 +164,7 @@ var _ = Describe("Runtime Actions", func() { config.Config = collector.Config{"upgrade": collector.Config{"recovery": true}} spec, err = agentConfig.NewUpgradeSpec(config) Expect(err).ShouldNot(HaveOccurred()) + Expect(spec.Entry).To(Equal(constants.BootEntryRecovery)) Expect(spec.Recovery.Size).To(Equal(uint(100 + dummySourceSizeMb))) // We adding 100Mb on top }) Describe(fmt.Sprintf("Booting from %s", constants.ActiveLabel), Label("active_label"), func() { diff --git a/pkg/config/spec.go b/pkg/config/spec.go index b28d58e..99ad65b 100644 --- a/pkg/config/spec.go +++ b/pkg/config/spec.go @@ -324,25 +324,7 @@ func NewUpgradeSpec(cfg *Config) (*v1.UpgradeSpec, error) { } } - // One way to set the entry is to use the cli arg "--recovery" - // which makes config.upgrade.entry be "recovery" - // Another way is by setting the cli arg "boot-entry" which set it to the - // specified value. - // Lastly, user can set "upgrade.recovery: true" in the kairos config, which - // should result in entry being "recovery". - entry := "" - _, ok := cfg.Config["upgrade"] - if ok { - // check value from --recovery and --boot-entry - entry, _ = cfg.Config["upgrade"].(collector.Config)["entry"].(string) - // check for "upgrade.recovery: true" in the kairos config - _, ok = cfg.Config["upgrade"].(collector.Config)["recovery"] - if ok { - if cfg.Config["upgrade"].(collector.Config)["recovery"].(bool) { - entry = constants.BootEntryRecovery - } - } - } + entry := findEntryFromConfig(cfg) spec := &v1.UpgradeSpec{ Entry: entry, @@ -1032,3 +1014,28 @@ func DetectPreConfiguredDevice(logger sdkTypes.KairosLogger) (string, error) { return "", nil } + +// findEntryFromConfig checks the passed config for one of the following ways +// to set the upgrade entry. +// - One way is to use the cli arg "--recovery" which makes config.upgrade.entry be "recovery" +// - Another way is by setting the cli arg "boot-entry" which set it to the specified value. +// - Lastly, user can set "upgrade.recovery: true" in the kairos config, which should result in entry being "recovery". +func findEntryFromConfig(cfg *Config) string { + if _, ok := cfg.Config["upgrade"]; !ok { + return "" + } + + // check value from --recovery and --boot-entry + if entryIface, ok := cfg.Config["upgrade"].(collector.Config)["entry"]; ok { + return entryIface.(string) + } + + // check for "upgrade.recovery: true" in the kairos config + if _, ok := cfg.Config["upgrade"].(collector.Config)["recovery"]; ok { + if cfg.Config["upgrade"].(collector.Config)["recovery"].(bool) { + return constants.BootEntryRecovery + } + } + + return "" +}