how can i update the pvc #3523
-
eg: i want to use reconciler.client to update but the error occurs: it seems like that create a new pvc |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
i dont want to use patch |
Beta Was this translation helpful? Give feedback.
-
The error you're seeing, "spec: Forbidden: spec is immutable after creation except resources.requests for bound claims", indicates that you're trying to modify a field of the PersistentVolumeClaim spec that isn't allowed to be changed after the PVC is created. In Kubernetes, once a PersistentVolumeClaim is created, most of its spec is immutable, with the exception of resources.requests for storage. The code you've provided only modifies the resources.requests for storage, which should be allowed. However, if during your reconcile process, seems that other parts of the PVC spec are being inadvertently changed, you would get this error. To update a PVC using the controller-runtime's client in a Kubebuilder-based reconciler, you can do the following:
Here's how you can do that: // Fetch the existing PVC
pvc := &corev1.PersistentVolumeClaim{}
err := r.Client.Get(context.TODO(), types.NamespacedName{Name: pvcName, Namespace: "namespace"}, pvc)
if err != nil {
// Handle error
}
// Modify the resources.requests
pvc.Spec.Resources.Requests[corev1.ResourceStorage] = resource.MustParse(newSize)
// Update the PVC
err = r.Client.Update(context.TODO(), pvc)
if err != nil {
// Handle error
} Closing this one as answered. |
Beta Was this translation helpful? Give feedback.
The error you're seeing, "spec: Forbidden: spec is immutable after creation except resources.requests for bound claims", indicates that you're trying to modify a field of the PersistentVolumeClaim spec that isn't allowed to be changed after the PVC is created. In Kubernetes, once a PersistentVolumeClaim is created, most of its spec is immutable, with the exception of resources.requests for storage.
The code you've provided only modifies the resources.requests for storage, which should be allowed. However, if during your reconcile process, seems that other parts of the PVC spec are being inadvertently changed, you would get this error.
To update a PVC using the controller-runtime's client …