From 85628b7fff6c3ff91cc7bd460b14becd355f5a57 Mon Sep 17 00:00:00 2001 From: Daniele Martinoli Date: Mon, 11 Jul 2022 15:29:52 +0200 Subject: [PATCH 1/7] Changes to comply with knative v1 instead of v1alpha1 --- deploy/lib/getKnativeEventConfig.js | 2 +- info/lib/displayInfo.js | 6 ++++-- invoke/lib/invokeFunction.js | 10 +++++++--- package.json | 2 +- 4 files changed, 13 insertions(+), 7 deletions(-) diff --git a/deploy/lib/getKnativeEventConfig.js b/deploy/lib/getKnativeEventConfig.js index 87e2e7b..c8e3418 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 knativeVersion = 'v1' // TODO: update this when we're dealing with services other // than the ones deployed by the Serverless Framework (e.g. K8S services) 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..d0635de 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,14 +20,17 @@ 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 + const externalUrl = ip && ip.length > 0 ? `http://${ip}` : functionUrl return fetch(externalUrl, { method: 'GET', - headers: { Host: host } + headers: { Host: host }, + agent: new Agent({ + rejectUnauthorized: false, + }) }).then((result) => result.text()) }) } diff --git a/package.json b/package.json index 72d9155..7d15b26 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ "@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-serving": "https://github.com/dmartinol/knative-serving.git", "@serverless/kubernetes-namespace": "^0.2.0", "bluebird": "^3.7.1", "chalk": "^2.4.2", From 51cbe53bfed3fdf1739009bb2413796903adb2dd Mon Sep 17 00:00:00 2001 From: Daniele Martinoli Date: Tue, 12 Jul 2022 15:16:06 +0200 Subject: [PATCH 2/7] Integrating changes to create default Broker and SinkBinding --- deploy/lib/getKnativeEventConfig.js | 26 ++++++++++++++++++++++++++ package.json | 2 +- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/deploy/lib/getKnativeEventConfig.js b/deploy/lib/getKnativeEventConfig.js index c8e3418..d43590f 100644 --- a/deploy/lib/getKnativeEventConfig.js +++ b/deploy/lib/getKnativeEventConfig.js @@ -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) { @@ -124,6 +132,7 @@ function getCustomConfig(sinkName, eventConfig) { knativeGroup: 'eventing.knative.dev', knativeVersion, spec: { + broker: "default", filter, subscriber: { ref: getRef(sinkName) @@ -132,6 +141,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 +170,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/package.json b/package.json index 7d15b26..bf0fbaf 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ "dependencies": { "@serverless/core": "^1.1.1", "@serverless/docker-image": "^0.3.0", - "@serverless/knative-eventing": "^0.2.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", From b8dba00125f26ffcbf932883f5d634ce3dcad7d5 Mon Sep 17 00:00:00 2001 From: Daniele Martinoli Date: Tue, 12 Jul 2022 15:36:35 +0200 Subject: [PATCH 3/7] Added sinkBinding to validEvents --- deploy/lib/getKnativeEventConfig.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deploy/lib/getKnativeEventConfig.js b/deploy/lib/getKnativeEventConfig.js index d43590f..88497ce 100644 --- a/deploy/lib/getKnativeEventConfig.js +++ b/deploy/lib/getKnativeEventConfig.js @@ -1,6 +1,6 @@ 'use strict' -const validEvents = ['custom', 'cron', 'gcpPubSub', 'awsSqs', 'kafka'] +const validEvents = ['custom', 'cron', 'gcpPubSub', 'awsSqs', 'kafka', 'sinkBinding'] const knativeVersion = 'v1' // TODO: update this when we're dealing with services other From 5c0895672287f946908a75a9a13028d5100efd22 Mon Sep 17 00:00:00 2001 From: Daniele Martinoli Date: Tue, 12 Jul 2022 16:49:41 +0200 Subject: [PATCH 4/7] Managing optional name attribute in event field to avoid duplicated names --- deploy/lib/ensureKnativeEvent.js | 4 ++-- deploy/lib/getKnativeEventConfig.js | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) 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/getKnativeEventConfig.js b/deploy/lib/getKnativeEventConfig.js index 88497ce..d0cdf73 100644 --- a/deploy/lib/getKnativeEventConfig.js +++ b/deploy/lib/getKnativeEventConfig.js @@ -131,6 +131,7 @@ function getCustomConfig(sinkName, eventConfig) { kind: 'Trigger', knativeGroup: 'eventing.knative.dev', knativeVersion, + configName: eventConfig.name, spec: { broker: "default", filter, From 15b718c2a6885ab5682b142cab9c0cbdfab81b8f Mon Sep 17 00:00:00 2001 From: Daniele Martinoli Date: Thu, 14 Jul 2022 11:25:40 +0200 Subject: [PATCH 5/7] Fix to invoke in k8s --- invoke/lib/invokeFunction.js | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/invoke/lib/invokeFunction.js b/invoke/lib/invokeFunction.js index d0635de..874f6ba 100644 --- a/invoke/lib/invokeFunction.js +++ b/invoke/lib/invokeFunction.js @@ -22,16 +22,21 @@ function invokeFunction() { return serving.info(inputs).then((res) => { const functionUrl = res.serviceUrls.get(getFuncName(service, this.options.function)) const { host } = url.parse(functionUrl, true) - const ip = res.istioIngressIp - const externalUrl = ip && ip.length > 0 ? `http://${ip}` : functionUrl - - return fetch(externalUrl, { - method: 'GET', - headers: { Host: host }, - agent: new Agent({ - rejectUnauthorized: false, - }) - }).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()) + } }) } From 17bd54be1a01b3dc48f2fa3f599326f5881751e1 Mon Sep 17 00:00:00 2001 From: Daniele Martinoli Date: Thu, 14 Jul 2022 16:26:09 +0200 Subject: [PATCH 6/7] Added propagation of autoscaler option of the function configuration --- deploy/lib/ensureKnativeService.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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) { From 9a2db26ad410b3d199baaf5bcfd2051472d7ce16 Mon Sep 17 00:00:00 2001 From: Tomer Figenblat Date: Mon, 22 Aug 2022 15:01:40 +0300 Subject: [PATCH 7/7] fix: pinned dockerode version to 3.3.2 Signed-off-by: Tomer Figenblat --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index bf0fbaf..cfb03df 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "@serverless/kubernetes-namespace": "^0.2.0", "bluebird": "^3.7.1", "chalk": "^2.4.2", + "dockerode": "3.3.2", "node-fetch": "^2.6.0" }, "devDependencies": {