{"id":25314705,"url":"https://github.com/rguske/kn-ps-apiserver-slack","last_synced_at":"2025-04-07T15:14:39.297Z","repository":{"id":194808595,"uuid":"691631403","full_name":"rguske/kn-ps-apiserver-slack","owner":"rguske","description":"Example Knative PowerShell function for sending CloudEvents emits by the Kubernetes Api Server to a Slack webhook.","archived":false,"fork":false,"pushed_at":"2023-09-15T10:28:21.000Z","size":1917,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-13T17:51:46.259Z","etag":null,"topics":["automation","eventdriven","eventing","faas","knative","kubernetes","serverless"],"latest_commit_sha":null,"homepage":"","language":"PowerShell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rguske.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2023-09-14T15:06:00.000Z","updated_at":"2024-01-28T19:17:12.000Z","dependencies_parsed_at":"2023-09-15T07:23:21.853Z","dependency_job_id":null,"html_url":"https://github.com/rguske/kn-ps-apiserver-slack","commit_stats":null,"previous_names":["rguske/kn-ps-apiserver-slack"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rguske%2Fkn-ps-apiserver-slack","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rguske%2Fkn-ps-apiserver-slack/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rguske%2Fkn-ps-apiserver-slack/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rguske%2Fkn-ps-apiserver-slack/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rguske","download_url":"https://codeload.github.com/rguske/kn-ps-apiserver-slack/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247675606,"owners_count":20977378,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["automation","eventdriven","eventing","faas","knative","kubernetes","serverless"],"created_at":"2025-02-13T17:35:42.167Z","updated_at":"2025-04-07T15:14:39.122Z","avatar_url":"https://github.com/rguske.png","language":"PowerShell","funding_links":[],"categories":[],"sub_categories":[],"readme":"# kn-ps-apiserver-slack\n\nExample 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](https://knative.dev/docs/eventing/sources/apiserversource/).\n\n![Alt text](image-1.png)\n\n# Step 1 - Build\n\nCreate the container image locally to test your function logic. Change the IMAGE name accordingly, example below for Docker.\n\n```console\nexport IMAGE=\u003cdocker-username\u003e/kn-ps-apiserver-slack:1.0\ndocker build -t ${IMAGE}\n```\n\n# Step 2 - Test\n\nVerify the container image works by executing it locally.\n\nChange into the `test` directory\n\n```console\ncd test\n```\n\nUpdate the following variable names within the `docker-test-env-variable` file\n\n* SLACK_WEBHOOK_URL - Slack webhook URL\n* SLACK_MESSAGE_PRETEXT - Text displayed for Slack notification\n\nStart the container image by running the following command:\n\n```console\ndocker run -e FUNCTION_DEBUG=true -e PORT=8080 --env-file docker-test-env-variable -it --rm -p 8080:8080 ${IMAGE}\n```\n\nIn 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\n\n```console\nTesting Function ...\nSee docker container console for output\n\n# Output from docker container console\n06/27/2022 09:47:31 - DEBUG: K8s Secrets:\n{\"SLACK_WEBHOOK_URL\":\"**********\",\"SLACK_MESSAGE_PRETEXT\":\":kubernetes: ApiServer Slack Function powered by :knative:\"}\n\n06/27/2022 09:47:31 - DEBUG: \"{\n      attachments = @(\n         @{\n            pretext = $(${jsonSecrets}.SLACK_MESSAGE_PRETEXT);\n            fields = @(\n               @{\n                  title = \"Kubernetes Api Server Message\";\n                  value = $cloudEventData.message;\n                  short = \"false\";\n               }\n               @{\n                  title = \"Reason\";\n                  value = $cloudEventData.reason;\n                  short = \"false\";\n               }\n               @{\n                  title = \"Kind:\";\n                  value = $cloudEventData.involvedObject.kind;\n                  short = \"false\";\n               }\n               @{\n                  title = \"Name:\";\n                  value = $cloudEventData.involvedObject.name;\n                  short = \"false\";\n               }\n               @{\n                  title = \"Namespace:\";\n                  value = $cloudEventData.metadata.namespace;\n                  short = \"false\";\n               }\n               @{\n                  title = \"Event Type:\";\n                  value = $cloudEvent.type;\n                  short = \"false\";\n               }\n               @{\n                  title = \"Unique ID:\";\n                  value = $cloudEvent.id;\n                  short = \"false\";\n               }\n               @{\n                  title = \"DateTime in UTC:\";\n                  value = $cloudEvent.time;\n                  short = \"false\";\n               }\n               @{\n                  title = \"Source:\";\n                  value = $cloudEvent.source;\n                  short = \"false\";\n               }\n            )\n            footer = \"Powered by Knative\";\n            footer_icon = \"https://github.com/knative/docs/blob/main/docs/images/logo/cmyk/knative-logo-cmyk.png\";\n         }\n      )\n   }\n\"\n06/27/2022 09:47:31 - Sending Webhook payload to Slack ...\n06/27/2022 09:47:31 - Successfully sent Webhook ...\n```\n\n![Alt text](image.png)\n\n# Step 3 - Deploy\n\n\u003e **Note:** The following steps assume a working Knative environment using the RabbitMQ `broker` or the [MTChannelBasedBroker](https://knative.dev/docs/eventing/brokers/broker-types/channel-based-broker/). The Knative `service` and `trigger` will be installed in the\n`vmware-functions` Kubernetes namespace, assuming that the `broker` is also available there.\n\u003e\n\nUpdate 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`.\n\n```console\n# create secret\n\nkubectl -n vmware-functions create secret generic apiserver-slack-secret --from-file=SLACK_SECRET=apiserver_slack_secret.json\n```\n\nEdit 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.\n\nFurthermore, use the [Sockeye Event](https://github.com/n3wscott/sockeye) viewer to display all incoming events.\n\nDeploy the function to the Knative environment.\n\n```console\n# deploy function\n\nkubectl -n vmware-functions apply -f function.yaml\n```\n\nFor 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.\n\n```yaml\nannotations:\n  autoscaling.knative.dev/maxScale: \"1\"\n  autoscaling.knative.dev/minScale: \"1\"\n```\n\n# Step 4 - Undeploy\n\n```console\n# undeploy function\n\nkubectl -n vmware-functions delete -f function.yaml\n\n# delete secret\nkubectl -n vmware-functions delete secret apiserver-slack-secret\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frguske%2Fkn-ps-apiserver-slack","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frguske%2Fkn-ps-apiserver-slack","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frguske%2Fkn-ps-apiserver-slack/lists"}