@@ -32,12 +32,12 @@ import (
3232 apierrors "k8s.io/apimachinery/pkg/api/errors"
3333 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3434 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
35- "k8s.io/apimachinery/pkg/types"
3635 "k8s.io/apimachinery/pkg/util/sets"
3736 "k8s.io/client-go/discovery"
3837 "k8s.io/klog/v2"
3938 "k8s.io/utils/pointer"
4039 "sigs.k8s.io/controller-runtime/pkg/client"
40+ "sigs.k8s.io/yaml"
4141
4242 clusterv1alpha3 "sigs.k8s.io/cluster-api/api/v1alpha3"
4343 clusterv1alpha4 "sigs.k8s.io/cluster-api/api/v1alpha4"
@@ -473,7 +473,7 @@ func ClusterctlUpgradeSpec(ctx context.Context, inputGetter func() ClusterctlUpg
473473 client.MatchingLabels {clusterv1 .ClusterNameLabel : workLoadClusterName },
474474 )
475475 Expect (err ).NotTo (HaveOccurred ())
476- return matchUnstructuredLists (preUpgradeMachineList , postUpgradeMachineList )
476+ return validateMachineRollout (preUpgradeMachineList , postUpgradeMachineList )
477477 }, "3m" , "30s" ).Should (BeTrue (), "Machines should remain the same after the upgrade" )
478478
479479 // After upgrading we are sure the version is the latest version of the API,
@@ -739,25 +739,56 @@ func waitForClusterDeletedV1alpha4(ctx context.Context, input waitForClusterDele
739739 }, intervals ... ).Should (BeTrue ())
740740}
741741
742- func matchUnstructuredLists (l1 * unstructured.UnstructuredList , l2 * unstructured.UnstructuredList ) bool {
743- if l1 == nil && l2 == nil {
742+ // validateMachineRollout compares preMachineList and postMachineList to detect a rollout.
743+ // Note: we are using unstructured lists because the Machines have different apiVersions.
744+ func validateMachineRollout (preMachineList , postMachineList * unstructured.UnstructuredList ) bool {
745+ if preMachineList == nil && postMachineList == nil {
744746 return true
745747 }
746- if l1 == nil || l2 == nil {
748+ if preMachineList == nil || postMachineList == nil {
747749 return false
748750 }
749- if len (l1 .Items ) != len (l2 .Items ) {
750- return false
751+
752+ if names (preMachineList ).Equal (names (postMachineList )) {
753+ return true
754+ }
755+
756+ log .Logf ("Rollout detected" )
757+ newMachines := names (postMachineList ).Difference (names (preMachineList ))
758+ deletedMachines := names (preMachineList ).Difference (names (postMachineList ))
759+
760+ if len (newMachines ) > 0 {
761+ log .Logf ("Detected new Machines" )
762+ for _ , obj := range postMachineList .Items {
763+ obj := obj
764+ if newMachines .Has (obj .GetName ()) {
765+ resourceYAML , err := yaml .Marshal (obj )
766+ Expect (err ).ToNot (HaveOccurred ())
767+ log .Logf ("New Machine %s:\n %s" , klog .KObj (& obj ), resourceYAML )
768+ }
769+ }
751770 }
752- s1 := sets.Set [string ]{}
753- for _ , i := range l1 .Items {
754- s1 .Insert (types.NamespacedName {Namespace : i .GetNamespace (), Name : i .GetName ()}.String ())
771+
772+ if len (deletedMachines ) > 0 {
773+ log .Logf ("Detected deleted Machines" )
774+ for _ , obj := range preMachineList .Items {
775+ obj := obj
776+ if deletedMachines .Has (obj .GetName ()) {
777+ resourceYAML , err := yaml .Marshal (obj )
778+ Expect (err ).ToNot (HaveOccurred ())
779+ log .Logf ("Deleted Machine %s:\n %s" , klog .KObj (& obj ), resourceYAML )
780+ }
781+ }
755782 }
756- s2 := sets.Set [string ]{}
757- for _ , i := range l2 .Items {
758- s2 .Insert (types.NamespacedName {Namespace : i .GetNamespace (), Name : i .GetName ()}.String ())
783+ return false
784+ }
785+
786+ func names (objs * unstructured.UnstructuredList ) sets.Set [string ] {
787+ ret := sets.Set [string ]{}
788+ for _ , obj := range objs .Items {
789+ ret .Insert (obj .GetName ())
759790 }
760- return s1 . Equal ( s2 )
791+ return ret
761792}
762793
763794// getValueOrFallback returns the input value unless it is empty, then it returns the fallback input.
0 commit comments