Skip to content

Commit

Permalink
Use the real Knative Service URL instead of assuming example.com
Browse files Browse the repository at this point in the history
This plumbs through the Knative Service URLs returned from
serverless-components/knative-serving#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 serverless#3.

It partially addresses serverless#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.
  • Loading branch information
bbrowning committed Nov 16, 2019
1 parent 1f019b9 commit e490f0f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
14 changes: 10 additions & 4 deletions info/lib/displayInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'

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

0 comments on commit e490f0f

Please sign in to comment.