Skip to content

Commit dec46d0

Browse files
authored
Merge pull request kubernetes-sigs#10530 from sbueringer/pr-rv-stable
🌱 Ensure resourceVersions are stable
2 parents 7bf7836 + bdd8ba2 commit dec46d0

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

test/e2e/quick_start_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ var _ = Describe("When following the Cluster API quick-start", func() {
6363
framework.ExpFinalizersAssertion,
6464
framework.DockerInfraFinalizersAssertion,
6565
)
66+
// This check ensures that the resourceVersions are stable, i.e. it verifies there are no
67+
// continuous reconciles when everything should be stable.
68+
framework.ValidateResourceVersionStable(ctx, proxy, namespace)
6669
},
6770
}
6871
})
@@ -104,6 +107,9 @@ var _ = Describe("When following the Cluster API quick-start with ClusterClass [
104107
framework.ExpFinalizersAssertion,
105108
framework.DockerInfraFinalizersAssertion,
106109
)
110+
// This check ensures that the resourceVersions are stable, i.e. it verifies there are no
111+
// continuous reconciles when everything should be stable.
112+
framework.ValidateResourceVersionStable(ctx, proxy, namespace)
107113
},
108114
}
109115
})
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
Copyright 2024 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package framework
18+
19+
import (
20+
"context"
21+
"fmt"
22+
"time"
23+
24+
. "github.com/onsi/gomega"
25+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26+
"sigs.k8s.io/controller-runtime/pkg/client"
27+
28+
clusterctlcluster "sigs.k8s.io/cluster-api/cmd/clusterctl/client/cluster"
29+
)
30+
31+
// ValidateResourceVersionStable checks that resource versions are stable.
32+
func ValidateResourceVersionStable(ctx context.Context, proxy ClusterProxy, namespace string) {
33+
// Wait until resource versions are stable for a bit.
34+
var previousResourceVersions map[string]string
35+
Eventually(func(g Gomega) {
36+
objectsWithResourceVersion, err := getObjectsWithResourceVersion(ctx, proxy, namespace)
37+
g.Expect(err).ToNot(HaveOccurred())
38+
39+
defer func() {
40+
// Set current resource versions as previous resource versions for the next try.
41+
previousResourceVersions = objectsWithResourceVersion
42+
}()
43+
// This is intentionally failing on the first run.
44+
g.Expect(objectsWithResourceVersion).To(Equal(previousResourceVersions))
45+
}, 1*time.Minute, 15*time.Second).Should(Succeed(), "Resource versions never became stable")
46+
47+
// Verify resource versions are stable for a while.
48+
Consistently(func(g Gomega) {
49+
objectsWithResourceVersion, err := getObjectsWithResourceVersion(ctx, proxy, namespace)
50+
g.Expect(err).ToNot(HaveOccurred())
51+
g.Expect(objectsWithResourceVersion).To(Equal(previousResourceVersions))
52+
}, 2*time.Minute, 15*time.Second).Should(Succeed(), "Resource versions didn't stay stable")
53+
}
54+
55+
func getObjectsWithResourceVersion(ctx context.Context, proxy ClusterProxy, namespace string) (map[string]string, error) {
56+
graph, err := clusterctlcluster.GetOwnerGraph(ctx, namespace, proxy.GetKubeconfigPath())
57+
if err != nil {
58+
return nil, err
59+
}
60+
61+
objectsWithResourceVersion := map[string]string{}
62+
for _, node := range graph {
63+
nodeNamespacedName := client.ObjectKey{Namespace: node.Object.Namespace, Name: node.Object.Name}
64+
obj := &metav1.PartialObjectMetadata{
65+
TypeMeta: metav1.TypeMeta{
66+
APIVersion: node.Object.APIVersion,
67+
Kind: node.Object.Kind,
68+
},
69+
}
70+
if err := proxy.GetClient().Get(ctx, nodeNamespacedName, obj); err != nil {
71+
return nil, err
72+
}
73+
objectsWithResourceVersion[fmt.Sprintf("%s/%s/%s", node.Object.Kind, node.Object.Namespace, node.Object.Name)] = obj.ResourceVersion
74+
}
75+
return objectsWithResourceVersion, nil
76+
}

0 commit comments

Comments
 (0)