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

Explicitly set GVK for IBMPoweVSCluster resource #1692

Merged
merged 2 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 12 additions & 2 deletions controllers/ibmpowervscluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ import (
// IBMPowerVSClusterReconciler reconciles a IBMPowerVSCluster object.
type IBMPowerVSClusterReconciler struct {
client.Client
UncachedClient client.Client
Recorder record.EventRecorder
ServiceEndpoint []endpoints.ServiceEndpoint
Scheme *runtime.Scheme
Expand All @@ -66,7 +65,7 @@ func (r *IBMPowerVSClusterReconciler) Reconcile(ctx context.Context, req ctrl.Re

// Fetch the IBMPowerVSCluster instance.
ibmCluster := &infrav1beta2.IBMPowerVSCluster{}
err := r.UncachedClient.Get(ctx, req.NamespacedName, ibmCluster)
err := r.Get(ctx, req.NamespacedName, ibmCluster)
if err != nil {
if apierrors.IsNotFound(err) {
return ctrl.Result{}, nil
Expand Down Expand Up @@ -301,6 +300,17 @@ func (r *IBMPowerVSClusterReconciler) deleteIBMPowerVSImage(ctx context.Context,
return reconcile.Result{}, err
}

// since we are avoiding using cache for IBMPowerVSCluster the Type meta of the retrieved object will be empty
// explicitly setting here to filter children
if gvk := cluster.GetObjectKind().GroupVersionKind(); gvk.Empty() {
gvk, err := r.GroupVersionKindFor(cluster)
if err != nil {
log.Error(err, "Failed to get GVK of cluster")
return reconcile.Result{}, err
}
cluster.SetGroupVersionKind(gvk)
}

children, err := descendants.filterOwnedDescendants(cluster)
if err != nil {
log.Error(err, "Failed to extract direct descendants")
Expand Down
9 changes: 3 additions & 6 deletions controllers/ibmpowervscluster_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ func TestIBMPowerVSClusterReconciler_Reconcile(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
g := NewWithT(t)
reconciler := &IBMPowerVSClusterReconciler{
Client: testEnv.Client,
UncachedClient: testEnv.Client,
Client: testEnv.Client,
}

ns, err := testEnv.CreateNamespace(ctx, fmt.Sprintf("namespace-%s", util.RandomString(5)))
Expand Down Expand Up @@ -158,8 +157,7 @@ func TestIBMPowerVSClusterReconciler_reconcile(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
g := NewWithT(t)
reconciler := &IBMPowerVSClusterReconciler{
Client: testEnv.Client,
UncachedClient: testEnv.Client,
Client: testEnv.Client,
}
_, _ = reconciler.reconcile(tc.powervsClusterScope)
g.Expect(tc.powervsClusterScope.IBMPowerVSCluster.Status.Ready).To(Equal(tc.clusterStatus))
Expand All @@ -174,8 +172,7 @@ func TestIBMPowerVSClusterReconciler_delete(t *testing.T) {
clusterScope *scope.PowerVSClusterScope
)
reconciler = IBMPowerVSClusterReconciler{
Client: testEnv.Client,
UncachedClient: testEnv.Client,
Client: testEnv.Client,
}
t.Run("Reconciling delete IBMPowerVSCluster", func(t *testing.T) {
t.Run("Should reconcile successfully if no descendants are found", func(t *testing.T) {
Expand Down
13 changes: 7 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,13 @@ func main() {
Port: webhookPort,
CertDir: webhookCertDir,
}),
Client: client.Options{
Cache: &client.CacheOptions{
DisableFor: []client.Object{
&infrav1beta2.IBMPowerVSCluster{},
},
},
},
})
if err != nil {
setupLog.Error(err, "unable to start manager")
Expand Down Expand Up @@ -243,14 +250,8 @@ func setupReconcilers(mgr ctrl.Manager, serviceEndpoint []endpoints.ServiceEndpo
os.Exit(1)
}

cfg := ctrl.GetConfigOrDie()
uncachedClient, err := client.New(cfg, client.Options{Scheme: mgr.GetScheme(), Mapper: mgr.GetRESTMapper()})
if err != nil {
setupLog.Error(err, "unable to set up uncached client")
}
if err := (&controllers.IBMPowerVSClusterReconciler{
Client: mgr.GetClient(),
UncachedClient: uncachedClient,
Recorder: mgr.GetEventRecorderFor("ibmpowervscluster-controller"),
ServiceEndpoint: serviceEndpoint,
Scheme: mgr.GetScheme(),
Expand Down