登录
首页 >  Golang >  Go问答

使用 Kubebuilder 实现 RBAC 规则

来源:stackoverflow

时间:2024-03-14 08:48:30 358浏览 收藏

最近发现不少小伙伴都对Golang很感兴趣,所以今天继续给大家介绍Golang相关的知识,本文《使用 Kubebuilder 实现 RBAC 规则》主要内容涉及到等等知识点,希望能帮到你!当然如果阅读本文时存在不同想法,可以在评论中表达,但是请勿使用过激的措辞~

问题内容

我的问题是我正在尝试使用 unstructed.unstructed 类型来创建部署,如下所示:

// +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 不是原因

解决方案


因此,当您定义非结构化类型时,与文档在 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",
                        },
                    },
                },
            },
        },
    }

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

好了,本文到此结束,带大家了解了《使用 Kubebuilder 实现 RBAC 规则》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>