Skip to content

Commit

Permalink
switch to _.each
Browse files Browse the repository at this point in the history
  • Loading branch information
juanvallejo committed Sep 27, 2017
1 parent 9d63190 commit 2baea1f
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 48 deletions.
60 changes: 31 additions & 29 deletions app/scripts/controllers/attachPVC.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,48 +119,55 @@ angular.module('openshiftConsole')
$scope.$watch('attach.containers', updateMountPaths, true);

var setVolumeMount = function(podTemplate, name, mountPath, subPath, readOnly) {
for (var i = 0; i < podTemplate.spec.containers.length; i++) {
var containers = podTemplate.spec.containers;

if (!isContainerSelected(containers[i])) {
continue;
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 (!containers[i].volumeMounts) {
containers[i].volumeMounts = [];
if (!container.volumeMounts) {
container.volumeMounts = [];
}

for (var idx = 0; idx < containers[i].volumeMounts.length; idx++) {
var m = containers[i].volumeMounts[idx];

_.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 (m.mountPath === newVolumeMount.mountPath && m.name !== newVolumeMount.name) {
displayError('The volume mount "' + m.mountPath + '" with name "' + m.name +'" already exists for container "' + containers[i].name + '"');
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;
}
}
});

for (var mountIdx = 0; mountIdx < containers[i].volumeMounts.length; mountIdx++) {
var existingMount = containers[i].volumeMounts[mountIdx];
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 (existingMount.name === newVolumeMount.name) {
containers[i].volumeMounts[mountIdx] = newVolumeMount;
return true;
if (mount.name === newVolumeMount.name) {
container.volumeMounts[idx] = newVolumeMount;
noError = true;
stopIteration = true;
return false;
}
}
});

if (stopIteration) {
return false;
}

containers[i].volumeMounts.push(newVolumeMount);
}
container.volumeMounts.push(newVolumeMount);
});

return true;
return noError;
};

// load resources required to show the page (list of pvcs and deployment or deployment config)
Expand Down Expand Up @@ -235,13 +242,8 @@ angular.module('openshiftConsole')

// if the newly created volume already exists, only
// fail if the "overwrite" option was not set
var volumeExists = false;
for (var i = 0; i < podTemplate.spec.volumes.length; i++) {
if (podTemplate.spec.volumes[i].name === newVolume.name) {
volumeExists = true;
break;
}
}
var volumeExists = _.some(podTemplate.spec.volumes, { name: newVolume.name });


if (!$scope.attach.overwrite || ($scope.attach.overwrite && !volumeExists)) {
podTemplate.spec.volumes.push(newVolume);
Expand Down
2 changes: 1 addition & 1 deletion app/views/attach-pvc.html
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ <h3>Volume</h3>
<button type="submit"
class="btn btn-primary btn-lg"
ng-click="attachPVC()"
ng-disabled="(attachPVCForm.$invalid) || disableInputs || !attachPVC">Add</button>
ng-disabled="attachPVCForm.$invalid || disableInputs || !attachPVC">Add</button>
<a class="btn btn-default btn-lg" role="button" ng-click="cancel()">Cancel</a>
</div>
</fieldset>
Expand Down
32 changes: 15 additions & 17 deletions dist/scripts/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -8429,20 +8429,19 @@ n.existingMountPaths = p.getMountPaths(e, k);
};
n.$watchGroup([ "attach.resource", "attach.allContainers" ], j), n.$watch("attach.containers", j, !0);
var P = function(e, t, n, a, r) {
for (var o = 0; o < e.spec.containers.length; o++) {
var i = e.spec.containers;
if (k(i[o])) {
var s = p.createVolumeMount(t, n, a, r);
i[o].volumeMounts || (i[o].volumeMounts = []);
for (var c = 0; c < i[o].volumeMounts.length; c++) {
var l = i[o].volumeMounts[c];
if (l.mountPath === s.mountPath && l.name !== s.name) return C('The volume mount "' + l.mountPath + '" with name "' + l.name + '" already exists for container "' + i[o].name + '"'), !1;
var o = !0;
return _.each(e.spec.containers, function(e) {
if (k(e)) {
var i = !1, s = p.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);
}
for (var u = 0; u < i[o].volumeMounts.length; u++) if (i[o].volumeMounts[u].name === s.name) return i[o].volumeMounts[u] = s, !0;
i[o].volumeMounts.push(s);
}
}
return !0;
}), o;
};
s.get(h, t.name, d).then(function(e) {
n.attach.resource = e, n.breadcrumbs = i.getBreadcrumbs({
Expand Down Expand Up @@ -8471,10 +8470,9 @@ var e = n.attach.resource, a = _.get(e, "spec.template"), r = n.attach.persisten
if (i && !P(a, o, i, c, l)) return void (n.disableInputs = !1);
var m = p.createVolume(o, r);
a.spec.volumes || (a.spec.volumes = []);
for (var v = !1, y = 0; y < a.spec.volumes.length; y++) if (a.spec.volumes[y].name === m.name) {
v = !0;
break;
}
var v = _.some(a.spec.volumes, {
name: m.name
});
(!n.attach.overwrite || n.attach.overwrite && !v) && a.spec.volumes.push(m), 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({
Expand Down
2 changes: 1 addition & 1 deletion dist/scripts/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -1122,7 +1122,7 @@ angular.module('openshiftConsoleTemplates', []).run(['$templateCache', function(
"<pause-rollouts-checkbox ng-if=\"attach.resource | managesRollouts\" deployment=\"attach.resource\">\n" +
"</pause-rollouts-checkbox>\n" +
"<div class=\"button-group gutter-top gutter-bottom\">\n" +
"<button type=\"submit\" class=\"btn btn-primary btn-lg\" ng-click=\"attachPVC()\" ng-disabled=\"(attachPVCForm.$invalid) || disableInputs || !attachPVC\">Add</button>\n" +
"<button type=\"submit\" class=\"btn btn-primary btn-lg\" ng-click=\"attachPVC()\" ng-disabled=\"attachPVCForm.$invalid || disableInputs || !attachPVC\">Add</button>\n" +
"<a class=\"btn btn-default btn-lg\" role=\"button\" ng-click=\"cancel()\">Cancel</a>\n" +
"</div>\n" +
"</fieldset>\n" +
Expand Down

0 comments on commit 2baea1f

Please sign in to comment.