|
| 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