Skip to content

Commit

Permalink
Make controller as mandatory param in external object tracker
Browse files Browse the repository at this point in the history
  • Loading branch information
Karthik-K-N committed Oct 15, 2024
1 parent 7b752c6 commit d44453e
Show file tree
Hide file tree
Showing 8 changed files with 215 additions and 7 deletions.
32 changes: 32 additions & 0 deletions controllers/external/fake/controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
Copyright 2024 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package fake

import (
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/source"
)

// Controller implements controller.Controller interface.
type Controller struct {
controller.Controller
}

// Watch helps in testing and does nothing but returns nil.
func (c Controller) Watch(_ source.Source) error {
return nil
}
18 changes: 18 additions & 0 deletions controllers/external/fake/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
Copyright 2024 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Package fake provides a fake controllers for testing.
package fake
9 changes: 2 additions & 7 deletions controllers/external/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,8 @@ type ObjectTracker struct {

// Watch uses the controller to issue a Watch only if the object hasn't been seen before.
func (o *ObjectTracker) Watch(log logr.Logger, obj client.Object, handler handler.EventHandler, p ...predicate.Predicate) error {
// Consider this a no-op if the controller isn't present.
if o.Controller == nil {
return nil
}

if o.Cache == nil || o.Scheme == nil {
return errors.New("both scheme and cache must be set for object tracker")
if o.Controller == nil || o.Cache == nil || o.Scheme == nil {
return errors.New("all of controller, cache and scheme must be set for object tracker")
}

gvk := obj.GetObjectKind().GroupVersionKind()
Expand Down
103 changes: 103 additions & 0 deletions exp/internal/controllers/machinepool_controller_phases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,19 @@ import (
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
"k8s.io/utils/ptr"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/cache/informertest"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"

clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/controllers/clustercache"
"sigs.k8s.io/cluster-api/controllers/external"
fakeController "sigs.k8s.io/cluster-api/controllers/external/fake"
expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1"
"sigs.k8s.io/cluster-api/internal/test/builder"
"sigs.k8s.io/cluster-api/internal/util/ssa"
Expand Down Expand Up @@ -128,6 +131,11 @@ func TestReconcileMachinePoolPhases(t *testing.T) {
r := &MachinePoolReconciler{
Client: fakeClient,
ClusterCache: clustercache.NewFakeClusterCache(fakeClient, client.ObjectKey{Name: defaultCluster.Name, Namespace: defaultCluster.Namespace}),
externalTracker: external.ObjectTracker{
Controller: fakeController.Controller{},
Cache: &informertest.FakeInformers{},
Scheme: runtime.NewScheme(),
},
}

scope := &scope{
Expand Down Expand Up @@ -165,6 +173,11 @@ func TestReconcileMachinePoolPhases(t *testing.T) {
r := &MachinePoolReconciler{
Client: fakeClient,
ClusterCache: clustercache.NewFakeClusterCache(fakeClient, client.ObjectKey{Name: defaultCluster.Name, Namespace: defaultCluster.Namespace}),
externalTracker: external.ObjectTracker{
Controller: fakeController.Controller{},
Cache: &informertest.FakeInformers{},
Scheme: runtime.NewScheme(),
},
}

scope := &scope{
Expand Down Expand Up @@ -199,6 +212,11 @@ func TestReconcileMachinePoolPhases(t *testing.T) {
r := &MachinePoolReconciler{
Client: fakeClient,
ClusterCache: clustercache.NewFakeClusterCache(fakeClient, client.ObjectKey{Name: defaultCluster.Name, Namespace: defaultCluster.Namespace}),
externalTracker: external.ObjectTracker{
Controller: fakeController.Controller{},
Cache: &informertest.FakeInformers{},
Scheme: runtime.NewScheme(),
},
}

scope := &scope{
Expand Down Expand Up @@ -249,6 +267,11 @@ func TestReconcileMachinePoolPhases(t *testing.T) {
r := &MachinePoolReconciler{
Client: fakeClient,
ClusterCache: clustercache.NewFakeClusterCache(fakeClient, client.ObjectKey{Name: defaultCluster.Name, Namespace: defaultCluster.Namespace}),
externalTracker: external.ObjectTracker{
Controller: fakeController.Controller{},
Cache: &informertest.FakeInformers{},
Scheme: runtime.NewScheme(),
},
}

scope := &scope{
Expand Down Expand Up @@ -311,6 +334,11 @@ func TestReconcileMachinePoolPhases(t *testing.T) {
r := &MachinePoolReconciler{
Client: fakeClient,
ClusterCache: clustercache.NewFakeClusterCache(fakeClient, client.ObjectKey{Name: defaultCluster.Name, Namespace: defaultCluster.Namespace}),
externalTracker: external.ObjectTracker{
Controller: fakeController.Controller{},
Cache: &informertest.FakeInformers{},
Scheme: runtime.NewScheme(),
},
}

scope := &scope{
Expand Down Expand Up @@ -351,6 +379,11 @@ func TestReconcileMachinePoolPhases(t *testing.T) {
r := &MachinePoolReconciler{
Client: fakeClient,
ClusterCache: clustercache.NewFakeClusterCache(fakeClient, client.ObjectKey{Name: defaultCluster.Name, Namespace: defaultCluster.Namespace}),
externalTracker: external.ObjectTracker{
Controller: fakeController.Controller{},
Cache: &informertest.FakeInformers{},
Scheme: runtime.NewScheme(),
},
}

scope := &scope{
Expand Down Expand Up @@ -398,6 +431,11 @@ func TestReconcileMachinePoolPhases(t *testing.T) {
r := &MachinePoolReconciler{
Client: fakeClient,
ClusterCache: clustercache.NewFakeClusterCache(fakeClient, client.ObjectKey{Name: defaultCluster.Name, Namespace: defaultCluster.Namespace}),
externalTracker: external.ObjectTracker{
Controller: fakeController.Controller{},
Cache: &informertest.FakeInformers{},
Scheme: runtime.NewScheme(),
},
}

scope := &scope{
Expand Down Expand Up @@ -458,6 +496,11 @@ func TestReconcileMachinePoolPhases(t *testing.T) {
r := &MachinePoolReconciler{
Client: fakeClient,
ClusterCache: clustercache.NewFakeClusterCache(fakeClient, client.ObjectKey{Name: defaultCluster.Name, Namespace: defaultCluster.Namespace}),
externalTracker: external.ObjectTracker{
Controller: fakeController.Controller{},
Cache: &informertest.FakeInformers{},
Scheme: runtime.NewScheme(),
},
}

scope := &scope{
Expand Down Expand Up @@ -524,6 +567,11 @@ func TestReconcileMachinePoolPhases(t *testing.T) {
r := &MachinePoolReconciler{
Client: fakeClient,
ClusterCache: clustercache.NewFakeClusterCache(fakeClient, client.ObjectKey{Name: defaultCluster.Name, Namespace: defaultCluster.Namespace}),
externalTracker: external.ObjectTracker{
Controller: fakeController.Controller{},
Cache: &informertest.FakeInformers{},
Scheme: runtime.NewScheme(),
},
}

scope := &scope{
Expand Down Expand Up @@ -588,6 +636,11 @@ func TestReconcileMachinePoolPhases(t *testing.T) {
r := &MachinePoolReconciler{
Client: fakeClient,
ClusterCache: clustercache.NewFakeClusterCache(fakeClient, client.ObjectKey{Name: defaultCluster.Name, Namespace: defaultCluster.Namespace}),
externalTracker: external.ObjectTracker{
Controller: fakeController.Controller{},
Cache: &informertest.FakeInformers{},
Scheme: runtime.NewScheme(),
},
}

scope := &scope{
Expand Down Expand Up @@ -674,6 +727,11 @@ func TestReconcileMachinePoolPhases(t *testing.T) {
r := &MachinePoolReconciler{
Client: fakeClient,
ClusterCache: clustercache.NewFakeClusterCache(fakeClient, client.ObjectKey{Name: defaultCluster.Name, Namespace: defaultCluster.Namespace}),
externalTracker: external.ObjectTracker{
Controller: fakeController.Controller{},
Cache: &informertest.FakeInformers{},
Scheme: runtime.NewScheme(),
},
}

scope := &scope{
Expand Down Expand Up @@ -990,6 +1048,11 @@ func TestReconcileMachinePoolBootstrap(t *testing.T) {
bootstrapConfig := &unstructured.Unstructured{Object: tc.bootstrapConfig}
r := &MachinePoolReconciler{
Client: fake.NewClientBuilder().WithObjects(tc.machinepool, bootstrapConfig, builder.TestBootstrapConfigCRD, builder.TestInfrastructureMachineTemplateCRD).Build(),
externalTracker: external.ObjectTracker{
Controller: fakeController.Controller{},
Cache: &informertest.FakeInformers{},
Scheme: runtime.NewScheme(),
},
}

scope := &scope{
Expand Down Expand Up @@ -1285,6 +1348,11 @@ func TestReconcileMachinePoolInfrastructure(t *testing.T) {
r := &MachinePoolReconciler{
Client: fakeClient,
ClusterCache: clustercache.NewFakeClusterCache(fakeClient, client.ObjectKey{Name: defaultCluster.Name, Namespace: defaultCluster.Namespace}),
externalTracker: external.ObjectTracker{
Controller: fakeController.Controller{},
Cache: &informertest.FakeInformers{},
Scheme: runtime.NewScheme(),
},
}

scope := &scope{
Expand Down Expand Up @@ -1368,6 +1436,11 @@ func TestReconcileMachinePoolMachines(t *testing.T) {
r := &MachinePoolReconciler{
Client: env,
ssaCache: ssa.NewCache(),
externalTracker: external.ObjectTracker{
Controller: fakeController.Controller{},
Cache: &informertest.FakeInformers{},
Scheme: runtime.NewScheme(),
},
}
scope := &scope{
machinePool: &machinePool,
Expand Down Expand Up @@ -1431,6 +1504,11 @@ func TestReconcileMachinePoolMachines(t *testing.T) {
r := &MachinePoolReconciler{
Client: env,
ssaCache: ssa.NewCache(),
externalTracker: external.ObjectTracker{
Controller: fakeController.Controller{},
Cache: &informertest.FakeInformers{},
Scheme: runtime.NewScheme(),
},
}

scope := &scope{
Expand Down Expand Up @@ -1789,6 +1867,11 @@ func TestReconcileMachinePoolScaleToFromZero(t *testing.T) {
Client: fakeClient,
ClusterCache: clustercache.NewFakeClusterCache(env.GetClient(), client.ObjectKey{Name: testCluster.Name, Namespace: testCluster.Namespace}),
recorder: record.NewFakeRecorder(32),
externalTracker: external.ObjectTracker{
Controller: fakeController.Controller{},
Cache: &informertest.FakeInformers{},
Scheme: runtime.NewScheme(),
},
}

scope := &scope{
Expand Down Expand Up @@ -1851,6 +1934,11 @@ func TestReconcileMachinePoolScaleToFromZero(t *testing.T) {
Client: fakeClient,
ClusterCache: clustercache.NewFakeClusterCache(env.GetClient(), client.ObjectKey{Name: testCluster.Name, Namespace: testCluster.Namespace}),
recorder: record.NewFakeRecorder(32),
externalTracker: external.ObjectTracker{
Controller: fakeController.Controller{},
Cache: &informertest.FakeInformers{},
Scheme: runtime.NewScheme(),
},
}

scope := &scope{
Expand Down Expand Up @@ -1896,6 +1984,11 @@ func TestReconcileMachinePoolScaleToFromZero(t *testing.T) {
Client: fakeClient,
recorder: record.NewFakeRecorder(32),
ClusterCache: clustercache.NewFakeClusterCache(fakeClient, client.ObjectKey{Name: testCluster.Name, Namespace: testCluster.Namespace}),
externalTracker: external.ObjectTracker{
Controller: fakeController.Controller{},
Cache: &informertest.FakeInformers{},
Scheme: runtime.NewScheme(),
},
}

scope := &scope{
Expand Down Expand Up @@ -1937,6 +2030,11 @@ func TestReconcileMachinePoolScaleToFromZero(t *testing.T) {
Client: fakeClient,
recorder: record.NewFakeRecorder(32),
ClusterCache: clustercache.NewFakeClusterCache(fakeClient, client.ObjectKey{Name: testCluster.Name, Namespace: testCluster.Namespace}),
externalTracker: external.ObjectTracker{
Controller: fakeController.Controller{},
Cache: &informertest.FakeInformers{},
Scheme: runtime.NewScheme(),
},
}

scope := &scope{
Expand Down Expand Up @@ -2000,6 +2098,11 @@ func TestReconcileMachinePoolScaleToFromZero(t *testing.T) {
Client: fakeClient,
ClusterCache: clustercache.NewFakeClusterCache(env.GetClient(), client.ObjectKey{Name: testCluster.Name, Namespace: testCluster.Namespace}),
recorder: record.NewFakeRecorder(32),
externalTracker: external.ObjectTracker{
Controller: fakeController.Controller{},
Cache: &informertest.FakeInformers{},
Scheme: runtime.NewScheme(),
},
}

scope := &scope{
Expand Down
13 changes: 13 additions & 0 deletions exp/internal/controllers/machinepool_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,16 @@ import (
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/utils/ptr"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/cache/informertest"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/client/interceptor"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/controllers/clustercache"
"sigs.k8s.io/cluster-api/controllers/external"
fakeController "sigs.k8s.io/cluster-api/controllers/external/fake"
expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1"
"sigs.k8s.io/cluster-api/internal/test/builder"
"sigs.k8s.io/cluster-api/util"
Expand Down Expand Up @@ -556,6 +559,11 @@ func TestReconcileMachinePoolRequest(t *testing.T) {
Client: clientFake,
APIReader: clientFake,
ClusterCache: clustercache.NewFakeClusterCache(trackerClientFake, client.ObjectKey{Name: testCluster.Name, Namespace: testCluster.Namespace}),
externalTracker: external.ObjectTracker{
Controller: fakeController.Controller{},
Cache: &informertest.FakeInformers{},
Scheme: runtime.NewScheme(),
},
}

result, err := r.Reconcile(ctx, reconcile.Request{NamespacedName: util.ObjectKey(&tc.machinePool)})
Expand Down Expand Up @@ -1103,6 +1111,11 @@ func TestMachinePoolConditions(t *testing.T) {
Client: clientFake,
APIReader: clientFake,
ClusterCache: clustercache.NewFakeClusterCache(clientFake, client.ObjectKey{Name: testCluster.Name, Namespace: testCluster.Namespace}),
externalTracker: external.ObjectTracker{
Controller: fakeController.Controller{},
Cache: &informertest.FakeInformers{},
Scheme: runtime.NewScheme(),
},
}

_, err := r.Reconcile(ctx, reconcile.Request{NamespacedName: util.ObjectKey(machinePool)})
Expand Down
Loading

0 comments on commit d44453e

Please sign in to comment.