question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

KubernetesClient sends volumeName as empty string for PersistentVolumeClaim causing Forbidden: spec is immutable after creation

See original GitHub issue

I want to apply the following yaml multiple times with the fabric8 kubernetes-client

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-pvc
spec:
  storageClassName: my-storage-class
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 1Gi

I apply the yaml using createOrReplace()

Config config = new ConfigBuilder()
		.withMasterUrl("https://my-kubernetes-root:6443")
		.withNamespace("my-namespace")
		.withOauthToken(token)
		.withTrustCerts(true)
		.build();

KubernetesClient client = new DefaultKubernetesClient(config);
ClasspathResource resource = new ClasspathResource("my-pvc.yaml");

client.load(resource.getInputStream()).createOrReplace(); // this works
TimeUnit.MINUTES.sleep(1); // volumeName is dynamically assigned during this period
client.load(resource.getInputStream()).createOrReplace(); // this fails

This works the first time (when the PVC does not exist in the namespace) but fails the second time that createOrReplace() is called for the same yaml with the following error

io.fabric8.kubernetes.client.KubernetesClientException: Failure executing: PUT at: https://my-kubernetes-root:6443/api/v1/namespaces/my-namespace/persistentvolumeclaims/my-pvc. Message: PersistentVolumeClaim "my-pvc" is invalid: spec: Forbidden: spec is immutable after creation except resources.requests for bound claims
  core.PersistentVolumeClaimSpec{
  	AccessModes:      []core.PersistentVolumeAccessMode{"ReadWriteMany"},
  	Selector:         nil,
  	Resources:        core.ResourceRequirements{Requests: core.ResourceList{s"storage": {i: resource.int64Amount{value: 1073741824}, s: "1Gi", Format: "BinarySI"}}},
- 	VolumeName:       "",
+ 	VolumeName:       "pvc-b79ebfcb-d5cb-4450-9f17-d69ec10b8712",
  	StorageClassName: &"my-storage-class",
  	VolumeMode:       &"Filesystem",
  	DataSource:       nil,
  }

Notice how “volumeName” is not present in the yaml (nil) but in the error message “volumeName” is changing from empty string to the dynamically assigned volumeName.

I can reproduce this exact same behavior using kubectl and empty string for volumeName

I can kubectl apply the following yaml as many times as I like

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-pvc
spec:
  storageClassName: my-storage-class
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 1Gi

But if I kubectl apply a yaml with volumeName of empty string it works the first time and fails the second time (The error message is the same as above)

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-pvc
spec:
  storageClassName: my-storage-class
  volumeName: ""
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 1Gi

How can I stop KubernetesClient from sending the volumeName as empty string? Or is there any other way to fix this so I can apply the same PersistentVolumeClaim yaml multiple times?

I’ve asked this same issue on stack overflow KubernetesClient sends volumeName as empty string for PersistentVolumeClaim causing Forbidden: spec is immutable after creation

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
shawkinscommented, Aug 16, 2022

There isn’t a top level operation on a resource list for patching, but 6.0 has added the resources method to help process composite operations:

client.load(resource.getInputStream()).resources().forEach(r -> r.patch(PatchContext.of(PatchType.SERVER_SIDE_APPLY)));
0reactions
stale[bot]commented, Nov 29, 2022

This issue has been automatically marked as stale because it has not had any activity since 90 days. It will be closed if no further activity occurs within 7 days. Thank you for your contributions!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fabric8 KubernetesClient sends volumeName as empty string ...
For a PVC, after its state is Bound , the volumeName field in its spec is immutable; you can't change the volume your...
Read more >
Update to 1.93.1 fails because of immutable pvc #1431 - GitHub
failed to replace object: PersistentVolumeClaim "kubecost-prometheus-server" is invalid: spec: Forbidden: spec is immutable after creation ...
Read more >
Cannot create PersistentVolume in Kubernetes - Server Fault
Sample configuration for Persistent volume using NFS apiVersion: v1 kind: Service metadata: name: nfs-server spec: # clusterIP: 10.3.240.20 ...
Read more >
Configuring AMQ Streams on OpenShift
After you have created the cluster and user authentication credentials, you specify them in your MirrorMaker configuration for secure ...
Read more >
Index (client-java-api 16.0.2 API) - Javadoc.io
create a PersistentVolumeClaim. createNamespacedPod(String, V1Pod, String, String, String, String) - Method in class io.kubernetes.client.openapi.apis.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found