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 12, 2017
1 parent 5e7f02a commit c3e279c
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 35 deletions.
22 changes: 21 additions & 1 deletion app/scripts/directives/addConfigToApplication.js
Expand Up @@ -25,6 +25,26 @@
function AddConfigToApplication($filter, $scope, APIService, ApplicationsService, DataService, Navigate, NotificationsService, StorageService) {
var ctrl = this;
var humanizeKind = $filter('humanizeKind');
ctrl.canAddRefToApplication = false;

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

ctrl.checkApplicationContainersRefs = function(application) {
ctrl.canAddRefToApplication = false;
_.each(_.get(application, 'spec.template.spec.containers'), function(container) {
if (!checkContainerForRef(container)) {
ctrl.canAddRefToApplication = true;
return false;
}
});
};

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

// For each container, add the new volume mount.
_.each(podTemplate.spec.containers, function(container) {
if (isContainerSelected(container)) {
if (isContainerSelected(container) && !checkContainerForRef(container)) {
container.envFrom = container.envFrom || [];
container.envFrom.push(newEnvFrom);
}
Expand Down
40 changes: 23 additions & 17 deletions app/views/directives/add-config-to-application.html
Expand Up @@ -5,10 +5,14 @@ <h3>Add to Application</h3>
<div class="dialog-body">
<form name="addToApplicationForm" novalidate>
<fieldset ng-disabled="disableInputs">
<div class="alert alert-warning" ng-if="ctrl.addType === 'env' && ctrl.application && !ctrl.canAddRefToApplication">
<span class="pficon pficon-warning-triangle-o" aria-hidden="true"></span>
<strong>Containers of the selected application already contain this {{ctrl.apiObject.kind | humanizeKind}}.</strong>
</div>
<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 Down Expand Up @@ -80,21 +84,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 +114,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
49 changes: 34 additions & 15 deletions dist/scripts/scripts.js
Expand Up @@ -10991,7 +10991,26 @@ 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 = !1;
var u = function(e) {
var t = c.apiObject.metadata.name;
return "ConfigMap" === c.apiObject.kind ? _.find(e.envFrom, {
configMapRef: {
name: t
}
}) : _.find(e.envFrom, {
secretRef: {
name: t
}
});
};
c.checkApplicationContainersRefs = function(e) {
c.canAddRefToApplication = !1, _.each(_.get(e, "spec.template.spec.containers"), function(e) {
if (!u(e)) return c.canAddRefToApplication = !0, !1;
});
};
var d = function() {
var e = {
namespace: c.project.metadata.name
};
Expand All @@ -11000,13 +11019,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 +11054,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
10 changes: 8 additions & 2 deletions dist/scripts/templates.js
Expand Up @@ -5674,10 +5674,14 @@ angular.module('openshiftConsoleTemplates', []).run(['$templateCache', function(
"<div class=\"dialog-body\">\n" +
"<form name=\"addToApplicationForm\" novalidate>\n" +
"<fieldset ng-disabled=\"disableInputs\">\n" +
"<div class=\"alert alert-warning\" ng-if=\"ctrl.addType === 'env' && ctrl.application && !ctrl.canAddRefToApplication\">\n" +
"<span class=\"pficon pficon-warning-triangle-o\" aria-hidden=\"true\"></span>\n" +
"<strong>Containers of the selected application already contain this {{ctrl.apiObject.kind | humanizeKind}}.</strong>\n" +
"</div>\n" +
"<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 Down Expand Up @@ -5730,6 +5734,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 +5748,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 c3e279c

Please sign in to comment.