Skip to content

Commit cb67bfb

Browse files
committed
Should not be able to add the same secret or config map to an application twice
1 parent 8b7c0d9 commit cb67bfb

File tree

6 files changed

+93
-52
lines changed

6 files changed

+93
-52
lines changed

Diff for: app/scripts/directives/addConfigToApplication.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,21 @@
2525
function AddConfigToApplication($filter, $scope, APIService, ApplicationsService, DataService, Navigate, NotificationsService, StorageService) {
2626
var ctrl = this;
2727
var humanizeKind = $filter('humanizeKind');
28+
ctrl.canAddRefToApplication = true;
29+
30+
var conatinerHasRef = function(container) {
31+
var addRefName = ctrl.apiObject.metadata.name;
32+
if (ctrl.apiObject.kind === "ConfigMap") {
33+
return _.some(container.envFrom, {configMapRef: {name: addRefName}});
34+
} else {
35+
return _.some(container.envFrom, {secretRef: {name: addRefName}});
36+
}
37+
};
38+
39+
ctrl.checkApplicationContainersRefs = function(application) {
40+
var containers = _.get(application, 'spec.template.spec.containers');
41+
ctrl.canAddRefToApplication = !_.every(containers, conatinerHasRef);
42+
};
2843

2944
var getApplications = function() {
3045
var context = {
@@ -91,7 +106,7 @@
91106

92107
// For each container, add the new volume mount.
93108
_.each(podTemplate.spec.containers, function(container) {
94-
if (isContainerSelected(container)) {
109+
if (isContainerSelected(container) && !conatinerHasRef(container)) {
95110
container.envFrom = container.envFrom || [];
96111
container.envFrom.push(newEnvFrom);
97112
}

Diff for: app/styles/_add-config-to-application.less

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@
4848
}
4949
}
5050

51-
.env-warning {
52-
margin-left: 20px;
51+
.help-block {
52+
margin-bottom: 10px;
5353
}
5454

5555
.updating {

Diff for: app/views/directives/add-config-to-application.html

+27-24
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ <h3>Add to Application</h3>
66
<form name="addToApplicationForm" novalidate>
77
<fieldset ng-disabled="disableInputs">
88
<legend>Add this {{ctrl.apiObject.kind | humanizeKind}} to application:</legend>
9-
<div class="form-group">
9+
<div class="form-group" ng-class="{'has-error' : ctrl.addType === 'env' && ctrl.application && !ctrl.canAddRefToApplication}">
1010
<div class="application-select">
11-
<ui-select id="application" ng-model="ctrl.application" required="true" ng-disabled="ctrl.disableInputs">
11+
<ui-select id="application" ng-model="ctrl.application" on-select="ctrl.checkApplicationContainersRefs($item)" required="true" ng-disabled="ctrl.disableInputs">
1212
<ui-select-match placeholder="{{ctrl.applications.length ? 'Select an application' : 'There are no applications in this project'}}">
1313
<span>
1414
{{$select.selected.metadata.name}}
@@ -23,6 +23,11 @@ <h3>Add to Application</h3>
2323
</ui-select>
2424
</div>
2525
</div>
26+
<div class="has-error" ng-if="ctrl.addType === 'env' && ctrl.application && !ctrl.canAddRefToApplication">
27+
<span class="help-block">
28+
The {{ctrl.apiObject.kind | humanizeKind}} has already been added to this application.
29+
</span>
30+
</div>
2631
<legend>Add {{ctrl.apiObject.kind | humanizeKind}} as:</legend>
2732
<div class="form-group">
2833
<div class="radio">
@@ -31,12 +36,8 @@ <h3>Add to Application</h3>
3136
<input type="radio" ng-model="ctrl.addType" value="env" ng-disabled="ctrl.disableInputs">
3237
Environment variables
3338
</label>
34-
<div class="alert alert-warning env-warning" ng-show="ctrl.hasInvalidEnvVars">
35-
<span class="pficon pficon-warning-triangle-o" aria-hidden="true"></span>
36-
<span>Some of the keys for {{ctrl.apiObject.kind | humanizeKind}}
37-
<strong>{{ctrl.apiObject.metadata.name}}</strong> are not
38-
valid environment variable names and will not be
39-
added.</span>
39+
<div class="has-warning" ng-if="ctrl.hasInvalidEnvVars">
40+
<span class="help-block">Some of the keys for {{ctrl.apiObject.kind | humanizeKind}} <strong>{{ctrl.apiObject.metadata.name}}</strong> are not valid environment variable names and will not be added.</span>
4041
</div>
4142
</div>
4243
<div>
@@ -80,21 +81,23 @@ <h3>Add to Application</h3>
8081
</div>
8182
</div>
8283
</div>
83-
<legend ng-if-start="ctrl.application.spec.template.spec.containers.length > 1">Containers:</legend>
84-
<div ng-if-end class="form-group container-options">
85-
<div ng-if="ctrl.attachAllContainers">
86-
The {{ctrl.apiObject.kind | humanizeKind}} will be added to all containers. You can
87-
<a href="" ng-click="ctrl.attachAllContainers = false">select specific containers</a>
88-
instead.
89-
</div>
90-
<div ng-if="!ctrl.attachAllContainers" class="form-group">
91-
<label class="sr-only required">Containers</label>
92-
<select-containers
93-
ng-model="ctrl.attachContainers"
94-
pod-template="ctrl.application.spec.template"
95-
ng-required="true"
96-
help-text="Add the {{ctrl.apiObject.kind | humanizeKind}} to the selected containers.">
97-
</select-containers>
84+
<div ng-if="ctrl.canAddRefToApplication">
85+
<legend ng-if-start="ctrl.application.spec.template.spec.containers.length > 1">Containers:</legend>
86+
<div ng-if-end class="form-group container-options">
87+
<div ng-if="ctrl.attachAllContainers">
88+
The {{ctrl.apiObject.kind | humanizeKind}} will be added to all containers. You can
89+
<a href="" ng-click="ctrl.attachAllContainers = false">select specific containers</a>
90+
instead.
91+
</div>
92+
<div ng-if="!ctrl.attachAllContainers" class="form-group">
93+
<label class="sr-only required">Containers</label>
94+
<select-containers
95+
ng-model="ctrl.attachContainers"
96+
pod-template="ctrl.application.spec.template"
97+
ng-required="true"
98+
help-text="Add the {{ctrl.apiObject.kind | humanizeKind}} to the selected containers.">
99+
</select-containers>
100+
</div>
98101
</div>
99102
</div>
100103
<div class="button-group pull-right">
@@ -108,7 +111,7 @@ <h3>Add to Application</h3>
108111
class="btn btn-primary"
109112
ng-class="{'dialog-btn': isDialog}"
110113
ng-click="ctrl.addToApplication()"
111-
ng-disabled="ctrl.addType === 'volume' && addToApplicationForm.$invalid || !ctrl.application"
114+
ng-disabled="(ctrl.addType === 'volume' && addToApplicationForm.$invalid) || (ctrl.addType === 'env' && !ctrl.canAddRefToApplication)"
112115
value="">
113116
Save
114117
</button>

Diff for: dist/scripts/scripts.js

+33-15
Original file line numberDiff line numberDiff line change
@@ -10993,7 +10993,25 @@ templateUrl: "views/directives/action-chip.html"
1099310993
}), function() {
1099410994
angular.module("openshiftConsole").component("addConfigToApplication", {
1099510995
controller: [ "$filter", "$scope", "APIService", "ApplicationsService", "DataService", "Navigate", "NotificationsService", "StorageService", function(e, t, n, a, r, o, i, s) {
10996-
var c = this, l = e("humanizeKind"), u = function() {
10996+
var c = this, l = e("humanizeKind");
10997+
c.canAddRefToApplication = !0;
10998+
var u = function(e) {
10999+
var t = c.apiObject.metadata.name;
11000+
return "ConfigMap" === c.apiObject.kind ? _.some(e.envFrom, {
11001+
configMapRef: {
11002+
name: t
11003+
}
11004+
}) : _.some(e.envFrom, {
11005+
secretRef: {
11006+
name: t
11007+
}
11008+
});
11009+
};
11010+
c.checkApplicationContainersRefs = function(e) {
11011+
var t = _.get(e, "spec.template.spec.containers");
11012+
c.canAddRefToApplication = !_.every(t, u);
11013+
};
11014+
var d = function() {
1099711015
var e = {
1099811016
namespace: c.project.metadata.name
1099911017
};
@@ -11002,13 +11020,13 @@ c.applications = e, c.updating = !1;
1100211020
});
1100311021
};
1100411022
c.$onInit = function() {
11005-
c.addType = "env", c.disableInputs = !1, u();
11023+
c.addType = "env", c.disableInputs = !1, d();
1100611024
var e = new RegExp("^[A-Za-z_][A-Za-z0-9_]*$");
1100711025
c.hasInvalidEnvVars = _.some(c.apiObject.data, function(t, n) {
1100811026
return !e.test(n);
1100911027
});
1101011028
};
11011-
var d = function(e) {
11029+
var m = function(e) {
1101211030
return c.attachAllContainers || c.attachContainers[e.name];
1101311031
};
1101411032
c.$postLink = function() {
@@ -11037,51 +11055,51 @@ name: c.apiObject.metadata.name
1103711055
};
1103811056
}
1103911057
_.each(a.spec.containers, function(e) {
11040-
d(e) && (e.envFrom = e.envFrom || [], e.envFrom.push(s));
11058+
m(e) && !u(e) && (e.envFrom = e.envFrom || [], e.envFrom.push(s));
1104111059
});
1104211060
} else {
11043-
var l = e("generateName")(c.apiObject.metadata.name + "-"), u = {
11061+
var l = e("generateName")(c.apiObject.metadata.name + "-"), d = {
1104411062
name: l,
1104511063
mountPath: c.mountVolume,
1104611064
readOnly: !0
1104711065
};
1104811066
_.each(a.spec.containers, function(e) {
11049-
d(e) && (e.volumeMounts = e.volumeMounts || [], e.volumeMounts.push(u));
11067+
m(e) && (e.volumeMounts = e.volumeMounts || [], e.volumeMounts.push(d));
1105011068
});
11051-
var m = {
11069+
var p = {
1105211070
name: l
1105311071
};
1105411072
switch (c.apiObject.kind) {
1105511073
case "Secret":
11056-
m.secret = {
11074+
p.secret = {
1105711075
secretName: c.apiObject.metadata.name
1105811076
};
1105911077
break;
1106011078

1106111079
case "ConfigMap":
11062-
m.configMap = {
11080+
p.configMap = {
1106311081
name: c.apiObject.metadata.name
1106411082
};
1106511083
}
11066-
a.spec.volumes = a.spec.volumes || [], a.spec.volumes.push(m);
11084+
a.spec.volumes = a.spec.volumes || [], a.spec.volumes.push(p);
1106711085
}
11068-
var p = e("humanizeKind"), f = p(c.apiObject.kind), g = p(t.kind), v = {
11086+
var f = e("humanizeKind"), g = f(c.apiObject.kind), v = f(t.kind), h = {
1106911087
namespace: c.project.metadata.name
1107011088
};
11071-
r.update(n.kindToResource(t.kind), t.metadata.name, t, v).then(function() {
11089+
r.update(n.kindToResource(t.kind), t.metadata.name, t, h).then(function() {
1107211090
i.addNotification({
1107311091
type: "success",
11074-
message: "Successfully added " + f + " " + c.apiObject.metadata.name + " to " + g + " " + t.metadata.name + ".",
11092+
message: "Successfully added " + g + " " + c.apiObject.metadata.name + " to " + v + " " + t.metadata.name + ".",
1107511093
links: [ {
1107611094
href: o.resourceURL(t),
11077-
label: "View " + p(t.kind, !0)
11095+
label: "View " + f(t.kind, !0)
1107811096
} ]
1107911097
}), angular.isFunction(c.onComplete) && c.onComplete();
1108011098
}, function(n) {
1108111099
var a = e("getErrorDetails");
1108211100
i.addNotification({
1108311101
type: "error",
11084-
message: "An error occurred adding " + f + " " + c.apiObject.metadata.name + " to " + g + " " + t.metadata.name + ". " + a(n)
11102+
message: "An error occurred adding " + g + " " + c.apiObject.metadata.name + " to " + v + " " + t.metadata.name + ". " + a(n)
1108511103
});
1108611104
}).finally(function() {
1108711105
c.disableInputs = !1;

Diff for: dist/scripts/templates.js

+12-7
Original file line numberDiff line numberDiff line change
@@ -5677,9 +5677,9 @@ angular.module('openshiftConsoleTemplates', []).run(['$templateCache', function(
56775677
"<form name=\"addToApplicationForm\" novalidate>\n" +
56785678
"<fieldset ng-disabled=\"disableInputs\">\n" +
56795679
"<legend>Add this {{ctrl.apiObject.kind | humanizeKind}} to application:</legend>\n" +
5680-
"<div class=\"form-group\">\n" +
5680+
"<div class=\"form-group\" ng-class=\"{'has-error' : ctrl.addType === 'env' && ctrl.application && !ctrl.canAddRefToApplication}\">\n" +
56815681
"<div class=\"application-select\">\n" +
5682-
"<ui-select id=\"application\" ng-model=\"ctrl.application\" required=\"true\" ng-disabled=\"ctrl.disableInputs\">\n" +
5682+
"<ui-select id=\"application\" ng-model=\"ctrl.application\" on-select=\"ctrl.checkApplicationContainersRefs($item)\" required=\"true\" ng-disabled=\"ctrl.disableInputs\">\n" +
56835683
"<ui-select-match placeholder=\"{{ctrl.applications.length ? 'Select an application' : 'There are no applications in this project'}}\">\n" +
56845684
"<span>\n" +
56855685
"{{$select.selected.metadata.name}}\n" +
@@ -5692,6 +5692,11 @@ angular.module('openshiftConsoleTemplates', []).run(['$templateCache', function(
56925692
"</ui-select>\n" +
56935693
"</div>\n" +
56945694
"</div>\n" +
5695+
"<div class=\"has-error\" ng-if=\"ctrl.addType === 'env' && ctrl.application && !ctrl.canAddRefToApplication\">\n" +
5696+
"<span class=\"help-block\">\n" +
5697+
"The {{ctrl.apiObject.kind | humanizeKind}} has already been added to this application.\n" +
5698+
"</span>\n" +
5699+
"</div>\n" +
56955700
"<legend>Add {{ctrl.apiObject.kind | humanizeKind}} as:</legend>\n" +
56965701
"<div class=\"form-group\">\n" +
56975702
"<div class=\"radio\">\n" +
@@ -5700,10 +5705,8 @@ angular.module('openshiftConsoleTemplates', []).run(['$templateCache', function(
57005705
"<input type=\"radio\" ng-model=\"ctrl.addType\" value=\"env\" ng-disabled=\"ctrl.disableInputs\">\n" +
57015706
"Environment variables\n" +
57025707
"</label>\n" +
5703-
"<div class=\"alert alert-warning env-warning\" ng-show=\"ctrl.hasInvalidEnvVars\">\n" +
5704-
"<span class=\"pficon pficon-warning-triangle-o\" aria-hidden=\"true\"></span>\n" +
5705-
"<span>Some of the keys for {{ctrl.apiObject.kind | humanizeKind}}\n" +
5706-
"<strong>{{ctrl.apiObject.metadata.name}}</strong> are not valid environment variable names and will not be added.</span>\n" +
5708+
"<div class=\"has-warning\" ng-if=\"ctrl.hasInvalidEnvVars\">\n" +
5709+
"<span class=\"help-block\">Some of the keys for {{ctrl.apiObject.kind | humanizeKind}} <strong>{{ctrl.apiObject.metadata.name}}</strong> are not valid environment variable names and will not be added.</span>\n" +
57075710
"</div>\n" +
57085711
"</div>\n" +
57095712
"<div>\n" +
@@ -5732,6 +5735,7 @@ angular.module('openshiftConsoleTemplates', []).run(['$templateCache', function(
57325735
"</div>\n" +
57335736
"</div>\n" +
57345737
"</div>\n" +
5738+
"<div ng-if=\"ctrl.canAddRefToApplication\">\n" +
57355739
"<legend ng-if-start=\"ctrl.application.spec.template.spec.containers.length > 1\">Containers:</legend>\n" +
57365740
"<div ng-if-end class=\"form-group container-options\">\n" +
57375741
"<div ng-if=\"ctrl.attachAllContainers\">\n" +
@@ -5745,11 +5749,12 @@ angular.module('openshiftConsoleTemplates', []).run(['$templateCache', function(
57455749
"</select-containers>\n" +
57465750
"</div>\n" +
57475751
"</div>\n" +
5752+
"</div>\n" +
57485753
"<div class=\"button-group pull-right\">\n" +
57495754
"<button class=\"btn btn-default\" ng-class=\"{'dialog-btn': isDialog}\" ng-click=\"ctrl.onCancel()\">\n" +
57505755
"Cancel\n" +
57515756
"</button>\n" +
5752-
"<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" +
5757+
"<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" +
57535758
"Save\n" +
57545759
"</button>\n" +
57555760
"</div>\n" +

Diff for: dist/styles/main.css

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)