Joe
Joe

Reputation: 693

Controller get wrong namespace name for multipe operators instances in different namespaces

I developed a k8s Operator, after I deploy the first Operator in first namespace, it works well. Then I deploy the 2nd Operator in second namespace, I saw the 2nd controller to get the request that's namespace still is the first name, but the expected namespace should be second.

Please see the following code, when I play with second operator in the second namespace, request's namespace still is the first namespace.

func (r *AnexampleReconciler) Reconcile(request ctrl.Request) (ctrl.Result, error) {

    log := r.Log.WithValues("Anexample", request.NamespacedName)

    instance := &v1alpha1.Anexample{}
    err := r.Get(context.TODO(), request.NamespacedName, instance)
    if err != nil {
        if errors.IsNotFound(err) {
            log.Info("Anexample resource not found. Ignoring since object must be deleted.")
            return reconcile.Result{}, nil
        }

        log.Error(err, "Failed to get Anexample.")
        return reconcile.Result{}, err
    }

I suspect it might be related to election, but I don't understand them.

    mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
        Scheme:             scheme,
        MetricsBindAddress: metricsAddr,
        Port:               9443,
        LeaderElection:     enableLeaderElection,
        LeaderElectionID:   "2eeda3e4.com.aaa.bbb.ccc",
    })
    if err != nil {
        setupLog.Error(err, "unable to start manager")
        os.Exit(1)
    }

what happen in Controller? How to fix it?

Upvotes: 2

Views: 493

Answers (2)

Narasimha Karumanchi
Narasimha Karumanchi

Reputation: 31

We are seeing a similar issue. The issue is about getting the wrong namespace. Might be a bug in controller-runtime.

request.NamespacedName from controller-runtime is returning the wrong namespace.

Upvotes: 1

oemel09
oemel09

Reputation: 763

request.Namespaced depends on the namespace of the custom resource which you are deploying.

Operators are deployed in namespaces, but can still be configured to listen to custom resources in all namespaces. This should not be related to the election but with the way you setup your manager. You didn't specify a namespace in the ctrl.Options for the manager, so it will listen to CR changes in all namespaces. If you want your operator to only listen to one single namespace, pass the namespace to to manager.

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
        Scheme:             scheme,
        MetricsBindAddress: metricsAddr,
        Port:               9443,
        LeaderElection:     enableLeaderElection,
        LeaderElectionID:   "2eeda3e4.com.aaa.bbb.ccc",
        Namesace:           "<namespace-of-operator-two>",
    })
    if err != nil {
        setupLog.Error(err, "unable to start manager")
        os.Exit(1)
    }

See also here: https://developers.redhat.com/blog/2020/06/26/migrating-a-namespace-scoped-operator-to-a-cluster-scoped-operator#migration_guide__namespace_scoped_to_cluster_scoped

Upvotes: 0

Related Questions