UNPKG

@vroomlabs/gsdk-deploy

Version:

Google Cloud deployment script for kubernetes clusters using Global Load Balancer

103 lines (95 loc) 4.44 kB
'use strict'; /****************************************************************************** * MIT License * Copyright (c) 2017 https://github.com/vroomlabs * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. * * Created by rogerk on 7/4/17. ******************************************************************************/Object.defineProperty(exports,'__esModule',{value:true});var _createClass=function(){function defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||false;descriptor.configurable=true;if('value'in descriptor)descriptor.writable=true;Object.defineProperty(target,descriptor.key,descriptor)}}return function(Constructor,protoProps,staticProps){if(protoProps)defineProperties(Constructor.prototype,protoProps);if(staticProps)defineProperties(Constructor,staticProps);return Constructor}}();function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError('Cannot call a class as a function')}}var BackendConfig=exports.BackendConfig=function(){ /** * @param {Configuration} config * @param {object[]} clusters * @param {string} [healthCheck] */ function BackendConfig(config,clusters,healthCheck){_classCallCheck(this,BackendConfig); this.serviceName=config.name; this.hostname=config.current.host; this.name=this.hostname.replace(/\./g,'-').replace(/[^a-z0-9-]+/gi,'-'); this.nodePort=config.getNodePort(); this.timeoutSec=config.current.timeoutSec; this.livenessProbe=config.current.livenessProbe; this.enableCDN=config.current.enableCDN; this.healthCheck=healthCheck||null; this.clusters=clusters; }_createClass(BackendConfig,[{key:'getInstanceGroups',value:function getInstanceGroups() {var _this=this; var all=[]; Object.keys(this.clusters). forEach(function(name){ _this.clusters[name].instanceGroups.forEach(function(g){return all.push(g)}); }); return all; } /** * Returns the backend configuration */},{key:'getBackendConfig',value:function getBackendConfig() { if(!this.healthCheck){ throw new Error('healthCheck was not provided.'); } var backends=this.getInstanceGroups(). map(function(group){return{group:group,balancingMode:'UTILIZATION',maxUtilization:0.8}}); return{ name:this.name, description:'Generated backend for '+this.serviceName, port:this.nodePort, portName:this.serviceName+'-port', protocol:'HTTPS', enableCDN:this.enableCDN, backends:backends, healthChecks:[this.healthCheck], timeoutSec:this.timeoutSec||150}; } /** * Returns null, or an updated configuration if one is needed * @param {object} existing backend service definition */},{key:'getBackendUpdate',value:function getBackendUpdate( existing){ var desired=this.getBackendConfig(); var updateFields=[]; // TEST field delta Object.keys(desired).forEach(function(fld){ if(!Array.isArray(desired[fld])&&desired[fld]!==existing[fld]){ updateFields.push(fld); } }); if(JSON.stringify(desired.healthChecks)!==JSON.stringify(existing.healthChecks)){ updateFields.push('healthChecks'); } var v1=JSON.stringify((existing.backends||[]).map(function(b){return b.group}).sort()); var v2=JSON.stringify((desired.backends||[]).map(function(b){return b.group}).sort()); if(v1!==v2){ updateFields.push('backends'); } // UPDATE if any modified if(updateFields.length===0){ return null; } return Object.assign({},existing,desired); }}]);return BackendConfig}();