Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions pkg/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,10 +535,19 @@ func ReexecuteForTargetRoot(target string) error {
if sourceOsVersion.IsLikeRHEL() && targetOsVersion.IsLikeRHEL() {
sourceMajor := sourceOsVersion.BaseVersionMajor()
targetMajor := targetOsVersion.BaseVersionMajor()
if sourceMajor == "9" && targetMajor == "8" {

// When container is newer than target, use target-compatible binary
switch {
case sourceMajor == "10" && targetMajor == "9":
sourceBinarySuffix = ".rhel9"
klog.Info("container is rhel10, target is rhel9")
case sourceMajor == "10" && targetMajor == "8":
sourceBinarySuffix = ".rhel8"
klog.Info("container is rhel10, target is rhel8")
case sourceMajor == "9" && targetMajor == "8":
Comment on lines -538 to +547
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just my 2 cents, but I don't think continuing with version-specific conditionals is the right direction. This piece of code will need a revisit each time there's a new major, like now.
WDYT about something like this?

diff --git a/pkg/daemon/daemon.go b/pkg/daemon/daemon.go
index e990021ed..01170f701 100644
--- a/pkg/daemon/daemon.go
+++ b/pkg/daemon/daemon.go
@@ -14,7 +14,9 @@ import (
        "os/exec"
        "os/signal"
        "path/filepath"
+       "regexp"
        "slices"
+       "strconv"
        "strings"
        "sync"
        "syscall"
@@ -531,20 +533,34 @@ func ReexecuteForTargetRoot(target string) error {
                return fmt.Errorf("failed to get target OS: %w", err)
        }
 
-       var sourceBinarySuffix string
+       sourceBinary := "/usr/bin/machine-config-daemon"
        if sourceOsVersion.IsLikeRHEL() && targetOsVersion.IsLikeRHEL() {
-               sourceMajor := sourceOsVersion.BaseVersionMajor()
-               targetMajor := targetOsVersion.BaseVersionMajor()
-               if sourceMajor == "9" && targetMajor == "8" {
-                       sourceBinarySuffix = ".rhel8"
-                       klog.Info("container is rhel9, target is rhel8")
+               sourceVersion, err := strconv.Atoi(sourceOsVersion.BaseVersionMajor())
+               if err != nil {
+                       return fmt.Errorf("failed to convert source major version: %w", err)
+               }
+               targetVersion, err := strconv.Atoi(targetOsVersion.BaseVersionMajor())
+               if err != nil {
+                       return fmt.Errorf("failed to convert target major version: %w", err)
+               }
+               if sourceVersion > targetVersion {
+                       suffixes, err := getVersionedBinarySuffixes(sourceBinary + "*")
+                       if err != nil {
+                               return fmt.Errorf("failed to get machine-config-daemon versioned binaries: %w", err)
+                       }
+                       suffix, exists := suffixes[targetVersion]
+                       if exists {
+                               sourceBinary += suffix
+                       }
+                       klog.Infof("container is rhel%d, target is rhel%d", sourceVersion, targetVersion)
                } else {
-                       klog.Infof("using appropriate binary for source=rhel-%s target=rhel-%s", sourceMajor, targetMajor)
+                       klog.Infof("using appropriate binary for source=rhel-%d target=rhel-%d", sourceVersion, targetVersion)
                }
+
        } else {
                klog.Info("assuming we can use container binary chroot() to host")
        }
-       sourceBinary := "/usr/bin/machine-config-daemon" + sourceBinarySuffix
+
        src, err := os.Open(sourceBinary)
        if err != nil {
                return fmt.Errorf("opening %s: %w", sourceBinary, err)
@@ -596,6 +612,33 @@ func ReexecuteForTargetRoot(target string) error {
        return syscall.Exec(targetBin, newArgv, newEnv)
 }
 
+var regexDigits = regexp.MustCompile(`(\d)*$`)
+
+func getVersionedBinarySuffixes(basePath string) (map[int]string, error) {
+       result := map[int]string{}
+       matches, err := filepath.Glob(basePath)
+       if err != nil {
+               return nil, err
+       }
+       for _, match := range matches {
+               extension := filepath.Ext(match)
+               if extension != "" {
+                       versionBytes := regexDigits.Find([]byte(extension))
+                       if versionBytes == nil {
+                               // Skip, is a binary that doesn't have the pattern we are looking for
+                               continue
+                       }
+
+                       version, err := strconv.Atoi(string(versionBytes))
+                       if err != nil {
+                               return nil, err
+                       }
+                       result[version] = extension
+               }
+       }
+       return result, nil
+}
+
 // worker runs a worker thread that just dequeues items, processes them, and marks them done.
 // It enforces that the syncHandler is never invoked concurrently with the same key.
 func (dn *Daemon) worker() {

sourceBinarySuffix = ".rhel8"
klog.Info("container is rhel9, target is rhel8")
} else {
default:
klog.Infof("using appropriate binary for source=rhel-%s target=rhel-%s", sourceMajor, targetMajor)
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion pkg/daemon/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -2288,7 +2288,7 @@ func (dn *Daemon) updateKubeConfigPermission() error {
// (/home/core/.ssh/authorized_keys.d/ignition) or the old SSH key path
// (/home/core/.ssh/authorized_keys)
func (dn *Daemon) useNewSSHKeyPath() bool {
return dn.os.IsEL9() || dn.os.IsFCOS() || dn.os.IsSCOS()
return dn.os.IsEL9() || dn.os.IsEL10() || dn.os.IsFCOS() || dn.os.IsSCOS()
}

// Update a given PasswdUser's SSHKey
Expand Down