Skip to content

Commit 4c48c75

Browse files
committed
tests: handle updates that only change labels
1 parent 7076e97 commit 4c48c75

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

config/tests/samples/create/samples.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import (
3939
"github.com/ghodss/yaml" //nolint:depguard
4040
apierrors "k8s.io/apimachinery/pkg/api/errors"
4141
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
42+
"k8s.io/apimachinery/pkg/types"
4243
"k8s.io/apimachinery/pkg/util/sets"
4344
"k8s.io/apimachinery/pkg/util/wait"
4445
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -149,12 +150,44 @@ func RunCreateDeleteTest(t *Harness, opt CreateDeleteTestOptions) {
149150
if len(opt.Updates) != 0 {
150151
// treat as a patch
151152
for _, updateUnstruct := range opt.Updates {
153+
// We compute the diff so we can see if we only update labels
154+
old := &unstructured.Unstructured{}
155+
old.SetGroupVersionKind(updateUnstruct.GroupVersionKind())
156+
157+
id := types.NamespacedName{
158+
Name: updateUnstruct.GetName(),
159+
Namespace: updateUnstruct.GetNamespace(),
160+
}
161+
162+
if err := t.GetClient().Get(ctx, id, old); err != nil {
163+
t.Fatalf("error getting existing resource: %v", err)
164+
}
165+
166+
oldGeneration := old.GetGeneration()
167+
152168
if err := t.GetClient().Patch(ctx, updateUnstruct, client.Apply, client.FieldOwner("kcc-tests"), client.ForceOwnership); err != nil {
153169
t.Fatalf("error updating resource: %v", err)
154170
}
171+
172+
newGeneration := updateUnstruct.GetGeneration()
173+
174+
// Check if we are going to update the metadata.generation; only spec updates increment generation.
175+
// Note in particular that updates that only touch labels/annotations do not update the generation field
176+
willUpdateMetadataGeneration := true
177+
if newGeneration == oldGeneration {
178+
willUpdateMetadataGeneration = false
179+
t.Logf("WARN: metadata.generation did not change for %v; consider updating this test to include an update to the spec", id)
180+
}
181+
155182
if opt.CreateInOrder && !opt.SkipWaitForReady {
156183
waitForReadySingleResource(t, updateUnstruct, DefaultWaitForReadyTimeout)
157184
}
185+
186+
if !willUpdateMetadataGeneration {
187+
// Allow some time for reconciliation
188+
t.Logf("sleeping for two seconds to allow for reconiliation (on an update that did not update metadata.generation)")
189+
time.Sleep(2 * time.Second)
190+
}
158191
}
159192

160193
if !opt.CreateInOrder && !opt.SkipWaitForReady {

0 commit comments

Comments
 (0)