【问题标题】:Controller get wrong namespace name for multipe operators instances in different namespaces控制器为不同命名空间中的多个运算符实例获取错误的命名空间名称
【发布时间】:2020-11-02 21:17:12
【问题描述】:

我开发了一个 k8s Operator,在第一个命名空间中部署了第一个 Operator 后,它运行良好。然后我在第二个命名空间中部署第二个操作员,我看到第二个控制器获取命名空间仍然是第一个名称的请求,但预期的命名空间应该是第二个。

请看以下代码,当我在第二个命名空间中使用第二个运算符时,请求的命名空间仍然是第一个命名空间。

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
    }

我怀疑这可能与选举有关,但我不明白。

    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)
    }

Controller 中发生了什么?如何解决?

【问题讨论】:

    标签: kubernetes openshift operator-sdk


    【解决方案1】:

    request.Namespaced 取决于您正在部署的自定义资源的命名空间。

    Operator 部署在命名空间中,但仍可以配置为侦听所有命名空间中的自定义资源。 这不应该与选举有关,而是与您设置经理的方式有关。 您没有在 ctrl.Options 中为管理器指定命名空间,因此它将侦听所有命名空间中的 CR 更改。 如果您希望您的操作员只监听一个命名空间,请将命名空间传递给管理器。

    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)
        }
    

    参见此处: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

    【讨论】:

      【解决方案2】:

      我们看到了类似的问题。问题在于获取错误的命名空间。可能是控制器运行时的错误。

      来自控制器运行时的 request.NamespacedName 返回错误的命名空间。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-06-17
        • 2016-05-26
        • 1970-01-01
        • 2011-07-13
        • 1970-01-01
        • 1970-01-01
        • 2013-05-24
        • 1970-01-01
        相关资源
        最近更新 更多