Skip to content

Commit

Permalink
Merge pull request #1149 from lamw/issue-1146
Browse files Browse the repository at this point in the history
feat: Google Chat Notification function
  • Loading branch information
lamw authored Jan 22, 2024
2 parents f1ad96d + 7df53d6 commit ba7c68e
Show file tree
Hide file tree
Showing 9 changed files with 274 additions and 0 deletions.
8 changes: 8 additions & 0 deletions examples/knative/powershell/kn-ps-google-chat/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM ghcr.io/vmware-samples/vcenter-event-broker-appliance/ce-ps-base:1.5

LABEL maintainer="vCenter Event Broker Appliance Community"
LABEL org.opencontainers.image.source="https://github.com/vmware-samples/vcenter-event-broker-appliance"

COPY handler.ps1 handler.ps1

CMD ["pwsh","./server.ps1"]
101 changes: 101 additions & 0 deletions examples/knative/powershell/kn-ps-google-chat/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# kn-ps-google-chat
Example Knative PowerShell function for sending to a [Google Chat webhook](https://developers.google.com/chat/how-tos/webhooks) when a failed vCenter Server backup has been detected.

# Step 1 - Build


Create the container image locally to test your function logic.

```
export TAG=<version>
docker build -t <docker-username>/kn-ps-google-chat:${TAG} .
```

# 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

* GOOGLE_CHAT_WEBHOOK_URL - Google Chat webhook URL

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 <docker-username>/kn-ps-google-chat:${TAG}
```

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
01/17/2024 00:22:13 - Sending message to Google Chat Webhook ...
01/17/2024 00:22:13 - Successfully sent Google Chat message ...
01/17/2024 00:23:25 - PowerShell HTTP Server stop requested. Waiting for server to stop
01/17/2024 00:23:25 - Processing Shutdown

01/17/2024 00:23:25 - Shutdown Processing Completed

01/17/2024 00:23:25 - PowerShell HTTP server stop requested
```

# 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.

Push your container image to an accessible registry such as Docker once you're done developing and testing your function logic.

```console
docker push <docker-username>/kn-ps-google-chat:${TAG}
```

Update the `google_chat_secret.json` file with your Google Chat webhook configurations and then create the kubernetes secret which can then be accessed from within the function by using the environment variable named called `GOOGLE_CHAT_SECRET`.

```console
# create secret

kubectl -n vmware-functions create secret generic google-chat-secret --from-file=GOOGLE_CHAT_SECRET=google_chat_secret.json

# update label for secret to show up in VEBA UI
kubectl -n vmware-functions label secret google-chat-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.vsphere.com.vmware.applmgmt.backup.job.failed.event.v0` vCenter Server Event. If you wish to change this, update the `type` field within `function.yaml` to the desired event type.


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 google-chat-secret
```
38 changes: 38 additions & 0 deletions examples/knative/powershell/kn-ps-google-chat/function.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: kn-ps-google-chat
labels:
app: veba-ui
spec:
template:
metadata:
annotations:
autoscaling.knative.dev/maxScale: "1"
autoscaling.knative.dev/minScale: "1"
spec:
containers:
- image: ghcr.io/vmware-samples/vcenter-event-broker-appliance/kn-ps-google-chat:1.0
envFrom:
- secretRef:
name: google-chat-secret
env:
- name: FUNCTION_DEBUG
value: "false"
---
apiVersion: eventing.knative.dev/v1
kind: Trigger
metadata:
name: veba-ps-google-chat-trigger
labels:
app: veba-ui
spec:
broker: default
filter:
attributes:
type: com.vmware.vsphere.com.vmware.applmgmt.backup.job.failed.event.v0
subscriber:
ref:
apiVersion: serving.knative.dev/v1
kind: Service
name: kn-ps-google-chat
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"GOOGLE_CHAT_WEBHOOK_URL": "FILL_ME_IN"
}
71 changes: 71 additions & 0 deletions examples/knative/powershell/kn-ps-google-chat/handler.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
Function Process-Init {
[CmdletBinding()]
param()
Write-Host "$(Get-Date) - Processing Init`n"

Write-Host "$(Get-Date) - Init Processing Completed`n"
}

Function Process-Shutdown {
[CmdletBinding()]
param()
Write-Host "$(Get-Date) - Processing Shutdown`n"

Write-Host "$(Get-Date) - Shutdown Processing Completed`n"
}

Function Process-Handler {
[CmdletBinding()]
param(
[Parameter(Position=0,Mandatory=$true)][CloudNative.CloudEvents.CloudEvent]$CloudEvent
)

# Decode CloudEvent
try {
$cloudEventData = $cloudEvent | Read-CloudEventJsonData -Depth 10
} catch {
throw "`nPayload must be JSON encoded"
}

try {
$jsonSecrets = ${env:GOOGLE_CHAT_SECRET} | ConvertFrom-Json
} catch {
throw "`nK8s secrets `$env:GOOGLE_CHAT_SECRET does not look to be defined"
}

if(${env:FUNCTION_DEBUG} -eq "true") {
Write-Host "$(Get-Date) - DEBUG: K8s Secrets:`n${env:GOOGLE_CHAT_SECRET}`n"

Write-Host "$(Get-Date) - DEBUG: CloudEventData`n $(${cloudEventData} | Out-String)`n"
}

$vc = (${cloudEvent}.Source).toString().replace("/sdk","")
$dateTime = ${cloudEvent}.time

$payload = @{
"text" = "`VCSA Backup Failure Alert` - `\n\t*DateTime*: ${dateTime}`\n\t*VCSA*: ${vc}/ui`\n\t*VCSA VAMI*: ${vc}:5480"
}

# Convert payload to JSON and un-escape "\n" which is used to annotate line break for Google Chat messages
$body = ($payload | ConvertTo-Json -Depth 2).Replace('\\n','\n').Replace('\\t','\t')

$headers = @{
"Content-Type" = "application/json";
}

if(${env:FUNCTION_DEBUG} -eq "true") {
Write-Host "$(Get-Date) - DEBUG: `"$($headers | Out-String)`""
Write-Host "$(Get-Date) - DEBUG: `"$body`""
}

Write-Host "$(Get-Date) - Sending message to Google Chat Webhook ..."
$ProgressPreference = "SilentlyContinue"

try {
Invoke-WebRequest -Uri $(${jsonSecrets}.GOOGLE_CHAT_WEBHOOK_URL) -Method POST -Headers $headers -Body $body
} catch {
throw "$(Get-Date) - Failed to send Google Chat message: $($_)"
}

Write-Host "$(Get-Date) - Successfully sent Google Chat message ..."
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GOOGLE_CHAT_SECRET={"GOOGLE_CHAT_WEBHOOK_URL": "FILL_ME_IN"}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

$headers = @{
"Content-Type" = "application/json";
"ce-specversion" = "1.0";
"ce-id" = "2112913";
"ce-source" = "https://vcenter.primp-industries.local/sdk";
"ce-type" = "com.vmware.vsphere.com.vmware.applmgmt.backup.job.failed.event.v0";
}

$body = Get-Content -Raw -Path "./test-payload.json"

Write-Host "Testing Function ..."
Invoke-WebRequest -Uri http://localhost:8080 -Method POST -Headers $headers -Body $body

Write-host "See docker container console for output"
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

echo "Testing Function ..."
curl [email protected] \
-H "Content-Type: application/json" \
-H 'ce-specversion: 1.0' \
-H 'ce-id: 2112913' \
-H 'ce-source: https://vcenter.primp-industries.local/sdk' \
-H 'ce-type: com.vmware.vsphere.com.vmware.applmgmt.backup.job.failed.event.v0' \
-X POST localhost:8080

echo "See docker container console for output"
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"ChangeTag": "",
"UserName": "VSPHERE.LOCAL\\machine-767f8016-870d-4a98-a457-8247454fa759",
"FullFormattedMessage": "Backup job failed",
"EventTypeId": "com.vmware.applmgmt.backup.job.failed.event",
"Severity": "info",
"Fault": null,
"Dvs": null,
"Message": "",
"Key": 2112913,
"Vm": null,
"ChainId": 2112913,
"Host": null,
"CreatedTime": "2024-01-16T17:10:30.181Z",
"Net": null,
"ComputeResource": null,
"Datacenter": null,
"Arguments": [
{
"Key": "_sourcehost_",
"Value": "vcsa.primp-industries.local"
}
],
"Ds": null
}

0 comments on commit ba7c68e

Please sign in to comment.