UNPKG

@vroomlabs/gsdk-deploy

Version:

Google Cloud deployment script for kubernetes clusters using Global Load Balancer

121 lines (96 loc) 6.73 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/3/17. ******************************************************************************/Object.defineProperty(exports,'__esModule',{value:true});exports.GoogleNetworking=undefined;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}}(); var _logger=require('../util/logger'); var _callback=require('../util/callback'); var _googleAsyncTask=require('../google/googleAsyncTask');function _asyncToGenerator(fn){return function(){var gen=fn.apply(this,arguments);return new Promise(function(resolve,reject){function step(key,arg){try{var info=gen[key](arg);var value=info.value}catch(error){reject(error);return}if(info.done){resolve(value)}else{return Promise.resolve(value).then(function(value){step('next',value)},function(err){step('throw',err)})}}return step('next')})}}function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError('Cannot call a class as a function')}}var GoogleNetworking=exports.GoogleNetworking=function(){ /** * @param {GoogleApi} gauth */ function GoogleNetworking(gauth){_classCallCheck(this,GoogleNetworking); this.gauth=gauth; } /** * @param {string} name - name of the network to fetch or create * @param {boolean=} create = true to auto-create if missing */_createClass(GoogleNetworking,[{key:'getNetwork',value:function(){var _ref=_asyncToGenerator(/*#__PURE__*/regeneratorRuntime.mark(function _callee( name,create){var waiting,net,task;return regeneratorRuntime.wrap(function _callee$(_context){while(1){switch(_context.prev=_context.next){case 0: waiting=new _googleAsyncTask.GoogleAsyncTask(this.gauth);_context.next=3;return( this._fetchNetwork(name));case 3:net=_context.sent;if(!( !net&&create)){_context.next=18;break}_context.next=7;return( this._createNetwork(name));case 7:task=_context.sent;_context.next=10;return( waiting.completeTask(task));case 10:_context.next=12;return( this._fetchNetwork(name,true));case 12:net=_context.sent;_context.next=15;return( this._createFirewallRules(net));case 15:task=_context.sent;_context.next=18;return( waiting.completeTask(task));case 18:return _context.abrupt('return', {name:net.name,selfLink:net.selfLink});case 19:case'end':return _context.stop();}}},_callee,this)}));function getNetwork(_x,_x2){return _ref.apply(this,arguments)}return getNetwork}() /** * @private * @param {string} network * @param {boolean=} throwMissing */},{key:'_fetchNetwork',value:function(){var _ref2=_asyncToGenerator(/*#__PURE__*/regeneratorRuntime.mark(function _callee2( network,throwMissing){var _this=this;var fetch;return regeneratorRuntime.wrap(function _callee2$(_context2){while(1){switch(_context2.prev=_context2.next){case 0: fetch={project:this.gauth.project,network:network};return _context2.abrupt('return', new Promise(function(accept,reject){ _this.gauth.compute.networks.get(fetch,function(err,network){ if(err&&(throwMissing||err.code!==404)){ return reject(err); } return accept(network); }); }));case 2:case'end':return _context2.stop();}}},_callee2,this)}));function _fetchNetwork(_x3,_x4){return _ref2.apply(this,arguments)}return _fetchNetwork}() /** * @param {string} network */},{key:'_createNetwork',value:function(){var _ref3=_asyncToGenerator(/*#__PURE__*/regeneratorRuntime.mark(function _callee3( network){var createNet,self;return regeneratorRuntime.wrap(function _callee3$(_context3){while(1){switch(_context3.prev=_context3.next){case 0: createNet={ project:this.gauth.project, resource:{ name:network, description:'Kubernetes VPC network', autoCreateSubnetworks:true}}; self=this; _logger.logger.silly('network config',createNet); _logger.logger.warn('Creating network '+network+'...');return _context3.abrupt('return', (0,_callback.promisify)(self.gauth.compute.networks,'insert',createNet));case 5:case'end':return _context3.stop();}}},_callee3,this)}));function _createNetwork(_x5){return _ref3.apply(this,arguments)}return _createNetwork}() /** * @param {object} network */},{key:'_createFirewallRules',value:function(){var _ref4=_asyncToGenerator(/*#__PURE__*/regeneratorRuntime.mark(function _callee4( network){var createFw,self;return regeneratorRuntime.wrap(function _callee4$(_context4){while(1){switch(_context4.prev=_context4.next){case 0: createFw={ project:this.gauth.project, resource:{ name:network.name+'-ingress', description:'Kubernetes VPC firewall rules', network:network.selfLink, sourceRanges:['10.0.0.0/8','130.211.0.0/22','35.191.0.0/16'], sourceTags:[], targetTags:['kube'], allowed:[{ IPProtocol:'tcp', ports:['30000-32767']}]}}; self=this; _logger.logger.silly('firewall config',createFw); _logger.logger.warn('Creating firewall '+createFw.resource.name+'...');return _context4.abrupt('return', (0,_callback.promisify)(self.gauth.compute.firewalls,'insert',createFw));case 5:case'end':return _context4.stop();}}},_callee4,this)}));function _createFirewallRules(_x6){return _ref4.apply(this,arguments)}return _createFirewallRules}()}]);return GoogleNetworking}();