Skip to content

Commit

Permalink
Merge pull request #8953 from sbueringer/pr-e2e-improve-rollout-logging
Browse files Browse the repository at this point in the history
🌱 test/e2e: improve logging for a detected rollout
  • Loading branch information
k8s-ci-robot authored Jul 6, 2023
2 parents 80b1e2a + cd8c474 commit a301f20
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 15 deletions.
59 changes: 45 additions & 14 deletions test/e2e/clusterctl_upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ import (
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/discovery"
"k8s.io/klog/v2"
"k8s.io/utils/pointer"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/yaml"

clusterv1alpha3 "sigs.k8s.io/cluster-api/api/v1alpha3"
clusterv1alpha4 "sigs.k8s.io/cluster-api/api/v1alpha4"
Expand Down Expand Up @@ -473,7 +473,7 @@ func ClusterctlUpgradeSpec(ctx context.Context, inputGetter func() ClusterctlUpg
client.MatchingLabels{clusterv1.ClusterNameLabel: workLoadClusterName},
)
Expect(err).NotTo(HaveOccurred())
return matchUnstructuredLists(preUpgradeMachineList, postUpgradeMachineList)
return validateMachineRollout(preUpgradeMachineList, postUpgradeMachineList)
}, "3m", "30s").Should(BeTrue(), "Machines should remain the same after the upgrade")

// After upgrading we are sure the version is the latest version of the API,
Expand Down Expand Up @@ -739,25 +739,56 @@ func waitForClusterDeletedV1alpha4(ctx context.Context, input waitForClusterDele
}, intervals...).Should(BeTrue())
}

func matchUnstructuredLists(l1 *unstructured.UnstructuredList, l2 *unstructured.UnstructuredList) bool {
if l1 == nil && l2 == nil {
// validateMachineRollout compares preMachineList and postMachineList to detect a rollout.
// Note: we are using unstructured lists because the Machines have different apiVersions.
func validateMachineRollout(preMachineList, postMachineList *unstructured.UnstructuredList) bool {
if preMachineList == nil && postMachineList == nil {
return true
}
if l1 == nil || l2 == nil {
if preMachineList == nil || postMachineList == nil {
return false
}
if len(l1.Items) != len(l2.Items) {
return false

if names(preMachineList).Equal(names(postMachineList)) {
return true
}

log.Logf("Rollout detected")
newMachines := names(postMachineList).Difference(names(preMachineList))
deletedMachines := names(preMachineList).Difference(names(postMachineList))

if len(newMachines) > 0 {
log.Logf("Detected new Machines")
for _, obj := range postMachineList.Items {
obj := obj
if newMachines.Has(obj.GetName()) {
resourceYAML, err := yaml.Marshal(obj)
Expect(err).ToNot(HaveOccurred())
log.Logf("New Machine %s:\n%s", klog.KObj(&obj), resourceYAML)
}
}
}
s1 := sets.Set[string]{}
for _, i := range l1.Items {
s1.Insert(types.NamespacedName{Namespace: i.GetNamespace(), Name: i.GetName()}.String())

if len(deletedMachines) > 0 {
log.Logf("Detected deleted Machines")
for _, obj := range preMachineList.Items {
obj := obj
if deletedMachines.Has(obj.GetName()) {
resourceYAML, err := yaml.Marshal(obj)
Expect(err).ToNot(HaveOccurred())
log.Logf("Deleted Machine %s:\n%s", klog.KObj(&obj), resourceYAML)
}
}
}
s2 := sets.Set[string]{}
for _, i := range l2.Items {
s2.Insert(types.NamespacedName{Namespace: i.GetNamespace(), Name: i.GetName()}.String())
return false
}

func names(objs *unstructured.UnstructuredList) sets.Set[string] {
ret := sets.Set[string]{}
for _, obj := range objs.Items {
ret.Insert(obj.GetName())
}
return s1.Equal(s2)
return ret
}

// getValueOrFallback returns the input value unless it is empty, then it returns the fallback input.
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/self_hosted.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ func SelfHostedSpec(ctx context.Context, inputGetter func() SelfHostedSpecInput)
client.MatchingLabels{clusterv1.ClusterNameLabel: workloadClusterName},
)
Expect(err).NotTo(HaveOccurred(), "Failed to list machines after move")
return matchUnstructuredLists(preMoveMachineList, postMoveMachineList)
return validateMachineRollout(preMoveMachineList, postMoveMachineList)
}, "3m", "30s").Should(BeTrue(), "Machines should not roll out after move to self-hosted cluster")

if input.SkipUpgrade {
Expand Down

0 comments on commit a301f20

Please sign in to comment.