|
| 1 | +/* |
| 2 | + Copyright 2017 Bitnami. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +'use strict'; |
| 18 | + |
| 19 | +const _ = require('lodash'); |
| 20 | +const BbPromise = require('bluebird'); |
| 21 | +const Api = require('kubernetes-client'); |
| 22 | +const helpers = require('./helpers'); |
| 23 | +const moment = require('moment'); |
| 24 | +const url = require('url'); |
| 25 | + |
| 26 | +function getIngressRuleLabels(functions) { |
| 27 | + const labels = {}; |
| 28 | + _.each(functions, (desc, f) => { |
| 29 | + labels[desc.id || f] = '1'; |
| 30 | + }); |
| 31 | + return labels; |
| 32 | +} |
| 33 | + |
| 34 | +function addIngressRuleIfNecessary(functions, options) { |
| 35 | + const opts = _.defaults({}, options, { |
| 36 | + verbose: false, |
| 37 | + log: console.log, |
| 38 | + namespace: 'default', |
| 39 | + hostname: null, |
| 40 | + }); |
| 41 | + const config = helpers.loadKubeConfig(); |
| 42 | + const extensions = new Api.Extensions(helpers.getConnectionOptions( |
| 43 | + config, { namespace: options.namespace }) |
| 44 | + ); |
| 45 | + const defaultHostname = `${url.parse(helpers.getKubernetesAPIURL(config)).hostname}.nip.io`; |
| 46 | + const rules = []; |
| 47 | + _.each(functions, (description) => { |
| 48 | + _.each(description.events, event => { |
| 49 | + if (event.type === 'http') { |
| 50 | + const fpath = event.path || '/'; |
| 51 | + if (event.path !== '/' || !_.isEmpty(event.hostname)) { |
| 52 | + const hostname = event.hostname || opts.hostname || defaultHostname; |
| 53 | + const absolutePath = _.startsWith(fpath, '/') ? |
| 54 | + fpath : |
| 55 | + `/${fpath}`; |
| 56 | + const previousRule = _.findIndex(rules, r => r.host === hostname); |
| 57 | + if (previousRule >= 0) { |
| 58 | + rules[previousRule].http.paths.push({ |
| 59 | + path: absolutePath, |
| 60 | + backend: { serviceName: description.id, servicePort: 8080 }, |
| 61 | + }); |
| 62 | + } else { |
| 63 | + rules.push({ |
| 64 | + host: hostname, |
| 65 | + http: { |
| 66 | + paths: [{ |
| 67 | + path: absolutePath, |
| 68 | + backend: { serviceName: description.id, servicePort: 8080 }, |
| 69 | + }], |
| 70 | + }, |
| 71 | + }); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + }); |
| 76 | + }); |
| 77 | + return new BbPromise((resolve, reject) => { |
| 78 | + if (!_.isEmpty(rules)) { |
| 79 | + // Found a path to deploy the function |
| 80 | + const labels = getIngressRuleLabels(functions); |
| 81 | + const ingressDef = { |
| 82 | + kind: 'Ingress', |
| 83 | + metadata: { |
| 84 | + name: `ingress-${moment.now()}`, |
| 85 | + labels, |
| 86 | + annotations: { |
| 87 | + 'kubernetes.io/ingress.class': 'nginx', |
| 88 | + 'ingress.kubernetes.io/rewrite-target': '/', |
| 89 | + }, |
| 90 | + }, |
| 91 | + spec: { rules }, |
| 92 | + }; |
| 93 | + extensions.ns.ingress.post({ body: ingressDef }, (err) => { |
| 94 | + if (err) { |
| 95 | + reject( |
| 96 | + 'Unable to deploy the ingress rule. ' + |
| 97 | + `Received: ${err.message}` |
| 98 | + ); |
| 99 | + } else { |
| 100 | + if (opts.verbose) { |
| 101 | + opts.log('Deployed Ingress rule'); |
| 102 | + } |
| 103 | + resolve(); |
| 104 | + } |
| 105 | + }); |
| 106 | + } else { |
| 107 | + if (opts.verbose) { |
| 108 | + opts.log('Skipping ingress rule generation'); |
| 109 | + } |
| 110 | + resolve(); |
| 111 | + } |
| 112 | + }); |
| 113 | +} |
| 114 | + |
| 115 | +function removeIngressRuleIfNecessary(functions, namespace, options) { |
| 116 | + const opts = _.defaults({}, options, { |
| 117 | + verbose: false, |
| 118 | + log: console.log, |
| 119 | + }); |
| 120 | + const extensions = new Api.Extensions(helpers.getConnectionOptions(helpers.loadKubeConfig(), { |
| 121 | + namespace, |
| 122 | + })); |
| 123 | + return new BbPromise((resolve, reject) => { |
| 124 | + extensions.ns.ingress.get((err, ingressInfo) => { |
| 125 | + const expectedLabels = getIngressRuleLabels(functions); |
| 126 | + const ingressRule = _.find(ingressInfo.items, item => ( |
| 127 | + item.metadata.labels && _.isEqual(item.metadata.labels, expectedLabels) |
| 128 | + )); |
| 129 | + if (!_.isEmpty(ingressRule)) { |
| 130 | + extensions.ns.ingress.delete(ingressRule, (ingErr) => { |
| 131 | + if (ingErr) { |
| 132 | + reject( |
| 133 | + `Unable to remove the ingress rule ${ingressRule}. Received:\n` + |
| 134 | + ` Code: ${ingErr.code}\n` + |
| 135 | + ` Message: ${ingErr.message}` |
| 136 | + ); |
| 137 | + } else { |
| 138 | + if (opts.verbose) { |
| 139 | + opts.log(`Removed Ingress rule ${ingressRule.metadata.name}`); |
| 140 | + } |
| 141 | + resolve(); |
| 142 | + } |
| 143 | + }); |
| 144 | + } else { |
| 145 | + if (opts.verbose) { |
| 146 | + opts.log('Skipping ingress rule clean up'); |
| 147 | + } |
| 148 | + resolve(); |
| 149 | + } |
| 150 | + }); |
| 151 | + }); |
| 152 | +} |
| 153 | + |
| 154 | +module.exports = { |
| 155 | + getIngressRuleLabels, |
| 156 | + addIngressRuleIfNecessary, |
| 157 | + removeIngressRuleIfNecessary, |
| 158 | +}; |
0 commit comments