-
Notifications
You must be signed in to change notification settings - Fork 231
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the ability to add a secret to an application
- Loading branch information
1 parent
e724d33
commit c9f4d62
Showing
9 changed files
with
641 additions
and
92 deletions.
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); | ||
}); | ||
} 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.