From f118eb1a607144150c1cbb512911050298c2bc1d Mon Sep 17 00:00:00 2001 From: juanvallejo Date: Tue, 26 Sep 2017 19:05:49 -0400 Subject: [PATCH] add "overwrite" option to attachPVC view --- app/scripts/controllers/attachPVC.js | 77 ++++++++++++++++++++++++---- app/views/attach-pvc.html | 14 +++++ dist/scripts/scripts.js | 28 +++++++--- dist/scripts/templates.js | 15 +++++- 4 files changed, 114 insertions(+), 20 deletions(-) diff --git a/app/scripts/controllers/attachPVC.js b/app/scripts/controllers/attachPVC.js index e719e4444b..978ee722da 100644 --- a/app/scripts/controllers/attachPVC.js +++ b/app/scripts/controllers/attachPVC.js @@ -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( @@ -175,16 +227,11 @@ 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 @@ -192,7 +239,15 @@ angular.module('openshiftConsole') 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() { diff --git a/app/views/attach-pvc.html b/app/views/attach-pvc.html index f0eb14a80f..26cca90ef1 100644 --- a/app/views/attach-pvc.html +++ b/app/views/attach-pvc.html @@ -83,6 +83,7 @@

Volume

ng-model="attach.mountPath" ng-pattern="/^\/.*$/" osc-unique="existingMountPaths" + osc-unique-disabled="attach.overwrite" placeholder="example: /data" autocorrect="off" autocapitalize="none" @@ -143,6 +144,7 @@

Volume

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)" @@ -171,6 +173,18 @@

Volume

+
+
+ +
+ Overwrite the volume mount config (if it already exists). +
+
+
+
\n" + "
\n" + "\n" + - "\n" + + "\n" + "
\n" + "Mount path for the volume inside the container. If not specified, the volume will not be mounted automatically.\n" + "
\n" + @@ -1064,7 +1064,7 @@ angular.module('openshiftConsoleTemplates', []).run(['$templateCache', function( "
\n" + "\n" + "\n" + - "\n" + + "\n" + "
\n" + "Unique name used to identify this volume. If not specified, a volume name is generated.\n" + "
\n" + @@ -1087,6 +1087,17 @@ angular.module('openshiftConsoleTemplates', []).run(['$templateCache', function( "
\n" + "
\n" + "\n" + + "
\n" + + "Overwrite the volume mount config (if it already exists).\n" + + "
\n" + + "
\n" + + "
\n" + + "
\n" + + "
\n" + + "\n" +