|
6 | 6 | "fmt" |
7 | 7 | "reflect" |
8 | 8 | "sort" |
| 9 | + "strings" |
9 | 10 | "time" |
10 | 11 |
|
11 | 12 | helpers "github.com/openshift/machine-config-operator/pkg/helpers" |
@@ -266,11 +267,11 @@ func (ctrl *Controller) Run(workers int, stopCh <-chan struct{}) { |
266 | 267 | defer klog.Info("Shutting down MachineConfigController-NodeController") |
267 | 268 |
|
268 | 269 | // 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 |
272 | 273 | if ctrl.fgHandler.Enabled(features.FeatureGateImageModeStatusReporting) { |
273 | | - go ctrl.performStartupLegacyCleanup() |
| 274 | + go ctrl.performImageModeStatusReportingConditionMigration() |
274 | 275 | } |
275 | 276 |
|
276 | 277 | for i := 0; i < workers; i++ { |
@@ -1630,63 +1631,133 @@ func (ctrl *Controller) isConfigAndBuildPresent(mosc *mcfgv1.MachineOSConfig, mo |
1630 | 1631 | return (mosc != nil && mosb != nil) |
1631 | 1632 | } |
1632 | 1633 |
|
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) { |
1638 | 1640 | // Get all MachineConfigNodes |
1639 | 1641 | mcns, err := ctrl.client.MachineconfigurationV1().MachineConfigNodes().List(context.TODO(), metav1.ListOptions{}) |
1640 | 1642 | 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) |
1642 | 1644 | } |
1643 | 1645 |
|
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 |
1646 | 1648 | for _, mcn := range mcns.Items { |
1647 | 1649 | // Check if the legacy condition exists and filter it out |
1648 | 1650 | 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 |
1649 | 1656 | var newConditions []metav1.Condition |
1650 | 1657 | for _, condition := range mcn.Status.Conditions { |
1651 | 1658 | if condition.Type == string(mcfgv1.MachineConfigNodeUpdateFilesAndOS) { |
1652 | 1659 | hasLegacyCondition = true |
1653 | 1660 | klog.V(4).Infof("Removing legacy MachineConfigNodeUpdateFilesAndOS condition from MCN %s", mcn.Name) |
1654 | 1661 | 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 |
1655 | 1668 | } |
| 1669 | + |
1656 | 1670 | newConditions = append(newConditions, condition) |
1657 | 1671 | } |
1658 | 1672 |
|
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 | + |
1661 | 1712 | mcnCopy := mcn.DeepCopy() |
1662 | 1713 | mcnCopy.Status.Conditions = newConditions |
1663 | 1714 | _, err = ctrl.client.MachineconfigurationV1().MachineConfigNodes().UpdateStatus(context.TODO(), mcnCopy, metav1.UpdateOptions{}) |
1664 | 1715 | 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) |
1666 | 1717 | } |
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 ")) |
1669 | 1740 | } |
1670 | 1741 | } |
1671 | 1742 |
|
1672 | | - return cleanedCount, nil |
| 1743 | + return migratedCount, nil |
1673 | 1744 | } |
1674 | 1745 |
|
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. |
1677 | 1748 | // 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") |
1680 | 1751 |
|
1681 | | - cleanedCount, err := ctrl.cleanupLegacyMCNConditions() |
| 1752 | + migratedCount, err := ctrl.migrateMCNConditionsToImageModeStatusReporting() |
1682 | 1753 | 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) |
1684 | 1755 | return |
1685 | 1756 | } |
1686 | 1757 |
|
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) |
1689 | 1760 | } else { |
1690 | | - klog.Info("No legacy MCN conditions found requiring cleanup") |
| 1761 | + klog.Info("No MCN condition migration to ImageModeStatusReporting format required") |
1691 | 1762 | } |
1692 | 1763 | } |
0 commit comments