Skip to content

Commit

Permalink
make the implementation more MCD-like
Browse files Browse the repository at this point in the history
  • Loading branch information
cheesesashimi committed Mar 27, 2024
1 parent a8e9663 commit 2f6ba4a
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 17 deletions.
4 changes: 4 additions & 0 deletions pkg/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -1285,6 +1285,10 @@ func (dn *Daemon) RunFirstbootCompleteMachineconfig() error {
return fmt.Errorf("failed to rename encapsulated MachineConfig after processing on firstboot: %w", err)
}

if err := dn.toggleRevertSystemdUnit(&mc, false); err != nil {
return err
}

dn.skipReboot = false
return dn.reboot(fmt.Sprintf("Completing firstboot provisioning to %s", mc.GetName()))
}
Expand Down
61 changes: 44 additions & 17 deletions pkg/daemon/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
pivottypes "github.com/openshift/machine-config-operator/pkg/daemon/pivot/types"
pivotutils "github.com/openshift/machine-config-operator/pkg/daemon/pivot/utils"
"github.com/openshift/machine-config-operator/pkg/upgrademonitor"
"github.com/openshift/machine-config-operator/test/helpers"
)

const (
Expand Down Expand Up @@ -65,6 +66,9 @@ const (
// ImageRegistryDrainOverrideConfigmap is the name of the Configmap a user can apply to force all
// image registry changes to not drain
ImageRegistryDrainOverrideConfigmap = "image-registry-override-drain"

// name of the systemd unit that re-bootstraps a node after reverting from layered to non-layered
layeringRevertSystemdUnitName = "machine-config-daemon-revert.service"
)

func getNodeRef(node *corev1.Node) *corev1.ObjectReference {
Expand Down Expand Up @@ -686,25 +690,10 @@ func (dn *Daemon) finalizeRevertToNonLayering(newConfig *mcfgv1.MachineConfig) e

klog.Infof("Wrote MachineConfig %q to %q", newConfig.Name, constants.MachineConfigEncapsulatedPath)

clonedRevertUnitName := "machine-config-daemon-revert-layered.service"
if _, err := exec.Command("cp", filepath.Join(pathSystemd, "machine-config-daemon-revert.service"), filepath.Join(pathSystemd, clonedRevertUnitName)).CombinedOutput(); err != nil {
return fmt.Errorf("could not clone systemd unit: %w", err)
}

klog.Infof("Cloned systemd unit to %q", filepath.Join(pathSystemd, clonedRevertUnitName))

if _, err := exec.Command("systemctl", "daemon-reload").CombinedOutput(); err != nil {
return fmt.Errorf("could not reload systemd daemon: %w", err)
}

klog.Infof("Reloaded systemd daemon")

if _, err := exec.Command("systemctl", "enable", clonedRevertUnitName).CombinedOutput(); err != nil {
return fmt.Errorf("could not enable systemd unit %q: %w", clonedRevertUnitName, err)
if err := dn.toggleRevertSystemdUnit(newConfig, true); err != nil {
return err
}

klog.Infof("Enabled systemd unit %q", clonedRevertUnitName)

// Clear the current image field
odc := &onDiskConfig{
currentImage: "",
Expand Down Expand Up @@ -2513,3 +2502,41 @@ func (dn *Daemon) hasImageRegistryDrainOverrideConfigMap() (bool, error) {

return false, fmt.Errorf("Error fetching image registry drain override configmap: %w", err)
}

func (dn *Daemon) toggleRevertSystemdUnit(mc *mcfgv1.MachineConfig, enabled bool) error {
clonedUnit, err := getRevertSystemdUnitFromMachineConfig(mc)
if err != nil {
return err
}

clonedUnit.Enabled = helpers.BoolToPtr(enabled)
clonedUnit.Mask = nil

if err := dn.writeUnits([]ign3types.Unit{*clonedUnit}); err != nil {
return err
}

if !enabled {
return os.RemoveAll(filepath.Join(pathSystemd, clonedUnit.Name))
}

return nil
}

func getRevertSystemdUnitFromMachineConfig(mc *mcfgv1.MachineConfig) (*ign3types.Unit, error) {
ignConfig, err := ctrlcommon.ParseAndConvertConfig(mc.Spec.Config.Raw)
if err != nil {
return nil, err
}

for _, unit := range ignConfig.Systemd.Units {
unit := unit
if unit.Name == layeringRevertSystemdUnitName {
unit.Name = "machine-config-daemon-revert-layered.service"
unit.Mask = nil
return &unit, nil
}
}

return nil, fmt.Errorf("could not find %s in MachineConfig %s", layeringRevertSystemdUnitName, mc.Name)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
name: machine-config-daemon-revert.service
mask: true
enabled: false
contents: |
[Unit]
Expand Down
2 changes: 2 additions & 0 deletions test/e2e-techpreview/onclusterbuild_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ func assertNodeRevertsToNonLayered(t *testing.T, cs *framework.ClientSet, node c

assert.Contains(t, helpers.GetRPMOstreeStatus(t, cs, node), workerMC.Spec.OSImageURL, "node %q did not rollback to OS image %q", node.Name, workerMC.Spec.OSImageURL)
t.Logf("Node %s has reverted to OS image %q", node.Name, workerMC.Spec.OSImageURL)

helpers.AssertFileNotOnNode(t, cs, node, "/etc/systemd/system/machine-config-daemon-revert-layered.service")
}

// Sets up and performs an on-cluster build for a given set of parameters.
Expand Down

0 comments on commit 2f6ba4a

Please sign in to comment.