Skip to content

Commit ad2ad3f

Browse files
committed
feat: Add dig function
**What** - Add a `dig` function that can be used to verify DNS resultion inside a cluster. The function is implemented as a small bash script that invokes `dig` and then returns the result as JSON or text, depending on an ENV configuration. This function can also be used to demonstrate how to lightly wrap the CLI tool and provide some logging to stderr To test ```sh $ faas-cli build --filter dig $ docker run --name digtest --rm -p 8080:8080 ghcr.io/openfaas/dig:latest $ curl localhost:8080 -d "google.com" {"ip_address": "172.217.23.110"} $ docker kill digtest ``` Signed-off-by: Lucas Roesler <[email protected]>
1 parent 75d6169 commit ad2ad3f

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

dig/Dockerfile

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
FROM --platform=${TARGETPLATFORM:-linux/amd64} ghcr.io/openfaas/of-watchdog:0.8.4 as watchdog
2+
3+
FROM --platform=${TARGETPLATFORM:-linux/amd64} alpine:3.13
4+
5+
RUN mkdir -p /home/app
6+
7+
RUN apk add --no-cache bind-tools
8+
9+
COPY --from=watchdog /fwatchdog /usr/bin/fwatchdog
10+
RUN chmod +x /usr/bin/fwatchdog
11+
12+
# Add non root user
13+
RUN addgroup -S app && adduser app -S -G app
14+
RUN chown app /home/app
15+
16+
WORKDIR /home/app
17+
18+
USER app
19+
20+
COPY --chown=app:app dig.sh .
21+
RUN chmod +x dig.sh
22+
23+
# customize the json key name for the success responses
24+
ENV response_key="ip_address"
25+
26+
# configure of-watchdog
27+
# https://github.com/openfaas/of-watchdog#configuration
28+
ENV fprocess="/home/app/dig.sh"
29+
ENV mode="streaming"
30+
ENV content_type="application/json"
31+
ENV suppress_lock="false"
32+
ENV prefix_logs="false"
33+
# Set to true to see request in function logs
34+
ENV write_debug="false"
35+
36+
EXPOSE 8080
37+
38+
HEALTHCHECK --interval=3s CMD [ -e /tmp/.lock ] || exit 1
39+
40+
CMD ["fwatchdog"]

dig/dig.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env sh
2+
3+
4+
# write log message to stderr
5+
log() {
6+
echo "$@" 1>&2;
7+
}
8+
9+
IFS='' read -d '' -r request
10+
response=$(dig "$request" +short 2>&1)
11+
status=$?
12+
13+
log "request=\"$request\" response=\"$response\" content_type=\"$content_type\""
14+
15+
if [ "$content_type" = "application/json" ]; then
16+
key="error"
17+
if [ $status -eq 0 ]; then
18+
key="${response_key:-response}"
19+
fi
20+
21+
echo "{\"$key\": \"$response\"}"
22+
else
23+
echo "$response"
24+
fi

stack.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ functions:
1212
handler: ./curl
1313
image: ghcr.io/${OWNER:-openfaas}/curl:${TAG:-latest}
1414

15+
dig:
16+
lang: dockerfile
17+
handler: ./dig
18+
image: ghcr.io/${OWNER:-openfaas}/dig:${TAG:-latest}
19+
1520
shasum:
1621
lang: dockerfile
1722
handler: ./shasum

0 commit comments

Comments
 (0)