Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🌱 Add SSA cache metrics #11635

Merged
merged 6 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion controlplane/kubeadm/internal/controllers/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (r *KubeadmControlPlaneReconciler) SetupWithManager(ctx context.Context, mg

r.controller = c
r.recorder = mgr.GetEventRecorderFor("kubeadmcontrolplane-controller")
r.ssaCache = ssa.NewCache()
r.ssaCache = ssa.NewCache("controlplane/kubeadm")
sbueringer marked this conversation as resolved.
Show resolved Hide resolved

if r.managementCluster == nil {
r.managementCluster = &internal.Management{
Expand Down
4 changes: 2 additions & 2 deletions controlplane/kubeadm/internal/controllers/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1351,7 +1351,7 @@ kubernetesVersion: metav1.16.1
Status: internal.ClusterStatus{},
},
},
ssaCache: ssa.NewCache(),
ssaCache: ssa.NewCache("test-controller"),
}

result, err := r.Reconcile(ctx, ctrl.Request{NamespacedName: util.ObjectKey(kcp)})
Expand Down Expand Up @@ -1678,7 +1678,7 @@ func TestKubeadmControlPlaneReconciler_syncMachines(t *testing.T) {
reconciler := &KubeadmControlPlaneReconciler{
Client: env,
SecretCachingClient: secretCachingClient,
ssaCache: ssa.NewCache(),
ssaCache: ssa.NewCache("test-controller"),
}
g.Expect(reconciler.syncMachines(ctx, controlPlane)).To(Succeed())

Expand Down
2 changes: 1 addition & 1 deletion controlplane/kubeadm/internal/controllers/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func TestKubeadmControlPlaneReconciler_RolloutStrategy_ScaleUp(t *testing.T) {
Status: internal.ClusterStatus{Nodes: 1},
},
},
ssaCache: ssa.NewCache(),
ssaCache: ssa.NewCache("test-controller"),
}
controlPlane := &internal.ControlPlane{
KCP: kcp,
Expand Down
2 changes: 1 addition & 1 deletion exp/internal/controllers/machinepool_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func (r *MachinePoolReconciler) SetupWithManager(ctx context.Context, mgr ctrl.M
Scheme: mgr.GetScheme(),
PredicateLogger: r.predicateLog,
}
r.ssaCache = ssa.NewCache()
r.ssaCache = ssa.NewCache("machinepool")

return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import (
const (
clusterName = "test-cluster"
wrongNamespace = "wrong-namespace"
testController = "test-controller"
)

func TestReconcileMachinePoolPhases(t *testing.T) {
Expand Down Expand Up @@ -1424,7 +1425,7 @@ func TestReconcileMachinePoolMachines(t *testing.T) {

r := &MachinePoolReconciler{
Client: env,
ssaCache: ssa.NewCache(),
ssaCache: ssa.NewCache(testController),
externalTracker: external.ObjectTracker{
Controller: externalfake.Controller{},
Cache: &informertest.FakeInformers{},
Expand Down Expand Up @@ -1493,7 +1494,7 @@ func TestReconcileMachinePoolMachines(t *testing.T) {

r := &MachinePoolReconciler{
Client: env,
ssaCache: ssa.NewCache(),
ssaCache: ssa.NewCache(testController),
externalTracker: external.ObjectTracker{
Controller: externalfake.Controller{},
Cache: &informertest.FakeInformers{},
Expand Down Expand Up @@ -1558,7 +1559,7 @@ func TestReconcileMachinePoolMachines(t *testing.T) {

r := &MachinePoolReconciler{
Client: env,
ssaCache: ssa.NewCache(),
ssaCache: ssa.NewCache(testController),
}

scope := &scope{
Expand Down
3 changes: 0 additions & 3 deletions internal/controllers/machine/machine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ import (
"sigs.k8s.io/cluster-api/controllers/noderefutil"
"sigs.k8s.io/cluster-api/feature"
"sigs.k8s.io/cluster-api/internal/controllers/machine/drain"
"sigs.k8s.io/cluster-api/internal/util/ssa"
"sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/annotations"
"sigs.k8s.io/cluster-api/util/cache"
Expand Down Expand Up @@ -107,7 +106,6 @@ type Reconciler struct {
// nodeDeletionRetryTimeout determines how long the controller will retry deleting a node
// during a single reconciliation.
nodeDeletionRetryTimeout time.Duration
ssaCache ssa.Cache

// reconcileDeleteCache is used to store when reconcileDelete should not be executed before a
// specific time for a specific Request. This is used to implement rate-limiting to avoid
Expand Down Expand Up @@ -182,7 +180,6 @@ func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, opt
Scheme: mgr.GetScheme(),
PredicateLogger: r.predicateLog,
}
r.ssaCache = ssa.NewCache()
r.reconcileDeleteCache = cache.New[cache.ReconcileEntry]()
return nil
}
Expand Down
3 changes: 0 additions & 3 deletions internal/controllers/machine/machine_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ import (
"sigs.k8s.io/cluster-api/controllers/external"
externalfake "sigs.k8s.io/cluster-api/controllers/external/fake"
"sigs.k8s.io/cluster-api/feature"
"sigs.k8s.io/cluster-api/internal/util/ssa"
"sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/cache"
"sigs.k8s.io/cluster-api/util/conditions"
Expand Down Expand Up @@ -958,7 +957,6 @@ func TestReconcileRequest(t *testing.T) {
r := &Reconciler{
Client: clientFake,
ClusterCache: clustercache.NewFakeClusterCache(clientFake, client.ObjectKey{Name: testCluster.Name, Namespace: testCluster.Namespace}),
ssaCache: ssa.NewCache(),
recorder: record.NewFakeRecorder(10),
reconcileDeleteCache: cache.New[cache.ReconcileEntry](),
externalTracker: external.ObjectTracker{
Expand Down Expand Up @@ -1254,7 +1252,6 @@ func TestMachineConditions(t *testing.T) {
Client: clientFake,
recorder: record.NewFakeRecorder(10),
ClusterCache: clustercache.NewFakeClusterCache(clientFake, client.ObjectKey{Name: testCluster.Name, Namespace: testCluster.Namespace}),
ssaCache: ssa.NewCache(),
externalTracker: external.ObjectTracker{
Controller: externalfake.Controller{},
Cache: &informertest.FakeInformers{},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, opt
}

r.recorder = mgr.GetEventRecorderFor("machinedeployment-controller")
r.ssaCache = ssa.NewCache()
r.ssaCache = ssa.NewCache("machinedeployment")
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion internal/controllers/machineset/machineset_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, opt
}

r.recorder = mgr.GetEventRecorderFor("machineset-controller")
r.ssaCache = ssa.NewCache()
r.ssaCache = ssa.NewCache("machineset")
return nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1283,7 +1283,7 @@ func TestMachineSetReconciler_syncMachines(t *testing.T) {
// for Machines, InfrastructureMachines and BootstrapConfigs.
reconciler := &Reconciler{
Client: env,
ssaCache: ssa.NewCache(),
ssaCache: ssa.NewCache("test-controller"),
}
s := &scope{
machineSet: ms,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, opt
r.desiredStateGenerator = desiredstate.NewGenerator(r.Client, r.ClusterCache, r.RuntimeClient)
r.recorder = mgr.GetEventRecorderFor("topology/cluster-controller")
if r.patchHelperFactory == nil {
r.patchHelperFactory = serverSideApplyPatchHelperFactory(r.Client, ssa.NewCache())
r.patchHelperFactory = serverSideApplyPatchHelperFactory(r.Client, ssa.NewCache("topology/cluster"))
}
return nil
}
Expand Down
44 changes: 23 additions & 21 deletions internal/controllers/topology/cluster/reconcile_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ var IgnoreNameGenerated = IgnorePaths{
"metadata.name",
}

const testController = "test-controller"

func TestReconcileShim(t *testing.T) {
infrastructureCluster := builder.TestInfrastructureCluster(metav1.NamespaceDefault, "infrastructure-cluster1").Build()
controlPlane := builder.TestControlPlane(metav1.NamespaceDefault, "controlplane-cluster1").Build()
Expand Down Expand Up @@ -95,7 +97,7 @@ func TestReconcileShim(t *testing.T) {
r := Reconciler{
Client: env,
APIReader: env.GetAPIReader(),
patchHelperFactory: serverSideApplyPatchHelperFactory(env, ssa.NewCache()),
patchHelperFactory: serverSideApplyPatchHelperFactory(env, ssa.NewCache(testController)),
}
err = r.reconcileClusterShim(ctx, s)
g.Expect(err).ToNot(HaveOccurred())
Expand Down Expand Up @@ -138,7 +140,7 @@ func TestReconcileShim(t *testing.T) {
r := Reconciler{
Client: env,
APIReader: env.GetAPIReader(),
patchHelperFactory: serverSideApplyPatchHelperFactory(env, ssa.NewCache()),
patchHelperFactory: serverSideApplyPatchHelperFactory(env, ssa.NewCache(testController)),
}
err = r.reconcileClusterShim(ctx, s)
g.Expect(err).ToNot(HaveOccurred())
Expand Down Expand Up @@ -188,7 +190,7 @@ func TestReconcileShim(t *testing.T) {
r := Reconciler{
Client: env,
APIReader: env.GetAPIReader(),
patchHelperFactory: serverSideApplyPatchHelperFactory(env, ssa.NewCache()),
patchHelperFactory: serverSideApplyPatchHelperFactory(env, ssa.NewCache(testController)),
}
err = r.reconcileClusterShim(ctx, s)
g.Expect(err).ToNot(HaveOccurred())
Expand Down Expand Up @@ -239,7 +241,7 @@ func TestReconcileShim(t *testing.T) {
r := Reconciler{
Client: env,
APIReader: env.GetAPIReader(),
patchHelperFactory: serverSideApplyPatchHelperFactory(env, ssa.NewCache()),
patchHelperFactory: serverSideApplyPatchHelperFactory(env, ssa.NewCache(testController)),
}
err = r.reconcileClusterShim(ctx, s)
g.Expect(err).ToNot(HaveOccurred())
Expand Down Expand Up @@ -280,7 +282,7 @@ func TestReconcileShim(t *testing.T) {
r := Reconciler{
Client: nil,
APIReader: env.GetAPIReader(),
patchHelperFactory: serverSideApplyPatchHelperFactory(nil, ssa.NewCache()),
patchHelperFactory: serverSideApplyPatchHelperFactory(nil, ssa.NewCache(testController)),
}
err = r.reconcileClusterShim(ctx, s)
g.Expect(err).ToNot(HaveOccurred())
Expand Down Expand Up @@ -1156,7 +1158,7 @@ func TestReconcileCluster(t *testing.T) {

r := Reconciler{
Client: env,
patchHelperFactory: serverSideApplyPatchHelperFactory(env, ssa.NewCache()),
patchHelperFactory: serverSideApplyPatchHelperFactory(env, ssa.NewCache(testController)),
recorder: env.GetEventRecorderFor("test"),
}
err = r.reconcileCluster(ctx, s)
Expand Down Expand Up @@ -1283,7 +1285,7 @@ func TestReconcileInfrastructureCluster(t *testing.T) {

r := Reconciler{
Client: env,
patchHelperFactory: serverSideApplyPatchHelperFactory(env, ssa.NewCache()),
patchHelperFactory: serverSideApplyPatchHelperFactory(env, ssa.NewCache(testController)),
recorder: env.GetEventRecorderFor("test"),
}
created, err := r.reconcileInfrastructureCluster(ctx, s)
Expand Down Expand Up @@ -1557,7 +1559,7 @@ func TestReconcileControlPlane(t *testing.T) {

r := Reconciler{
Client: env,
patchHelperFactory: serverSideApplyPatchHelperFactory(env, ssa.NewCache()),
patchHelperFactory: serverSideApplyPatchHelperFactory(env, ssa.NewCache(testController)),
recorder: env.GetEventRecorderFor("test"),
}

Expand Down Expand Up @@ -1713,7 +1715,7 @@ func TestReconcileControlPlaneCleanup(t *testing.T) {

r := Reconciler{
Client: env,
patchHelperFactory: serverSideApplyPatchHelperFactory(env, ssa.NewCache()),
patchHelperFactory: serverSideApplyPatchHelperFactory(env, ssa.NewCache(testController)),
recorder: env.GetEventRecorderFor("test"),
}
created, err := r.reconcileControlPlane(ctx, s)
Expand Down Expand Up @@ -1878,7 +1880,7 @@ func TestReconcileControlPlaneMachineHealthCheck(t *testing.T) {

r := Reconciler{
Client: env,
patchHelperFactory: serverSideApplyPatchHelperFactory(env, ssa.NewCache()),
patchHelperFactory: serverSideApplyPatchHelperFactory(env, ssa.NewCache(testController)),
recorder: env.GetEventRecorderFor("test"),
}

Expand Down Expand Up @@ -2177,7 +2179,7 @@ func TestReconcileMachineDeployments(t *testing.T) {
r := Reconciler{
Client: env.GetClient(),
APIReader: env.GetAPIReader(),
patchHelperFactory: serverSideApplyPatchHelperFactory(env, ssa.NewCache()),
patchHelperFactory: serverSideApplyPatchHelperFactory(env, ssa.NewCache(testController)),
recorder: env.GetEventRecorderFor("test"),
}
err = r.reconcileMachineDeployments(ctx, s)
Expand Down Expand Up @@ -2295,7 +2297,7 @@ func TestReconcileMachineDeploymentsCleanup(t *testing.T) {
r := Reconciler{
Client: env.GetClient(),
APIReader: env.GetAPIReader(),
patchHelperFactory: serverSideApplyPatchHelperFactory(env, ssa.NewCache()),
patchHelperFactory: serverSideApplyPatchHelperFactory(env, ssa.NewCache(testController)),
recorder: env.GetEventRecorderFor("test"),
}
err = r.reconcileMachineDeployments(ctx, s)
Expand Down Expand Up @@ -2360,7 +2362,7 @@ func TestReconcileMachineDeploymentsCleanup(t *testing.T) {
r := Reconciler{
Client: env.GetClient(),
APIReader: env.GetAPIReader(),
patchHelperFactory: serverSideApplyPatchHelperFactory(env, ssa.NewCache()),
patchHelperFactory: serverSideApplyPatchHelperFactory(env, ssa.NewCache(testController)),
recorder: env.GetEventRecorderFor("test"),
}
err = r.reconcileMachineDeployments(ctx, s)
Expand Down Expand Up @@ -2633,7 +2635,7 @@ func TestReconcileMachinePools(t *testing.T) {
r := Reconciler{
Client: env.GetClient(),
APIReader: env.GetAPIReader(),
patchHelperFactory: serverSideApplyPatchHelperFactory(env, ssa.NewCache()),
patchHelperFactory: serverSideApplyPatchHelperFactory(env, ssa.NewCache(testController)),
recorder: env.GetEventRecorderFor("test"),
}
err = r.reconcileMachinePools(ctx, s)
Expand Down Expand Up @@ -2753,7 +2755,7 @@ func TestReconcileMachinePoolsCleanup(t *testing.T) {
r := Reconciler{
Client: env.GetClient(),
APIReader: env.GetAPIReader(),
patchHelperFactory: serverSideApplyPatchHelperFactory(env, ssa.NewCache()),
patchHelperFactory: serverSideApplyPatchHelperFactory(env, ssa.NewCache(testController)),
recorder: env.GetEventRecorderFor("test"),
}
err = r.reconcileMachinePools(ctx, s)
Expand Down Expand Up @@ -3184,7 +3186,7 @@ func TestReconcileReferencedObjectSequences(t *testing.T) {

r := Reconciler{
Client: env,
patchHelperFactory: serverSideApplyPatchHelperFactory(env, ssa.NewCache()),
patchHelperFactory: serverSideApplyPatchHelperFactory(env, ssa.NewCache(testController)),
recorder: env.GetEventRecorderFor("test"),
}

Expand Down Expand Up @@ -3458,7 +3460,7 @@ func TestReconcileMachineDeploymentMachineHealthCheck(t *testing.T) {
r := Reconciler{
Client: env.GetClient(),
APIReader: env.GetAPIReader(),
patchHelperFactory: serverSideApplyPatchHelperFactory(env, ssa.NewCache()),
patchHelperFactory: serverSideApplyPatchHelperFactory(env, ssa.NewCache(testController)),
recorder: env.GetEventRecorderFor("test"),
}

Expand Down Expand Up @@ -3527,7 +3529,7 @@ func TestReconcileState(t *testing.T) {

r := Reconciler{
Client: env,
patchHelperFactory: serverSideApplyPatchHelperFactory(env, ssa.NewCache()),
patchHelperFactory: serverSideApplyPatchHelperFactory(env, ssa.NewCache(testController)),
recorder: env.GetEventRecorderFor("test"),
}
err = r.reconcileState(ctx, s)
Expand Down Expand Up @@ -3574,7 +3576,7 @@ func TestReconcileState(t *testing.T) {

r := Reconciler{
Client: env,
patchHelperFactory: serverSideApplyPatchHelperFactory(env, ssa.NewCache()),
patchHelperFactory: serverSideApplyPatchHelperFactory(env, ssa.NewCache(testController)),
recorder: env.GetEventRecorderFor("test"),
}
err = r.reconcileState(ctx, s)
Expand Down Expand Up @@ -3627,7 +3629,7 @@ func TestReconcileState(t *testing.T) {

r := Reconciler{
Client: env,
patchHelperFactory: serverSideApplyPatchHelperFactory(env, ssa.NewCache()),
patchHelperFactory: serverSideApplyPatchHelperFactory(env, ssa.NewCache(testController)),
recorder: env.GetEventRecorderFor("test"),
}
err = r.reconcileState(ctx, s)
Expand Down Expand Up @@ -3799,7 +3801,7 @@ func TestReconciler_reconcileMachineHealthCheck(t *testing.T) {

r := Reconciler{
Client: env,
patchHelperFactory: serverSideApplyPatchHelperFactory(env, ssa.NewCache()),
patchHelperFactory: serverSideApplyPatchHelperFactory(env, ssa.NewCache(testController)),
recorder: env.GetEventRecorderFor("test"),
}
if tt.current != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func dryRunSSAPatch(ctx context.Context, dryRunCtx *dryRunSSAPatchInput) (bool,

// Check if we already ran this request before by checking if the cache already contains this identifier.
// Note: We only add an identifier to the cache if the result of the dry run was no diff.
if exists := dryRunCtx.ssaCache.Has(requestIdentifier); exists {
if exists := dryRunCtx.ssaCache.Has(requestIdentifier, dryRunCtx.originalUnstructured.GetKind()); exists {
return false, false, nil, nil
}

Expand Down
Loading
Loading