How to create your first Helm plugin?
Tags: kubernetes, devops, cloudnative, docker
Published: 2022-05-03 07:12:02

## Helm
Helm helps you manage Kubernetes applications using helm charts. Helm Charts help you define, install, and upgrade Kubernetes applications.

Example:
```
# add chart repository
helm repo add bitnami https://charts.bitnami.com/bitnami

# update charts
helm repo update

# install mysql using helm chart by bitnami
helm install bitnami/mysql --generate-name
```

## Helm plugins
Helm plugins provide a way to extend Helm features without modifying the helm core codebase.

Helm plugins have the following features:
- They can be added and removed from a Helm installation without impacting the core Helm tool.
- They can be written in any programming language.
- They integrate with Helm, and will show up in helm help and other places.

Helm plugins are stored in `$HELM_PLUGINS` directory, you can change this using `helm env` command. 

## Install a helm plugin
A Helm plugin can be installed from a git repo, a tar release or from a local directory.

```
helm plugin install <path|url>
```

## Helm plugin anatomy
A helm plugin constists of a plugin.yaml file, which has the plugin definition and its associated commands.
```
─ fullenv
   ├── plugin.yaml
   └── run.sh
```

### plugin definition 
plugin.yaml defines the name, usage, description and command to run when invoked.
- The `Command` can point to your own script of any programming language. 
- The `ignoreFlags` switch tells Helm to not pass flags to the plugin. 

So if a plugin is called with `helm myplugin --foo` and ignoreFlags: true, then --foo is silently discarded.

plugin.yaml
```
name: fullenv
usage: "show env vars"
description: "show all env vars"
command: "$HELM_PLUGIN_DIR/run.sh"
ignoreFlags: true
```

run.sh - just prints helm environment variables.
```
#!/bin/sh
echo $HELM_PLUGIN_NAME
echo $HELM_PLUGIN_DIR
echo $HELM_PLUGINS
echo $HELM_REPOSITORY_CONFIG
echo $HELM_REPOSITORY_CACHE
echo $HELM_BIN
```

### Platform specific commands
Helm plugin commands can be configured to target a os platform and architecture.

```
name: fullenv
usage: "show env vars"
description: "show all env vars"
command: "$HELM_PLUGIN_DIR/run.sh"
platformCommand:
  - os: linux
    arch: i386
    command: "$HELM_PLUGIN_DIR/run.sh"
  - os: linux
    arch: amd64
    command: "$HELM_PLUGIN_DIR/run.sh"
  
```

Helm follows the below rules to choose a command.
- If `platformCommand` is present, it will be searched first.
- If both `os` and `arch` match the current platform, search will stop and the command will be used.
- If `os` matches and there is no more specific `arch` match, the command will be used.
- If no `platformCommand` match is found, the default `command` will be used.
- If no matches are found in `platformCommand` and no `command` is present, Helm will exit with an error.

## Install the plugin
cd into the plugin directory and then run the command below 

```
# install
helm plugin install .

# check if the installation is successful
helm plugin list
``` 

## Test the plugin
```
helm fullenv
```


> If you like this article, subscribe to the newsletter and Connect with me on [twitter](https://twitter.com/sureshdsk) to get updates on my future articles. ✅