Skip to content

Commit 819a296

Browse files
committed
CRD migration: extend storageVersionMigrationCache ttl to 10h and
refresh cash entries Signed-off-by: Stefan Büringer buringerst@vmware.com
1 parent 6a07e76 commit 819a296

File tree

8 files changed

+22
-19
lines changed

8 files changed

+22
-19
lines changed

controllers/crdmigrator/crd_migrator.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"encoding/json"
2323
"fmt"
2424
"strconv"
25+
"time"
2526

2627
"github.com/pkg/errors"
2728
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
@@ -164,7 +165,7 @@ func (r *CRDMigrator) setup(scheme *runtime.Scheme) error {
164165
r.configByCRDName[contract.CalculateCRDName(gvk.Group, gvk.Kind)] = cfg
165166
}
166167

167-
r.storageVersionMigrationCache = cache.New[objectEntry]()
168+
r.storageVersionMigrationCache = cache.New[objectEntry](1 * time.Hour)
168169
return nil
169170
}
170171

@@ -385,6 +386,9 @@ func (r *CRDMigrator) reconcileStorageVersionMigration(ctx context.Context, crd
385386
}
386387

387388
if _, alreadyMigrated := r.storageVersionMigrationCache.Has(e.Key()); alreadyMigrated {
389+
// Refresh the cache entry, so that we don't try to migrate the storage version for CRs that were
390+
// already migrated successfully in cases where storage migrations failed for a subset of the CRs.
391+
r.storageVersionMigrationCache.Add(e)
388392
continue
389393
}
390394

internal/controllers/clusterclass/clusterclass_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, opt
100100
return errors.Wrap(err, "failed setting up with a controller manager")
101101
}
102102

103-
r.discoverVariablesCache = cache.New[runtimeclient.CallExtensionCacheEntry]()
103+
r.discoverVariablesCache = cache.New[runtimeclient.CallExtensionCacheEntry](cache.DefaultTTL)
104104
return nil
105105
}
106106

internal/controllers/clusterclass/clusterclass_controller_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1158,7 +1158,7 @@ func TestReconciler_reconcileVariables(t *testing.T) {
11581158

11591159
r := &Reconciler{
11601160
RuntimeClient: fakeRuntimeClient,
1161-
discoverVariablesCache: cache.New[runtimeclient.CallExtensionCacheEntry](),
1161+
discoverVariablesCache: cache.New[runtimeclient.CallExtensionCacheEntry](cache.DefaultTTL),
11621162
}
11631163

11641164
// Pin the compatibility version used in variable CEL validation to 1.29, so we don't have to continuously refactor

internal/controllers/machine/machine_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, opt
184184
Scheme: mgr.GetScheme(),
185185
PredicateLogger: r.predicateLog,
186186
}
187-
r.reconcileDeleteCache = cache.New[cache.ReconcileEntry]()
187+
r.reconcileDeleteCache = cache.New[cache.ReconcileEntry](cache.DefaultTTL)
188188
return nil
189189
}
190190

internal/controllers/machine/machine_controller_test.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -827,8 +827,6 @@ func TestReconcileRequest(t *testing.T) {
827827
},
828828
}
829829

830-
time := metav1.Now()
831-
832830
testCluster := clusterv1.Cluster{
833831
ObjectMeta: metav1.ObjectMeta{
834832
Name: "test-cluster",
@@ -922,7 +920,7 @@ func TestReconcileRequest(t *testing.T) {
922920
clusterv1.MachineControlPlaneLabel: "",
923921
},
924922
Finalizers: []string{clusterv1.MachineFinalizer},
925-
DeletionTimestamp: &time,
923+
DeletionTimestamp: ptr.To(metav1.Now()),
926924
},
927925
Spec: clusterv1.MachineSpec{
928926
ClusterName: "test-cluster",
@@ -958,7 +956,7 @@ func TestReconcileRequest(t *testing.T) {
958956
Client: clientFake,
959957
ClusterCache: clustercache.NewFakeClusterCache(clientFake, client.ObjectKey{Name: testCluster.Name, Namespace: testCluster.Namespace}),
960958
recorder: record.NewFakeRecorder(10),
961-
reconcileDeleteCache: cache.New[cache.ReconcileEntry](),
959+
reconcileDeleteCache: cache.New[cache.ReconcileEntry](cache.DefaultTTL),
962960
externalTracker: external.ObjectTracker{
963961
Controller: externalfake.Controller{},
964962
Cache: &informertest.FakeInformers{},
@@ -1314,7 +1312,7 @@ func TestRemoveMachineFinalizerAfterDeleteReconcile(t *testing.T) {
13141312
mr := &Reconciler{
13151313
Client: c,
13161314
ClusterCache: clustercache.NewFakeClusterCache(c, client.ObjectKeyFromObject(testCluster)),
1317-
reconcileDeleteCache: cache.New[cache.ReconcileEntry](),
1315+
reconcileDeleteCache: cache.New[cache.ReconcileEntry](cache.DefaultTTL),
13181316
}
13191317
_, err := mr.Reconcile(ctx, reconcile.Request{NamespacedName: key})
13201318
g.Expect(err).ToNot(HaveOccurred())
@@ -1709,7 +1707,7 @@ func TestDrainNode(t *testing.T) {
17091707
r := &Reconciler{
17101708
Client: c,
17111709
ClusterCache: clustercache.NewFakeClusterCache(remoteClient, client.ObjectKeyFromObject(testCluster)),
1712-
reconcileDeleteCache: cache.New[cache.ReconcileEntry](),
1710+
reconcileDeleteCache: cache.New[cache.ReconcileEntry](cache.DefaultTTL),
17131711
}
17141712

17151713
testMachine.Status.NodeRef = &corev1.ObjectReference{
@@ -1840,7 +1838,7 @@ func TestDrainNode_withCaching(t *testing.T) {
18401838
WithObjects(remoteObjs...).
18411839
Build()
18421840

1843-
reconcileDeleteCache := cache.New[cache.ReconcileEntry]()
1841+
reconcileDeleteCache := cache.New[cache.ReconcileEntry](cache.DefaultTTL)
18441842
r := &Reconciler{
18451843
Client: c,
18461844
ClusterCache: clustercache.NewFakeClusterCache(remoteClient, client.ObjectKeyFromObject(testCluster)),
@@ -2498,7 +2496,7 @@ func TestShouldWaitForNodeVolumes(t *testing.T) {
24982496
r := &Reconciler{
24992497
Client: fakeClient,
25002498
ClusterCache: clustercache.NewFakeClusterCache(remoteFakeClient, client.ObjectKeyFromObject(testCluster)),
2501-
reconcileDeleteCache: cache.New[cache.ReconcileEntry](),
2499+
reconcileDeleteCache: cache.New[cache.ReconcileEntry](cache.DefaultTTL),
25022500
}
25032501

25042502
testMachine.Status.NodeRef = &corev1.ObjectReference{
@@ -3335,7 +3333,7 @@ func TestNodeDeletion(t *testing.T) {
33353333
ClusterCache: clustercache.NewFakeClusterCache(fakeClient, client.ObjectKeyFromObject(&testCluster)),
33363334
recorder: record.NewFakeRecorder(10),
33373335
nodeDeletionRetryTimeout: 10 * time.Millisecond,
3338-
reconcileDeleteCache: cache.New[cache.ReconcileEntry](),
3336+
reconcileDeleteCache: cache.New[cache.ReconcileEntry](cache.DefaultTTL),
33393337
}
33403338

33413339
cluster := testCluster.DeepCopy()
@@ -3469,7 +3467,7 @@ func TestNodeDeletionWithoutNodeRefFallback(t *testing.T) {
34693467
ClusterCache: clustercache.NewFakeClusterCache(fakeClient, client.ObjectKeyFromObject(&testCluster)),
34703468
recorder: record.NewFakeRecorder(10),
34713469
nodeDeletionRetryTimeout: 10 * time.Millisecond,
3472-
reconcileDeleteCache: cache.New[cache.ReconcileEntry](),
3470+
reconcileDeleteCache: cache.New[cache.ReconcileEntry](cache.DefaultTTL),
34733471
}
34743472

34753473
s := &scope{

internal/runtime/client/client_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -809,7 +809,7 @@ func TestClient_CallExtension(t *testing.T) {
809809

810810
// Call again with caching.
811811
serverCallCount = 0
812-
cache := cache.New[runtimeclient.CallExtensionCacheEntry]()
812+
cache := cache.New[runtimeclient.CallExtensionCacheEntry](cache.DefaultTTL)
813813
err = c.CallExtension(context.Background(), tt.args.hook, obj, tt.args.name, tt.args.request, tt.args.response,
814814
runtimeclient.WithCaching{Cache: cache, CacheKeyFunc: cacheKeyFunc})
815815
if tt.wantErr {

util/cache/cache.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ import (
2626
)
2727

2828
const (
29-
// ttl is the duration for which we keep entries in the cache.
30-
ttl = 10 * time.Minute
29+
// DefaultTTL is the duration for which we keep entries in the cache.
30+
DefaultTTL = 10 * time.Minute
3131

3232
// expirationInterval is the interval in which we will remove expired entries
3333
// from the cache.
@@ -52,7 +52,8 @@ type Cache[E Entry] interface {
5252
}
5353

5454
// New creates a new cache.
55-
func New[E Entry]() Cache[E] {
55+
// ttl is the duration for which we keep entries in the cache.
56+
func New[E Entry](ttl time.Duration) Cache[E] {
5657
r := &cache[E]{
5758
Store: kcache.NewTTLStore(func(obj interface{}) (string, error) {
5859
// We only add objects of type E to the cache, so it's safe to cast to E.

util/cache/cache_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
func TestCache(t *testing.T) {
3030
g := NewWithT(t)
3131

32-
c := New[ReconcileEntry]()
32+
c := New[ReconcileEntry](DefaultTTL)
3333

3434
machine := &clusterv1.Machine{
3535
ObjectMeta: metav1.ObjectMeta{

0 commit comments

Comments
 (0)