Deploy helm charts with go lang
Tags: go, helm, kubernetes
Published: 2024-06-20 07:55:37

> **What is Helm ?**

Helm is designed to simplify the deployment and management of complex application workloads in Kubernetes. It functions similarly to a package manager for Kubernetes, where the packages are known as Helm Charts. A Helm Chart combines a template file, which outlines the Kubernetes resources to be deployed, and a values file that provides the necessary configuration for the template. The templating system in Helm Charts allows for package reusability.


> **The Helm go SDK ?**

Helm includes a convenient CLI, a robust command-line tool that helps end-users manage all stages of a Chart’s lifecycle. However, using the CLI for programmatically installing Helm Charts on a Kubernetes cluster is not ideal for building a reliable and predictable application. Fortunately, the Helm developers designed the CLI as an interface for the [Go SDK](https://github.com/mittwald/go-helm-client), which I will use for this purpose.


> **Installing and Using Helm client ….**

First we need to install go-helm-client, we can use below command and it will add this module into `go.mod` file.


```sh
$ go get github.com/mittwald/go-helm-client
```

Now we can import this like `helmcilent "github.com/mittwald/go-helm-client"` and use this in our application.

```go
package main

import (
  "fmt"
  "os"
  helmclient "github.com/mittwald/go-helm-client"
)


func GetHelmClient(kubeConfig string, namespace string) (helmclient.Client) {

 opt := &helmclient.KubeConfClientOptions{
  Options: &helmclient.Options{
   Namespace:        namespace, // Change this to the namespace you wish to install the chart in.
   RepositoryCache:  "/tmp/.helmcache",
   RepositoryConfig: "/tmp/.helmrepo",
   Debug:            true,
  },
  KubeContext: "",
  KubeConfig:  []byte(kubeConfig),
 }
 
 helmClient, err := helmclient.NewClientFromKubeConf(opt)
 if err != nil {
  fmt.Printf("Failed to initialize Helm Client: %v", err)
  return nil
 }
 return helmClient
}

func main() {
 dirname, err := os.UserHomeDir()
 kubeConfig, err := os.ReadFile(fmt.Sprintf("%s/.kube/config", dirname))
 if err != nil {
  t.Fatalf("Not able to read File: %v", err)
 }

 helmClient := helmc.GetHelmClient(string(kubeConfig), "default")
}

```

you can run and test this on your local Kubernetes if your [docker-kubernetes](https://docs.docker.com/desktop/kubernetes/) is enabled. providing `kubeConfig` and `namespace` you can create helmClient object and using that you can perform helm operations.

Here are **list of methods** that you are able to call using this client.

```go
type Client interface {
   AddOrUpdateChartRepo(entry repo.Entry) error
   UpdateChartRepos() error
   InstallOrUpgradeChart(ctx context.Context, spec *ChartSpec, opts *GenericHelmOptions) (*release.Release, error)
   InstallChart(ctx context.Context, spec *ChartSpec, opts *GenericHelmOptions) (*release.Release, error)
   UpgradeChart(ctx context.Context, spec *ChartSpec, opts *GenericHelmOptions) (*release.Release, error)
   ListDeployedReleases() ([]*release.Release, error)
   ListReleasesByStateMask(action.ListStates) ([]*release.Release, error)
   GetRelease(name string) (*release.Release, error)
   // RollBack is an interface to abstract a rollback action.
   RollBack
   GetReleaseValues(name string, allValues bool) (map[string]interface{}, error)
   GetSettings() *cli.EnvSettings
   GetProviders() getter.Providers
   UninstallRelease(spec *ChartSpec) error
   UninstallReleaseByName(name string) error
   TemplateChart(spec *ChartSpec, options *HelmTemplateOptions) ([]byte, error)
   LintChart(spec *ChartSpec) error
   SetDebugLog(debugLog action.DebugLog)
   ListReleaseHistory(name string, max int) ([]*release.Release, error)
   GetChart(chartName string, chartPathOptions *action.ChartPathOptions) (*chart.Chart, string, error)
   RunChartTests(releaseName string) (bool, error)
}
```

Let me give you an example how you can use the helmClient and installChart from local directory.

```go
import (
  "context"
)

func main() {
  // using old helmClient that we have created before
  helmClient := helmc.GetHelmClient(string(kubeConfig), "default")
  
  // Define the chart to be installed
  chartSpec := ChartSpec{
   ReleaseName: "nginx",
   ChartName:   "/tmp/nginx",
   Namespace:   "default",
   UpgradeCRDs: true,
   Wait:        true,
  }

  // Install a chart release.
  // Note that helmclient.Options.Namespace should ideally match 
  // the namespace in chartSpec.Namespace.
  if _, err := helmClient.InstallChart(context.Background(), &chartSpec, 
  nil); err != nil {
   panic(err)
  }
}
```

You can also provide a zip or link to zip file as value in `ChartName `.

If you want more example you can look into this. [https://github.com/mittwald/go-helm-client/blob/v0.12.9/client_test.go](https://github.com/mittwald/go-helm-client/blob/v0.12.9/client_test.go)

---
Thank you for taking the time to read my blog. If you found the content helpful or interesting, please consider giving it a clap! 👏 Your support is greatly appreciated.

Support me on [PayPal.Me/rathod0045](PayPal.Me/rathod0045)