dongqiao9015 2019-07-16 07:12
浏览 204
已采纳

Rbac与Kubebuilder规则

My probelm is that i am trying to use the unstructured.Unstructured type to create a deployment as such:

// +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:

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
  • used both make run and make deploy (obviously after running make manifests etc..)
  • The roles generator is working
  • I have started a new project to make sure me playing around with env is not the cause
  • 写回答

1条回答 默认 最新

  • dsflxcfuw27742248 2019-07-16 13:50
    关注

    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:

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏