Skip to content

Commit fd0dc60

Browse files
authored
Merge pull request #8918 from sbueringer/pr-cache-mapper
🌱 util: cache list calls in cluster to objects mapper
2 parents 34e2fa7 + abe0b00 commit fd0dc60

File tree

14 files changed

+92
-22
lines changed

14 files changed

+92
-22
lines changed

bootstrap/kubeadm/internal/controllers/kubeadmconfig_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ func (r *KubeadmConfigReconciler) Reconcile(ctx context.Context, req ctrl.Reques
158158
}
159159

160160
// Look up the owner of this kubeadm config if there is one
161-
configOwner, err := bsutil.GetConfigOwnerFromCache(ctx, r.Client, config)
161+
configOwner, err := bsutil.GetTypedConfigOwner(ctx, r.Client, config)
162162
if apierrors.IsNotFound(err) {
163163
// Could not find the owner yet, this is not an error and will rereconcile when the owner gets set.
164164
return ctrl.Result{}, nil

bootstrap/util/configowner.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,15 @@ func (co ConfigOwner) KubernetesVersion() string {
127127

128128
// GetConfigOwner returns the Unstructured object owning the current resource
129129
// using the uncached unstructured client. For performance-sensitive uses,
130-
// consider GetConfigOwnerCached.
130+
// consider GetTypedConfigOwner.
131131
func GetConfigOwner(ctx context.Context, c client.Client, obj metav1.Object) (*ConfigOwner, error) {
132132
return getConfigOwner(ctx, c, obj, GetOwnerByRef)
133133
}
134134

135-
// GetConfigOwnerFromCache returns the Unstructured object owning the current
135+
// GetTypedConfigOwner returns the Unstructured object owning the current
136136
// resource. The implementation ensures a typed client is used, so the objects are read from the cache.
137-
func GetConfigOwnerFromCache(ctx context.Context, c client.Client, obj metav1.Object) (*ConfigOwner, error) {
138-
return getConfigOwner(ctx, c, obj, GetOwnerByRefFromCache)
137+
func GetTypedConfigOwner(ctx context.Context, c client.Client, obj metav1.Object) (*ConfigOwner, error) {
138+
return getConfigOwner(ctx, c, obj, GetTypedOwnerByRef)
139139
}
140140

141141
func getConfigOwner(ctx context.Context, c client.Client, obj metav1.Object, getFn func(context.Context, client.Client, *corev1.ObjectReference) (*ConfigOwner, error)) (*ConfigOwner, error) {
@@ -183,9 +183,9 @@ func GetOwnerByRef(ctx context.Context, c client.Client, ref *corev1.ObjectRefer
183183
return &ConfigOwner{obj}, nil
184184
}
185185

186-
// GetOwnerByRefFromCache finds and returns the owner by looking at the object
186+
// GetTypedOwnerByRef finds and returns the owner by looking at the object
187187
// reference. The implementation ensures a typed client is used, so the objects are read from the cache.
188-
func GetOwnerByRefFromCache(ctx context.Context, c client.Client, ref *corev1.ObjectReference) (*ConfigOwner, error) {
188+
func GetTypedOwnerByRef(ctx context.Context, c client.Client, ref *corev1.ObjectReference) (*ConfigOwner, error) {
189189
obj, err := c.Scheme().New(ref.GroupVersionKind())
190190
if err != nil {
191191
return nil, errors.Wrapf(err, "failed to construct object of type %s", ref.GroupVersionKind())

bootstrap/util/configowner_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ func TestGetConfigOwner(t *testing.T) {
175175
doTests(t, GetConfigOwner)
176176
})
177177
t.Run("cached", func(t *testing.T) {
178-
doTests(t, GetConfigOwnerFromCache)
178+
doTests(t, GetTypedConfigOwner)
179179
})
180180
}
181181

docs/book/src/developer/providers/migrations/v1.4-to-v1.5.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ maintainers of providers and consumers of our Go API.
3636
- clusterctl move is adding the new annotation `clusterctl.cluster.x-k8s.io/delete-for-move` before object deletion.
3737
- Providers running CAPI release-0.3 clusterctl upgrade tests should set `WorkloadKubernetesVersion` field to the maximum workload cluster kubernetes version supported by the old providers in `ClusterctlUpgradeSpecInput`. For more information, please see: https://github.com/kubernetes-sigs/cluster-api/pull/8518#issuecomment-1508064859
3838
- Introduced function `CollectInfrastructureLogs` at the `ClusterLogCollector` interface in `test/framework/cluster_proxy.go` to allow collecting infrastructure related logs during tests.
39-
- A `GetConfigOwnerFromCache` function has been added to the `sigs.k8s.io./cluster-api/bootstrap/util` package. It is equivalent to `GetConfigOwner` except that it uses the cached typed client instead of the uncached unstructured client, so `GetConfigOwnerFromCache` is expected to be more performant.
39+
- A `GetTypedConfigOwner` function has been added to the `sigs.k8s.io./cluster-api/bootstrap/util` package. It is equivalent to `GetConfigOwner` except that it uses the cached typed client instead of the uncached unstructured client, so `GetTypedConfigOwner` is expected to be more performant.
40+
- `ClusterToObjectsMapper` in `sigs.k8s.io./cluster-api/util` has been deprecated, please use `ClusterToTypedObjectsMapper` instead.
4041

4142
### Suggested changes for providers
4243

exp/internal/controllers/machinepool_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ type MachinePoolReconciler struct {
7474
}
7575

7676
func (r *MachinePoolReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error {
77-
clusterToMachinePools, err := util.ClusterToObjectsMapper(mgr.GetClient(), &expv1.MachinePoolList{}, mgr.GetScheme())
77+
clusterToMachinePools, err := util.ClusterToTypedObjectsMapper(mgr.GetClient(), &expv1.MachinePoolList{}, mgr.GetScheme())
7878
if err != nil {
7979
return err
8080
}

internal/controllers/machine/machine_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ type Reconciler struct {
9292
}
9393

9494
func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error {
95-
clusterToMachines, err := util.ClusterToObjectsMapper(mgr.GetClient(), &clusterv1.MachineList{}, mgr.GetScheme())
95+
clusterToMachines, err := util.ClusterToTypedObjectsMapper(mgr.GetClient(), &clusterv1.MachineList{}, mgr.GetScheme())
9696
if err != nil {
9797
return err
9898
}

internal/controllers/machinedeployment/machinedeployment_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ type Reconciler struct {
7575
}
7676

7777
func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error {
78-
clusterToMachineDeployments, err := util.ClusterToObjectsMapper(mgr.GetClient(), &clusterv1.MachineDeploymentList{}, mgr.GetScheme())
78+
clusterToMachineDeployments, err := util.ClusterToTypedObjectsMapper(mgr.GetClient(), &clusterv1.MachineDeploymentList{}, mgr.GetScheme())
7979
if err != nil {
8080
return err
8181
}

internal/controllers/machineset/machineset_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ type Reconciler struct {
9292
}
9393

9494
func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error {
95-
clusterToMachineSets, err := util.ClusterToObjectsMapper(mgr.GetClient(), &clusterv1.MachineSetList{}, mgr.GetScheme())
95+
clusterToMachineSets, err := util.ClusterToTypedObjectsMapper(mgr.GetClient(), &clusterv1.MachineSetList{}, mgr.GetScheme())
9696
if err != nil {
9797
return err
9898
}

main.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -367,13 +367,12 @@ func setupReconcilers(ctx context.Context, mgr ctrl.Manager) {
367367
Unstructured: true,
368368
},
369369
})
370+
if err != nil {
371+
setupLog.Error(err, "unable to create unstructured caching client")
372+
os.Exit(1)
373+
}
370374

371375
if feature.Gates.Enabled(feature.ClusterTopology) {
372-
if err != nil {
373-
setupLog.Error(err, "unable to create unstructured caching client", "controller", "ClusterTopology")
374-
os.Exit(1)
375-
}
376-
377376
if err := (&controllers.ClusterClassReconciler{
378377
Client: mgr.GetClient(),
379378
APIReader: mgr.GetAPIReader(),

test/infrastructure/docker/exp/internal/controllers/dockermachinepool_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func (r *DockerMachinePoolReconciler) Reconcile(ctx context.Context, req ctrl.Re
143143

144144
// SetupWithManager will add watches for this controller.
145145
func (r *DockerMachinePoolReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error {
146-
clusterToDockerMachinePools, err := util.ClusterToObjectsMapper(mgr.GetClient(), &infraexpv1.DockerMachinePoolList{}, mgr.GetScheme())
146+
clusterToDockerMachinePools, err := util.ClusterToTypedObjectsMapper(mgr.GetClient(), &infraexpv1.DockerMachinePoolList{}, mgr.GetScheme())
147147
if err != nil {
148148
return err
149149
}

0 commit comments

Comments
 (0)