Rollbacking Helm releases from an UI with Windmill - Part 2
Tags: kubernetes, lowcode, windmill, serverless
Published: 2023-12-10 12:48:59

Welcome back to part two of our Windmill series! Today, we'll learn how to chain together our scripts into a flow and trigger it using the user interface we made before. Let's begin!

## Creating a test release

To avoid disrupting too much our Kubernetes infrastructure, we’ll install a test release so we can play with it:

```bash
helm upgrade -i test-a oci://ghcr.io/stefanprodan/charts/podinfo
# another time so we can rollback it
helm upgrade -i test-a oci://ghcr.io/stefanprodan/charts/podinfo
```

And refreshing our UI, the release will appear ready to be controlled from our table.

## Going with the flow

To showcase the last (and not the least) feature of Windmill, let’s put together a script to rollback a release and then send a notification on Slack, we’re going to build a Flow!

Let’s start by clicking on the “+ Flow” button on our homepage, and we’ll start by giving it a good name, and then click on the “Input” box on the diagram on the left.

We’ll be prompted for the inputs of our flow. You can go a few ways, one really nice feature is that Windmill can open an endpoint to which you can POST data, which will then get converted automatically into inputs. But in this case, we’ll need only two inputs: the release name and its namespace.

And it will look like this:

![Input of our flow](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mua91vkxo66laaf8cbna.png)

With that, let’s click on the “+” button just below inputs to add our script:

![New action](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rnoixygiphhbs7i0kgmh.png)

We’ll create a new Bash inline script:

```bash
# shellcheck shell=bash
# arguments of the form X="$I" are parsed as parameters X of type string
helm_release="$1"
namespace="$2"

out=$(helm rollback -n "$namespace" "$helm_release")
status=$?

jq --null-input --arg output "$out" --arg status "$status" \
    '{"output": $output, "status": $status}' > ./result.json
```

In this script, we take our two inputs, store the rollback command and its status in variables then use JQ to quickly construct a JSON valid output. This will enable us to handle any errors.

And, as for the UI, by clicking on the “Plug” connect icon, we can pass our flow inputs to this step:

![Rollback script](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lax7i4h5n4gxn3paa2ro.png)

Before testing the rollback, we will need to add new permissions to our workers so they can actually do the rollback. This step will be left to your discretion.

> ⚠️ This is all for the sake of the demo but remember to properly separate the workers into their own pool with their proper permissions and lock down on their usage.

Now that we have the rollback, let’s also send a notification to Slack! You will need to create a new application for your workspace, the [Quick Start](https://api.slack.com/start/quickstart#scopes) guide is the recommended way. Come back when you have an App Token (it should start with `xoxb`)

Let’s click again on the “+” sign after our rollback script, and let’s do a quick search on the community hub for a script to send a message, you can directly search for “Send message to channel (slack)”. If you’re prompted to add inputs to your flow, reject it.

This community script is the perfect occasion to showcase another feature of Windmill: “Resources”.

A resource is simply a connector to another service like Datadog, AWS, SQL database, etc… It’s a great way to share scoped accesses to services inside a script. Here,  we’ll go ahead and click on the “+” sign next to the Slack Resource input, paste our App token and save it.  

We’ll also provide the channel and write a templated text message, and since it’s a JS expression we can interpolate some variables: 

```jsx
`Rollbacked \`${flow_input.helm_release}\` in namespace \`${flow_input.namespace}\``
```

It should now look like this:

![Slack script](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b0j5tr4n2o7tdhzn92ok.png)

However, if our rollback fails, we may want to handle this error and send a different Slack notification. We could do this using a ternary condition in our 'text' input. But for the sake of this example, let's create a branch. Click on the "+" sign before our Slack script and select the "Branch to one" option. To handle cases when our status is not 0, we'll edit the predicate. We'll then duplicate and align our Slack messages accordingly:

![Branch configuration](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o2kti8sm6t1q44yost1c.png)

> Note: in practice you would fork the Slack script and handle the status directly with JS for example, this way you could also customize the message sent using [Slack Blocks](https://api.slack.com/block-kit).

Let's test our new flow. If everything was configured correctly, we should see the result window:

![Test run](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0n845fijfwyj3iu5vmsl.png)

Finally, let's integrate this into our UI to wrap up for the day! Deploy the flow and then return to the App UI editor. Here, select our table component. On the right, there's a section titled "Table Actions", where we can set up various interactions with our table rows. For now, let's add a new button.

![Table configuration](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tgoer61z3cwab0gmaewx.png)

You can now rename the button, change its color, and even add an icon. Once you're satisfied, select "Event Handler → Select a script or flow" to choose the Helm Release flow we created earlier. Then, you can input the release and namespace based on the table row. The final result should look like this:

![Button configuration](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gksiwifgk6xggwdsh6qp.png)

At the end of the settings, make sure to toggle the recompute of the table so changes are reloaded! And now, if all goes well, you should have a shiny UI with a functional rollback buttons, and Slack notifications 🎉

![Full table](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oxnaqvdx47ios0pwifh2.png)

When you click on Rollback, the following notification will be sent to your Slack channel:

![Slack notification](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rgsiogm2o9k3u84yxfle.png)

## Wrapping up

That's all for now! If you enjoyed this series, let me know and we can certainly plan another deep dive into the flow feature. We can explore how to connect more data sources to build more complex user interfaces! I hope you enjoyed this presentation of Windmill. See you soon!