Code Monkey home page Code Monkey logo

fluxcloud's Introduction

Fluxcloud is a tool to receive events from the Weave flux.

Actions Status

Weave Flux is a useful tool for managing the state of your Kubernetes cluster.

Fluxcloud is a valid upstream for Weave, allowing you to send Flux events to Slack or a webhook without using Weave Cloud.

Docker

Images are available at DockerHub and Quay

Setup

Please see the Weave Flux setup documentation for setting up Flux.

To use Fluxcloud, you can deploy fluxcloud as either a sidecar to Flux or a seperate deployment.

To deploy as a sidecar, see examples/flux-deployment-sidecar.yaml. To deploy independently, see examples/fluxcloud.yaml.

Set the following environment variables in your chosen deployment:

  • SLACK_URL: the Slack webhook URL to use.
  • SLACK_USERNAME: the Slack username to use when sending messages.
  • SLACK_TOKEN (optional): legacy Slack API token to use.
  • SLACK_CHANNEL: the Slack channel to send messages to.
  • SLACK_ICON_EMOJI: the Slack emoji to use as the icon.
  • MATTERMOST_URL: the Mattermost webhook URL to use.
  • MATTERMOST_USERNAME: the Mattermost username to use when sending messages.
  • MATTERMOST_CHANNEL: the Mattermost channel to send messages to.
  • MATTERMOST_ICON_URL: the Mattermost Icon URL to use as the icon.
  • DATADOG_API_KEY: the Datadog API key used to push events.
  • DATADOG_APP_KEY: the Datadog APP key used to push events.
  • DATADOG_ADITIONAL_TAGS: Datadog aditional tags to be added to the generated event.
  • MSTEAMS_URL: the Microsoft Teams webhook URL to use
  • GITHUB_URL: the URL to the Github repository that Flux uses, used for Slack links.
  • WEBHOOK_URL: if the exporter is "webhook", then the URL to use for the webhook.
  • EXPORTER_TYPE (optional): The types of exporter to use in comma delimited form. (Ex: slack,webhook) (Choices: slack, msteams, datadog, webhook, Default: slack)
  • JAEGER_ENDPOINT (optional): endpoint to report Jaeger traces to.

And then apply the configuration:

kubectl apply -f examples/fluxcloud.yaml

Set the --connect flag on Flux to --connect=ws://fluxcloud.

Exporters

There are multiple exporters that you can use with fluxcloud. If there is not a suitable one already, feel free to contribute one by implementing the exporter interface!

Formatters

Templates

The default formatter uses go templates for the three different sections that compose an event: the title, the body and the commit message.

There are default values for all these templates, but it's possible to redefine them, you only need to ensure there is a templates/ folder in the working directory of FluxCloud with the files:

  • body.tmpl
  • title.tmpl
  • commit.tmpl

Not all the three files are required to exist, you may define only a subset.

The values passed to the templates are defined by the tplValues struct of the pkg/formatters/default.go file. You may also look the passed functions at the tplFuncMap function map.

Before using the defaults, the templates are also fetched from these environment variables:

  • BODY_TEMPLATE
  • TITLE_TEMPLATE
  • COMMIT_TEMPLATE

The files have precedence over the environment variables.

Formatting commit links

By default, commit links are formatted for Github. It is possible to format them for another VCS system, such as Bitbucket, by overriding the commit template.

The commit template is a go template that supports two variables:

  • VCSLink: which is the GITHUB_URL configuration option.
  • Commit: which is the commit id.

The default is:

{{ .VCSLink }}/commit/{{ .Commit }}

For example, to override to work for Bitbucket, set the COMMIT_TEMPLATE environment variable to:

{{ .VCSLink }}/commits/{{ .Commit }}

Slack

The default exporter to use is Slack. To use the Slack exporter, set the SLACK_URL, SLACK_USERNAME, and SLACK_CHANNEL environment variables to use. You can also optionally set the EXPORTER_TYPE to "slack".

Sending notifications to multiple channels

If sending notifications to only one channel is unsufficient for your use case you can configure fluxcloud to send them to multiple channels based upon the namespace(s) from the created and/or updated resources. This is done by setting a comma separated <channel>=<namespace> string as the SLACK_CHANNEL environment variable.

If you for example want to send notifications of all events to #k8s-events but only events from namespace team-b to #teamb you would set the following string: SLACK_CHANNEL=#k8s-events=*,#team-b=team-b.

Microsoft Teams

Set the environment variable MSTEAMS_URL to the URL generated on activation of an Incoming Webhook in a Microsoft Teams channel.

Mattermost

To use the Mattermost exporter, set the MATTERMOST_URL, MATTERMOST_USERNAME, and MATTERMOST_CHANNEL environment variables to use. You can also optionally set the EXPORTER_TYPE to "mattermost".

Datadog

Events can be sent to Datadog by adding "datadog" to to EXPORTER_TYPE and then setting the DATADOG_API_KEY and the DATADOG_APP_KEY. More information about generating those keys can be found in Datadog documentation.

You can also add additional tags to the event by setting DATADOG_ADDITIONAL_TAGS.

Webhooks

Events can be sent to an arbitrary webhook by setting the EXPORTER_TYPE to "webhook" and then setting the WEBHOOK_URL to the URL to send the webhook to.

Fluxcloud will send a POST request to the provided URL with the encoded event as the payload.

Versioning

Fluxcloud follows semver for versioning, but also publishes development images tagged with $BRANCH-$COMMIT.

To track release images:

fluxctl policy -c kube-system:deployment/fluxcloud --tag-all='v0*'

To track the latest pre-release images:

fluxctl policy -c kube-system:deployment/fluxcloud --tag-all='master-*'

And then you can automate it:

fluxctl automate -c kube-system:deployment/fluxcloud

Build

To build fluxcloud, you can either use go:

go build -o fluxcloud ./cmd/

Or, to run a full CI build, download hone:

hone

fluxcloud's People

Contributors

b-souty avatar bernardolins avatar blmhemu avatar cpanato avatar davidkarlsen avatar demikl avatar evq avatar fdpeiter avatar geoah avatar hiddeco avatar latchmihay avatar marcosnils avatar marratj avatar matheuscscp avatar nagypeterjob avatar torresdal avatar vterdunov avatar willholley avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

fluxcloud's Issues

Add Helm chart

Would love to see a Helm chart for ease of templated deployments. Saw a PR for this in the original repo. Happy to cherry pick from it or create a new one from scratch if need be.

Alert spam with every flux sync

I've been running the justinbarrick/fluxcloud:v0.3.0 image for a while with no issues. I looked into this fork to try out a newer version. I alert to a Slack channel. It seems that on every flux sync, fluxcloud floods the webhook seemingly with a status of every prior release. It does this on every flux sync regardless of any actual changes.

This happens while running flux:1.20.1 and fluxcloud:v0.4.0.

Same context used for both exporters

I found an issue with context.Context cancelation when having a large changelog and multiple exporters.

Fluxcloud uses the same Context for every exporters and use the net/http Context.

In our setup, we publish the changelog on two differents Slack channels and a webhook. The changelog is pretty large and a context cancelation occurs on the post to the second channel (because Flux closes the request due to timeout). All subsequents reporters are failing due to the context canceled by the caller.

Here the flux logs

caller=sync.go:434 component=daemon err="executing HTTP request: executing HTTP request: Post \"http://fluxcloud/v6/events\": context deadline exceeded (Client.Timeout exceeded while awaiting headers)"

Here the fluxcloud logs

Could not post to slack:Post https://hooks.slack.com/services/TXXX/BXXX/XXXX: context canceled

I don't know how the interaction should work with Flux, but is it possible to define a timeout per exporters (by wrapping Context in WithTimeout and/or run the exporters calls asynchronously within a goroutine ? This way fluxcloud always respond OK to Flux, and logs tell us if something goes wrong, each exporter has its dedicated timeout and context that don't interfere with others.

Support configuration using files

Background

Recognizing that some of the configuration points are secrets, it would be great to support the ability to provide the configuration using files. This would also help support other mechanisms to load secrets (such as sidecars that load secrets from stores into a shared volume).

Proposal

For each environment variable, support the variable with a _FILE suffix. If defined, the contents of the file are used as the secret. I would expect only the following to be needed:

  • SLACK_URL_FILE
  • DATADOG_API_KEY_FILE
  • MSTEAMS_URL_FILE

I'd be happy to help contribute some time to making this work. But, figured I'd open the issue first to see if others are ok/want changes to it before doing the work ๐Ÿ‘

arm64 build to Docker Hub

Is there any chance the arm64 image can be pushed to Docker Hub as part of your CI? I see there's already an arm64 Dockerfile at the root of the repo

Provide a method for the formatters package to fetch values from env vars in templates

The ability to fetch environment variables in the formatters' templates would be very interesting. For example, it would allow achieving the title "Applied flux changes to cluster Production-US", where Production-US is the value assigned to the CLUSTER environment variable. Also, the parameterization of templates should come from files instead of environment variables, since they are multiline strings.

Proposal

  1. Create a getenv function to be used on the templates.
  2. Add a config.MapConfig type alias for map[string]string and implement config.Config.
  3. Fetch the three templates from files at hardcoded paths and feed them into an instance of config.MapConfig with the same keys used by formatters.NewDefaultFormatter().
  4. Add a config.ChainConfig implementation of config.Config with a Head and a Tail, both references to config.Config. The implementation would first try to fetch a key from the Head, and then fallback to the Tail if necessary.
  5. Feed an instance of config.ChainConfig to formatters.NewDefaultFormatter(), with the Head being an instance of config.MapConfig as described in 3, and Tail being config.DefaultConfig.

Slack rate limiting errors aren't retried

When Slack returns a 429 rate limiting error, fluxcloud pushes it back to flux as a 500. When this happens, flux considers the sync incomplete and tries to sync again from the last ref. This causes the same amount or more messages so fluxcloud gets rate limited again. This effectively blocks flux from ever updating its last successful sync position and causes channel flooding.

fluxcloud should retry rate limiting errors (probably within the confines of the context/timeout it's invoked with) to help avoid this situation.

Example flux error

ts=2022-07-26T14:57:02.328431881Z caller=sync.go:103 component=daemon err="executing HTTP request: 500 Internal Server Error Could not post to slack, status: 429

It looks like flux v1 bails out right here https://github.com/fluxcd/flux/blob/bdc94da60e0d4c0f30c21a8e9e63eccb7b226d3e/pkg/daemon/sync.go#L105 directly above where it'd update the git note with sync state.

In Slack, you see messages like

Applied flux changes to cluster
Event: Sync: f188e78..244d8ec,

where the first commit never changes and the message/event list continues to grow

Possibly related: #7
The change proposed here would also fix the situation and allow better rate limit handling #20 (comment)

Ability to filter events

Thanks for the tool!

I wonder if there is a way to filter events, because it becomes really noisy sometimes even in regards of the simple update operation

image

If there is no, consider, please, as feature request! :)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.