From e490f0f0e15f1a09a2348c540fe08fd38b344c8c Mon Sep 17 00:00:00 2001 From: Ben Browning Date: Sat, 16 Nov 2019 09:24:24 -0500 Subject: [PATCH] Use the real Knative Service URL instead of assuming example.com This plumbs through the Knative Service URLs returned from https://github.com/serverless-components/knative-serving/pull/2 into the CLI. This change depends on that serverless-components/knative-serving one to get merged first and the version bumped in package.json to provide the new Knative Service URLs. This fixes #3. It partially addresses #2 as well, but the actual function invocation logic still tries the Istio IP if one is found. If not, it falls back to directly using the Knative Service URL given. --- info/lib/displayInfo.js | 14 ++++++++++---- invoke/lib/invokeFunction.js | 18 ++++++++++++++---- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/info/lib/displayInfo.js b/info/lib/displayInfo.js index 441b89f..737c15f 100644 --- a/info/lib/displayInfo.js +++ b/info/lib/displayInfo.js @@ -3,7 +3,7 @@ const chalk = require('chalk') const { Context } = require('@serverless/core') const KnativeServing = require('@serverless/knative-serving') -const { getNamespace, getFuncUrl } = require('../../shared/utils') +const { getNamespace, getFuncName } = require('../../shared/utils') function displayInfo() { const { service } = this.serverless.service @@ -14,13 +14,19 @@ function displayInfo() { const ctx = new Context() const serving = new KnativeServing(undefined, ctx) - return serving.info().then((res) => { + const inputs = { + namespace + } + + return serving.info(inputs).then((res) => { let message = '' message += `${chalk.yellow.underline('Service Information')}\n` message += `${chalk.yellow('service:')} ${service}\n` message += `${chalk.yellow('namespace:')} ${namespace}\n` - message += `${chalk.yellow('ingress ip:')} ${res.istioIngressIp}\n` + if (res.istioIngressIp.length > 0) { + message += `${chalk.yellow('ingress ip:')} ${res.istioIngressIp}\n` + } message += '\n' @@ -30,7 +36,7 @@ function displayInfo() { } functionNames.forEach((funcName) => { message += `${chalk.yellow(funcName)}:\n` - message += ` - ${chalk.yellow('url:')} ${getFuncUrl(service, funcName, stage)}\n` + message += ` - ${chalk.yellow('url:')} ${res.serviceUrls[getFuncName(service, funcName)]}\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 aaf5ad6..df33cb0 100644 --- a/invoke/lib/invokeFunction.js +++ b/invoke/lib/invokeFunction.js @@ -1,23 +1,33 @@ 'use strict' +const url = require('url') const fetch = require('node-fetch') const { Context } = require('@serverless/core') const KnativeServing = require('@serverless/knative-serving/') -const { getFuncUrl } = require('../../shared/utils') +const { getNamespace, getFuncName } = require('../../shared/utils') function invokeFunction() { const { service } = this.serverless.service const stage = this.provider.getStage() + const namespace = getNamespace(service, stage) + const ctx = new Context() const serving = new KnativeServing(undefined, ctx) - return serving.info().then((res) => { + const inputs = { + namespace + } + + return serving.info(inputs).then((res) => { + const functionUrl = res.serviceUrls[getFuncName(service, this.options.function)] + const host = url.parse(functionUrl, true).host const ip = res.istioIngressIp + const externalUrl = ip.length > 0 ? `http://${ip}` : functionUrl - return fetch(`http://${ip}`, { + return fetch(externalUrl, { method: 'GET', - headers: { Host: `${getFuncUrl(service, this.options.function, stage)}` } + headers: { Host: host } }).then((result) => result.text()) }) }