Example Knative PowerShell function for sending CloudEvents emits by the Kubernetes Api Server to a Slack webhook. This function requires a deployed and working Knative ApiServerSource.
Create the container image locally to test your function logic. Change the IMAGE name accordingly, example below for Docker.
export IMAGE=<docker-username>/kn-ps-apiserver-slack:1.0
docker build -t ${IMAGE}
Verify the container image works by executing it locally.
Change into the test
directory
cd test
Update the following variable names within the docker-test-env-variable
file
- SLACK_WEBHOOK_URL - Slack webhook URL
- SLACK_MESSAGE_PRETEXT - Text displayed for Slack notification
Start the container image by running the following command:
docker run -e FUNCTION_DEBUG=true -e PORT=8080 --env-file docker-test-env-variable -it --rm -p 8080:8080 ${IMAGE}
In a separate terminal, run either send-cloudevent-test.ps1
(PowerShell Script) or send-cloudevent-test.sh
(Bash Script) to simulate a CloudEvent payload being sent to the local container image
Testing Function ...
See docker container console for output
# Output from docker container console
06/27/2022 09:47:31 - DEBUG: K8s Secrets:
{"SLACK_WEBHOOK_URL":"**********","SLACK_MESSAGE_PRETEXT":":kubernetes: ApiServer Slack Function powered by :knative:"}
06/27/2022 09:47:31 - DEBUG: "{
attachments = @(
@{
pretext = $(${jsonSecrets}.SLACK_MESSAGE_PRETEXT);
fields = @(
@{
title = "Kubernetes Api Server Message";
value = $cloudEventData.message;
short = "false";
}
@{
title = "Reason";
value = $cloudEventData.reason;
short = "false";
}
@{
title = "Kind:";
value = $cloudEventData.involvedObject.kind;
short = "false";
}
@{
title = "Name:";
value = $cloudEventData.involvedObject.name;
short = "false";
}
@{
title = "Namespace:";
value = $cloudEventData.metadata.namespace;
short = "false";
}
@{
title = "Event Type:";
value = $cloudEvent.type;
short = "false";
}
@{
title = "Unique ID:";
value = $cloudEvent.id;
short = "false";
}
@{
title = "DateTime in UTC:";
value = $cloudEvent.time;
short = "false";
}
@{
title = "Source:";
value = $cloudEvent.source;
short = "false";
}
)
footer = "Powered by Knative";
footer_icon = "https://github.com/knative/docs/blob/main/docs/images/logo/cmyk/knative-logo-cmyk.png";
}
)
}
"
06/27/2022 09:47:31 - Sending Webhook payload to Slack ...
06/27/2022 09:47:31 - Successfully sent Webhook ...
Note: The following steps assume a working Knative environment using the RabbitMQ
broker
or the MTChannelBasedBroker. The Knativeservice
andtrigger
will be installed in thevmware-functions
Kubernetes namespace, assuming that thebroker
is also available there.
Update the apiserver_slack_secret.json
file with your Slack webhook configurations and then create the Kubernetes secret which can then be accessed from within the function by using the environment variable named called SLACK_SECRET
.
# create secret
kubectl -n vmware-functions create secret generic apiserver-slack-secret --from-file=SLACK_SECRET=apiserver_slack_secret.json
Edit the function.yaml
file with the name of the container image from Step 1 if you made any changes. If not, the default VMware container image will suffice. By default, the function deployment will filter on the dev.knative.apiserver.resource.update
event. If you wish to change this, update the type
field within function.yaml
to the desired event type.
Furthermore, use the Sockeye Event viewer to display all incoming events.
Deploy the function to the Knative environment.
# deploy function
kubectl -n vmware-functions apply -f function.yaml
For testing purposes, the function.yaml
contains the following annotations, which will ensure the Knative Service Pod will always run exactly one instance for debugging purposes.
annotations:
autoscaling.knative.dev/maxScale: "1"
autoscaling.knative.dev/minScale: "1"
# undeploy function
kubectl -n vmware-functions delete -f function.yaml
# delete secret
kubectl -n vmware-functions delete secret apiserver-slack-secret