MEF更新设备资源CRD问题总结

 

1 CRD生成问题

l  controller-gen以及controller-tools来进行代码自动生成

具体参考https://juejin.cn/post/7096772191567478792

l  KubeEdge源码

cloud\pkg\apis

cloud\pkg\client

2 权限问题

2.1 查看资源角色

kubectl api-resources -o wide

devices.kubeedge.io

2.2 遇到forbidden问题

1、"services is forbidden: User \"system:serviceaccount:mindx-dl:edge-manager\" cannot create resource \"services\" in API group \"\" in the namespace \"default\""

资源组: services

资源:services

 

2、devices.devices.kubeedge.io "test-4" is forbidden: User "system:serviceaccount:mindx-dl:edge-manager" cannot update resource "devices/status" in API group "devices.kubeedge.io" in the namespace "default"

资源组: devices.kubeedge.io

资源: devices/status

修改对应的RBCA权限yaml

 

2.3 更新问题

1、KubeEdge中device CRD 有三个接口可以实现更新

 

2、参考KubeEdge源码可以使用PATCH方法

cloud\pkg\devicecontroller\controller\upstream.go

updateDeviceStatus方法

 

3、测试updateDeviceStatusJob方法

updateDeviceStatusJob主要更新TwinStatus、而updateDeviceJob更新其他字段(咨询意见)

第一次使用yaml方式重新生成device对象,传入updateDeviceStatusJob中报了一个错误:

Failed to patch device job err: devices.devices.kubeedge.io "test-4" not found

修改为获取CRD的device对象后使用updateDeviceStatusJob仍然报这个错误

4、定位devices.devices.kubeedge.io "xx" not found

4.1 根据报错定位资源devices.devices.kubeedge.io没有,但是使用

Kubectl get devices.devices.kubeedge.io 又可以找到

使用Kubectl get devices.devices.kubeedge.io -v 9 可以查看访问k8s的restful接口

4.2 判断是否是命名空间namespace的问题,切换namespace,仍然不行

4.3 使用Kubectl edit devices.devices.kubeedge.io -v 9 查看restful接口

使用 /root/.kube/config 中的证书和密钥  /root/.kube/test.key  /root/.kube/test.crt

4.4 定位Restful访问路径

在源代码中打印restful

 

得到

https://10.96.0.1:443/apis/devices.kubeedge.io/v1alpha2/namespaces/default/devices/test-4/

https://10.96.0.1:443/apis/devices.kubeedge.io/v1alpha2/namespaces/default/devices/test-4/status

通过 curl 访问

curl -k -v -XPUT  -H "Accept: application/json" -H "Content-Type: application/merge-patch+json" -H "User-Agent: kubectl/v1.21.2 (linux/amd64) kubernetes/092fbfb" 'https://10.44.115.108:6443/apis/devices.kubeedge.io/v1alpha2/namespaces/default/devices/test-4/status?fieldManager=kubectl-edit' --cert ./test.crt --key ./test.key

返回结果是:

{"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"devices.devices.kubeedge.io \"test-4\" not found","reason":"NotFound","details":{"name":"test-4","group":"devices.kubeedge.io","kind":"devices"},"code":404}

使用

curl -k -v -XPUT  -H "Accept: application/json" -H "Content-Type: application/merge-patch+json" -H "User-Agent: kubectl/v1.21.2 (linux/amd64) kubernetes/092fbfb" 'https://10.44.115.108:6443/apis/devices.kubeedge.io/v1alpha2/namespaces/default/devices/test-4?fieldManager=kubectl-edit' --cert ./test.crt --key ./test.key

返回结果是

{"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"Invalid JSON Patch","code":500}

5、使用updateDeviceJob更新字段

可以正常更新

2.4 最终解决方案(重点)

1、    updateDeviceJob和patchDeviceJob都可以实现更新动作

2、    更新是使用Get获取device对象

3、    restful路径中多加了任何一个参数都返回如下json

{

  "kind": "Status",

  "apiVersion": "v1",

  "metadata": {},

  "status": "Failure",

  "message": "devices.devices.kubeedge.io \"test-4\" not found",

  "reason": "NotFound",

  "details": {

    "name": "test-4",

    "group": "devices.kubeedge.io",

    "kind": "devices"

  },

  "code": 404}

posted @ 2023-01-05 15:57  易先讯  阅读(107)  评论(0编辑  收藏  举报