Skip to content

Commit

Permalink
Merge pull request #8916 from sbueringer/pr-cluster-md-ms-cache-unstr…
Browse files Browse the repository at this point in the history
…uctured

🌱 Cache unstructured in Cluster, MD and MS controller
  • Loading branch information
k8s-ci-robot committed Jun 26, 2023
2 parents 9a6b99b + 49da5bc commit 4920e6e
Show file tree
Hide file tree
Showing 17 changed files with 221 additions and 106 deletions.
40 changes: 23 additions & 17 deletions controllers/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,20 @@ import (

// ClusterReconciler reconciles a Cluster object.
type ClusterReconciler struct {
Client client.Client
APIReader client.Reader
Client client.Client
UnstructuredCachingClient client.Client
APIReader client.Reader

// WatchFilterValue is the label value used to filter events prior to reconciliation.
WatchFilterValue string
}

func (r *ClusterReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error {
return (&clustercontroller.Reconciler{
Client: r.Client,
APIReader: r.APIReader,
WatchFilterValue: r.WatchFilterValue,
Client: r.Client,
UnstructuredCachingClient: r.UnstructuredCachingClient,
APIReader: r.APIReader,
WatchFilterValue: r.WatchFilterValue,
}).SetupWithManager(ctx, mgr, options)
}

Expand All @@ -79,37 +81,41 @@ func (r *MachineReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manag

// MachineSetReconciler reconciles a MachineSet object.
type MachineSetReconciler struct {
Client client.Client
APIReader client.Reader
Tracker *remote.ClusterCacheTracker
Client client.Client
UnstructuredCachingClient client.Client
APIReader client.Reader
Tracker *remote.ClusterCacheTracker

// WatchFilterValue is the label value used to filter events prior to reconciliation.
WatchFilterValue string
}

func (r *MachineSetReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error {
return (&machinesetcontroller.Reconciler{
Client: r.Client,
APIReader: r.APIReader,
Tracker: r.Tracker,
WatchFilterValue: r.WatchFilterValue,
Client: r.Client,
UnstructuredCachingClient: r.UnstructuredCachingClient,
APIReader: r.APIReader,
Tracker: r.Tracker,
WatchFilterValue: r.WatchFilterValue,
}).SetupWithManager(ctx, mgr, options)
}

// MachineDeploymentReconciler reconciles a MachineDeployment object.
type MachineDeploymentReconciler struct {
Client client.Client
APIReader client.Reader
Client client.Client
UnstructuredCachingClient client.Client
APIReader client.Reader

// WatchFilterValue is the label value used to filter events prior to reconciliation.
WatchFilterValue string
}

func (r *MachineDeploymentReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error {
return (&machinedeploymentcontroller.Reconciler{
Client: r.Client,
APIReader: r.APIReader,
WatchFilterValue: r.WatchFilterValue,
Client: r.Client,
UnstructuredCachingClient: r.UnstructuredCachingClient,
APIReader: r.APIReader,
WatchFilterValue: r.WatchFilterValue,
}).SetupWithManager(ctx, mgr, options)
}

Expand Down
9 changes: 5 additions & 4 deletions internal/controllers/cluster/cluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ const (

// Reconciler reconciles a Cluster object.
type Reconciler struct {
Client client.Client
APIReader client.Reader
Client client.Client
UnstructuredCachingClient client.Client
APIReader client.Reader

// WatchFilterValue is the label value used to filter events prior to reconciliation.
WatchFilterValue string
Expand Down Expand Up @@ -270,7 +271,7 @@ func (r *Reconciler) reconcileDelete(ctx context.Context, cluster *clusterv1.Clu
}

if cluster.Spec.ControlPlaneRef != nil {
obj, err := external.Get(ctx, r.Client, cluster.Spec.ControlPlaneRef, cluster.Namespace)
obj, err := external.Get(ctx, r.UnstructuredCachingClient, cluster.Spec.ControlPlaneRef, cluster.Namespace)
switch {
case apierrors.IsNotFound(errors.Cause(err)):
// All good - the control plane resource has been deleted
Expand Down Expand Up @@ -301,7 +302,7 @@ func (r *Reconciler) reconcileDelete(ctx context.Context, cluster *clusterv1.Clu
}

if cluster.Spec.InfrastructureRef != nil {
obj, err := external.Get(ctx, r.Client, cluster.Spec.InfrastructureRef, cluster.Namespace)
obj, err := external.Get(ctx, r.UnstructuredCachingClient, cluster.Spec.InfrastructureRef, cluster.Namespace)
switch {
case apierrors.IsNotFound(errors.Cause(err)):
// All good - the infra resource has been deleted
Expand Down
2 changes: 1 addition & 1 deletion internal/controllers/cluster/cluster_controller_phases.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (r *Reconciler) reconcileExternal(ctx context.Context, cluster *clusterv1.C
return external.ReconcileOutput{}, err
}

obj, err := external.Get(ctx, r.Client, ref, cluster.Namespace)
obj, err := external.Get(ctx, r.UnstructuredCachingClient, ref, cluster.Namespace)
if err != nil {
if apierrors.IsNotFound(errors.Cause(err)) {
log.Info("Could not find external object for cluster, requeuing", "refGroupVersionKind", ref.GroupVersionKind(), "refName", ref.Name)
Expand Down
21 changes: 13 additions & 8 deletions internal/controllers/cluster/cluster_controller_phases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,9 @@ func TestClusterReconcilePhases(t *testing.T) {
Build()
}
r := &Reconciler{
Client: c,
recorder: record.NewFakeRecorder(32),
Client: c,
UnstructuredCachingClient: c,
recorder: record.NewFakeRecorder(32),
}

res, err := r.reconcileInfrastructure(ctx, tt.cluster)
Expand Down Expand Up @@ -217,8 +218,9 @@ func TestClusterReconcilePhases(t *testing.T) {
Build()
}
r := &Reconciler{
Client: c,
recorder: record.NewFakeRecorder(32),
Client: c,
UnstructuredCachingClient: c,
recorder: record.NewFakeRecorder(32),
}
res, err := r.reconcileKubeconfig(ctx, tt.cluster)
if tt.wantErr {
Expand Down Expand Up @@ -368,8 +370,9 @@ func TestClusterReconciler_reconcilePhase(t *testing.T) {
Build()

r := &Reconciler{
Client: c,
recorder: record.NewFakeRecorder(32),
Client: c,
UnstructuredCachingClient: c,
recorder: record.NewFakeRecorder(32),
}
r.reconcilePhase(ctx, tt.cluster)
g.Expect(tt.cluster.Status.GetTypedPhase()).To(Equal(tt.wantPhase))
Expand Down Expand Up @@ -483,9 +486,11 @@ func TestClusterReconcilePhases_reconcileFailureDomains(t *testing.T) {
objs = append(objs, &unstructured.Unstructured{Object: tt.infraRef})
}

c := fake.NewClientBuilder().WithObjects(objs...).Build()
r := &Reconciler{
Client: fake.NewClientBuilder().WithObjects(objs...).Build(),
recorder: record.NewFakeRecorder(32),
Client: c,
UnstructuredCachingClient: c,
recorder: record.NewFakeRecorder(32),
}

_, err := r.reconcileInfrastructure(ctx, tt.cluster)
Expand Down
9 changes: 6 additions & 3 deletions internal/controllers/cluster/cluster_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,8 +389,9 @@ func TestClusterReconciler_reconcileDelete(t *testing.T) {
g := NewWithT(t)
fakeClient := fake.NewClientBuilder().WithObjects(fakeInfraCluster, tt.cluster).Build()
r := &Reconciler{
Client: fakeClient,
APIReader: fakeClient,
Client: fakeClient,
UnstructuredCachingClient: fakeClient,
APIReader: fakeClient,
}

_, _ = r.reconcileDelete(ctx, tt.cluster)
Expand Down Expand Up @@ -524,8 +525,10 @@ func TestClusterReconcilerNodeRef(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)

c := fake.NewClientBuilder().WithObjects(cluster, controlPlaneWithNoderef, controlPlaneWithoutNoderef, nonControlPlaneWithNoderef, nonControlPlaneWithoutNoderef).Build()
r := &Reconciler{
Client: fake.NewClientBuilder().WithObjects(cluster, controlPlaneWithNoderef, controlPlaneWithoutNoderef, nonControlPlaneWithNoderef, nonControlPlaneWithoutNoderef).Build(),
Client: c,
UnstructuredCachingClient: c,
}
requests := r.controlPlaneMachineToCluster(ctx, tt.o)
g.Expect(requests).To(Equal(tt.want))
Expand Down
20 changes: 17 additions & 3 deletions internal/controllers/cluster/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"

clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
Expand Down Expand Up @@ -81,15 +82,28 @@ func TestMain(m *testing.M) {
}).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: 1}); err != nil {
panic(fmt.Sprintf("Failed to start ClusterCacheReconciler: %v", err))
}

unstructuredCachingClient, err := client.New(mgr.GetConfig(), client.Options{
HTTPClient: mgr.GetHTTPClient(),
Cache: &client.CacheOptions{
Reader: mgr.GetCache(),
Unstructured: true,
},
})
if err != nil {
panic(fmt.Sprintf("unable to create unstructuredCachineClient: %v", err))
}

if err := (&Reconciler{
Client: mgr.GetClient(),
APIReader: mgr.GetClient(),
Client: mgr.GetClient(),
UnstructuredCachingClient: unstructuredCachingClient,
APIReader: mgr.GetClient(),
}).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: 1}); err != nil {
panic(fmt.Sprintf("Failed to start ClusterReconciler: %v", err))
}
if err := (&machinecontroller.Reconciler{
Client: mgr.GetClient(),
UnstructuredCachingClient: mgr.GetClient(),
UnstructuredCachingClient: unstructuredCachingClient,
APIReader: mgr.GetAPIReader(),
Tracker: tracker,
}).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: 1}); err != nil {
Expand Down
15 changes: 14 additions & 1 deletion internal/controllers/machine/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"

clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
Expand Down Expand Up @@ -86,9 +87,21 @@ func TestMain(m *testing.M) {
}).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: 1}); err != nil {
panic(fmt.Sprintf("Failed to start ClusterCacheReconciler: %v", err))
}

unstructuredCachingClient, err := client.New(mgr.GetConfig(), client.Options{
HTTPClient: mgr.GetHTTPClient(),
Cache: &client.CacheOptions{
Reader: mgr.GetCache(),
Unstructured: true,
},
})
if err != nil {
panic(fmt.Sprintf("unable to create unstructuredCachineClient: %v", err))
}

if err := (&Reconciler{
Client: mgr.GetClient(),
UnstructuredCachingClient: mgr.GetClient(),
UnstructuredCachingClient: unstructuredCachingClient,
APIReader: mgr.GetAPIReader(),
Tracker: tracker,
}).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: 1}); err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ const machineDeploymentManagerName = "capi-machinedeployment"

// Reconciler reconciles a MachineDeployment object.
type Reconciler struct {
Client client.Client
APIReader client.Reader
Client client.Client
UnstructuredCachingClient client.Client
APIReader client.Reader

// WatchFilterValue is the label value used to filter events prior to reconciliation.
WatchFilterValue string
Expand Down Expand Up @@ -213,12 +214,12 @@ func (r *Reconciler) reconcile(ctx context.Context, cluster *clusterv1.Cluster,
}))

// Make sure to reconcile the external infrastructure reference.
if err := reconcileExternalTemplateReference(ctx, r.Client, cluster, &md.Spec.Template.Spec.InfrastructureRef); err != nil {
if err := reconcileExternalTemplateReference(ctx, r.UnstructuredCachingClient, cluster, &md.Spec.Template.Spec.InfrastructureRef); err != nil {
return ctrl.Result{}, err
}
// Make sure to reconcile the external bootstrap reference, if any.
if md.Spec.Template.Spec.Bootstrap.ConfigRef != nil {
if err := reconcileExternalTemplateReference(ctx, r.Client, cluster, md.Spec.Template.Spec.Bootstrap.ConfigRef); err != nil {
if err := reconcileExternalTemplateReference(ctx, r.UnstructuredCachingClient, cluster, md.Spec.Template.Spec.Bootstrap.ConfigRef); err != nil {
return ctrl.Result{}, err
}
}
Expand Down
24 changes: 19 additions & 5 deletions internal/controllers/machinedeployment/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,30 @@ func TestMain(m *testing.M) {
}).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: 1}); err != nil {
panic(fmt.Sprintf("Failed to start ClusterCacheReconciler: %v", err))
}

unstructuredCachingClient, err := client.New(mgr.GetConfig(), client.Options{
HTTPClient: mgr.GetHTTPClient(),
Cache: &client.CacheOptions{
Reader: mgr.GetCache(),
Unstructured: true,
},
})
if err != nil {
panic(fmt.Sprintf("unable to create unstructuredCachineClient: %v", err))
}

if err := (&machinesetcontroller.Reconciler{
Client: mgr.GetClient(),
APIReader: mgr.GetAPIReader(),
Tracker: tracker,
Client: mgr.GetClient(),
UnstructuredCachingClient: unstructuredCachingClient,
APIReader: mgr.GetAPIReader(),
Tracker: tracker,
}).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: 1}); err != nil {
panic(fmt.Sprintf("Failed to start MMachineSetReconciler: %v", err))
}
if err := (&Reconciler{
Client: mgr.GetClient(),
APIReader: mgr.GetAPIReader(),
Client: mgr.GetClient(),
UnstructuredCachingClient: unstructuredCachingClient,
APIReader: mgr.GetAPIReader(),
}).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: 1}); err != nil {
panic(fmt.Sprintf("Failed to start MMachineDeploymentReconciler: %v", err))
}
Expand Down
27 changes: 21 additions & 6 deletions internal/controllers/machinehealthcheck/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"

clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
Expand Down Expand Up @@ -84,9 +85,22 @@ func TestMain(m *testing.M) {
}).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: 1}); err != nil {
panic(fmt.Sprintf("Failed to start ClusterCacheReconciler: %v", err))
}

unstructuredCachingClient, err := client.New(mgr.GetConfig(), client.Options{
HTTPClient: mgr.GetHTTPClient(),
Cache: &client.CacheOptions{
Reader: mgr.GetCache(),
Unstructured: true,
},
})
if err != nil {
panic(fmt.Sprintf("unable to create unstructuredCachineClient: %v", err))
}

if err := (&clustercontroller.Reconciler{
Client: mgr.GetClient(),
APIReader: mgr.GetClient(),
Client: mgr.GetClient(),
UnstructuredCachingClient: unstructuredCachingClient,
APIReader: mgr.GetClient(),
}).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: 1}); err != nil {
panic(fmt.Sprintf("Failed to start ClusterReconciler: %v", err))
}
Expand All @@ -98,16 +112,17 @@ func TestMain(m *testing.M) {
}
if err := (&machinecontroller.Reconciler{
Client: mgr.GetClient(),
UnstructuredCachingClient: mgr.GetClient(),
UnstructuredCachingClient: unstructuredCachingClient,
APIReader: mgr.GetAPIReader(),
Tracker: tracker,
}).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: 1}); err != nil {
panic(fmt.Sprintf("Failed to start MachineReconciler: %v", err))
}
if err := (&machinesetcontroller.Reconciler{
Client: mgr.GetClient(),
APIReader: mgr.GetAPIReader(),
Tracker: tracker,
Client: mgr.GetClient(),
UnstructuredCachingClient: unstructuredCachingClient,
APIReader: mgr.GetAPIReader(),
Tracker: tracker,
}).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: 1}); err != nil {
panic(fmt.Sprintf("Failed to start MMachineSetReconciler: %v", err))
}
Expand Down
Loading

0 comments on commit 4920e6e

Please sign in to comment.