@@ -32,12 +32,12 @@ import (
32
32
apierrors "k8s.io/apimachinery/pkg/api/errors"
33
33
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
34
34
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
35
- "k8s.io/apimachinery/pkg/types"
36
35
"k8s.io/apimachinery/pkg/util/sets"
37
36
"k8s.io/client-go/discovery"
38
37
"k8s.io/klog/v2"
39
38
"k8s.io/utils/pointer"
40
39
"sigs.k8s.io/controller-runtime/pkg/client"
40
+ "sigs.k8s.io/yaml"
41
41
42
42
clusterv1alpha3 "sigs.k8s.io/cluster-api/api/v1alpha3"
43
43
clusterv1alpha4 "sigs.k8s.io/cluster-api/api/v1alpha4"
@@ -473,7 +473,7 @@ func ClusterctlUpgradeSpec(ctx context.Context, inputGetter func() ClusterctlUpg
473
473
client.MatchingLabels {clusterv1 .ClusterNameLabel : workLoadClusterName },
474
474
)
475
475
Expect (err ).NotTo (HaveOccurred ())
476
- return matchUnstructuredLists (preUpgradeMachineList , postUpgradeMachineList )
476
+ return validateMachineRollout (preUpgradeMachineList , postUpgradeMachineList )
477
477
}, "3m" , "30s" ).Should (BeTrue (), "Machines should remain the same after the upgrade" )
478
478
479
479
// 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
739
739
}, intervals ... ).Should (BeTrue ())
740
740
}
741
741
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 {
744
746
return true
745
747
}
746
- if l1 == nil || l2 == nil {
748
+ if preMachineList == nil || postMachineList == nil {
747
749
return false
748
750
}
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
+ }
751
770
}
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
+ }
755
782
}
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 ())
759
790
}
760
- return s1 . Equal ( s2 )
791
+ return ret
761
792
}
762
793
763
794
// getValueOrFallback returns the input value unless it is empty, then it returns the fallback input.
0 commit comments