Skip to content

Commit

Permalink
fix(gce): normalize server group load balancers (#3415)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielpeach committed Mar 23, 2017
1 parent 74ad6eb commit f9cd763
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 12 deletions.
6 changes: 3 additions & 3 deletions app/scripts/modules/core/cluster/cluster.service.js
Expand Up @@ -15,9 +15,9 @@ module.exports = angular.module('spinnaker.core.cluster.service', [
])
.factory('clusterService', function ($q, API, serverGroupTransformer, namingService) {

function loadServerGroups(applicationName) {
function loadServerGroups(application) {
var serverGroupLoader = $q.all({
serverGroups: API.one('applications').one(applicationName).all('serverGroups').getList().then(g => g, () => []),
serverGroups: API.one('applications').one(application.name).all('serverGroups').getList().then(g => g, () => []),
});
return serverGroupLoader.then(function(results) {
results.serverGroups = results.serverGroups || [];
Expand All @@ -28,7 +28,7 @@ module.exports = angular.module('spinnaker.core.cluster.service', [
serverGroup.category = 'serverGroup'
);

return $q.all(results.serverGroups.map(serverGroupTransformer.normalizeServerGroup));
return $q.all(results.serverGroups.map(serverGroup => serverGroupTransformer.normalizeServerGroup(serverGroup, application)));
});
}

Expand Down
Expand Up @@ -15,7 +15,7 @@ module.exports = angular
.run(function($q, applicationDataSourceRegistry, clusterService, entityTagsReader, serverGroupTransformer, settings) {

let loadServerGroups = (application) => {
return clusterService.loadServerGroups(application.name);
return clusterService.loadServerGroups(application);
};

let addServerGroups = (application, serverGroups) => {
Expand Down
Expand Up @@ -7,9 +7,9 @@ module.exports = angular.module('spinnaker.core.serverGroup.transformer', [
])
.factory('serverGroupTransformer', function (serviceDelegate) {

function normalizeServerGroup(serverGroup) {
function normalizeServerGroup(serverGroup, application) {
return serviceDelegate.getDelegate(serverGroup.provider || serverGroup.type, 'serverGroup.transformer').
normalizeServerGroup(serverGroup);
normalizeServerGroup(serverGroup, application);
}

function convertServerGroupCommandToDeployConfiguration(base) {
Expand Down
31 changes: 25 additions & 6 deletions app/scripts/modules/google/serverGroup/serverGroup.transformer.js
@@ -1,21 +1,40 @@
'use strict';

import _ from 'lodash';
import {defaults, uniq} from 'lodash';

let angular = require('angular');

module.exports = angular.module('spinnaker.gce.serverGroup.transformer', [])
.factory('gceServerGroupTransformer', function ($q) {
.factory('gceServerGroupTransformer', function () {

function normalizeServerGroup(serverGroup) {
return $q.when(serverGroup); // no-op
function normalizeServerGroup(serverGroup, application) {
return application.getDataSource('loadBalancers').ready().then(() => {
if (serverGroup.loadBalancers) {
let normalizedServerGroupLoadBalancerNames = [];
// At this point, the HTTP(S) load balancers have been normalized (listener names mapped to URL map names).
// Our server groups' lists of load balancer names still need to make this mapping.
serverGroup.loadBalancers.forEach(loadBalancerName => {
let matchingUrlMap = application.getDataSource('loadBalancers').data.find(loadBalancer => {
return serverGroup.account === loadBalancer.account &&
loadBalancer.listeners &&
loadBalancer.listeners.map(listener => listener.name).includes(loadBalancerName);
});

matchingUrlMap
? normalizedServerGroupLoadBalancerNames.push(matchingUrlMap.name)
: normalizedServerGroupLoadBalancerNames.push(loadBalancerName);
});
serverGroup.loadBalancers = uniq(normalizedServerGroupLoadBalancerNames);
}
return serverGroup;
});
}

function convertServerGroupCommandToDeployConfiguration(base) {
var truncatedZones = base.backingData.filtered.truncatedZones;

// use _.defaults to avoid copying the backingData, which is huge and expensive to copy over
var command = _.defaults({backingData: [], viewState: []}, base);
// use defaults to avoid copying the backingData, which is huge and expensive to copy over
var command = defaults({backingData: [], viewState: []}, base);
if (base.viewState.mode !== 'clone') {
delete command.source;
}
Expand Down
@@ -0,0 +1,57 @@
'use strict';

describe('gceServerGroupTransformer', () => {
let transformer, $q, $scope;
beforeEach(
window.module(
require('./serverGroup.transformer.js')
)
);

beforeEach(() => {
window.inject((_$q_, $rootScope, _gceServerGroupTransformer_) => {
$q = _$q_;
$scope = $rootScope.$new();
transformer = _gceServerGroupTransformer_;
});
});

describe('normalize server group load balancers', () => {
let app;
beforeEach(() => {
app = {
getDataSource: () => {
return {
ready: () => $q.resolve(),
data: [
{name: 'network-load-balancer'},
{name: 'internal-load-balancer'},
{name: 'url-map-name',
listeners: [{name: 'http-load-balancer-listener'}, {name: 'https-load-balancer-listener'}]}
]
};
}
};
});

it('should map listener names to url map names', function () {
let serverGroup = {
loadBalancers: [
'network-load-balancer',
'internal-load-balancer',
'http-load-balancer-listener',
'https-load-balancer-listener'
]
};

let normalizedServerGroup;
transformer.normalizeServerGroup(serverGroup, app)
.then(normalized => normalizedServerGroup = normalized);
$scope.$digest();
expect(normalizedServerGroup.loadBalancers.length).toBe(3);
expect(normalizedServerGroup.loadBalancers.includes('url-map-name')).toEqual(true);
expect(normalizedServerGroup.loadBalancers.includes('network-load-balancer')).toEqual(true);
expect(normalizedServerGroup.loadBalancers.includes('internal-load-balancer')).toEqual(true);
});
});
});

0 comments on commit f9cd763

Please sign in to comment.