Skip to content

Commit

Permalink
Add support for ELEMENTAL_REGISTER_UPGRADE_CORRELATION_ID
Browse files Browse the repository at this point in the history
Signed-off-by: Andrea Mazzotti <andrea.mazzotti@suse.com>
  • Loading branch information
anmazzotti committed Oct 22, 2024
1 parent a81978d commit 3098c28
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 27 deletions.
60 changes: 36 additions & 24 deletions cmd/register/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ import (
"fmt"
"os"
"os/exec"
"strings"

"github.com/rancher/elemental-operator/pkg/elementalcli"
"github.com/rancher/elemental-operator/pkg/install"
"github.com/rancher/elemental-operator/pkg/log"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/twpayne/go-vfs"
)

Expand All @@ -35,13 +37,6 @@ var (
)

func newUpgradeCommand() *cobra.Command {
var hostDir string
var cloudConfigPath string
var recovery bool
var recoveryOnly bool
var debug bool
var system string
var correlationID string

cmd := &cobra.Command{
Use: "upgrade",
Expand All @@ -59,20 +54,18 @@ func newUpgradeCommand() *cobra.Command {
// If system is not shutting down we can proceed.
upgradeConfig := elementalcli.UpgradeConfig{
Debug: debug,
Recovery: recovery,
RecoveryOnly: recoveryOnly,
System: system,
Recovery: viper.GetBool("recovery"),
RecoveryOnly: viper.GetBool("recovery-only"),
System: viper.GetString("system"),
Bootloader: true,
}
upgradeContext := install.UpgradeContext{
Config: upgradeConfig,
HostDir: hostDir,
CloudConfigPath: cloudConfigPath,
CorrelationID: correlationID,
HostDir: viper.GetString("host-dir"),
CloudConfigPath: viper.GetString("cloud-config"),
CorrelationID: viper.GetString("correlation-id"),
}

log.Infof("Upgrade context: %+v", upgradeContext)

installer := install.NewInstaller(vfs.OSFS, nil, nil)

needsReboot, err := installer.UpgradeElemental(upgradeContext)
Expand All @@ -84,23 +77,42 @@ func newUpgradeCommand() *cobra.Command {
// If the machine needs a reboot after an upgrade has been applied,
// so that consumers can try again after reboot to validate the upgrade has been applied successfully.
if needsReboot {
log.Infof("Rebooting machine after %s upgrade", correlationID)
log.Infof("Rebooting machine after %s upgrade", upgradeContext.CorrelationID)
reboot()
return ErrRebooting
}
// Upgrade has been applied successfully, nothing to do.
log.Infof("Upgrade %s applied successfully", correlationID)
log.Infof("Upgrade %s applied successfully", upgradeContext.CorrelationID)
return nil
},
}

cmd.Flags().StringVar(&hostDir, "host-dir", "/host", "The machine root directory where to apply the upgrade")
cmd.Flags().StringVar(&cloudConfigPath, "cloud-config", "/run/data/cloud-config", "The path of a cloud-config file to install on the machine during upgrade")
cmd.Flags().StringVar(&system, "system", "dir:/", "The system image uri or filesystem location to upgrade to")
cmd.Flags().StringVar(&correlationID, "correlation-id", "", "A correlationID to label the upgrade snapshot with")
cmd.Flags().BoolVar(&recovery, "recovery", false, "Upgrades the recovery partition together with the system")
cmd.Flags().BoolVar(&recoveryOnly, "recovery-only", false, "Upgrades the recovery partition only")
cmd.Flags().BoolVar(&debug, "debug", true, "Prints debug logs when performing upgrade")
viper.AutomaticEnv()
replacer := strings.NewReplacer("-", "_")
viper.SetEnvKeyReplacer(replacer)
viper.SetEnvPrefix("ELEMENTAL_REGISTER_UPGRADE")

cmd.Flags().String("host-dir", "/host", "The machine root directory where to apply the upgrade")
_ = viper.BindPFlag("host-dir", cmd.Flags().Lookup("host-dir"))

cmd.Flags().String("cloud-config", "/run/data/cloud-config", "The path of a cloud-config file to install on the machine during upgrade")
_ = viper.BindPFlag("cloud-config", cmd.Flags().Lookup("cloud-config"))

cmd.Flags().String("system", "dir:/", "The system image uri or filesystem location to upgrade to")
_ = viper.BindPFlag("system", cmd.Flags().Lookup("system"))

cmd.Flags().String("correlation-id", "", "A correlationID to label the upgrade snapshot with")
_ = viper.BindPFlag("correlation-id", cmd.Flags().Lookup("correlation-id"))

cmd.Flags().Bool("recovery", false, "Upgrades the recovery partition together with the system")
_ = viper.BindPFlag("recovery", cmd.Flags().Lookup("recovery"))

cmd.Flags().Bool("recovery-only", false, "Upgrades the recovery partition only")
_ = viper.BindPFlag("recovery-only", cmd.Flags().Lookup("recovery-only"))

cmd.Flags().Bool("debug", true, "Prints debug logs when performing upgrade")
_ = viper.BindPFlag("debug", cmd.Flags().Lookup("debug"))

return cmd
}

Expand Down
6 changes: 6 additions & 0 deletions controllers/managedosimage_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,12 @@ func (r *ManagedOSImageReconciler) newFleetBundleResources(ctx context.Context,
// we just do a safe name conversion here.
uniqueName = toDNSLabel(uniqueName)

// Use the plan name as correlation ID that will be applied as snapshot label on the machine.
upgradeContainerSpec.Env = append(upgradeContainerSpec.Env, corev1.EnvVar{
Name: "ELEMENTAL_REGISTER_UPGRADE_CORRELATION_ID",
Value: uniqueName,
})

objs := []runtime.Object{
&rbacv1.ClusterRole{
ObjectMeta: metav1.ObjectMeta{
Expand Down
4 changes: 2 additions & 2 deletions controllers/managedosimage_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ var _ = Describe("newFleetBundleResources", func() {
Expect(bundleResources[1].Name).To(Equal("ClusterRoleBinding--os-upgrader-test-name-cc7ce4275b54.yaml"))
Expect(bundleResources[2].Name).To(Equal("ServiceAccount-cattle-system-os-upgrader-test-name-08929531f5c0.yaml"))
Expect(bundleResources[3].Name).To(Equal("Secret-cattle-system-os-upgrader-test-name-52e9d8e041f4.yaml"))
Expect(bundleResources[4].Name).To(Equal("Plan-cattle-system-os-upgrader-test-name-24d63a562894.yaml"))
Expect(bundleResources[4].Name).To(Equal("Plan-cattle-system-os-upgrader-test-name-2663ce8e6968.yaml"))
})

It("should create fleet bundle when managedOSVersion exists", func() {
Expand Down Expand Up @@ -170,7 +170,7 @@ var _ = Describe("newFleetBundleResources", func() {
Expect(bundleResources[1].Name).To(Equal("ClusterRoleBinding--os-upgrader-test-name-cc7ce4275b54.yaml"))
Expect(bundleResources[2].Name).To(Equal("ServiceAccount-cattle-system-os-upgrader-test-name-08929531f5c0.yaml"))
Expect(bundleResources[3].Name).To(Equal("Secret-cattle-system-os-upgrader-test-name-52e9d8e041f4.yaml"))
Expect(bundleResources[4].Name).To(Equal("Plan-cattle-system-os-upgrader-test-name-24d63a562894.yaml"))
Expect(bundleResources[4].Name).To(Equal("Plan-cattle-system-os-upgrader-test-name-2663ce8e6968.yaml"))
})
})

Expand Down
7 changes: 6 additions & 1 deletion pkg/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ const (
// Upgrade constants
upgradeCloudConfigPath = "/oem/90_operator.yaml"
correlationIDLabelKey = "correlationID"
upgradeRunDir = "/run/elemental"
)

var (
Expand Down Expand Up @@ -607,12 +608,16 @@ func (i *installer) cleanupResetPlan() error {
func (i *installer) UpgradeElemental(context UpgradeContext) (bool, error) {
log.Infof("Applying upgrade: %s", context.CorrelationID)

runner := elementalcli.NewRunner()
runDir := filepath.Join(context.HostDir, upgradeRunDir)
if err := vfs.MkdirAll(i.fs, runDir, os.ModePerm); err != nil {
return false, fmt.Errorf("creating directory '%s': %w", runDir, err)
}

if err := i.applyCloudConfig(context.HostDir, context.CloudConfigPath); err != nil {
return false, fmt.Errorf("applying upgrade cloud config: %w", err)
}

runner := elementalcli.NewRunner()
elementalState, err := runner.GetState()
if err != nil {
return false, fmt.Errorf("reading installation state: %w", err)
Expand Down

0 comments on commit 3098c28

Please sign in to comment.