Skip to content

Commit

Permalink
Should not be able to add the same secret or config map to an applica…
Browse files Browse the repository at this point in the history
…tion twice
  • Loading branch information
jhadvig committed Oct 16, 2017
1 parent 5e7f02a commit d6ce337
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 35 deletions.
17 changes: 16 additions & 1 deletion app/scripts/directives/addConfigToApplication.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@
function AddConfigToApplication($filter, $scope, APIService, ApplicationsService, DataService, Navigate, NotificationsService, StorageService) {
var ctrl = this;
var humanizeKind = $filter('humanizeKind');
ctrl.canAddRefToApplication = true;

var conatinerHasRef = function(container) {
var addRefName = ctrl.apiObject.metadata.name;
if (ctrl.apiObject.kind === "ConfigMap") {
return _.some(container.envFrom, {configMapRef: {name: addRefName}});
} else {
return _.some(container.envFrom, {secretRef: {name: addRefName}});
}
};

ctrl.checkApplicationContainersRefs = function(application) {
var containers = _.get(application, 'spec.template.spec.containers');
ctrl.canAddRefToApplication = !_.every(containers, conatinerHasRef);
};

var getApplications = function() {
var context = {
Expand Down Expand Up @@ -91,7 +106,7 @@

// For each container, add the new volume mount.
_.each(podTemplate.spec.containers, function(container) {
if (isContainerSelected(container)) {
if (isContainerSelected(container) && !conatinerHasRef(container)) {
container.envFrom = container.envFrom || [];
container.envFrom.push(newEnvFrom);
}
Expand Down
41 changes: 24 additions & 17 deletions app/views/directives/add-config-to-application.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ <h3>Add to Application</h3>
<legend>Add this {{ctrl.apiObject.kind | humanizeKind}} to application:</legend>
<div class="form-group">
<div class="application-select">
<ui-select id="application" ng-model="ctrl.application" required="true" ng-disabled="ctrl.disableInputs">
<ui-select id="application" ng-model="ctrl.application" on-select="ctrl.checkApplicationContainersRefs($item)" required="true" ng-disabled="ctrl.disableInputs">
<ui-select-match placeholder="{{ctrl.applications.length ? 'Select an application' : 'There are no applications in this project'}}">
<span>
{{$select.selected.metadata.name}}
Expand All @@ -23,6 +23,11 @@ <h3>Add to Application</h3>
</ui-select>
</div>
</div>
<div class="has-error" ng-if="ctrl.addType === 'env' && ctrl.application && !ctrl.canAddRefToApplication">
<span class="help-block">
The {{ctrl.apiObject.kind | humanizeKind}} has already been added to this application.
</span>
</div>
<legend>Add {{ctrl.apiObject.kind | humanizeKind}} as:</legend>
<div class="form-group">
<div class="radio">
Expand Down Expand Up @@ -80,21 +85,23 @@ <h3>Add to Application</h3>
</div>
</div>
</div>
<legend ng-if-start="ctrl.application.spec.template.spec.containers.length > 1">Containers:</legend>
<div ng-if-end class="form-group container-options">
<div ng-if="ctrl.attachAllContainers">
The {{ctrl.apiObject.kind | humanizeKind}} will be added to all containers. You can
<a href="" ng-click="ctrl.attachAllContainers = false">select specific containers</a>
instead.
</div>
<div ng-if="!ctrl.attachAllContainers" class="form-group">
<label class="sr-only required">Containers</label>
<select-containers
ng-model="ctrl.attachContainers"
pod-template="ctrl.application.spec.template"
ng-required="true"
help-text="Add the {{ctrl.apiObject.kind | humanizeKind}} to the selected containers.">
</select-containers>
<div ng-if="ctrl.canAddRefToApplication">
<legend ng-if-start="ctrl.application.spec.template.spec.containers.length > 1">Containers:</legend>
<div ng-if-end class="form-group container-options">
<div ng-if="ctrl.attachAllContainers">
The {{ctrl.apiObject.kind | humanizeKind}} will be added to all containers. You can
<a href="" ng-click="ctrl.attachAllContainers = false">select specific containers</a>
instead.
</div>
<div ng-if="!ctrl.attachAllContainers" class="form-group">
<label class="sr-only required">Containers</label>
<select-containers
ng-model="ctrl.attachContainers"
pod-template="ctrl.application.spec.template"
ng-required="true"
help-text="Add the {{ctrl.apiObject.kind | humanizeKind}} to the selected containers.">
</select-containers>
</div>
</div>
</div>
<div class="button-group pull-right">
Expand All @@ -108,7 +115,7 @@ <h3>Add to Application</h3>
class="btn btn-primary"
ng-class="{'dialog-btn': isDialog}"
ng-click="ctrl.addToApplication()"
ng-disabled="ctrl.addType === 'volume' && addToApplicationForm.$invalid || !ctrl.application"
ng-disabled="(ctrl.addType === 'volume' && addToApplicationForm.$invalid) || (ctrl.addType === 'env' && !ctrl.canAddRefToApplication)"
value="">
Save
</button>
Expand Down
48 changes: 33 additions & 15 deletions dist/scripts/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -10991,7 +10991,25 @@ templateUrl: "views/directives/action-chip.html"
}), function() {
angular.module("openshiftConsole").component("addConfigToApplication", {
controller: [ "$filter", "$scope", "APIService", "ApplicationsService", "DataService", "Navigate", "NotificationsService", "StorageService", function(e, t, n, a, r, o, i, s) {
var c = this, l = e("humanizeKind"), u = function() {
var c = this, l = e("humanizeKind");
c.canAddRefToApplication = !0;
var u = function(e) {
var t = c.apiObject.metadata.name;
return "ConfigMap" === c.apiObject.kind ? _.some(e.envFrom, {
configMapRef: {
name: t
}
}) : _.some(e.envFrom, {
secretRef: {
name: t
}
});
};
c.checkApplicationContainersRefs = function(e) {
var t = _.get(e, "spec.template.spec.containers");
c.canAddRefToApplication = !_.every(t, u);
};
var d = function() {
var e = {
namespace: c.project.metadata.name
};
Expand All @@ -11000,13 +11018,13 @@ c.applications = e, c.updating = !1;
});
};
c.$onInit = function() {
c.addType = "env", c.disableInputs = !1, u();
c.addType = "env", c.disableInputs = !1, d();
var e = new RegExp("^[A-Za-z_][A-Za-z0-9_]*$");
c.hasInvalidEnvVars = _.some(c.apiObject.data, function(t, n) {
return !e.test(n);
});
};
var d = function(e) {
var m = function(e) {
return c.attachAllContainers || c.attachContainers[e.name];
};
c.$postLink = function() {
Expand Down Expand Up @@ -11035,51 +11053,51 @@ name: c.apiObject.metadata.name
};
}
_.each(a.spec.containers, function(e) {
d(e) && (e.envFrom = e.envFrom || [], e.envFrom.push(s));
m(e) && !u(e) && (e.envFrom = e.envFrom || [], e.envFrom.push(s));
});
} else {
var l = e("generateName")(c.apiObject.metadata.name + "-"), u = {
var l = e("generateName")(c.apiObject.metadata.name + "-"), d = {
name: l,
mountPath: c.mountVolume,
readOnly: !0
};
_.each(a.spec.containers, function(e) {
d(e) && (e.volumeMounts = e.volumeMounts || [], e.volumeMounts.push(u));
m(e) && (e.volumeMounts = e.volumeMounts || [], e.volumeMounts.push(d));
});
var m = {
var p = {
name: l
};
switch (c.apiObject.kind) {
case "Secret":
m.secret = {
p.secret = {
secretName: c.apiObject.metadata.name
};
break;

case "ConfigMap":
m.configMap = {
p.configMap = {
name: c.apiObject.metadata.name
};
}
a.spec.volumes = a.spec.volumes || [], a.spec.volumes.push(m);
a.spec.volumes = a.spec.volumes || [], a.spec.volumes.push(p);
}
var p = e("humanizeKind"), f = p(c.apiObject.kind), g = p(t.kind), v = {
var f = e("humanizeKind"), g = f(c.apiObject.kind), v = f(t.kind), h = {
namespace: c.project.metadata.name
};
r.update(n.kindToResource(t.kind), t.metadata.name, t, v).then(function() {
r.update(n.kindToResource(t.kind), t.metadata.name, t, h).then(function() {
i.addNotification({
type: "success",
message: "Successfully added " + f + " " + c.apiObject.metadata.name + " to " + g + " " + t.metadata.name + ".",
message: "Successfully added " + g + " " + c.apiObject.metadata.name + " to " + v + " " + t.metadata.name + ".",
links: [ {
href: o.resourceURL(t),
label: "View " + p(t.kind, !0)
label: "View " + f(t.kind, !0)
} ]
}), angular.isFunction(c.onComplete) && c.onComplete();
}, function(n) {
var a = e("getErrorDetails");
i.addNotification({
type: "error",
message: "An error occurred adding " + f + " " + c.apiObject.metadata.name + " to " + g + " " + t.metadata.name + ". " + a(n)
message: "An error occurred adding " + g + " " + c.apiObject.metadata.name + " to " + v + " " + t.metadata.name + ". " + a(n)
});
}).finally(function() {
c.disableInputs = !1;
Expand Down
11 changes: 9 additions & 2 deletions dist/scripts/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -5677,7 +5677,7 @@ angular.module('openshiftConsoleTemplates', []).run(['$templateCache', function(
"<legend>Add this {{ctrl.apiObject.kind | humanizeKind}} to application:</legend>\n" +
"<div class=\"form-group\">\n" +
"<div class=\"application-select\">\n" +
"<ui-select id=\"application\" ng-model=\"ctrl.application\" required=\"true\" ng-disabled=\"ctrl.disableInputs\">\n" +
"<ui-select id=\"application\" ng-model=\"ctrl.application\" on-select=\"ctrl.checkApplicationContainersRefs($item)\" required=\"true\" ng-disabled=\"ctrl.disableInputs\">\n" +
"<ui-select-match placeholder=\"{{ctrl.applications.length ? 'Select an application' : 'There are no applications in this project'}}\">\n" +
"<span>\n" +
"{{$select.selected.metadata.name}}\n" +
Expand All @@ -5690,6 +5690,11 @@ angular.module('openshiftConsoleTemplates', []).run(['$templateCache', function(
"</ui-select>\n" +
"</div>\n" +
"</div>\n" +
"<div class=\"has-error\" ng-if=\"ctrl.addType === 'env' && ctrl.application && !ctrl.canAddRefToApplication\">\n" +
"<span class=\"help-block\">\n" +
"The {{ctrl.apiObject.kind | humanizeKind}} has already been added to this application.\n" +
"</span>\n" +
"</div>\n" +
"<legend>Add {{ctrl.apiObject.kind | humanizeKind}} as:</legend>\n" +
"<div class=\"form-group\">\n" +
"<div class=\"radio\">\n" +
Expand Down Expand Up @@ -5730,6 +5735,7 @@ angular.module('openshiftConsoleTemplates', []).run(['$templateCache', function(
"</div>\n" +
"</div>\n" +
"</div>\n" +
"<div ng-if=\"ctrl.canAddRefToApplication\">\n" +
"<legend ng-if-start=\"ctrl.application.spec.template.spec.containers.length > 1\">Containers:</legend>\n" +
"<div ng-if-end class=\"form-group container-options\">\n" +
"<div ng-if=\"ctrl.attachAllContainers\">\n" +
Expand All @@ -5743,11 +5749,12 @@ angular.module('openshiftConsoleTemplates', []).run(['$templateCache', function(
"</select-containers>\n" +
"</div>\n" +
"</div>\n" +
"</div>\n" +
"<div class=\"button-group pull-right\">\n" +
"<button class=\"btn btn-default\" ng-class=\"{'dialog-btn': isDialog}\" ng-click=\"ctrl.onCancel()\">\n" +
"Cancel\n" +
"</button>\n" +
"<button type=\"submit\" class=\"btn btn-primary\" ng-class=\"{'dialog-btn': isDialog}\" ng-click=\"ctrl.addToApplication()\" ng-disabled=\"ctrl.addType === 'volume' && addToApplicationForm.$invalid || !ctrl.application\" value=\"\">\n" +
"<button type=\"submit\" class=\"btn btn-primary\" ng-class=\"{'dialog-btn': isDialog}\" ng-click=\"ctrl.addToApplication()\" ng-disabled=\"(ctrl.addType === 'volume' && addToApplicationForm.$invalid) || (ctrl.addType === 'env' && !ctrl.canAddRefToApplication)\" value=\"\">\n" +
"Save\n" +
"</button>\n" +
"</div>\n" +
Expand Down

0 comments on commit d6ce337

Please sign in to comment.