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

Support for Kubernetes + Knative / Openshift + Openshift Serverless #13

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
4 changes: 2 additions & 2 deletions deploy/lib/ensureKnativeEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion deploy/lib/ensureKnativeService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
31 changes: 29 additions & 2 deletions deploy/lib/getKnativeEventConfig.js
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
Expand All @@ -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}"`)
Expand All @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions info/lib/displayInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`
}

Expand All @@ -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) => {
Expand Down
25 changes: 17 additions & 8 deletions invoke/lib/invokeFunction.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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())
}
})
}

Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down