Skip to content

Commit 39c2f90

Browse files
add image mode status reporting conditions when feature gate is enabled
1 parent 3163107 commit 39c2f90

File tree

1 file changed

+98
-27
lines changed

1 file changed

+98
-27
lines changed

pkg/controller/node/node_controller.go

Lines changed: 98 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"reflect"
88
"sort"
9+
"strings"
910
"time"
1011

1112
helpers "github.com/openshift/machine-config-operator/pkg/helpers"
@@ -266,11 +267,11 @@ func (ctrl *Controller) Run(workers int, stopCh <-chan struct{}) {
266267
defer klog.Info("Shutting down MachineConfigController-NodeController")
267268

268269
// TODO (MCO-1775): Once ImageModeStatusReporting has been GA for an entire release version
269-
// (for example >=4.22.0), the below legacy condition cleanup logic can be removed.
270-
// Perform one-time cleanup of legacy MachineConfigNodeUpdateFilesAndOS conditions
271-
// when ImageModeStatusReporting feature gate is enabled
270+
// (for example >=4.22.0), the below migration logic can be removed.
271+
// Perform one-time migration from legacy MachineConfigNodeUpdateFilesAndOS condition
272+
// to new ImageModeStatusReporting conditions when feature gate is enabled
272273
if ctrl.fgHandler.Enabled(features.FeatureGateImageModeStatusReporting) {
273-
go ctrl.performStartupLegacyCleanup()
274+
go ctrl.performImageModeStatusReportingConditionMigration()
274275
}
275276

276277
for i := 0; i < workers; i++ {
@@ -1630,63 +1631,133 @@ func (ctrl *Controller) isConfigAndBuildPresent(mosc *mcfgv1.MachineOSConfig, mo
16301631
return (mosc != nil && mosb != nil)
16311632
}
16321633

1633-
// cleanupLegacyMCNConditions removes MachineConfigNodeUpdateFilesAndOS conditions from all MCNs
1634-
// when ImageModeStatusReporting feature gate is enabled, as this condition is replaced by
1635-
// separate MachineConfigNodeUpdateFiles and MachineConfigNodeUpdateOS conditions.
1636-
// Returns the number of MCNs that had legacy conditions removed.
1637-
func (ctrl *Controller) cleanupLegacyMCNConditions() (int, error) {
1634+
// migrateMCNConditionsToImageModeStatusReporting migrates MCN condition formats from the legacy
1635+
// MachineConfigNodeUpdateFilesAndOS condition to the new ImageModeStatusReporting conditions
1636+
// (MachineConfigNodeUpdateFiles, MachineConfigNodeUpdateOS, and MachineConfigNodeImagePulledFromRegistry).
1637+
// Removes the legacy condition and adds the new conditions with appropriate default values.
1638+
// Returns the number of MCNs that had their conditions migrated.
1639+
func (ctrl *Controller) migrateMCNConditionsToImageModeStatusReporting() (int, error) {
16381640
// Get all MachineConfigNodes
16391641
mcns, err := ctrl.client.MachineconfigurationV1().MachineConfigNodes().List(context.TODO(), metav1.ListOptions{})
16401642
if err != nil {
1641-
return 0, fmt.Errorf("failed to list MachineConfigNodes: %w", err)
1643+
return 0, fmt.Errorf("failed to list MachineConfigNodes for condition migration: %w", err)
16421644
}
16431645

1644-
// Loop through all the cluster's MCNs and clean them if needed
1645-
cleanedCount := 0
1646+
// Loop through all the cluster's MCNs and migrate them if needed
1647+
migratedCount := 0
16461648
for _, mcn := range mcns.Items {
16471649
// Check if the legacy condition exists and filter it out
16481650
hasLegacyCondition := false
1651+
// If we need to clean the legacy condition, the new conditions should not exist, but we
1652+
// will check to fully prevent condition duplication
1653+
needsUpdateFiles := true
1654+
needsUpdateOS := true
1655+
needsImagePulledFromRegistry := true
16491656
var newConditions []metav1.Condition
16501657
for _, condition := range mcn.Status.Conditions {
16511658
if condition.Type == string(mcfgv1.MachineConfigNodeUpdateFilesAndOS) {
16521659
hasLegacyCondition = true
16531660
klog.V(4).Infof("Removing legacy MachineConfigNodeUpdateFilesAndOS condition from MCN %s", mcn.Name)
16541661
continue // Skip adding this condition to the new list
1662+
} else if condition.Type == string(mcfgv1.MachineConfigNodeUpdateFiles) {
1663+
needsUpdateFiles = false
1664+
} else if condition.Type == string(mcfgv1.MachineConfigNodeUpdateOS) {
1665+
needsUpdateOS = false
1666+
} else if condition.Type == string(mcfgv1.MachineConfigNodeImagePulledFromRegistry) {
1667+
needsImagePulledFromRegistry = false
16551668
}
1669+
16561670
newConditions = append(newConditions, condition)
16571671
}
16581672

1659-
// Only update the MCN if we found and removed the legacy condition
1660-
if hasLegacyCondition {
1673+
// Only update the MCN if we found and removed the legacy condition or if any new
1674+
// conditions need to be added
1675+
if hasLegacyCondition || needsUpdateOS || needsUpdateFiles || needsImagePulledFromRegistry {
1676+
// Add the new ImageModeStatusReporting conditions with default values only if they don't exist
1677+
now := metav1.Now()
1678+
1679+
if needsUpdateFiles {
1680+
defaultCondition := metav1.Condition{
1681+
Type: string(mcfgv1.MachineConfigNodeUpdateFiles),
1682+
Message: fmt.Sprintf("This node has not yet entered the %s phase", string(mcfgv1.MachineConfigNodeUpdateFiles)),
1683+
Reason: "NotYetOccurred",
1684+
LastTransitionTime: now,
1685+
Status: metav1.ConditionFalse,
1686+
}
1687+
newConditions = append(newConditions, defaultCondition)
1688+
}
1689+
1690+
if needsUpdateOS {
1691+
defaultCondition := metav1.Condition{
1692+
Type: string(mcfgv1.MachineConfigNodeUpdateOS),
1693+
Message: fmt.Sprintf("This node has not yet entered the %s phase", string(mcfgv1.MachineConfigNodeUpdateOS)),
1694+
Reason: "NotYetOccurred",
1695+
LastTransitionTime: now,
1696+
Status: metav1.ConditionFalse,
1697+
}
1698+
newConditions = append(newConditions, defaultCondition)
1699+
}
1700+
1701+
if needsImagePulledFromRegistry {
1702+
defaultCondition := metav1.Condition{
1703+
Type: string(mcfgv1.MachineConfigNodeImagePulledFromRegistry),
1704+
Message: fmt.Sprintf("This node has not yet entered the %s phase", string(mcfgv1.MachineConfigNodeImagePulledFromRegistry)),
1705+
Reason: "NotYetOccurred",
1706+
LastTransitionTime: now,
1707+
Status: metav1.ConditionFalse,
1708+
}
1709+
newConditions = append(newConditions, defaultCondition)
1710+
}
1711+
16611712
mcnCopy := mcn.DeepCopy()
16621713
mcnCopy.Status.Conditions = newConditions
16631714
_, err = ctrl.client.MachineconfigurationV1().MachineConfigNodes().UpdateStatus(context.TODO(), mcnCopy, metav1.UpdateOptions{})
16641715
if err != nil {
1665-
return cleanedCount, fmt.Errorf("failed to update MCN %s after removing legacy condition: %w", mcn.Name, err)
1716+
return migratedCount, fmt.Errorf("failed to update MCN %s during condition migration to ImageModeStatusReporting: %w", mcn.Name, err)
16661717
}
1667-
cleanedCount++
1668-
klog.Infof("Successfully removed MachineConfigNodeUpdateFilesAndOS condition from MCN %s", mcn.Name)
1718+
migratedCount++
1719+
1720+
// Create descriptive log message based on what was done
1721+
var actions []string
1722+
if hasLegacyCondition {
1723+
actions = append(actions, "removed legacy MachineConfigNodeUpdateFilesAndOS condition")
1724+
}
1725+
if needsUpdateFiles || needsUpdateOS || needsImagePulledFromRegistry {
1726+
var addedConditions []string
1727+
if needsUpdateFiles {
1728+
addedConditions = append(addedConditions, "MachineConfigNodeUpdateFiles")
1729+
}
1730+
if needsUpdateOS {
1731+
addedConditions = append(addedConditions, "MachineConfigNodeUpdateOS")
1732+
}
1733+
if needsImagePulledFromRegistry {
1734+
addedConditions = append(addedConditions, "MachineConfigNodeImagePulledFromRegistry")
1735+
}
1736+
actions = append(actions, fmt.Sprintf("added %s condition(s)", strings.Join(addedConditions, ", ")))
1737+
}
1738+
1739+
klog.Infof("Successfully migrated conditions for MCN %s: %s", mcn.Name, strings.Join(actions, " and "))
16691740
}
16701741
}
16711742

1672-
return cleanedCount, nil
1743+
return migratedCount, nil
16731744
}
16741745

1675-
// performStartupLegacyCleanup runs once at controller startup to clean up any existing
1676-
// MachineConfigNodeUpdateFilesAndOS conditions when ImageModeStatusReporting is enabled.
1746+
// performImageModeStatusReportingConditionMigration runs once at controller startup to migrate MCN conditions
1747+
// from legacy MachineConfigNodeUpdateFilesAndOS condition to new ImageModeStatusReporting condition format.
16771748
// This runs in a goroutine to avoid blocking controller startup.
1678-
func (ctrl *Controller) performStartupLegacyCleanup() {
1679-
klog.Info("Starting one-time legacy MCN condition cleanup")
1749+
func (ctrl *Controller) performImageModeStatusReportingConditionMigration() {
1750+
klog.Info("Starting one-time MCN condition migration to ImageModeStatusReporting format")
16801751

1681-
cleanedCount, err := ctrl.cleanupLegacyMCNConditions()
1752+
migratedCount, err := ctrl.migrateMCNConditionsToImageModeStatusReporting()
16821753
if err != nil {
1683-
klog.Errorf("Failed to clean up legacy MCN conditions: %v", err)
1754+
klog.Errorf("Failed to migrate MCN condition formats to ImageModeStatusReporting: %v", err)
16841755
return
16851756
}
16861757

1687-
if cleanedCount > 0 {
1688-
klog.Infof("Completed legacy MCN condition cleanup for %d total nodes", cleanedCount)
1758+
if migratedCount > 0 {
1759+
klog.Infof("Completed MCN condition migration to ImageModeStatusReporting format for %d total nodes", migratedCount)
16891760
} else {
1690-
klog.Info("No legacy MCN conditions found requiring cleanup")
1761+
klog.Info("No MCN condition migration to ImageModeStatusReporting format required")
16911762
}
16921763
}

0 commit comments

Comments
 (0)