Kapoor Akul
Kapoor Akul

Reputation: 1

Controllers creating/watching resources in different namespace than their Custom Resource

I have a controller which Reconciles MyKind Custom Resource in 'foo' namespace. Within the reconcile loop, it creates a deployment MyDeployment in 'bar' namespace. I am wondering how can I setup a watch on the MyDeployment created in 'bar' namespace which is different than namespace ('foo') where the custom resource live.

I tried setting up my manager with the following, but it doesnt seem to work since the deployment I am trying to watch are in different namespace hence the controller is not able to receive any events for the CRUD operation on the deployment.

    return controllerruntime.NewControllerManagedBy(mgr).
        For(&v1alpha1.MyKind{}).
        Owns(&appsv1.Deployment{}).
        Complete(r)
}

Is there any custom watch that I can configure my controller with in order to receive events for the deployment in a different namespace.

Note: I tried handler.EnqueueRequestsFromMapFunc, IIUC it also reconciles for Kinds in the same namespace.

Upvotes: 0

Views: 2694

Answers (2)

Vajira Prabuddhaka
Vajira Prabuddhaka

Reputation: 932

You can use MultiNamespacedCacheBuilder as the NewCache function when creating manager. This can be set in manager.Options.

For example:

namespace := "ns1,ns1"
options := ctrl.Options{
    .
    .
    .
    NewCache: cache.MultiNamespacedCacheBuilder(strings.Split(namespace, ","))
}

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), options)

Upvotes: 1

Hazim
Hazim

Reputation: 1451

You can specify namespaces in the manager options by passing in a ctrl.Options{} object, while creating it.

namespace := "namespace1,namespace2"
options := ctrl.Options{
        .
        .
        .
        Namespace: cache.MultiNamespacedCacheBuilder(strings.Split(namespace, ","))
    }

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), options)

Upvotes: 1

Related Questions