Go 基于Kubebuilder的Rbac规则

Go 基于Kubebuilder的Rbac规则,go,kubernetes,kubebuilder,Go,Kubernetes,Kubebuilder,我的问题是,我正在尝试使用非结构化.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=

我的问题是,我正在尝试使用
非结构化.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
我看到的所有示例都是基于使用实际部署类型的,我找不到任何地方有使用非结构化类型的示例,我是否遗漏了什么? 为了节省时间,我尝试了:

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

因此,当您定义非结构化类型时,请与文档中所述内容分开。非结构化您需要在元数据字段中设置名称空间和名称,如下所示:

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",
                        },
                    },
                },
            },
        },
    }

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

,因此与文档在定义非结构化类型时所述的内容不同。非结构化您需要在元数据字段中设置名称空间和名称,如下所示:

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",
                        },
                    },
                },
            },
        },
    }

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

是的,文档显然存在缺陷。非结构化方法GetNamespace()显式使用“metadata”字段来达到其值::)谢谢您的帮助!我已经为此挣扎了大约一周,文档肯定是有问题的。非结构化方法GetNamespace()显式使用“metadata”字段来达到其值::)谢谢您的帮助!我已经为此挣扎了大约一个星期