Skip to content

Commit bb9a408

Browse files
committed
break setVolumeMount into smaller fns
1 parent c3f815b commit bb9a408

File tree

4 files changed

+281
-296
lines changed

4 files changed

+281
-296
lines changed

app/scripts/controllers/attachPVC.js

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -118,56 +118,67 @@ 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+
// if a new volumeMount matches an existing mountPath,
123+
// fail if their names differ. This can happen when the
124+
// "overwrite" option is specified.
125+
// Returns a boolean true if no errors occurred.
126+
var success = true;
127+
_.each(container.volumeMounts, function(mount) {
128+
if (mount.mountPath === newVolumeMount.mountPath && mount.name !== newVolumeMount.name) {
129+
displayError('The volume mount "' + mount.mountPath + '" with name "' + mount.name +'" already exists for container "' + container.name + '"');
130+
success = false;
131+
return success;
132+
}
133+
});
134+
135+
return success;
136+
};
137+
138+
var replaceExistingVolumeMount = function(newVolumeMount, container) {
139+
// if the volume mount we are trying to add already exists,
140+
// replace the existing mount with the newly created one.
141+
// This can happen when the "overwrite" option is specified.
142+
// Returns a boolean true if an existing mount was found and
143+
// replaced with a newly-created volume mount.
144+
var didReplace = false;
145+
_.each(container.volumeMounts, function(mount, idx) {
146+
if (mount.name === newVolumeMount.name) {
147+
container.volumeMounts[idx] = newVolumeMount;
148+
didReplace = true;
149+
return false;
150+
}
151+
});
152+
153+
return didReplace;
154+
};
155+
121156
var setVolumeMount = function(podTemplate, name, mountPath, subPath, readOnly) {
122-
var noError = true;
157+
var success = true;
123158
_.each(podTemplate.spec.containers, function(container) {
124159
if (!isContainerSelected(container)) {
125160
return;
126161
}
127162

128-
var stopIteration = false;
129163
var newVolumeMount =
130164
StorageService.createVolumeMount(name, mountPath, subPath, readOnly);
131165
if (!container.volumeMounts) {
132166
container.volumeMounts = [];
133167
}
134168

135-
_.each(container.volumeMounts, function(mount) {
136-
// if a new volumeMount matches an existing mountPath,
137-
// fail if their names differ. This can happen when the
138-
// "overwrite" option is specified.
139-
if (mount.mountPath === newVolumeMount.mountPath && mount.name !== newVolumeMount.name) {
140-
displayError('The volume mount "' + mount.mountPath + '" with name "' + mount.name +'" already exists for container "' + container.name + '"');
141-
noError = false;
142-
stopIteration = true;
143-
return false;
144-
}
145-
});
146-
147-
if (stopIteration) {
169+
if (!checkVolumeMountPath(newVolumeMount, container)) {
170+
success = false;
148171
return false;
149172
}
150173

151-
_.each(container.volumeMounts, function(mount, idx) {
152-
// if the volume mount we are trying to add already exists,
153-
// replace the existing mount with the newly created one.
154-
// This can happen when the "overwrite" option is specified.
155-
if (mount.name === newVolumeMount.name) {
156-
container.volumeMounts[idx] = newVolumeMount;
157-
noError = true;
158-
stopIteration = true;
159-
return false;
160-
}
161-
});
162-
163-
if (stopIteration) {
174+
if (replaceExistingVolumeMount(newVolumeMount, container)) {
164175
return false;
165176
}
166177

167178
container.volumeMounts.push(newVolumeMount);
168179
});
169180

170-
return noError;
181+
return success;
171182
};
172183

173184
// load resources required to show the page (list of pvcs and deployment or deployment config)

dist/scripts/scripts.js

Lines changed: 25 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 = !0;
8495+
return _.each(t.volumeMounts, function(a) {
8496+
if (a.mountPath === e.mountPath && a.name !== e.name) return C('The volume mount "' + a.mountPath + '" with name "' + a.name + '" already exists for container "' + t.name + '"'), n = !1;
8497+
}), n;
8498+
}, R = function(e, t) {
8499+
var n = !1;
8500+
return _.each(t.volumeMounts, function(a, r) {
8501+
if (a.name === e.name) return t.volumeMounts[r] = e, n = !0, !1;
8502+
}), n;
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,13 @@ 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 = []);
8539+
var h = _.some(a.spec.volumes, {
8540+
name: p.name
8541+
});
8542+
(!n.attach.overwrite || n.attach.overwrite && !h) && a.spec.volumes.push(p), s.update(v, e.metadata.name, n.attach.resource, d).then(function() {
85258543
var e;
85268544
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({
85278545
type: "success",

0 commit comments

Comments
 (0)