Skip to content

Commit 3d5ae02

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

File tree

6 files changed

+92
-54
lines changed

6 files changed

+92
-54
lines changed

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

+15-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,20 @@
2626
var ctrl = this;
2727
var humanizeKind = $filter('humanizeKind');
2828

29+
var conatinerHasRef = function(container) {
30+
var addRefName = ctrl.apiObject.metadata.name;
31+
if (ctrl.apiObject.kind === "ConfigMap") {
32+
return _.some(container.envFrom, {configMapRef: {name: addRefName}});
33+
} else {
34+
return _.some(container.envFrom, {secretRef: {name: addRefName}});
35+
}
36+
};
37+
38+
ctrl.checkApplicationContainersRefs = function(application) {
39+
var containers = _.get(application, 'spec.template.spec.containers');
40+
ctrl.canAddRefToApplication = !_.every(containers, conatinerHasRef);
41+
};
42+
2943
var getApplications = function() {
3044
var context = {
3145
namespace: ctrl.project.metadata.name
@@ -91,7 +105,7 @@
91105

92106
// For each container, add the new volume mount.
93107
_.each(podTemplate.spec.containers, function(container) {
94-
if (isContainerSelected(container)) {
108+
if (isContainerSelected(container) && !conatinerHasRef(container)) {
95109
container.envFrom = container.envFrom || [];
96110
container.envFrom.push(newEnvFrom);
97111
}

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

+28-25
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>
@@ -56,7 +57,7 @@ <h3>Add to Application</h3>
5657
ng-pattern="/^\/.*$/"
5758
osc-unique="ctrl.existingMountPaths"
5859
aria-describedby="mount-path-help"
59-
ng-disabled="ctrl.addType !== 'volume' || ctrl.disableInputs"
60+
ng-disabled="ctrl.addType !== 'volume' || ctrl.disableInputs || !ctrl.application"
6061
ng-model="ctrl.mountVolume"
6162
autocorrect="off"
6263
autocapitalize="off"
@@ -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="addToApplicationForm.$invalid || (ctrl.addType === 'env' && !ctrl.canAddRefToApplication)"
112115
value="">
113116
Save
114117
</button>

Diff for: dist/scripts/scripts.js

+31-15
Original file line numberDiff line numberDiff line change
@@ -10993,7 +10993,23 @@ 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"), u = function(e) {
10997+
var t = c.apiObject.metadata.name;
10998+
return "ConfigMap" === c.apiObject.kind ? _.some(e.envFrom, {
10999+
configMapRef: {
11000+
name: t
11001+
}
11002+
}) : _.some(e.envFrom, {
11003+
secretRef: {
11004+
name: t
11005+
}
11006+
});
11007+
};
11008+
c.checkApplicationContainersRefs = function(e) {
11009+
var t = _.get(e, "spec.template.spec.containers");
11010+
c.canAddRefToApplication = !_.every(t, u);
11011+
};
11012+
var d = function() {
1099711013
var e = {
1099811014
namespace: c.project.metadata.name
1099911015
};
@@ -11002,13 +11018,13 @@ c.applications = e, c.updating = !1;
1100211018
});
1100311019
};
1100411020
c.$onInit = function() {
11005-
c.addType = "env", c.disableInputs = !1, u();
11021+
c.addType = "env", c.disableInputs = !1, d();
1100611022
var e = new RegExp("^[A-Za-z_][A-Za-z0-9_]*$");
1100711023
c.hasInvalidEnvVars = _.some(c.apiObject.data, function(t, n) {
1100811024
return !e.test(n);
1100911025
});
1101011026
};
11011-
var d = function(e) {
11027+
var m = function(e) {
1101211028
return c.attachAllContainers || c.attachContainers[e.name];
1101311029
};
1101411030
c.$postLink = function() {
@@ -11037,51 +11053,51 @@ name: c.apiObject.metadata.name
1103711053
};
1103811054
}
1103911055
_.each(a.spec.containers, function(e) {
11040-
d(e) && (e.envFrom = e.envFrom || [], e.envFrom.push(s));
11056+
m(e) && !u(e) && (e.envFrom = e.envFrom || [], e.envFrom.push(s));
1104111057
});
1104211058
} else {
11043-
var l = e("generateName")(c.apiObject.metadata.name + "-"), u = {
11059+
var l = e("generateName")(c.apiObject.metadata.name + "-"), d = {
1104411060
name: l,
1104511061
mountPath: c.mountVolume,
1104611062
readOnly: !0
1104711063
};
1104811064
_.each(a.spec.containers, function(e) {
11049-
d(e) && (e.volumeMounts = e.volumeMounts || [], e.volumeMounts.push(u));
11065+
m(e) && (e.volumeMounts = e.volumeMounts || [], e.volumeMounts.push(d));
1105011066
});
11051-
var m = {
11067+
var p = {
1105211068
name: l
1105311069
};
1105411070
switch (c.apiObject.kind) {
1105511071
case "Secret":
11056-
m.secret = {
11072+
p.secret = {
1105711073
secretName: c.apiObject.metadata.name
1105811074
};
1105911075
break;
1106011076

1106111077
case "ConfigMap":
11062-
m.configMap = {
11078+
p.configMap = {
1106311079
name: c.apiObject.metadata.name
1106411080
};
1106511081
}
11066-
a.spec.volumes = a.spec.volumes || [], a.spec.volumes.push(m);
11082+
a.spec.volumes = a.spec.volumes || [], a.spec.volumes.push(p);
1106711083
}
11068-
var p = e("humanizeKind"), f = p(c.apiObject.kind), g = p(t.kind), v = {
11084+
var f = e("humanizeKind"), g = f(c.apiObject.kind), v = f(t.kind), h = {
1106911085
namespace: c.project.metadata.name
1107011086
};
11071-
r.update(n.kindToResource(t.kind), t.metadata.name, t, v).then(function() {
11087+
r.update(n.kindToResource(t.kind), t.metadata.name, t, h).then(function() {
1107211088
i.addNotification({
1107311089
type: "success",
11074-
message: "Successfully added " + f + " " + c.apiObject.metadata.name + " to " + g + " " + t.metadata.name + ".",
11090+
message: "Successfully added " + g + " " + c.apiObject.metadata.name + " to " + v + " " + t.metadata.name + ".",
1107511091
links: [ {
1107611092
href: o.resourceURL(t),
11077-
label: "View " + p(t.kind, !0)
11093+
label: "View " + f(t.kind, !0)
1107811094
} ]
1107911095
}), angular.isFunction(c.onComplete) && c.onComplete();
1108011096
}, function(n) {
1108111097
var a = e("getErrorDetails");
1108211098
i.addNotification({
1108311099
type: "error",
11084-
message: "An error occurred adding " + f + " " + c.apiObject.metadata.name + " to " + g + " " + t.metadata.name + ". " + a(n)
11100+
message: "An error occurred adding " + g + " " + c.apiObject.metadata.name + " to " + v + " " + t.metadata.name + ". " + a(n)
1108511101
});
1108611102
}).finally(function() {
1108711103
c.disableInputs = !1;

Diff for: dist/scripts/templates.js

+13-8
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" +
@@ -5714,7 +5717,7 @@ angular.module('openshiftConsoleTemplates', []).run(['$templateCache', function(
57145717
"</div>\n" +
57155718
"<div class=\"volume-options\">\n" +
57165719
"<div ng-class=\"{'has-error': (addToApplicationForm.mountVolume.$error.oscUnique || (addToApplicationForm.mountVolume.$error.pattern && addToApplicationForm.mountVolume.$touched))}\">\n" +
5717-
"<input class=\"form-control\" name=\"mountVolume\" id=\"mountVolume\" placeholder=\"Enter a mount path\" type=\"text\" required ng-pattern=\"/^\\/.*$/\" osc-unique=\"ctrl.existingMountPaths\" aria-describedby=\"mount-path-help\" ng-disabled=\"ctrl.addType !== 'volume' || ctrl.disableInputs\" ng-model=\"ctrl.mountVolume\" autocorrect=\"off\" autocapitalize=\"off\" spellcheck=\"false\">\n" +
5720+
"<input class=\"form-control\" name=\"mountVolume\" id=\"mountVolume\" placeholder=\"Enter a mount path\" type=\"text\" required ng-pattern=\"/^\\/.*$/\" osc-unique=\"ctrl.existingMountPaths\" aria-describedby=\"mount-path-help\" ng-disabled=\"ctrl.addType !== 'volume' || ctrl.disableInputs || !ctrl.application\" ng-model=\"ctrl.mountVolume\" autocorrect=\"off\" autocapitalize=\"off\" spellcheck=\"false\">\n" +
57185721
"</div>\n" +
57195722
"<div class=\"help-block bind-description\">\n" +
57205723
"Mount Path for the volume. A file will be created in this directory for each key from the {{ctrl.apiObject.kind | humanizeKind}}. The file contents will be the value of the key.\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=\"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)