-
Notifications
You must be signed in to change notification settings - Fork 231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add the ability to add a secret to an application #2021
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
"use strict"; | ||
(function() { | ||
angular.module("openshiftConsole").component('addSecretToApplication', { | ||
controller: [ | ||
'$filter', | ||
'$scope', | ||
'APIService', | ||
'DataService', | ||
'Navigate', | ||
'NotificationsService', | ||
'StorageService', | ||
AddSecretToApplication | ||
], | ||
controllerAs: 'ctrl', | ||
bindings: { | ||
project: '<', | ||
secret: '<', | ||
onComplete: '<', | ||
onCancel: '<' | ||
}, | ||
templateUrl: 'views/directives/add-secret-to-application.html' | ||
}); | ||
|
||
function AddSecretToApplication($filter, $scope, APIService, DataService, Navigate, NotificationsService, StorageService) { | ||
var ctrl = this; | ||
var deploymentConfigs; | ||
var deployments; | ||
var replicationControllers; | ||
var replicaSets; | ||
var statefulSets; | ||
|
||
var sortApplications = function() { | ||
// Don't waste time sorting on each data load, just sort when we have them all | ||
if (deploymentConfigs && deployments && replicationControllers && replicaSets && statefulSets) { | ||
var apiObjects = deploymentConfigs.concat(deployments) | ||
.concat(replicationControllers) | ||
.concat(replicaSets) | ||
.concat(statefulSets); | ||
ctrl.applications = _.sortBy(apiObjects, ['metadata.name', 'kind']); | ||
ctrl.updating = false; | ||
} | ||
}; | ||
|
||
var getApplications = function() { | ||
var hasDeploymentFilter = $filter('hasDeployment'); | ||
var hasDeploymentConfigFilter = $filter('hasDeploymentConfig'); | ||
|
||
ctrl.updating = true; | ||
var context = { | ||
namespace: ctrl.project.metadata.name | ||
}; | ||
// Load all the "application" types | ||
DataService.list('deploymentconfigs', context).then(function(deploymentConfigData) { | ||
deploymentConfigs = _.toArray(deploymentConfigData.by('metadata.name')); | ||
sortApplications(); | ||
}); | ||
DataService.list('replicationcontrollers', context).then(function(replicationControllerData) { | ||
replicationControllers = _.reject(replicationControllerData.by('metadata.name'), hasDeploymentConfigFilter); | ||
sortApplications(); | ||
}); | ||
DataService.list({ | ||
group: 'apps', | ||
resource: 'deployments' | ||
}, context).then(function(deploymentData) { | ||
deployments = _.toArray(deploymentData.by('metadata.name')); | ||
sortApplications(); | ||
}); | ||
DataService.list({ | ||
group: 'extensions', | ||
resource: 'replicasets' | ||
}, context).then(function(replicaSetData) { | ||
replicaSets = _.reject(replicaSetData.by('metadata.name'), hasDeploymentFilter); | ||
sortApplications(); | ||
}); | ||
DataService.list({ | ||
group: 'apps', | ||
resource: 'statefulsets' | ||
}, context).then(function(statefulSetData) { | ||
statefulSets = _.toArray(statefulSetData.by('metadata.name')); | ||
sortApplications(); | ||
}); | ||
}; | ||
|
||
ctrl.$onInit = function() { | ||
ctrl.addType = 'env'; | ||
ctrl.disableInputs = false; | ||
getApplications(); | ||
}; | ||
|
||
ctrl.$postLink = function() { | ||
$scope.$watch(function() { | ||
return ctrl.application; | ||
}, function() { | ||
// Look at the existing mount paths so that we can warn if the new value is not unique. | ||
var podTemplate = _.get(ctrl.application, 'spec.template'); | ||
ctrl.existingMountPaths = StorageService.getMountPaths(podTemplate); | ||
}); | ||
}; | ||
|
||
ctrl.addToApplication = function() { | ||
var applicationToUpdate = angular.copy(ctrl.application); | ||
|
||
var podTemplate = _.get(applicationToUpdate, 'spec.template'); | ||
|
||
ctrl.disableInputs = true; | ||
|
||
if (ctrl.addType === 'env') { | ||
var newEnvFrom = { | ||
secretRef: { | ||
name: ctrl.secret.metadata.name | ||
} | ||
}; | ||
|
||
// For each container, add the new volume mount. | ||
_.each(podTemplate.spec.containers, function(container) { | ||
container.envFrom = container.envFrom || []; | ||
container.envFrom.push(newEnvFrom); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should call |
||
}); | ||
} else { | ||
var generateName = $filter('generateName'); | ||
var name = generateName(ctrl.secret.metadata.name + '-'); | ||
var newVolumeMount = { | ||
name: name, | ||
mountPath: ctrl.mountVolume, | ||
readOnly: true | ||
}; | ||
|
||
// For each selected container, add the new volume mount. | ||
_.each(podTemplate.spec.containers, function(container) { | ||
container.volumeMounts = container.volumeMounts || []; | ||
container.volumeMounts.push(newVolumeMount); | ||
}); | ||
|
||
var newVolume = { | ||
name: name, | ||
secret: { | ||
secretName: ctrl.secret.metadata.name | ||
} | ||
}; | ||
|
||
podTemplate.spec.volumes = podTemplate.spec.volumes || []; | ||
podTemplate.spec.volumes.push(newVolume); | ||
} | ||
|
||
var humanizeKind = $filter('humanizeKind'); | ||
var sourceKind = humanizeKind(ctrl.secret.kind); | ||
var targetKind = humanizeKind(applicationToUpdate.kind); | ||
var context = { | ||
namespace: ctrl.project.metadata.name | ||
}; | ||
|
||
DataService.update(APIService.kindToResource(applicationToUpdate.kind), applicationToUpdate.metadata.name, applicationToUpdate, context).then( | ||
function() { | ||
NotificationsService.addNotification({ | ||
type: "success", | ||
message: "Successfully added " + sourceKind + " " + ctrl.secret.metadata.name + " to " + targetKind + " " + applicationToUpdate.metadata.name + ".", | ||
links: [{ | ||
href: Navigate.resourceURL(applicationToUpdate), | ||
label: "View " + humanizeKind(applicationToUpdate.kind, true) | ||
}] | ||
}); | ||
if (angular.isFunction(ctrl.onComplete)) { | ||
ctrl.onComplete(); | ||
} | ||
}, | ||
function(result) { | ||
var getErrorDetails = $filter('getErrorDetails'); | ||
|
||
NotificationsService.addNotification({ | ||
type: "error", | ||
message: "An error occurred adding " + sourceKind + " " + ctrl.secret.metadata.name + " to " + targetKind + " " + applicationToUpdate.metadata.name + ". " + | ||
getErrorDetails(result) | ||
}); | ||
}).finally(function() { | ||
ctrl.disableInputs = false; | ||
} | ||
); | ||
}; | ||
} | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<div> | ||
<div class="dialog-title"> | ||
<h3>Add to Application</h3> | ||
</div> | ||
<div class="dialog-body"> | ||
<form name="addToApplicationForm" novalidate> | ||
<fieldset ng-disabled="disableInputs"> | ||
<legend>Add this secret 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-match placeholder="{{ctrl.applications.length ? 'Select an application' : 'There are no applications in this project'}}"> | ||
<span> | ||
{{$select.selected.metadata.name}} | ||
<small class="text-muted">– {{$select.selected.kind | humanizeKind : true}}</small> | ||
</span> | ||
</ui-select-match> | ||
<ui-select-choices | ||
repeat="application in (ctrl.applications) | filter : { metadata: { name: $select.search } } track by (application | uid)" | ||
group-by="ctrl.groupByKind"> | ||
<span ng-bind-html="application.metadata.name | highlight : $select.search"></span> | ||
</ui-select-choices> | ||
</ui-select> | ||
</div> | ||
</div> | ||
<legend>Add secret as:</legend> | ||
<div class="form-group"> | ||
<div class="radio"> | ||
<label class="add-choice" for="envFrom"> | ||
<input id="envFrom" type="radio" ng-model="ctrl.addType" value="env" ng-disabled="ctrl.disableInputs"> | ||
Environment variables | ||
</label> | ||
<div> | ||
<label class="add-choice" for="mountVolume"> | ||
<input type="radio" ng-model="ctrl.addType" value="volume" ng-disabled="ctrl.disableInputs"> | ||
Volume | ||
</label> | ||
</div> | ||
<div class="volume-options"> | ||
<div ng-class="{'has-error': (addToApplicationForm.mountVolume.$error.pattern && addToApplicationForm.mountVolume.$touched)}"> | ||
<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"> | ||
</div> | ||
<div class="help-block bind-description"> | ||
Mount Path for the volume. A file will be created in this director for each key from the secret. The file contents will be the value of the key. | ||
</div> | ||
<div class="has-error" ng-show="addToApplicationForm.mountVolume.$error.oscUnique"> | ||
<span class="help-block"> | ||
The mount path is already used. Please choose another mount path. | ||
</span> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
<div class="button-group pull-right"> | ||
<button | ||
class="btn btn-default" | ||
ng-class="{'dialog-btn': isDialog}" | ||
ng-click="ctrl.onCancel()"> | ||
Cancel | ||
</button> | ||
<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=""> | ||
Save | ||
</button> | ||
</div> | ||
</fieldset> | ||
</form> | ||
<div class="updating" ng-if="ctrl.updating"> | ||
<div class="spinner spinner-lg" aria-hidden="true"></div> | ||
<h3> | ||
<span class="sr-only">Updating</span> | ||
</h3> | ||
</div> | ||
</div> | ||
</div> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're doing this now in enough places now that it would be good to put this logic into a separate service (ApplicationsService?).
Not for this PR, though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Heh, I thought the same and started working on it but I ran out of time to make the change.
Added issue: openshift/origin-web-common#175