forked from openshift/origin-web-console
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoscRouting.js
201 lines (181 loc) · 7.17 KB
/
oscRouting.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
"use strict";
angular.module("openshiftConsole")
/**
* Widget for entering route information
*
* model:
* The model for the input. The model will either use or add the following keys:
* {
* name: "",
* host: "",
* path: "",
* to: {}, // object with service and weight properties
* alternateServices: [], // alternate backend objects, each with service and weight properties
* tls.termination: "",
* tls.insecureEdgeTerminationPolicy: "",
* tls.certificate: "",
* tls.key: "",
* tls.caCertificate: "",
* tls.destinationCACertificate: ""
* }
*
* services:
* Collection of services to choose from for the route (optional)
*
* showNameInput:
* Whether to prompt the user for a route name (default: false)
*
* routingDisabled:
* An expression that will disable the form (default: false)
*/
.directive("oscRouting", function() {
return {
require: '^form',
restrict: 'E',
scope: {
route: "=model",
services: "=",
showNameInput: "=",
routingDisabled: "=",
hostReadOnly: "="
},
templateUrl: 'views/directives/osc-routing.html',
controller: function($scope) {
$scope.disableCertificateInputs = function() {
var termination = _.get($scope, 'route.tls.termination');
return !termination || termination === 'passthrough';
};
},
link: function(scope, element, attrs, formCtl) {
scope.form = formCtl;
var updatePortOptions = function(service) {
if (!service) {
return;
}
scope.unnamedServicePort = service.spec.ports.length === 1 && !service.spec.ports[0].name;
// Only show port options when there is more than one port or when a
// single service port has a name. We want to use the service port
// name when creating a route. (Port name is required for services
// with more than one port.)
if (service.spec.ports.length && !scope.unnamedServicePort) {
scope.route.portOptions = _.map(service.spec.ports, function(portMapping) {
return {
port: portMapping.name,
// \u2192 is a Unicode right arrow.
label: portMapping.port + " \u2192 " +
portMapping.targetPort + " (" + portMapping.protocol + ")"
};
});
} else {
scope.route.portOptions = [];
}
};
if (scope.services && !scope.route.service) {
// Use _.find to get the first item.
scope.route.service = _.find(scope.services);
}
scope.$watch('route.to.service', function(newValue, oldValue) {
updatePortOptions(newValue);
// Don't overwrite the target port when editing an existing route unless the user picked a
// different service.
if (newValue !== oldValue || !scope.route.targetPort) {
scope.route.targetPort = _.get(scope, 'route.portOptions[0].port');
}
if (scope.services) {
// Update the options for alternate services.
scope.alternateServiceOptions = _.reject(scope.services, function(service) {
return newValue === service;
});
}
});
scope.$watch('route.alternateServices', function(alternateServices) {
// Find any duplicates.
scope.duplicateServices = _(alternateServices).map('service').filter(function(value, index, iteratee) {
return _.includes(iteratee, value, index + 1);
}).value();
formCtl.$setValidity("duplicateServices", !scope.duplicateServices.length);
}, true);
var showCertificateWarning = function() {
if (!scope.route.tls) {
return false;
}
if (scope.route.tls.termination && scope.route.tls.termination !== 'passthrough') {
return false;
}
// Check if any certificate or key is set with an incompatible termination.
return scope.route.tls.certificate ||
scope.route.tls.key ||
scope.route.tls.caCertificate ||
scope.route.tls.destinationCACertificate;
};
// Show a warning if previously-set certificates won't be used because
// the TLS termination is now incompatible.
scope.$watch('route.tls.termination', function() {
scope.secureRoute = !!_.get(scope, 'route.tls.termination');
scope.showCertificatesNotUsedWarning = showCertificateWarning();
});
var previousTermination;
scope.$watch('secureRoute', function(newValue, oldValue) {
if (newValue === oldValue) {
return;
}
var termination = _.get(scope, 'route.tls.termination');
if (!scope.securetRoute && termination) {
// Remember previous value if user switches back to secure.
previousTermination = termination;
delete scope.route.tls.termination;
}
if (scope.secureRoute && !termination) {
// Restore previous termination value or default to edge if no previous value.
_.set(scope, 'route.tls.termination', previousTermination || 'edge');
}
});
scope.addAlternateService = function() {
scope.route.alternateServices = scope.route.alternateServices || [];
var firstUnselected = _.find(scope.services, function(service) {
return service !== scope.route.to.service && !_.some(scope.route.alternateServices, { service: service });
});
// Add a new value.
scope.route.alternateServices.push({
service: firstUnselected
});
};
}
};
})
// Prompts for a service and optionally a weight for A/B traffic.
.directive("oscRoutingService", function() {
return {
restrict: 'E',
scope: {
// The model, an object with properties `service` and `weight`
model: "=",
// Collection of service objects
services: "=",
// `true` if this is an alternate route target for A/B traffic
// (optional). Changes the labels and help text.
isAlternate: "=?",
// Show a weight field (optional)
showWeight: "=?"
},
templateUrl: 'views/directives/osc-routing-service.html',
link: function(scope, element, attrs, formCtl) {
scope.form = formCtl;
scope.id = _.uniqueId('osc-routing-service-');
// Set an initial value for `model.service` if not set.
scope.$watchGroup(['model.service', 'services'], function() {
if (_.isEmpty(scope.services)) {
return;
}
// If the selected item is in the list, do nothing.
var selected = _.get(scope, 'model.service');
if (selected && _.includes(scope.services, selected)) {
return;
}
// Use _.find to get the first item.
var firstService = _.find(scope.services);
_.set(scope, 'model.service', firstService);
});
}
};
});