简体   繁体   English

Rbac与Kubebuilder规则

[英]Rbac rules with Kubebuilder

My probelm is that i am trying to use the unstructured.Unstructured type to create a deployment as such: 我万阿英,蒋达清是,我试图用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

}

My understanding is that I have specified the rbac rules, my operator should be able to create said Deployment, but I am still getting the error: 我的理解是,我已经指定了rbac规则,我的操作员应该能够创建所述的Deployment,但仍然出现错误:

the server does not allow this method on the requested resource

All the examples I have seen are based of using the actual deployment type, I cant find anywhere where there is an example of doing this with type unstructured, am i missing something? 我看到的所有示例都是基于使用实际的部署类型的,我找不到在任何使用非结构化类型进行此操作的示例的地方,我缺少什么吗? Just to save time, I have tried: 为了节省时间,我尝试过:

  • Applying the clusterroles manually 手动应用集群角色
  • Given the operator cluster-admin 给定操作员cluster-admin
  • used both make run and make deploy (obviously after running make manifests etc..) 同时使用了make run和make deploy(显然是在运行make manifest之后等。)
  • The roles generator is working 角色生成器正在运行
  • I have started a new project to make sure me playing around with env is not the cause 我已经开始一个新项目,以确保我不是在玩env

So seperatley from what the docs stated at https://godoc.org/sigs.k8s.io/controller-runtime/pkg/client when you define the type unstructured.Unstructured you need to set the namespace and name in the metadata field as such: 因此,当您定义非结构化类型时,文档与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",
                        },
                    },
                },
            },
        },
    }

otherwise unstructured client reads it as cluster scoped resource deployments, which does not exist 否则,非结构化客户端会将其读取为群集范围的资源部署,该部署不存在

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM