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
23 changes: 23 additions & 0 deletions internal/operator-controller/applier/phase.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package applier

import (
"cmp"
"slices"

"k8s.io/apimachinery/pkg/runtime/schema"

ocv1 "github.com/operator-framework/operator-controller/api/v1"
Expand Down Expand Up @@ -111,6 +114,23 @@ func init() {
}
}

// Sort objects within the phase deterministically by Group, Version, Kind, Namespace, Name
// to ensure consistent ordering regardless of input order. This is critical for
// Helm-to-Boxcutter migration where the same resources may come from different sources
// (Helm release manifest vs bundle manifest) and need to produce identical phases.
func compareClusterExtensionRevisionObjects(a, b ocv1.ClusterExtensionRevisionObject) int {
Copy link
Contributor

Choose a reason for hiding this comment

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

would using cmp.Compare be a nice alternative here? e.g.

cmp.Compare(cmp.Or(aGVK.Group, bGVK.Group), cmp.Or(...), ...)

Copy link
Contributor

Choose a reason for hiding this comment

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

return cmp.Compare(
  cmp.Or(a.Object.GroupVersionKind().Group, b.Object.GroupVersionKind().Group),
  cmp.Or(a.Object.GroupVersionKind().Version, b.Object.GroupVersionKind().Version),
  cmp.Or(a.Object.GroupVersionKind().Kind, b.Object.GroupVersionKind().Kind),
  cmp.Or(a.Object.GetNamespace(), b.Object.GetNamespace()),
  cmp.Or(a.Object.GetName(), b.Object.GetName()),
)

Copy link
Contributor

Choose a reason for hiding this comment

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

only a suggestion - if it doesn't spark joy, I'm happy to approve. Let me know.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not familiar with that API, right now, it's not sparking joy (😉), but what's here works... so let's leave as-is.

Copy link
Contributor

Choose a reason for hiding this comment

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

np - it's the cool kid in town: https://pkg.go.dev/cmp basically obviates a lot the branching here

aGVK := a.Object.GroupVersionKind()
bGVK := b.Object.GroupVersionKind()

return cmp.Or(
cmp.Compare(aGVK.Group, bGVK.Group),
cmp.Compare(aGVK.Version, bGVK.Version),
cmp.Compare(aGVK.Kind, bGVK.Kind),
cmp.Compare(a.Object.GetNamespace(), b.Object.GetNamespace()),
cmp.Compare(a.Object.GetName(), b.Object.GetName()),
)
}

// PhaseSort takes an unsorted list of objects and organizes them into sorted phases.
// Each phase will be applied in order according to DefaultPhaseOrder. Objects
// within a single phase are applied simultaneously.
Expand All @@ -125,6 +145,9 @@ func PhaseSort(unsortedObjs []ocv1.ClusterExtensionRevisionObject) []ocv1.Cluste

for _, phaseName := range defaultPhaseOrder {
if objs, ok := phaseMap[phaseName]; ok {
// Sort objects within the phase deterministically
slices.SortFunc(objs, compareClusterExtensionRevisionObjects)

phasesSorted = append(phasesSorted, ocv1.ClusterExtensionRevisionPhase{
Name: string(phaseName),
Objects: objs,
Expand Down
Loading
Loading