Skip to content

Commit 9fe84ee

Browse files
committed
add "overwrite" option to attachPVC view
1 parent bb25fca commit 9fe84ee

File tree

4 files changed

+119
-20
lines changed

4 files changed

+119
-20
lines changed

app/scripts/controllers/attachPVC.js

Lines changed: 69 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,63 @@ angular.module('openshiftConsole')
118118
$scope.$watchGroup(['attach.resource', 'attach.allContainers'], updateMountPaths);
119119
$scope.$watch('attach.containers', updateMountPaths, true);
120120

121+
var checkVolumeMountPath = function(newVolumeMount, container) {
122+
var duplicateMount = _.find(container.volumeMounts, function(mount) {
123+
return mount.mountPath === newVolumeMount && mount.name !== newVolumeMount.name;
124+
});
125+
126+
// if a new volumeMount matches an existing mountPath,
127+
// fail if their names differ. This can happen when the
128+
// "overwrite" option is specified.
129+
if (duplicateMount) {
130+
displayError('The volume mount "' + duplicateMount.mountPath + '" with name "' + duplicateMount.name +'" already exists for container "' + container.name + '"');
131+
return false;
132+
}
133+
134+
return true;
135+
};
136+
137+
var replaceExistingVolumeMount = function(newVolumeMount, container) {
138+
// if the volume mount we are trying to add already exists,
139+
// replace the existing mount with the newly created one.
140+
// This can happen when the "overwrite" option is specified.
141+
var index = _.findIndex(container.volumeMounts, { name: newVolumeMount.name });
142+
if (index === -1) {
143+
return false;
144+
}
145+
146+
container.volumeMounts[index] = newVolumeMount;
147+
return true;
148+
};
149+
150+
var setVolumeMount = function(podTemplate, name, mountPath, subPath, readOnly) {
151+
var success = true;
152+
_.each(podTemplate.spec.containers, function(container) {
153+
if (!isContainerSelected(container)) {
154+
return;
155+
}
156+
157+
var newVolumeMount =
158+
StorageService.createVolumeMount(name, mountPath, subPath, readOnly);
159+
if (!container.volumeMounts) {
160+
container.volumeMounts = [];
161+
}
162+
163+
if (!checkVolumeMountPath(newVolumeMount, container)) {
164+
success = false;
165+
return false;
166+
}
167+
168+
if (replaceExistingVolumeMount(newVolumeMount, container)) {
169+
return false;
170+
}
171+
172+
container.volumeMounts.push(newVolumeMount);
173+
});
174+
175+
return success;
176+
};
177+
121178
// load resources required to show the page (list of pvcs and deployment or deployment config)
122179
var load = function() {
123180
DataService.get(resourceGroupVersion, $routeParams.name, context).then(
@@ -175,24 +232,25 @@ angular.module('openshiftConsole')
175232
var readOnly = $scope.attach.readOnly;
176233
if (mountPath) {
177234
// for each container in the pod spec, add the new volume mount
178-
angular.forEach(podTemplate.spec.containers, function(container) {
179-
if (isContainerSelected(container)) {
180-
var newVolumeMount =
181-
StorageService.createVolumeMount(name, mountPath, subPath, readOnly);
182-
if (!container.volumeMounts) {
183-
container.volumeMounts = [];
184-
}
185-
container.volumeMounts.push(newVolumeMount);
186-
}
187-
});
235+
if(!setVolumeMount(podTemplate, name, mountPath, subPath, readOnly)) {
236+
$scope.disableInputs = false;
237+
return;
238+
}
188239
}
189240

190241
// add the new volume to the pod template
191242
var newVolume = StorageService.createVolume(name, persistentVolumeClaim);
192243
if (!podTemplate.spec.volumes) {
193244
podTemplate.spec.volumes = [];
194245
}
195-
podTemplate.spec.volumes.push(newVolume);
246+
247+
// if the newly created volume already exists, only
248+
// fail if the "overwrite" option was not set
249+
var volumeExists = _.some(podTemplate.spec.volumes, { name: newVolume.name });
250+
251+
if (!volumeExists) {
252+
podTemplate.spec.volumes.push(newVolume);
253+
}
196254

197255
DataService.update(resourceGroupVersion, resource.metadata.name, $scope.attach.resource, context).then(
198256
function() {

app/views/attach-pvc.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ <h3>Volume</h3>
8383
ng-model="attach.mountPath"
8484
ng-pattern="/^\/.*$/"
8585
osc-unique="existingMountPaths"
86+
osc-unique-disabled="attach.overwrite"
8687
placeholder="example: /data"
8788
autocorrect="off"
8889
autocapitalize="none"
@@ -143,6 +144,7 @@ <h3>Volume</h3>
143144
name="volumeName"
144145
ng-model="attach.volumeName"
145146
osc-unique="existingVolumeNames"
147+
osc-unique-disabled="attach.overwrite"
146148
ng-pattern="/^[a-z0-9]([-a-z0-9]*[a-z0-9])?$/"
147149
maxlength="63"
148150
placeholder="(generated if empty)"
@@ -171,6 +173,18 @@ <h3>Volume</h3>
171173
</div>
172174
</div>
173175

176+
<div class="form-group">
177+
<div class="checkbox">
178+
<label>
179+
<input type="checkbox" ng-model="attach.overwrite" aria-describedby="overwrite-help">
180+
Overwrite
181+
</label>
182+
<div id="overwrite-help" class="help-block">
183+
Overwrite a volume mount if it already exists.
184+
</div>
185+
</div>
186+
</div>
187+
174188
<div class="form-group">
175189
<div class="checkbox">
176190
<label>

dist/scripts/scripts.js

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8490,6 +8490,25 @@ var e = _.get(n, "attach.resource.spec.template");
84908490
n.existingMountPaths = m.getMountPaths(e, k);
84918491
};
84928492
n.$watchGroup([ "attach.resource", "attach.allContainers" ], j), n.$watch("attach.containers", j, !0);
8493+
var P = function(e, t) {
8494+
var n = _.find(t.volumeMounts, function(t) {
8495+
return t.mountPath === e && t.name !== e.name;
8496+
});
8497+
return !n || (C('The volume mount "' + n.mountPath + '" with name "' + n.name + '" already exists for container "' + t.name + '"'), !1);
8498+
}, R = function(e, t) {
8499+
var n = _.findIndex(t.volumeMounts, {
8500+
name: e.name
8501+
});
8502+
return -1 !== n && (t.volumeMounts[n] = e, !0);
8503+
}, I = function(e, t, n, a, r) {
8504+
var o = !0;
8505+
return _.each(e.spec.containers, function(e) {
8506+
if (k(e)) {
8507+
var i = m.createVolumeMount(t, n, a, r);
8508+
return e.volumeMounts || (e.volumeMounts = []), P(i, e) ? !R(i, e) && void e.volumeMounts.push(i) : (o = !1, !1);
8509+
}
8510+
}), o;
8511+
};
84938512
s.get(v, t.name, d).then(function(e) {
84948513
n.attach.resource = e, n.breadcrumbs = i.getBreadcrumbs({
84958514
object: e,
@@ -8514,14 +8533,11 @@ n.clusterQuotas = e.by("metadata.name"), n.outOfClaims = c.isAnyStorageQuotaExce
85148533
if (n.disableInputs = !0, S(), n.attachPVCForm.$valid) {
85158534
n.attach.volumeName || (n.attach.volumeName = b("volume-"));
85168535
var e = n.attach.resource, a = _.get(e, "spec.template"), r = n.attach.persistentVolumeClaim, o = n.attach.volumeName, i = n.attach.mountPath, c = n.attach.subPath, l = n.attach.readOnly;
8517-
i && angular.forEach(a.spec.containers, function(e) {
8518-
if (k(e)) {
8519-
var t = m.createVolumeMount(o, i, c, l);
8520-
e.volumeMounts || (e.volumeMounts = []), e.volumeMounts.push(t);
8521-
}
8522-
});
8536+
if (i && !I(a, o, i, c, l)) return void (n.disableInputs = !1);
85238537
var p = m.createVolume(o, r);
8524-
a.spec.volumes || (a.spec.volumes = []), a.spec.volumes.push(p), s.update(v, e.metadata.name, n.attach.resource, d).then(function() {
8538+
a.spec.volumes || (a.spec.volumes = []), _.some(a.spec.volumes, {
8539+
name: p.name
8540+
}) || a.spec.volumes.push(p), s.update(v, e.metadata.name, n.attach.resource, d).then(function() {
85258541
var e;
85268542
i || (e = "No mount path was provided. The volume reference was added to the configuration, but it will not be mounted into running pods."), u.addNotification({
85278543
type: "success",

dist/scripts/templates.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -990,7 +990,7 @@ angular.module('openshiftConsoleTemplates', []).run(['$templateCache', function(
990990
"</div>\n" +
991991
"<div class=\"form-group mar-top-xl\">\n" +
992992
"<label for=\"mount-path\">Mount Path</label>\n" +
993-
"<input id=\"mount-path\" class=\"form-control\" type=\"text\" name=\"mountPath\" ng-model=\"attach.mountPath\" ng-pattern=\"/^\\/.*$/\" osc-unique=\"existingMountPaths\" placeholder=\"example: /data\" autocorrect=\"off\" autocapitalize=\"none\" spellcheck=\"false\" aria-describedby=\"mount-path-help\">\n" +
993+
"<input id=\"mount-path\" class=\"form-control\" type=\"text\" name=\"mountPath\" ng-model=\"attach.mountPath\" ng-pattern=\"/^\\/.*$/\" osc-unique=\"existingMountPaths\" osc-unique-disabled=\"attach.overwrite\" placeholder=\"example: /data\" autocorrect=\"off\" autocapitalize=\"none\" spellcheck=\"false\" aria-describedby=\"mount-path-help\">\n" +
994994
"<div>\n" +
995995
"<span id=\"mount-path-help\" class=\"help-block\">Mount path for the volume inside the container. If not specified, the volume will not be mounted automatically.</span>\n" +
996996
"</div>\n" +
@@ -1020,7 +1020,7 @@ angular.module('openshiftConsoleTemplates', []).run(['$templateCache', function(
10201020
"<div class=\"form-group\">\n" +
10211021
"<label for=\"volume-name\">Volume Name</label>\n" +
10221022
"\n" +
1023-
"<input id=\"volume-path\" class=\"form-control\" type=\"text\" name=\"volumeName\" ng-model=\"attach.volumeName\" osc-unique=\"existingVolumeNames\" ng-pattern=\"/^[a-z0-9]([-a-z0-9]*[a-z0-9])?$/\" maxlength=\"63\" placeholder=\"(generated if empty)\" autocorrect=\"off\" autocapitalize=\"none\" spellcheck=\"false\" aria-describedby=\"volume-name-help\">\n" +
1023+
"<input id=\"volume-path\" class=\"form-control\" type=\"text\" name=\"volumeName\" ng-model=\"attach.volumeName\" osc-unique=\"existingVolumeNames\" osc-unique-disabled=\"attach.overwrite\" ng-pattern=\"/^[a-z0-9]([-a-z0-9]*[a-z0-9])?$/\" maxlength=\"63\" placeholder=\"(generated if empty)\" autocorrect=\"off\" autocapitalize=\"none\" spellcheck=\"false\" aria-describedby=\"volume-name-help\">\n" +
10241024
"<div>\n" +
10251025
"<span id=\"volume-name-help\" class=\"help-block\">Unique name used to identify this volume. If not specified, a volume name is generated.</span>\n" +
10261026
"</div>\n" +
@@ -1043,6 +1043,17 @@ angular.module('openshiftConsoleTemplates', []).run(['$templateCache', function(
10431043
"<div class=\"form-group\">\n" +
10441044
"<div class=\"checkbox\">\n" +
10451045
"<label>\n" +
1046+
"<input type=\"checkbox\" ng-model=\"attach.overwrite\" aria-describedby=\"overwrite-help\">\n" +
1047+
"Overwrite\n" +
1048+
"</label>\n" +
1049+
"<div id=\"overwrite-help\" class=\"help-block\">\n" +
1050+
"Overwrite a volume mount if it already exists.\n" +
1051+
"</div>\n" +
1052+
"</div>\n" +
1053+
"</div>\n" +
1054+
"<div class=\"form-group\">\n" +
1055+
"<div class=\"checkbox\">\n" +
1056+
"<label>\n" +
10461057
"<input type=\"checkbox\" ng-model=\"attach.readOnly\" aria-describedby=\"read-only-help\">\n" +
10471058
"Read only\n" +
10481059
"</label>\n" +

0 commit comments

Comments
 (0)