Skip to content

Commit

Permalink
feat: Add dig function
Browse files Browse the repository at this point in the history
**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]>
  • Loading branch information
LucasRoesler committed Apr 8, 2021
1 parent 75d6169 commit ad2ad3f
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
40 changes: 40 additions & 0 deletions dig/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
FROM --platform=${TARGETPLATFORM:-linux/amd64} ghcr.io/openfaas/of-watchdog:0.8.4 as watchdog

FROM --platform=${TARGETPLATFORM:-linux/amd64} alpine:3.13

RUN mkdir -p /home/app

RUN apk add --no-cache bind-tools

COPY --from=watchdog /fwatchdog /usr/bin/fwatchdog
RUN chmod +x /usr/bin/fwatchdog

# Add non root user
RUN addgroup -S app && adduser app -S -G app
RUN chown app /home/app

WORKDIR /home/app

USER app

COPY --chown=app:app dig.sh .
RUN chmod +x dig.sh

# customize the json key name for the success responses
ENV response_key="ip_address"

# configure of-watchdog
# https://github.com/openfaas/of-watchdog#configuration
ENV fprocess="/home/app/dig.sh"
ENV mode="streaming"
ENV content_type="application/json"
ENV suppress_lock="false"
ENV prefix_logs="false"
# Set to true to see request in function logs
ENV write_debug="false"

EXPOSE 8080

HEALTHCHECK --interval=3s CMD [ -e /tmp/.lock ] || exit 1

CMD ["fwatchdog"]
24 changes: 24 additions & 0 deletions dig/dig.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env sh


# write log message to stderr
log() {
echo "$@" 1>&2;
}

IFS='' read -d '' -r request
response=$(dig "$request" +short 2>&1)
status=$?

log "request=\"$request\" response=\"$response\" content_type=\"$content_type\""

if [ "$content_type" = "application/json" ]; then
key="error"
if [ $status -eq 0 ]; then
key="${response_key:-response}"
fi

echo "{\"$key\": \"$response\"}"
else
echo "$response"
fi
5 changes: 5 additions & 0 deletions stack.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ functions:
handler: ./curl
image: ghcr.io/${OWNER:-openfaas}/curl:${TAG:-latest}

dig:
lang: dockerfile
handler: ./dig
image: ghcr.io/${OWNER:-openfaas}/dig:${TAG:-latest}

shasum:
lang: dockerfile
handler: ./shasum
Expand Down

0 comments on commit ad2ad3f

Please sign in to comment.