Skip to content

Commit 8e010cd

Browse files
author
Jaehyeon1020
committed
fix: prevent duplicate volumes when same PVC is mounted to multiple paths
When the same PVC is configured with multiple mount paths in the web UI, add_notebook_volume appends a V1Volume with the same name for each mount, causing a Kubernetes validation error due to duplicate volume names in the PodSpec. Add a name-based deduplication check to skip volumes that already exist. Signed-off-by: Jaehyeon1020 <jaehyeon.kim1020@navercorp.com>
1 parent e001685 commit 8e010cd

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

components/crud-web-apps/jupyter/backend/apps/common/volumes.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,12 @@ def add_notebook_volume(notebook, volume):
136136
if "volumes" not in podspec:
137137
podspec["volumes"] = []
138138

139+
# Skip if a volume with the same name already exists to avoid
140+
# duplicate entries when the same PVC is mounted to multiple paths
141+
for existing in podspec["volumes"]:
142+
if existing["name"] == volume["name"]:
143+
return notebook
144+
139145
podspec["volumes"].append(volume)
140146
return notebook
141147

components/crud-web-apps/jupyter/backend/apps/common/volumes_test.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,37 @@ def test_use_pvc_name_in_volume_source(self):
111111
"claimName": PVC_NAME,
112112
},
113113
})
114+
115+
116+
class TestAddNotebookVolume(unittest.TestCase):
117+
"""Test the functionality of the add_notebook_volume function."""
118+
119+
def setUp(self):
120+
"""Create a minimal Notebook CR dict."""
121+
self.notebook = {
122+
"spec": {
123+
"template": {
124+
"spec": {
125+
"containers": [{"volumeMounts": []}],
126+
},
127+
},
128+
},
129+
}
130+
self.volume = {
131+
"name": PVC_NAME,
132+
"persistentVolumeClaim": {"claimName": PVC_NAME},
133+
}
134+
135+
def test_add_volume(self):
136+
"""Add a volume to the Notebook's PodSpec."""
137+
volumes.add_notebook_volume(self.notebook, self.volume)
138+
podspec = self.notebook["spec"]["template"]["spec"]
139+
self.assertEqual(len(podspec["volumes"]), 1)
140+
self.assertEqual(podspec["volumes"][0]["name"], PVC_NAME)
141+
142+
def test_skip_duplicate_volume(self):
143+
"""Skip adding a volume if one with the same name already exists."""
144+
volumes.add_notebook_volume(self.notebook, self.volume)
145+
volumes.add_notebook_volume(self.notebook, self.volume)
146+
podspec = self.notebook["spec"]["template"]["spec"]
147+
self.assertEqual(len(podspec["volumes"]), 1)

0 commit comments

Comments
 (0)