首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于Kubebuilder的Rbac规则

基于Kubebuilder的Rbac规则
EN

Stack Overflow用户
提问于 2019-07-16 07:12:21
回答 1查看 750关注 0票数 1

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

代码语言:javascript
复制
// +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规则,我的操作员应该能够创建上述部署,但我仍然得到了错误:

代码语言:javascript
复制
the server does not allow this method on the requested resource

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

  • 手动应用集群角色
  • 给定运算符集群-admin
  • 同时使用make运行和make部署(显然在运行make清单等之后)
  • 角色生成器正在工作。
  • 我已经开始了一个新的项目,以确保我玩env不是原因。
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-16 13:50:58

因此,在定义类型https://godoc.org/sigs.k8s.io/controller-runtime/pkg/client时,需要将元数据字段中的命名空间和名称设置为这样,这与文档在unstructured.Unstructured中声明的内容不同:

代码语言:javascript
复制
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",
                        },
                    },
                },
            },
        },
    }

否则,非结构化客户端将其读入集群范围内的资源部署,而该资源部署并不存在。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57051860

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档