Skip to content

Commit

Permalink
add "overwrite" option to attachPVC view
Browse files Browse the repository at this point in the history
  • Loading branch information
juanvallejo committed Oct 2, 2017
1 parent 9c144f3 commit afc011f
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 31 deletions.
77 changes: 66 additions & 11 deletions app/scripts/controllers/attachPVC.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,58 @@ angular.module('openshiftConsole')
$scope.$watchGroup(['attach.resource', 'attach.allContainers'], updateMountPaths);
$scope.$watch('attach.containers', updateMountPaths, true);

var setVolumeMount = function(podTemplate, name, mountPath, subPath, readOnly) {
var noError = true;
_.each(podTemplate.spec.containers, function(container) {
if (!isContainerSelected(container)) {
return;
}

var stopIteration = false;
var newVolumeMount =
StorageService.createVolumeMount(name, mountPath, subPath, readOnly);
if (!container.volumeMounts) {
container.volumeMounts = [];
}

_.each(container.volumeMounts, function(mount) {
// if a new volumeMount matches an existing mountPath,
// fail if their names differ. This can happen when the
// "overwrite" option is specified.
if (mount.mountPath === newVolumeMount.mountPath && mount.name !== newVolumeMount.name) {
displayError('The volume mount "' + mount.mountPath + '" with name "' + mount.name +'" already exists for container "' + container.name + '"');
noError = false;
stopIteration = true;
return false;
}
});

if (stopIteration) {
return false;
}

_.each(container.volumeMounts, function(mount, idx) {
// if the volume mount we are trying to add already exists,
// replace the existing mount with the newly created one.
// This can happen when the "overwrite" option is specified.
if (mount.name === newVolumeMount.name) {
container.volumeMounts[idx] = newVolumeMount;
noError = true;
stopIteration = true;
return false;
}
});

if (stopIteration) {
return false;
}

container.volumeMounts.push(newVolumeMount);
});

return noError;
};

// load resources required to show the page (list of pvcs and deployment or deployment config)
var load = function() {
DataService.get(resourceGroupVersion, $routeParams.name, context).then(
Expand Down Expand Up @@ -175,24 +227,27 @@ angular.module('openshiftConsole')
var readOnly = $scope.attach.readOnly;
if (mountPath) {
// for each container in the pod spec, add the new volume mount
angular.forEach(podTemplate.spec.containers, function(container) {
if (isContainerSelected(container)) {
var newVolumeMount =
StorageService.createVolumeMount(name, mountPath, subPath, readOnly);
if (!container.volumeMounts) {
container.volumeMounts = [];
}
container.volumeMounts.push(newVolumeMount);
}
});
// angular.forEach(podTemplate.spec.containers, function(container) {
if(!setVolumeMount(podTemplate, name, mountPath, subPath, readOnly)) {
$scope.disableInputs = false;
return;
}
}

// add the new volume to the pod template
var newVolume = StorageService.createVolume(name, persistentVolumeClaim);
if (!podTemplate.spec.volumes) {
podTemplate.spec.volumes = [];
}
podTemplate.spec.volumes.push(newVolume);

// if the newly created volume already exists, only
// fail if the "overwrite" option was not set
var volumeExists = _.some(podTemplate.spec.volumes, { name: newVolume.name });


if (!$scope.attach.overwrite || ($scope.attach.overwrite && !volumeExists)) {
podTemplate.spec.volumes.push(newVolume);
}

DataService.update(resourceGroupVersion, resource.metadata.name, $scope.attach.resource, context).then(
function() {
Expand Down
14 changes: 14 additions & 0 deletions app/views/attach-pvc.html
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ <h3>Volume</h3>
ng-model="attach.mountPath"
ng-pattern="/^\/.*$/"
osc-unique="existingMountPaths"
osc-unique-disabled="attach.overwrite"
placeholder="example: /data"
autocorrect="off"
autocapitalize="none"
Expand Down Expand Up @@ -143,6 +144,7 @@ <h3>Volume</h3>
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)"
Expand Down Expand Up @@ -171,6 +173,18 @@ <h3>Volume</h3>
</div>
</div>

<div class="form-group">
<div class="checkbox">
<label>
<input type="checkbox" ng-model="attach.overwrite" aria-describedby="overwrite-help">
Overwrite
</label>
<div id="overwrite-help" class="help-block">
Overwrite the volume mount config (if it already exists).
</div>
</div>
</div>

<div class="form-group">
<div class="checkbox">
<label>
Expand Down
28 changes: 21 additions & 7 deletions dist/scripts/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -8446,6 +8446,21 @@ var e = _.get(n, "attach.resource.spec.template");
n.existingMountPaths = m.getMountPaths(e, k);
};
n.$watchGroup([ "attach.resource", "attach.allContainers" ], j), n.$watch("attach.containers", j, !0);
var P = function(e, t, n, a, r) {
var o = !0;
return _.each(e.spec.containers, function(e) {
if (k(e)) {
var i = !1, s = m.createVolumeMount(t, n, a, r);
if (e.volumeMounts || (e.volumeMounts = []), _.each(e.volumeMounts, function(t) {
if (t.mountPath === s.mountPath && t.name !== s.name) return C('The volume mount "' + t.mountPath + '" with name "' + t.name + '" already exists for container "' + e.name + '"'), o = !1, i = !0, !1;
}), i) return !1;
if (_.each(e.volumeMounts, function(t, n) {
if (t.name === s.name) return e.volumeMounts[n] = s, o = !0, i = !0, !1;
}), i) return !1;
e.volumeMounts.push(s);
}
}), o;
};
s.get(h, t.name, d).then(function(e) {
n.attach.resource = e, n.breadcrumbs = i.getBreadcrumbs({
object: e,
Expand All @@ -8470,14 +8485,13 @@ n.clusterQuotas = e.by("metadata.name"), n.outOfClaims = c.isAnyStorageQuotaExce
if (n.disableInputs = !0, S(), n.attachPVCForm.$valid) {
n.attach.volumeName || (n.attach.volumeName = b("volume-"));
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;
i && angular.forEach(a.spec.containers, function(e) {
if (k(e)) {
var t = m.createVolumeMount(o, i, c, l);
e.volumeMounts || (e.volumeMounts = []), e.volumeMounts.push(t);
}
});
if (i && !P(a, o, i, c, l)) return void (n.disableInputs = !1);
var p = m.createVolume(o, r);
a.spec.volumes || (a.spec.volumes = []), a.spec.volumes.push(p), s.update(h, e.metadata.name, n.attach.resource, d).then(function() {
a.spec.volumes || (a.spec.volumes = []);
var v = _.some(a.spec.volumes, {
name: p.name
});
(!n.attach.overwrite || n.attach.overwrite && !v) && a.spec.volumes.push(p), s.update(h, e.metadata.name, n.attach.resource, d).then(function() {
var e;
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({
type: "success",
Expand Down
15 changes: 13 additions & 2 deletions dist/scripts/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -1034,7 +1034,7 @@ angular.module('openshiftConsoleTemplates', []).run(['$templateCache', function(
"</div>\n" +
"<div class=\"form-group mar-top-xl\">\n" +
"<label for=\"mount-path\">Mount Path</label>\n" +
"<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" +
"<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" +
"<div>\n" +
"<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" +
"</div>\n" +
Expand Down Expand Up @@ -1064,7 +1064,7 @@ angular.module('openshiftConsoleTemplates', []).run(['$templateCache', function(
"<div class=\"form-group\">\n" +
"<label for=\"volume-name\">Volume Name</label>\n" +
"\n" +
"<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" +
"<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" +
"<div>\n" +
"<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" +
"</div>\n" +
Expand All @@ -1087,6 +1087,17 @@ angular.module('openshiftConsoleTemplates', []).run(['$templateCache', function(
"<div class=\"form-group\">\n" +
"<div class=\"checkbox\">\n" +
"<label>\n" +
"<input type=\"checkbox\" ng-model=\"attach.overwrite\" aria-describedby=\"overwrite-help\">\n" +
"Overwrite\n" +
"</label>\n" +
"<div id=\"overwrite-help\" class=\"help-block\">\n" +
"Overwrite the volume mount config (if it already exists).\n" +
"</div>\n" +
"</div>\n" +
"</div>\n" +
"<div class=\"form-group\">\n" +
"<div class=\"checkbox\">\n" +
"<label>\n" +
"<input type=\"checkbox\" ng-model=\"attach.readOnly\" aria-describedby=\"read-only-help\">\n" +
"Read only\n" +
"</label>\n" +
Expand Down
Loading

0 comments on commit afc011f

Please sign in to comment.