Skip to content
This repository was archived by the owner on May 19, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ Streams k8s events from k8s namespace to Slack channel as a Slack bot using inco
Configuration is done via env variables that you set in deployment or configmap.

* `K8S_EVENTS_STREAMER_INCOMING_WEB_HOOK_URL` - Slack web hook URL where to send events. Mandatory parameter.
* `K8S_CLUSTER_NAME` - Allow multiple clusters to stream in the events.
* `K8S_SLACK_CHANNEL` - Allow overwrite slack channel name.
* `K8S_SLACK_USERNAME` - Allow overwrite slack username.
* `K8S_EVENTS_STREAMER_NAMESPACE` - k8s namespace to collect events from. Will use be sending events from all namespaces if not specified
* `K8S_EVENTS_STREAMER_DEBUG` - Enable debug print outs to the log. `False` if not defined. Set to `True` to enable.
* `K8S_EVENTS_STREAMER_SKIP_DELETE_EVENTS` - Skip all events of type DELETED by setting env variable to `True`. `False` if not defined. Very useful since those events tells you that k8s event was deleted which has no value to you as operator.
Expand Down
27 changes: 27 additions & 0 deletions example-deployment.tf
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,21 @@ resource "kubernetes_deployment" "streamer" {
value = var.streamer_hook_url
}

env {
name = "K8S_CLUSTER_NAME"
value = var.cluster_name
}

env {
name = "K8S_SLACK_CHANNEL"
value = var.slack_channel
}

env {
name = "K8S_SLACK_USERNAME"
value = var.slack_username
}

env {
name = "K8S_EVENTS_STREAMER_SKIP_DELETE_EVENTS"
value = "True"
Expand All @@ -105,3 +120,15 @@ resource "kubernetes_deployment" "streamer" {
variable "streamer_hook_url" {
type = string
}

variable "cluster_name" {
type = string
}

variable "slack_channel" {
type = string
}

variable "slack_username" {
type = string
}
9 changes: 9 additions & 0 deletions example-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ spec:
env:
- name: K8S_EVENTS_STREAMER_INCOMING_WEB_HOOK_URL
value: PUT-WEB-HOOK-URL-HERE
# if you don't put anything, default to mycluster
- name: K8S_CLUSTER_NAME
value: PUT-CLUSTER-NAME-HERE
# if you don't put anything, default to #random
- name: K8S_SLACK_CHANNEL
value: PUT-SLACK-CHANNEL-HERE
# if you don't put anything, default to k8s-events-to-slack-streamer
- name: K8S_SLACK_USERNAME
value: PUT-SLACK-USERNAME-HERE
# - name: K8S_EVENTS_STREAMER_NAMESPACE
# value: 'default'
# - name: K8S_EVENTS_STREAMER_DEBUG
Expand Down
8 changes: 6 additions & 2 deletions k8s-events-to-slack-streamer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@

logger = logging.getLogger()
logger.setLevel(logging.INFO)

slack_channel = os.environ.get('K8S_SLACK_CHANNEL', '#random')
slack_username = os.environ.get('K8S_SLACK_USERNAME', 'k8s-events-to-slack-streamer')
k8s_cluster_name = os.environ.get('K8S_CLUSTER_NAME', 'mycluster')

def read_env_variable_or_die(env_var_name):
value = os.environ.get(env_var_name, '')
Expand Down Expand Up @@ -50,9 +52,11 @@ def timestamp(obj):
return obj.strftime('%d/%m/%Y %H:%M:%S %Z') if obj else "no info"

message = {
'channel': slack_channel,
'username': slack_username,
'attachments': [{
'color': '#36a64f',
'title': event.message,
'title': 'Cluster: {}, {}'.format(k8s_cluster_name, event.message),
'text': 'event type: {}, event reason: {}'.format(event_object['type'], event.reason),
'footer': 'First time seen: {}, Last time seen: {}, Count: {}'.format(timestamp(event.first_timestamp),
timestamp(event.last_timestamp),
Expand Down