0

I'm following the Lab4 from OpenFaas. In the end of this lab you make a programmatic call for another function, but the function start a loop and never finishes:

enter image description here

And int the execution is give me an error:

enter image description here

Here is the pod that should be called:

enter image description here

Someone can help me?

Here is my code

import os
import requests
import sys

def handle(req):
"""handle a request to the function
Args:
    req (str): request body
"""

#gateway_hostname = os.getenv("gateway_hostname", "127.0.0.1") # uses a default of "gateway" for when "gateway_hostname" is not set

test_sentence = req

print(test_sentence)

r = requests.get("http://127.0.0.1:8080/function/sentimentanalysis", data= test_sentence)

if r.status_code != 200:
    sys.exit("Error with sentimentanalysis, expected: %d, got: %d\n" % (200, r.status_code))

result = r.json()
if result["polarity"] > 0.45:
    return "That was probably positive"
else:
    return "That was neutral or negative"

1 Answers1

0

tl;dr

r = requests.get("http://gateway.openfaas:8080/function/sentimentanalysis", data= test_sentence)

Floriano,

I know the issue you're facing cuz I just went through it. But you need to fix your question and mention Kubernetes there.

I mean, the code you put there is working perfectly fine, in Docker.

But, I'am going to assume that you're facing a problem with this, because you're doing the lab in Kubernetes.

And if I'm right, the answer for the question is as simple as:

Use KUBERNETES DNS!

That said

Docker World:

r = requests.get("http://" + os.getenv("gateway_hostname", "127.0.0.1") + ":8080/function/sentimentanalysis", data= test_sentence)

Kubernetes World

r = requests.get("http://pod.namespace:8080/function/sentimentanalysis", data= test_sentence)

My case, I installed the openfaas's almost default helm chart, so, the namespace was openfaas and the gateway service was just gateway.

r = requests.get("http://gateway.openfaas:8080/function/sentimentanalysis", data= test_sentence)
KodornaRocks
  • 425
  • 3
  • 14