-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: created kn-ps-harbor-slack function example
Closes: #916 Signed-off-by: Robert Guske <rguske@vmware.com>
- Loading branch information
Robert Guske
committed
Jun 27, 2022
1 parent
98df13d
commit 32f5996
Showing
10 changed files
with
396 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
FROM us.gcr.io/daisy-284300/veba/ce-ps-base:1.4 | ||
|
||
COPY handler.ps1 handler.ps1 | ||
|
||
CMD ["pwsh","./server.ps1"] |
157 changes: 157 additions & 0 deletions
157
examples/knative/powershell/kn-ps-harbor-slack/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
# kn-ps-harbor-slack | ||
|
||
Example Knative PowerShell function for sending Harbor CloudEvents to a Slack webhook. This function relies on the Harbor webhook [function example](https://github.com/vmware-samples/vcenter-event-broker-appliance/tree/development/examples/knative/go/kn-go-harbor-webhook) which is a requirement for this example. | ||
|
||
# Step 1 - Build | ||
|
||
Create the container image locally to test your function logic. Change the IMAGE name accordingly, example below for Docker. | ||
|
||
```console | ||
export IMAGE=<docker-username>/kn-ps-#REPLACE-FN-NAME#:1.0 | ||
docker build -t ${IMAGE} | ||
``` | ||
|
||
# Step 2 - Test | ||
|
||
Verify the container image works by executing it locally. | ||
|
||
Change into the `test` directory | ||
|
||
```console | ||
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: | ||
|
||
```console | ||
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 | ||
|
||
```console | ||
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":":harbor: Harbor Slack Function :veba_official:"} | ||
|
||
06/27/2022 09:47:31 - DEBUG: CloudEventData | ||
|
||
Name Value | ||
---- ----- | ||
event_data {resources, repository} | ||
occur_at 1656076946 | ||
type PUSH_ARTIFACT | ||
operator admin | ||
|
||
|
||
|
||
06/27/2022 09:47:31 - DEBUG: "{ | ||
"attachments": [ | ||
{ | ||
"footer_icon": "https://raw.githubusercontent.com/vmware-samples/vcenter-event-broker-appliance/development/logo/veba_icon_only.png", | ||
"footer": "Powered by VEBA", | ||
"pretext": ":harbor: Harbor Slack Function :veba_official:", | ||
"fields": [ | ||
{ | ||
"short": "false", | ||
"value": "PUSH_ARTIFACT", | ||
"title": "Event Type" | ||
}, | ||
{ | ||
"short": "false", | ||
"value": "2022-06-25T11:42:42+00:00", | ||
"title": "DateTime in UTC" | ||
}, | ||
{ | ||
"short": "false", | ||
"value": "admin", | ||
"title": "Username" | ||
}, | ||
{ | ||
"short": "false", | ||
"value": "veba-webhook/bitnami-nginx", | ||
"title": "Repository Name" | ||
}, | ||
{ | ||
"short": "false", | ||
"value": "public", | ||
"title": "Repository Type" | ||
}, | ||
{ | ||
"short": "false", | ||
"value": "1.21.6-debian-10-r117", | ||
"title": "Image Tag" | ||
}, | ||
{ | ||
"short": "false", | ||
"value": "harbor.jarvis.tanzu/veba-webhook/bitnami-nginx:1.21.6-debian-10-r117", | ||
"title": "Image Resource Data" | ||
}, | ||
{ | ||
"short": "false", | ||
"value": "sha256:d3890814cc5a7cfc02403435281cdf51adfb6b67e223934d9d6137a4ad364286", | ||
"title": "Image Digest" | ||
} | ||
] | ||
} | ||
] | ||
}" | ||
06/27/2022 09:47:31 - Sending Webhook payload to Slack ... | ||
06/27/2022 09:47:31 - Successfully sent Webhook ... | ||
``` | ||
|
||
# Step 3 - Deploy | ||
|
||
> **Note:** The following steps assume a working Knative environment using the | ||
`default` Rabbit `broker`. The Knative `service` and `trigger` will be installed in the | ||
`vmware-functions` Kubernetes namespace, assuming that the `broker` is also available there. | ||
> | ||
> **Note:** Also, in order to receive incoming Harbor events and to ultimately invoke the Harbor-Slack-Function, it's necessary to have the [kn-go-harbor-webhook function example](https://github.com/vmware-samples/vcenter-event-broker-appliance/tree/development/examples/knative/go/kn-go-harbor-webhook) running properly first. | ||
Update the `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`. | ||
|
||
```console | ||
# create secret | ||
|
||
kubectl -n vmware-functions create secret generic harbor-slack-secret --from-file=SLACK_SECRET=slack_secret.json | ||
|
||
# update label for secret to show up in VEBA UI | ||
kubectl -n vmware-functions label secret harbor-slack-secret app=veba-ui | ||
``` | ||
|
||
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 `com.vmware.harbor.push_artifact.v0` Harbor Event. If you wish to change this, update the `type` field within `function.yaml` to the desired event type. A list of supported notification events is available on the official Harbor documentation under [Configure Webhook Notifications](https://goharbor.io/docs/2.5.0/working-with-projects/project-configuration/configure-webhooks/). Furthermore, use the VEBA Event viewer endpoint (`https://<VEBA-FQDN>/events`) to display all incoming events. | ||
|
||
Deploy the function to the VMware Event Broker Appliance (VEBA). | ||
|
||
```console | ||
# 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. Functions deployed through through the VMware Event Broker Appliance UI defaults to scale to 0, which means the pods will only run when it is triggered by an vCenter Event. | ||
|
||
```yaml | ||
annotations: | ||
autoscaling.knative.dev/maxScale: "1" | ||
autoscaling.knative.dev/minScale: "1" | ||
``` | ||
# Step 4 - Undeploy | ||
```console | ||
# undeploy function | ||
|
||
kubectl -n vmware-functions delete -f function.yaml | ||
|
||
# delete secret | ||
kubectl -n vmware-functions delete secret harbor-slack-secret | ||
``` |
39 changes: 39 additions & 0 deletions
39
examples/knative/powershell/kn-ps-harbor-slack/function.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
apiVersion: serving.knative.dev/v1 | ||
kind: Service | ||
metadata: | ||
name: kn-ps-harbor-slack | ||
labels: | ||
app: veba-ui | ||
spec: | ||
template: | ||
metadata: | ||
annotations: | ||
autoscaling.knative.dev/maxScale: "1" | ||
autoscaling.knative.dev/minScale: "1" | ||
spec: | ||
containers: | ||
- image: us.gcr.io/daisy-284300/veba/kn-ps-harbor-slack:1.0 | ||
envFrom: | ||
- secretRef: | ||
name: harbor-slack-secret | ||
env: | ||
- name: FUNCTION_DEBUG | ||
value: "false" | ||
--- | ||
apiVersion: eventing.knative.dev/v1 | ||
kind: Trigger | ||
metadata: | ||
name: kn-ps-harbor-slack-trigger | ||
labels: | ||
app: veba-ui | ||
spec: | ||
broker: default | ||
filter: | ||
attributes: | ||
type: com.vmware.harbor.push_artifact.v0 | ||
subject: admin | ||
subscriber: | ||
ref: | ||
apiVersion: serving.knative.dev/v1 | ||
kind: Service | ||
name: kn-ps-harbor-slack |
Oops, something went wrong.