Samples on kubernetes helm golang client

8/15/2017

I want to create a service on kubernetes which manages helm charts on the cluster. It installs charts from a private chart repository. Since I didn't find any documents on how to use helm client api, I was looking for some samples or guidelines for creating a service on top of helm client.

-- Mehran Akhavan
go
kubernetes
kubernetes-helm

4 Answers

2/5/2020

FOR HELM3

As other answers pointed, with Helm 2, you need to talk with tiller which complicates stuff.

It is way more clean with Helm 3 since tiller was removed and helm client directly communicates with Kubernetes API Server.

Here is an example code to install a helm chart programmatically with helm3:

package main

import (
    "fmt"
    "os"

    "helm.sh/helm/v3/pkg/action"
    "helm.sh/helm/v3/pkg/chart/loader"
    "helm.sh/helm/v3/pkg/kube"
    _ "k8s.io/client-go/plugin/pkg/client/auth"
)

func main() {
    chartPath := "/tmp/my-chart-0.1.0.tgz"
    chart, err := loader.Load(chartPath)
    if err != nil {
        panic(err)
    }

    kubeconfigPath := "/tmp/my-kubeconfig"
    releaseName := "my-release"
    releaseNamespace := "default"
    actionConfig := new(action.Configuration)
    if err := actionConfig.Init(kube.GetConfig(kubeconfigPath, "", releaseNamespace), releaseNamespace, os.Getenv("HELM_DRIVER"), func(format string, v ...interface{}) {
        fmt.Sprintf(format, v)
    }); err != nil {
        panic(err)
    }

    iCli := action.NewInstall(actionConfig)
    iCli.Namespace = releaseNamespace
    iCli.ReleaseName = releaseName
    rel, err := iCli.Run(chart, nil)
    if err != nil {
        panic(err)
    }
    fmt.Println("Successfully installed release: ", rel.Name)
}
-- turkenh
Source: StackOverflow

10/21/2019

Since it took me some time to get this working here is a minimal example (no error handling, left details about kube config, ...) for listing release names:

package main

import (
  "k8s.io/client-go/kubernetes"
  "k8s.io/helm/pkg/helm"
  "k8s.io/helm/pkg/helm/portforwarder"
)

func main() {
  # omit getting kubeConfig, see: https://github.com/kubernetes/client-go/tree/master/examples

  # get kubernetes client
  client, _ := kubernetes.NewForConfig(kubeConfig)

  # port forward tiller
  tillerTunnel, _ := portforwarder.New("kube-system", client, config)

  # new helm client
  helmClient := helm.NewClient(helm.Host(host))

  # list/print releases
  resp, _ := helmClient.ListReleases()
  for _, release := range resp.Releases {
    fmt.Println(release.GetName())
  }
}
-- rabenhorst
Source: StackOverflow

6/3/2019

I was looking for the same answer, since I do know the solution now, sharing it here.

What you are looking for is to write a wrapper around helm library.

First you need a client which speaks to the tiller of your cluster. For that you need to create a tunnel to the tiller from your localhost. Use this (its the same link as kiran shared.)

  1. Setup the Helm environement variables look here
  2. Use this next. It will return a helm client. (you might need to write a wrapper around it to work with your setup of clusters)

After you get the *helm.Client handle, you can use helm's client API given here. You just have to use the one you need with the appropriate values.

You might need some utility functions defined here, like loading a chart as a folder/archive/file.

If you want to do something more, you pretty much locate the method in the doc and call it using the client.

-- Sufiyan Parkar
Source: StackOverflow

8/17/2017

Helm talk to kubernetes using Tiller service as Kiran pointed out look at the helm code, it will have sample code.

-- sfgroups
Source: StackOverflow