Skip to content
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

feature/AKD-144: Added Azure 'Scale Sets Health Monitoring Enabled' plugin and test cases #715

Merged
merged 5 commits into from
Jun 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ module.exports = {
'approvedVmImage' : require(__dirname + '/plugins/azure/virtualmachines/approvedVmImage.js'),
'autoOsUpgradesEnabled' : require(__dirname + '/plugins/azure/virtualmachines/autoOsUpgradesEnabled.js'),
'noUnattachedDisks' : require(__dirname + '/plugins/azure/virtualmachines/noUnattachedDisks.js'),
'scaleSetHealthMonitoring' : require(__dirname + '/plugins/azure/virtualmachines/scaleSetHealthMonitoring.js'),
'diskByokEncryptionEnabled' : require(__dirname + '/plugins/azure/virtualmachines/diskByokEncryptionEnabled.js'),

'lbLogAnalyticsEnabled' : require(__dirname + '/plugins/azure/monitor/lbLogAnalyticsEnabled.js'),
Expand Down
61 changes: 61 additions & 0 deletions plugins/azure/virtualmachines/scaleSetHealthMonitoring.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const async = require('async');
const helpers = require('../../../helpers/azure');

module.exports = {
title: 'Scale Sets Health Monitoring Enabled',
category: 'Virtual Machines',
description: 'Ensures that health monitoring is enabled for virtual machine scale sets.',
more_info: 'Scale set health monitoring feature reports on VM health from inside the scale set instance and can be configured to probe on an application endpoint and update the status of the application on that instance. That instance status is checked by Azure to determine whether an instance is eligible for upgrade operations.',
recommended_action: 'Enable health monitoring for virtual machine scale sets.',
link: 'https://docs.microsoft.com/en-us/azure/virtual-machine-scale-sets/virtual-machine-scale-sets-health-extension',
apis: ['virtualMachineScaleSets:listAll'],

run: function(cache, settings, callback) {
const results = [];
const source = {};
const locations = helpers.locations(settings.govcloud);

async.each(locations.virtualMachineScaleSets, (location, rcb) => {
const virtualMachineScaleSets = helpers.addSource(cache, source,
['virtualMachineScaleSets', 'listAll', location]);

if (!virtualMachineScaleSets) return rcb();

if (virtualMachineScaleSets.err || !virtualMachineScaleSets.data) {
helpers.addResult(results, 3,
'Unable to query for Virtual Machine Scale Sets: ' + helpers.addError(virtualMachineScaleSets), location);
return rcb();
}

if (!virtualMachineScaleSets.data.length) {
helpers.addResult(results, 0, 'No existing Virtual Machine Scale Sets found', location);
return rcb();
}

async.each(virtualMachineScaleSets.data, (virtualMachineScaleSet, scb) => {
const scaleSetExtensions = virtualMachineScaleSet.virtualMachineProfile && virtualMachineScaleSet.virtualMachineProfile.extensionProfile &&
virtualMachineScaleSet.virtualMachineProfile.extensionProfile.extensions
? virtualMachineScaleSet.virtualMachineProfile.extensionProfile.extensions
: [];

const healthMonitoring = scaleSetExtensions.length
? scaleSetExtensions.some((extension) => (extension.name === 'healthRepairExtension' || extension.type === 'ApplicationHealthLinux'))
: false;
AkhtarAmir marked this conversation as resolved.
Show resolved Hide resolved

if (healthMonitoring) {
helpers.addResult(results, 0,
'Virtual Machine Scale Set has health monitoring enabled', location, virtualMachineScaleSet.id);
} else {
helpers.addResult(results, 2,
'Virtual Machine Scale Set has health monitoring disabled', location, virtualMachineScaleSet.id);
}

scb();
}, function() {
rcb();
});
}, function() {
callback(null, results, source);
});
}
};
102 changes: 102 additions & 0 deletions plugins/azure/virtualmachines/scaleSetHealthMonitoring.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
var expect = require('chai').expect;
var scaleSetHealthMonitoring = require('./scaleSetHealthMonitoring');

const virtualMachineScaleSets = [
{
'name': 'test-vmss',
'id': '/subscriptions/123/resourceGroups/AQUA-RESOURCE-GROUP/providers/Microsoft.Compute/virtualMachineScaleSets/test-vmss',
'type': 'Microsoft.Compute/virtualMachineScaleSets',
'virtualMachineProfile': {
'extensionProfile': {
'extensions': [
{
'name': 'healthRepairExtension',
'properties': {
'autoUpgradeMinorVersion': false,
'publisher': 'Microsoft.ManagedServices',
'type': 'ApplicationHealthLinux',
'typeHandlerVersion': '1.0',
'settings': {
'protocol': 'http',
'port': 80,
'requestPath': '/'
}
}
}
]
}
}
},
{
'name': 'test-vmss',
'id': '/subscriptions/123/resourceGroups/AQUA-RESOURCE-GROUP/providers/Microsoft.Compute/virtualMachineScaleSets/test-vmss',
'type': 'Microsoft.Compute/virtualMachineScaleSets',
'virtualMachineProfile': {
'extensionProfile': {
'extensions': []
}
}
}
];

const createCache = (virtualMachineScaleSets) => {
let machine = {};
if (virtualMachineScaleSets) {
machine['data'] = virtualMachineScaleSets;
}
return {
virtualMachineScaleSets: {
listAll: {
'eastus': machine
}
}
};
};

describe('scaleSetHealthMonitoring', function() {
describe('run', function() {
it('should give passing result if no virtual machine scale sets', function(done) {
const cache = createCache([]);
scaleSetHealthMonitoring.run(cache, {}, (err, results) => {
expect(results.length).to.equal(1);
expect(results[0].status).to.equal(0);
expect(results[0].message).to.include('No existing Virtual Machine Scale Sets found');
expect(results[0].region).to.equal('eastus');
done();
});
});

it('should give unknown result if unable to query for virtual machine scale sets', function(done) {
const cache = createCache();
scaleSetHealthMonitoring.run(cache, {}, (err, results) => {
expect(results.length).to.equal(1);
expect(results[0].status).to.equal(3);
expect(results[0].message).to.include('Unable to query for Virtual Machine Scale Sets');
expect(results[0].region).to.equal('eastus');
done();
});
});

it('should give passing result if Virtual Machine Scale Set has health monitoring enabled', function(done) {
const cache = createCache([virtualMachineScaleSets[0]]);
scaleSetHealthMonitoring.run(cache, {}, (err, results) => {
expect(results.length).to.equal(1);
expect(results[0].status).to.equal(0);
expect(results[0].message).to.include('Virtual Machine Scale Set has health monitoring enabled');
expect(results[0].region).to.equal('eastus');
done();
});
});

it('should give failing result if Virtual Machine Scale Set has health monitoring disabled', function(done) {
const cache = createCache([virtualMachineScaleSets[1]]);
scaleSetHealthMonitoring.run(cache, {}, (err, results) => {
expect(results.length).to.equal(1);
expect(results[0].status).to.equal(2);
expect(results[0].message).to.include('Virtual Machine Scale Set has health monitoring disabled');
expect(results[0].region).to.equal('eastus');
done();
});
});
});
});