diff --git a/deploy/lib/ensureKnativeEvent.js b/deploy/lib/ensureKnativeEvent.js index 79e34b9..44face8 100644 --- a/deploy/lib/ensureKnativeEvent.js +++ b/deploy/lib/ensureKnativeEvent.js @@ -5,7 +5,7 @@ const KnativeEventing = require('@serverless/knative-eventing') const { getNamespace, getFuncName, getEventName } = require('../../shared/utils') function ensureKnativeEvent(funcName, eventName, config) { - const { knativeGroup, knativeVersion, kind, spec } = config + const { knativeGroup, knativeVersion, kind, configName, spec } = config const { service } = this.serverless.service const ctx = new Context() @@ -14,7 +14,7 @@ function ensureKnativeEvent(funcName, eventName, config) { const sinkName = getFuncName(service, funcName) const namespace = getNamespace(this.serverless) // TODO: this should be unique since we can have multiple such event definitions - const name = getEventName(sinkName, eventName) + const name = configName ? configName : getEventName(sinkName, eventName) const inputs = { name, diff --git a/deploy/lib/ensureKnativeService.js b/deploy/lib/ensureKnativeService.js index 48798ae..8713e0c 100644 --- a/deploy/lib/ensureKnativeService.js +++ b/deploy/lib/ensureKnativeService.js @@ -35,11 +35,13 @@ function ensureKnativeService(funcName) { tag = image.substring(firstColon + 1) } + const autoscaler = funcObject.autoscaler const inputs = { name, repository, tag, - namespace + namespace, + autoscaler } if (registryAddress) { diff --git a/deploy/lib/getKnativeEventConfig.js b/deploy/lib/getKnativeEventConfig.js index 87e2e7b..d0cdf73 100644 --- a/deploy/lib/getKnativeEventConfig.js +++ b/deploy/lib/getKnativeEventConfig.js @@ -1,7 +1,7 @@ 'use strict' -const validEvents = ['custom', 'cron', 'gcpPubSub', 'awsSqs', 'kafka'] -const knativeVersion = 'v1alpha1' +const validEvents = ['custom', 'cron', 'gcpPubSub', 'awsSqs', 'kafka', 'sinkBinding'] +const knativeVersion = 'v1' // TODO: update this when we're dealing with services other // than the ones deployed by the Serverless Framework (e.g. K8S services) @@ -13,6 +13,14 @@ function getRef(sinkName) { } } +function getBrokerRef(brokerName) { + return { + apiVersion: `eventing.knative.dev/${knativeVersion}`, + kind: 'Broker', + name: brokerName + } +} + function getCronConfig(sinkName, eventConfig) { const { schedule, data } = eventConfig if (!schedule) { @@ -123,7 +131,9 @@ function getCustomConfig(sinkName, eventConfig) { kind: 'Trigger', knativeGroup: 'eventing.knative.dev', knativeVersion, + configName: eventConfig.name, spec: { + broker: "default", filter, subscriber: { ref: getRef(sinkName) @@ -132,6 +142,21 @@ function getCustomConfig(sinkName, eventConfig) { } } +function getSinkBindingConfig(sinkName, eventConfig) { + const { filter } = eventConfig + return { + kind: 'SinkBinding', + knativeGroup: 'sources.knative.dev', + knativeVersion, + spec: { + sink: { + ref: getBrokerRef("default") + }, + subject: getRef(sinkName) + } + } +} + function getKnativeEventConfig(sinkName, eventName, eventConfig) { if (!validEvents.includes(eventName)) { this.serverless.cli.log(`Unknown event "${eventName}"`) @@ -146,6 +171,8 @@ function getKnativeEventConfig(sinkName, eventName, eventConfig) { return getAwsSqsConfig(sinkName, eventConfig) } else if (eventName === 'kafka') { return getKafkaConfig(sinkName, eventConfig) + } else if (eventName === 'sinkBinding') { + return getSinkBindingConfig(sinkName, eventConfig) } return getCustomConfig(sinkName, eventConfig) diff --git a/info/lib/displayInfo.js b/info/lib/displayInfo.js index db911a2..5096e85 100644 --- a/info/lib/displayInfo.js +++ b/info/lib/displayInfo.js @@ -23,7 +23,7 @@ function displayInfo() { message += `${chalk.yellow.underline('Service Information')}\n` message += `${chalk.yellow('service:')} ${service}\n` message += `${chalk.yellow('namespace:')} ${namespace}\n` - if (res.istioIngressIp.length > 0) { + if (res.istioIngressIp && res.istioIngressIp.length > 0) { message += `${chalk.yellow('ingress ip:')} ${res.istioIngressIp}\n` } @@ -35,7 +35,9 @@ function displayInfo() { } functionNames.forEach((funcName) => { message += `${chalk.yellow(funcName)}:\n` - message += ` - ${chalk.yellow('url:')} ${res.serviceUrls[getFuncName(service, funcName)]}\n` + + const ksvcName = getFuncName(service, funcName) + message += ` - ${chalk.yellow('url:')} ${res.serviceUrls.get(ksvcName)}\n` const events = this.serverless.service.getAllEventsInFunction(funcName) if (events.length) { events.forEach((event) => { diff --git a/invoke/lib/invokeFunction.js b/invoke/lib/invokeFunction.js index 003f19a..874f6ba 100644 --- a/invoke/lib/invokeFunction.js +++ b/invoke/lib/invokeFunction.js @@ -2,6 +2,7 @@ const url = require('url') const fetch = require('node-fetch') +const { Agent } = require('https'); const { Context } = require('@serverless/core') const KnativeServing = require('@serverless/knative-serving/') const { getNamespace, getFuncName } = require('../../shared/utils') @@ -19,15 +20,23 @@ function invokeFunction() { } return serving.info(inputs).then((res) => { - const functionUrl = res.serviceUrls[getFuncName(service, this.options.function)] + const functionUrl = res.serviceUrls.get(getFuncName(service, this.options.function)) const { host } = url.parse(functionUrl, true) - const ip = res.istioIngressIp - const externalUrl = ip.length > 0 ? `http://${ip}` : functionUrl - - return fetch(externalUrl, { - method: 'GET', - headers: { Host: host } - }).then((result) => result.text()) + const istioIngressIp = res.istioIngressIp + if (istioIngressIp && istioIngressIp.length > 0) { + return fetch(`http://${istioIngressIp}`, { + method: 'GET', + headers: { Host: host } + }).then((result) => result.text()) + } else { + return fetch(functionUrl, { + method: 'GET', + headers: { Host: host }, + agent: new Agent({ + rejectUnauthorized: false, + }) + }).then((result) => result.text()) + } }) } diff --git a/package.json b/package.json index 72d9155..cfb03df 100644 --- a/package.json +++ b/package.json @@ -14,11 +14,12 @@ "dependencies": { "@serverless/core": "^1.1.1", "@serverless/docker-image": "^0.3.0", - "@serverless/knative-eventing": "^0.2.0", - "@serverless/knative-serving": "^0.1.0", + "@serverless/knative-eventing": "https://github.com/dmartinol/knative-eventing.git", + "@serverless/knative-serving": "https://github.com/dmartinol/knative-serving.git", "@serverless/kubernetes-namespace": "^0.2.0", "bluebird": "^3.7.1", "chalk": "^2.4.2", + "dockerode": "3.3.2", "node-fetch": "^2.6.0" }, "devDependencies": {