【问题标题】:Rbac rules with KubebuilderKubebuilder 的 Rbac 规则
【发布时间】:2019-11-24 20:54:54
【问题描述】:

我的问题是我正在尝试使用 unstructured.Unstructured 类型来创建这样的部署:

// +kubebuilder:rbac:groups=stable.resource.operator.io,resources=resource,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=stable.resource.operator.io,resources=resource/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=apps,resources=deployments,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=apps,resources=deployments/status,verbs=get;list;watch;create;update;patch;delete
func (r *ResourceReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {

    ctx := context.Background()
    log := r.Log.WithValues("resource", req.NamespacedName)
    instance := &stablev1.Resource{}
    // your logic here

    if err := r.Get(ctx, req.NamespacedName, instance); err != nil {
        log.Error(err, "unable to fetch Resource")
        // we'll ignore not-found errors, since they can't be fixed by an immediate
        // requeue (we'll need to wait for a new notification), and we can get them
        // on deleted requests.
        return ctrl.Result{}, ignoreNotFound(err)
    }
    // your logic here
    u := &unstructured.Unstructured{}
    u.Object = map[string]interface{}{
        "name":      "name",
        "namespace": "namespace",
        "spec": map[string]interface{}{
            "replicas": 2,
            "selector": map[string]interface{}{
                "matchLabels": map[string]interface{}{
                    "foo": "bar",
                },
            },
            "template": map[string]interface{}{
                "labels": map[string]interface{}{
                    "foo": "bar",
                },
                "spec": map[string]interface{}{
                    "containers": []map[string]interface{}{
                        {
                            "name":  "nginx",
                            "image": "nginx",
                        },
                    },
                },
            },
        },
    }
    u.SetGroupVersionKind(schema.GroupVersionKind{
        Group:   "apps",
        Kind:    "Deployment",
        Version: "v1",
    })
    err = r.Create(context.Background(), u)
    log.Error(err, "unable to get object")
    log.V(1).Info("reconciling")
    return ctrl.Result{}, nil

}

我的理解是我已经指定了 rbac 规则,我的操作员应该能够创建所说的部署,但我仍然收到错误:

the server does not allow this method on the requested resource

我看到的所有示例都是基于使用实际部署类型的,我找不到任何地方有使用非结构化类型执行此操作的示例,我是否遗漏了什么? 为了节省时间,我试过了:

  • 手动应用集群角色
  • 给定操作员 cluster-admin
  • 同时使用了 make run 和 make deploy(显然是在运行 make manifests 等之后)
  • 角色生成器正在工作
  • 我已经开始了一个新项目,以确保我玩​​ env 不是原因

【问题讨论】:

    标签: go kubernetes kubebuilder


    【解决方案1】:

    因此,当您定义类型 unstructured.Unstructured 时,与文档在 https://godoc.org/sigs.k8s.io/controller-runtime/pkg/client 中所述分开,您需要在元数据字段中设置命名空间和名称,如下所示:

    u.Object = map[string]interface{}{
            "metadata": map[string]interface{}{
                "name":      "name",
                "namespace": "namespace"},
            "spec": map[string]interface{}{
                "replicas": 2,
                "selector": map[string]interface{}{
                    "matchLabels": map[string]interface{}{
                        "foo": "bar",
                    },
                },
                "template": map[string]interface{}{
                    "labels": map[string]interface{}{
                        "foo": "bar",
                    },
                    "spec": map[string]interface{}{
                        "containers": []map[string]interface{}{
                            {
                                "name":  "nginx",
                                "image": "nginx",
                            },
                        },
                    },
                },
            },
        }
    

    否则非结构化客户端将其读取为集群范围的资源部署,不存在

    【讨论】:

    猜你喜欢
    • 2019-11-11
    • 2021-09-30
    • 2016-06-09
    • 2019-12-30
    • 2014-04-26
    • 1970-01-01
    • 2015-09-28
    • 1970-01-01
    • 2015-01-01
    相关资源
    最近更新 更多