-
Notifications
You must be signed in to change notification settings - Fork 4
Tutorial Part 3
- Having followed part 2
Now that we have a working lambda to wich we can pass an input message we can now dive into the next part : Giving a response back to Gitfaas.
Each lambda comme with a uniq function UID. This UID can be used to POST a response from the lambda to Gitfaas.
Then the creator of the lambda gets access to the returned value.
apiVersion: batch/v1
kind: Job
metadata:
name: replace-job-{{RANDOM}}
spec:
ttlSecondsAfterFinished: 3600
backoffLimit: 1
template:
spec:
containers:
- name: reverse
image: <URL_OR_ACCOUNT_NAME_OF_REGISTRY>/demo-gitfaas-replace:0.0.1 # Update the image with your own
env:
- name: PAYLOAD
value: "{{PAYLOAD}}"
- name: FUNCTION_UID # <==
value: "{{FUNCTION_UID}}" # <== Here we define a new template variable that will be replaced during runtime
restartPolicy: Never
➡️ Commit this new deployment
To force Gitfaas to pull your commit immediately you can do the following request (still needs the port forward though):
$ curl http://127.0.0.1:5000/refresh
import os
import base64
import json
import requests # <== we add the new dependency
# Ie: Expected payload format in input message
# {
# "source": "I like bananas",
# "str-to-replace": "bananas",
# "replace-with": "apples"
# }
def main():
# Retrieving the base64 payload (the input message sent to this function)
b64_message = os.environ.get('PAYLOAD', None)
# Decoding base64 JSON payload into usable JSON
str_message = base64.b64decode(b64_message).decode("utf-8")
json_message = json.loads(str_message)
replaced_string = json_message["source"].replace(json_message["str-to-replace"], json_message["replace-with"])
print("Replaced string is : %s" % replaced_string)
# ==> New lines are underneath <===
# Extracting the function UID from the ENV
function_uid = os.environ.get('FUNCTION_UID', None)
# Creating the url
url = "http://gitfaas:5000/response/" + function_uid # We append our function_UID so that Gitfaas knows who is talking to him.
# We send a response to Gitfaas containing the replaced string. Setting content type to text/plain or application/json is a good practice.
ret = requests.post(url, data=replaced_string, headers={'Content-type': 'text/plain'})
print("Gitfaas responded with = %s" % ret.text)
main()
Now that we use the Request library to post our response to Gitfaas we need to add a requirements.txt
to our sources files with the python dependency.
requirements.txt
requests
We must also add it to the image by updating the dockerfile
FROM python:3.8-slim-buster
ENV PYTHONUNBUFFERED=1
WORKDIR /app
RUN groupadd -r demo && useradd -r -g demo demo && mkdir /home/demo && chown demo:demo /home/demo
USER demo
COPY requirements.txt requirements.txt # <== We copy the new dependency file.
RUN pip install -r requirements.txt # <== We install the dependency
CMD [ "python3", "-u", "main.py" ]
COPY main.py main.py
➡️ Rebuild the application
docker build . -t <URL_OR_ACCOUNT_NAME_OF_REGISTRY>/demo-gitfaas-replace:0.0.1
docker push <URL_OR_ACCOUNT_NAME_OF_REGISTRY>/demo-gitfaas-replace:0.0.1
$ PAYLOAD=$(cat << EOF
{
"source": "I like bananas",
"str-to-replace": "bananas",
"replace-with": "apples"
}
EOF
)
$ curl -X POST http://127.0.0.1:5000/publish/replace -d "$PAYLOAD" -H 'Content-Type: application/json'
{"applies":[{"error":false,"message":"Applied successfully","path":"apply2/mygitrepo/replace_job.yaml"}],
"error":false,"requestUid":"r-33291581-9310-4d00-ba1a-2a2c5b05e66b"}
The Job has been applied successfully. But the relevant section here is the requestUid
key and value.
We can use the request UID to ask Gitfaas if the corresponding lambda has posted any response.
- If it hasn't you'll get a null value, meaning that something has been launched but didn't responded yet.
- If the lambda did respond then you'll get the response encoded to base64 (next section).
$ kubectl logs -n gitfaas replace-job-XXXXXXX
Replaced string is : I like apples
Gitfaas responded with = {"error":false,"message":"Response stored correctly"}
Logs indicate that all when well. Let's look at the reponse stored inside Gitfaas.
$ curl "http://127.0.0.1:5000/response/r-33291581-9310-4d00-ba1a-2a2c5b05e66b" # <== Replace with the requestUid that you got from /publish
{"error":false,"responses":["SSBsaWtlIGFwcGxlcwo="]}
In the above snipet you must replace the request UID r-33291581-9310-4d00-ba1a-2a2c5b05e66b
by the one you got in the reponse from/publish
.
Gitfaas anwsers with an array containing the reponse from your lambdas in base64 !
Let's decode the string:
echo SSBsaWtlIGFwcGxlcwo= | base64 -d
I like apples
Well done ! You now know how a lambda can send back information !
You can now finish the tutorial with Part 4.