k3s: CSI plugin functionality is broken: "attacher.Attach failed: volumeattachments.storage.k8s.io is forbidden"
Bug
When installing a CSI Driver, PVCs and PVs are created but will not attach to a pod with this error:
E0727 14:03:55.220699 2205 csi_attacher.go:93] kubernetes.io/csi: attacher.Attach failed: volumeattachments.storage.k8s.io is forbidden: User "system:node:master1" cannot create resource "volumeattachments" in API group "storage.k8s.io" at the cluster scope: can only get individual resources of this type
To Reproduce
- A k3s cluster (0.7.0 and 0.8.0)
- Install a CSI driver on it https://kubernetes-csi.github.io/docs/drivers.html
- Create a pod + pvc
Expected behavior PVs are able to attach to a pod without issue
Additional context The issue has been reported for multiple CSI drivers and all things point to k3s. I can personally confirm that this works normally when used with a kubespray cluster.
hetznercloud/csi-driver: https://github.com/hetznercloud/csi-driver/issues/46 longhorn CSI driver: https://forums.rancher.com/t/longhorn-on-k3s-pv-attach-error/14920
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 6
- Comments: 36 (8 by maintainers)
I don’t know much about authentication and authotirzation yet… but I was playing a little and got a volume attached and mounted by doing this:
Because the system:node ClusterRole had only the verb ‘get’ for ‘volumeattachments’, I added ‘create’, ‘delete’, ‘patch’, ‘update’, ‘list’ and ‘watch’ after seeing what’s in the other sections of the role…
I edited the ClusterRoleBinding for system:node and since there were no subjects I tried adding these:
I have no idea of what I’m doing but with this change it works LOL 😄 the volume is mounted and working correctly. Is this something that should be done in K3s?
FWIW I ran across this issue today - had a test v1.19.7+k3s1 cluster running Longhorn, and for reasons decided to run
sudo service k3s restartandsudo service k3s-agent restarton each of my nodes in sequence.One of the pods kept failing with
MountVolume.WaitForAttach failed for volume "pvc-da55e64f-4928-4a78-bb15-2b811712a17d" : volume pvc-da55e64f-4928-4a78-bb15-2b811712a17d has GET error for volume attachment csi-03f2267d55258d7abc4cfa13778de5e53882fa7d96d2d1decb616cb26b9d1472: volumeattachments.storage.k8s.io "csi-03f2267d55258d7abc4cfa13778de5e53882fa7d96d2d1decb616cb26b9d1472" is forbidden: User "system:node:hostname" cannot get resource "volumeattachments" in API group "storage.k8s.io" at the cluster scope: no relationship found between node 'hostname' and this objectI modified and pushed a new PVC, adding a “2” to the end of the
metadata.namefield, then edited the deploymentspec.template.spec.volumes.persistentVolumeClaim.claimNameto match and pushed that… the pod came up just fine.Finally, I edited the deployment config’s volume claim name back to the original, and it appears to be attaching again now (logs appear normal, and I can
kubectl execinto the container and read the mounted files which appear to be intact).I have no idea how things got into this state - but I hope this helps other folks who come across this problem.
This will be fixed with the next release and using k8s v1.15. The
nimpackage variable is the main culprit here since it contains host information which is different for the attachdetach controller and kubelet. In general we can say that package level state like this is a bad idea, unfortunately there is no good way for us to audit the code for variables like this. We could try forking to help isolate processes but that will likely come with an additional memory cost.This has something to do with us running a combined binary, it looks like some CSI operations are being picked up by the kubelet instead of the attachdetach controller. If you run the server with
--disable-agentand run a separate agent the CSI attach will work correctly. Stack trace of attacher code:Similarly we are running with
--use-service-account-credentials=truefor controller-manager and appear to be using the attach detach controller service account.I think like @vitobotta found an RBAC rule is going to be the easiest fix at the moment. Instead of modifying the
system:noderole for create volume attachment and bindingsystem:nodesto it I just created a new role and binding forsystem:nodeswith minimal permissions:Yes! I just tried 0.9.0 RC2 and I didn’t need the hack this time. Thanks! 😃
As a temporary workaround, are there any changes I can make to permissions or something to make it work? Thanks